-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_even_fibonacci_sum.jl
More file actions
101 lines (77 loc) · 1.93 KB
/
02_even_fibonacci_sum.jl
File metadata and controls
101 lines (77 loc) · 1.93 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
#!/usr/bin/env julia
#=
Find the sum of the even Fibonacci numbers up to (but not including) 4_000_000
=#
"""
rfibonacci(n)
# Description
Recursive method to calculate fibonacci numbers.
# Example
```jldoctest
julia> rfibonacci(25)
46368
```
# Warning
Works well for smaller values. Unknown behavior when values get too high.
Values that are too large will cause integer overflow (looks like integer
wrapping) and quite possibly stack overflow errors.
Need to either alter the stack size or create an iterative method to compute
large values of Fibonacci numbers.
"""
function rfibonacci(n)
if n <= 0
return 0
elseif n == 1
return 1
else
return rfibonacci(n-1) + rfibonacci(n-2)
end
end
"""
fibonacci(n)
# Description
Iterative method to calculate fibonacci numbers.
# Example
```jldoctest
julia> fibonacci(10_000)
2788724563990792802
```
# Warning
Able to calculate very large numbers. Better way to do this would be to store
already calculated values in a hashmap so don't have to spend time recalculating
them (use some kind of LRU algorithm).
Significantly large numbers will need a different type to prevent the integer
overflow from occurring.
Not sure how to handle that in julia...
"""
function fibonacci(n)
if n <= 0
return 0
elseif n == 1
return 1
end
prev = 0
curr = 1
result = 0
for i in 1:n
result = prev
prev = curr
curr = result + prev
end
return result
end
rfib = rfibonacci(35)
println("Check Fibonacci(35) is over 4_000_000 $rfib")
# Only need to calculate up to 34 to be within problem constraints
sum_even_fibonacci = 0
for i in 1:34
fib = fibonacci(i)
if fib > 4_000_000
break
end
if fib % 2 == 0
global sum_even_fibonacci += fib
println("Fibonacci number $i is $fib")
end
end
println("Sum of even Fibonacci values are under 4_000_000 is: $sum_even_fibonacci")