Question

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!

Was it helpful?

Solution 2

You need to add the fibonacci numbers to a list first so you can use them later when you do the division check. Second, you need to take the total out of the loop, because as kgiannakakis said, it will always be replaced by 0. So it should look like this:

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

fib_list = []
# while b is less than or equal to the ending number, it will loop.
while b <= c:
    print b
    fib_list.append(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 fib_list:
  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

This code will also consider the number that you provided as being last e.g if it's 21 and 3 the total will be 24 (21 and 3 dividable by 3) as you wanted. However this is not what your print says 'and are less than the other number you chose is'...so you should consider changing it to 'less or equal'

OTHER TIPS

You are resetting total to 0 in every loop. Move this out of the loop. Also do not test for range in every loop. This will also give you wrong results. Only test for the new number found.

the generator uses less memory than list comprehensive.

def fibNum(n):
    a,b = 1,1
    while b < n:
        yield b
        b,a = a+b,b
n = int(raw_input("Enter the end number here "))
d = int(raw_input("Sum all numbers in the sequence that divide by: ")) 
total = sum(i for i in fibNum(n) if i%d == 0)
print ("Sum of all natural numbers that divide by the number you chose") 
print ("and are less than the other number you chose is: %d" %total)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top