Last active
August 29, 2015 14:03
-
-
Save philcallister/c3fc0ead7074999152ff to your computer and use it in GitHub Desktop.
Multiplication Table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mult_table(num, spaces) | |
max_space = (num * num).to_s.length + spaces | |
rows = [] | |
(1..num).each do |row| | |
rows << (1..num).inject('') { |r,col| r += sprintf("%#{max_space}d", row * col) } | |
end | |
rows | |
end | |
mult_table(12, 2).each { |row| puts row } | |
mult_table(4, 5).each { |row| puts row } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment