samedi 8 octobre 2016

How to count 1 to 9 on a single line in ruby

I'm struggling to figure out how to loop numbers in a single line on ruby.

x = 0
while x <= 9
  puts x
  x +=1
end

This would give you

0
1
2
3
4
5
6
7
8
9

Each on different lines.

But what I want is to get this on a single line so like

01234567891011121314151617181920

Also not limited to just 0-9 more like 0 to infinity on a single line.

The purpose is to make an triangle of any size that follows this pattern.

1
12
123
1234
12345
123456

Each of these would be on a different line. The formatting here won't let me put in on different lines.

Would really like to solve this. It is hurting my head.

Aucun commentaire:

Enregistrer un commentaire