Python implementation
import numpy as np
# Function to generate the Fibonacci series
def fibonacci_fun(fib_series_fun, num_terms_fun):
# Initialize the first value in series
first_num_fun = 1
# Append the first value to series
fib_series_fun.append(first_num_fun)
# Initialize the value preceeding the first number; technically always 0 but 1 in this problem
num_bef_start_fun = 1
# Add the first_num to the num_bef_start value and append it
next_num_fun = first_num_fun + num_bef_start_fun
fib_series_fun.append(next_num_fun)
# While we do not have num_terms number of elements in Fibonacci series, repeat
while len(fib_series_fun) != num_terms_fun:
# Add the previous 2 numbers of the series to itself
new_num_fun = fib_series_fun[len(fib_series_fun)-2] + fib_series_fun[len(fib_series_fun)-1];
fib_series_fun.append(new_num_fun)
return fib_series_fun
# Main function
def main():
# Define the number of terms in Fibonacci series
num_terms = 25
# Define empty array of Int16 to store Fibonacci sereis values
fib_series = []
# Run the fibonacci_fun to get the Fibonacci series
fib_series = fibonacci_fun(fib_series, num_terms)
print(fib_series)
fib_series = np.array(fib_series)
# Values less than 4 million
values_less_than = fib_series[fib_series < 4000000]
# Get only the even values in the Fibonacci series
even_values_less_than_fib_series = values_less_than[values_less_than % 2 == 0]
print(even_values_less_than_fib_series)
sum_of_fib_series = sum(even_values_less_than_fib_series)
print("The sum of even values in the Fibonacci series is %d.\n" %sum_of_fib_series)
print("Process finished!")
main()
Julia Implementation
using Printf
# Function to generate the Fibonacci series
function fibonacci_fun(fib_series_fun, num_terms_fun)
# Initialize the first value in series
first_num_fun = 1
# Append the first value to series
append!(fib_series_fun, first_num_fun)
# Initialize the value preceeding the first number; technically always 0 but 1 in this problem
num_bef_start_fun = 1
# Add the first_num to the num_bef_start value and append it
next_num_fun = first_num_fun + num_bef_start_fun
append!(fib_series_fun, next_num_fun)
# While we do not have num_terms number of elements in Fibonacci series, repeat
while length(fib_series_fun) != num_terms_fun
# Add the previous 2 numbers of the series to itself
new_num_fun = fib_series_fun[length(fib_series_fun)-1] + fib_series_fun[length(fib_series_fun)];
append!(fib_series_fun, new_num_fun)
end
return fib_series_fun
end
# Main function
function main()
# Define the number of terms in Fibonacci series
num_terms = 25
# Define empty array of Int16 to store Fibonacci sereis values
global fib_series = Int16[]
# Run the fibonacci_fun to get the Fibonacci series
fib_series = fibonacci_fun(fib_series, num_terms)
println(fib_series)
# Values less than 4 million
values_less_than = fib_series[fib_series .< 4000000]
# Get only the even values in the Fibonacci series
even_values_less_than_fib_series = values_less_than[values_less_than .% 2 .== 0]
println(even_values_less_than_fib_series)
sum_of_fib_series = sum(even_values_less_than_fib_series)
@printf("The sum of even values in the Fibonacci series is %d.\n", sum_of_fib_series)
println("Process finished!")
end
main()
Python Implementation Output:
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393]
[ 2 8 34 144 610 2584 10946 46368]
The sum of even values in the Fibonacci series is 60696.
Julia Implementation Output:
Int16[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, -19168, 9489, -9679]
Int16[2, 8, 34, 144, 610, 2584, 10946, -19168]
The sum of even values in the Fibonacci series is -4840. Process finished!