-
Notifications
You must be signed in to change notification settings - Fork 153
/
fibonacci.rb
93 lines (87 loc) · 2.14 KB
/
fibonacci.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
=begin
@author: aaditkamat
@date: 07/01/2019
=end
def fibonacci_recursive_value(num, array, ctr)
if num < 1
return "Incorrect num #{num} for fibonacci sequence"
end
if num == 1 or num == 2
array[num - 1] = 1
return array[num - 1]
end
if array[num - 2] != 0 and array[num - 1] != 0
return array[num - 2] + array[num - 3]
elsif array[num - 2] != 0
array[num - 3] = fibonacci_recursive_value(num - 2, array, ctr + 1)
elsif array[num - 3] != 0
array[num - 2] = fibonacci_recursive_value(num - 1, array, ctr + 1)
else
array[num - 2] = fibonacci_recursive_value(num - 1, array, ctr + 1)
array[num - 3] = fibonacci_recursive_value(num - 2, array, ctr + 1)
end
value = array[num - 2] + array[num - 3]
value
end
def fibonacci_recursive_sequence(num, arr)
if num < 1
return fibonacci_recursive_value(num, arr, 0)
end
start = 1
print "The sequence is: "
until start > num do
if start < num
print"#{fibonacci_recursive_value(start, Array.new(arr), 0)}, "
else
puts "#{fibonacci_recursive_value(start, Array.new(arr), 0)}"
end
start += 1
end
end
def fibonacci_iterative_sequence(num)
if num < 1
return "Incorrect num #{num} for fibonacci sequence"
end
start = 1
print "The sequence is: "
until start > num do
if start < num
print"#{fibonacci_iterative_value(start)}, "
else
puts "#{fibonacci_iterative_value(start)}"
end
start += 1
end
end
def fibonacci_iterative_value(num)
if num < 1
return "Incorrect num #{num} for fibonacci sequence"
end
if num == 1 || num == 2
return 1
end
first = 1
second = 1
ctr = 2
until ctr >= num do
first += second
ctr += 1
if ctr == num
return first
end
second += first
ctr += 1
end
second
end
def main
print("Enter an integer: ")
num = gets.chomp!.to_i
a = []
a.fill(0, 0, num)
puts "The number at position #{num} of fibonacci sequence: #{fibonacci_recursive_value(num, a, 0)}"
fibonacci_recursive_sequence(num, a)
puts "The number at position #{num} of fibonacci sequence: #{fibonacci_iterative_value(num)}"
fibonacci_iterative_sequence(num)
end
main