I am trying to use variables for finding all Fibonacci and being able to sum them up if dividable by a chosen variable. I also want to try to write it without using any fib() or memoization.
Here is my code:
endNum = int(raw_input("Enter the end number here "))
divisable_by = int(raw_input("Sum all numbers in the sequence that divide by: "))
# below is the same as "a, b = 0, 1"
a = 0
b = 1
""" gave single letter variables to those above to use in formula below
(longer strings do not seem to work)"""
c = endNum
d = divisable_by
# while b is less than or equal to the ending number, it will loop.
while b <= c:
print b
# below is the same as "a, b = b, a+b"
a_old = a
a = b
b = a_old + b
# the below helps with summing the numbers that are divisable by number chosen.
total = 0
for i in range(a, c):
if i%d == 0:
total += i
#prints text and number calculated above.
print "Sum of all natural numbers that divide by the number you chose"
print "and are less than the other number you chose is: ", total
I am able to run the code, but I am getting wrong answers. For example, if I run the code to sum all Fibonacci up to 21 that are divisible by 3, I get the answer "0". The answer should be "24 = (3 + 21)".
Any help with a simple modification would be great!