-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.erl
More file actions
108 lines (94 loc) · 3.26 KB
/
fibonacci.erl
File metadata and controls
108 lines (94 loc) · 3.26 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
-module(fibonacci).
-export([num/1, sequence/1, max/1, num_lucas/1, sequence_lucas/1, max_lucas/1]).
num(Num) ->
% Dict = dict:from_list([]),
% New_Dict = dict:append(1, "a", Dict),
% io:format("~p~n", [New_Dict]),
if Num > 0 ->
{Result,_} = get_fibonacci(Num, []),
io:format("~p~n", [Result]);
Num =< 0 ->
throw("Invalid number.")
end.
sequence(Count) ->
if Count > 0 -> display_sequence(Count, 1);
Count =< 0 -> throw("Invalid number.")
end.
display_sequence(Count, Num) ->
% Display the first "Num" fibonacci numbers
if
Count > 0 ->
{Fibonacci,_} = get_fibonacci(Num, []),
io:format("~p, ", [Fibonacci]),
display_sequence(Count - 1, Num + 1);
Count =< 0 ->
io:format("~n")
end.
max(Max) ->
if Max > 0 -> display_max(Max, 1);
Max =< 0 -> throw("Invalid number.")
end.
display_max(Max, Num) ->
% Display the fibonacci numbers up to a maximum number.
{Fibonacci,_} = get_fibonacci(Num, []),
if
Fibonacci =< Max ->
io:format("~p, ", [Fibonacci]),
display_max(Max, Num + 1);
Fibonacci > Max ->
io:format("~n")
end.
get_fibonacci(Num, Recorded_Nums) ->
% This function gets the fibonacci number in the "Num"-th position.
% TODO: It uses Recorded_Nums to memoize numbers and run faster.
% io:format("Recorded Nums: ~p", [Recorded_Nums]),
if
Num =< 1 -> {0, Recorded_Nums};
Num == 2 -> {1, Recorded_Nums};
Num >= 3 ->
{New_Num_2, New_Record_2} = get_fibonacci(Num - 2, Recorded_Nums),
{New_Num_1, New_Record_1} = get_fibonacci(Num - 1, New_Record_2),
{New_Num_1 + New_Num_2, New_Record_1 ++ [New_Num_1 + New_Num_2]}
end.
num_lucas(Num) ->
{Result,_} = get_lucas(Num, []),
io:format("~p~n", [Result]).
sequence_lucas(Count) ->
if Count > 0 -> display_sequence_lucas(Count, 1);
Count =< 0 -> throw("Invalid number.")
end.
display_sequence_lucas(Count, Num) ->
% Display the first "Num" lucas numbers
if
Count > 0 ->
{Lucas,_} = get_lucas(Num, []),
io:format("~p, ", [Lucas]),
display_sequence_lucas(Count - 1, Num + 1);
Count =< 0 ->
io:format("~n")
end.
max_lucas(Max) ->
if Max > 0 -> display_max_lucas(Max, 1);
Max =< 0 -> throw("Invalid number.")
end.
display_max_lucas(Max, Num) ->
% Display the lucas numbers up to a maximum number.
{Lucas,_} = get_lucas(Num, []),
if
Lucas =< Max ->
io:format("~p, ", [Lucas]),
display_max(Max, Num + 1);
Lucas > Max ->
io:format("~n")
end.
get_lucas(Num, Recorded_Nums) ->
% This function gets the lucas number in the "Num"-th position.
% TODO: It uses Recorded_Nums to memoize numbers and run faster.
if
Num =< 1 -> {2, Recorded_Nums};
Num == 2 -> {1, Recorded_Nums};
Num >= 3 ->
{New_Num_2, New_Record_2} = get_lucas(Num - 2, Recorded_Nums),
{New_Num_1, New_Record_1} = get_lucas(Num - 1, New_Record_2),
{New_Num_1 + New_Num_2, New_Record_1 ++ [New_Num_1 + New_Num_2]}
end.