Skip to content

Commit bf42c93

Browse files
committed
N-bonacci numbers
1 parent cabb853 commit bf42c93

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def n_bonacci (n, m)
2+
array_bonacci = Array.new(m, 0)
3+
4+
array_bonacci[n - 1] = 1
5+
array_bonacci[n] = 1
6+
7+
for i in (n+1)..(m-1)
8+
array_bonacci[i] = 2 * array_bonacci[i - 1] - array_bonacci[i - n - 1]
9+
end
10+
11+
for i in 0..(m-1)
12+
if i<(m-1)
13+
print(array_bonacci[i].to_s + ", ")
14+
else
15+
print(array_bonacci[i].to_s)
16+
end
17+
end
18+
end
19+
20+
def check_range_number (n)
21+
if n <= 1 || n> 10**6
22+
print("Oh, invalid range!\n")
23+
exit
24+
end
25+
end
26+
27+
### Main
28+
print("Enter value of N: ")
29+
n = gets.chomp.to_i
30+
check_range_number(n)
31+
32+
print("Enter value of M: ")
33+
m = gets.chomp.to_i
34+
check_range_number(m)
35+
36+
print("N-bonacci: ")
37+
n_bonacci(n, m)
38+
print("\n")
39+
###

0 commit comments

Comments
 (0)