File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Data Structure/Array Or Vector/N-bonacci Numbers Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 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+ ###
You can’t perform that action at this time.
0 commit comments