Print simple x*x table in ruby

I’ve needed to print simple table as

1  2  3  5  7
2  4  6 10 14
3  6  9 15 21
5 10 15 25 35
7 14 21 35 49

as you can see, x = y, cell in table = x*y (x*x to be exact)

primes = [2, 3, 5, 7]
primes = [1] + primes # we need that to print x/y axis
puts primes.map{|i| primes.map{|a| a*i}}.map{|i| ['%4i']*primes.length*' '%i}

Leave a Reply

Your email address will not be published. Required fields are marked *

× 1 = 1