Skip to main content
3 of 3
thanks to G B
manatwork
  • 20.9k
  • 5
  • 53
  • 82

Ruby: 51 49 46 characters

(45 characters code + 1 character command line option)

p=[];$_.to_i.times{n=0;p p.map!{|i|n+n=i}<<1} 

Thanks to:

  • jsvnm for suggesting an alternative for the value switching (2 characters)
  • G B for spotting out a variable unused after previous improvement (4 characters)

Sample run:

bash-4.4$ ruby -ne 'p=[];$_.to_i.times{n=0;p p.map!{|i|n+n=i}<<1}' <<< 1 [1] bash-4.4$ ruby -ne 'p=[];$_.to_i.times{n=0;p p.map!{|i|n+n=i}<<1}' <<< 9 [1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1] [1, 5, 10, 10, 5, 1] [1, 6, 15, 20, 15, 6, 1] [1, 7, 21, 35, 35, 21, 7, 1] [1, 8, 28, 56, 70, 56, 28, 8, 1] 

Try it online!

manatwork
  • 20.9k
  • 5
  • 53
  • 82