In: Advanced Math
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than 1000. The program prints the resulting number. You may only use while loops.
Use python language
Python Script (Be careful with the indentation):
from math import floor
f = [1,1] # vector that will increase with generated Fibonacci
numbers
indx = 2;
count = 0 # counter for number of required Fibonacci numbers
while True:
f.append(f[indx-1] + f[indx-2]) # generate next Fibonacci number
and append to list
if f[indx] >= 1000:
# considering only numbers less than 1000
break
if floor(f[indx]/3) == f[indx]/3:
# if divisible by 3
count += 1
indx += 1
print('Count =', count)
Screenshot of the above script:
Output:
Count = 4