RB
rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
##Triangle sum from three adjacents - by André; August 15th, 2017
##Response to challenge by Julian Fletcher:
https://www.sololearn.com/Discuss/628136/?ref=app
#See link above for full description. In brief, outputs a triangle
#of height n made from integers, where rows 3-(n-1) are made up of
#integers summed from those to the left, above, and above-left the
#focal integer (all rows start and end with 1). All other values
#are 1.
def sum_triangle(x)
triangle = [[1],[1,1]]
for i in 2..(x - 2)
triangle[i] = [1]
for ig in 0..(i-2)
triangle[i].push(triangle[i][ig].to_i + triangle[i-1][ig].to_i + triangle[i-1][ig+1].to_i)
end
triangle[i].push(1)
end
triangle.push([1].cycle(x).to_a)
triangle.each { | g | puts g.join(" ") }
end
input = gets.chomp
if input.to_i < 3
puts "Invalid input; enter integer greater than 2."
else
sum_triangle(input.to_i)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run