Skip to content

Instantly share code, notes, and snippets.

@Ojha-Shashikant
Created January 30, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ojha-Shashikant/1101ea72323c64c0bb03bb06959dc753 to your computer and use it in GitHub Desktop.
Save Ojha-Shashikant/1101ea72323c64c0bb03bb06959dc753 to your computer and use it in GitHub Desktop.
Program to print the first 12 "even" Fibonacci numbers
''' Write a Python program to print the first 12 "even" Fibonacci numbers '''
def calculate_fibo():
previous_value = 1
current_value = 1
series = [1,1]
for i in range(1, 1000):
new_value = current_value + previous_value
series.append(new_value)
previous_value = current_value
current_value = new_value
return series
def first_12_even(series):
even_fib_series = list()
for num in series:
if num % 2 == 0:
even_fib_series.append(num)
first12_even_fib_series = even_fib_series[:12]
for val in first12_even_fib_series:
print(val, end = " ")
if __name__ == "__main__":
series = calculate_fibo()
first_12_even(series)
@asarfraaz
Copy link

1> What is the significance of 1000 in this code ?
2> Can you combine lines 8, 10 and 11 into a single statement ? [ and thus eliminate the use of "new_value" ]
3> Can you combine the code to calculate fibonacci numbers and finding the first 12 even numbers into a single loop ?
4> The output of the "for" loop in line 20, 21 is in a single line. Can you update the code to print a new line character after the last number is printed ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment