0

[Sorry in advance, I'm very new to programming.]

This is for project euler problem #2. The goal is to calculate the sum of all even fibonacci numbers that do not exceed 4 million. For anyone not familiar with fibonacci numbers, a fibonacci number is simply a number that is the sum of the two previous numbers in the sequence. For example, the first few fibonacci numbers are 1,2,3,5,8,13,21,34 ...

My code is below beginning with some variables, then my while loop, and finally my for loop.

n = 0
n2 = 1
fibsum = 0
fibrange = range(1,4000001)

while (n2 <= 4000000):
    n2 = n2 + n
    n = n2 - n
    if n2 % 2 == 0:
        fibsum += n2
print (fibsum)


# for n2 in fibrange:
#     n2 = n2 + n
#     n = n2 - n
#     if n2 % 2 == 0:
#         fibsum += n2
# print(fibsum)

As I said, my while loop works like a charm, but when I run the for loop the output of fibsum is 0. So the value is not changing at all.

I've tried range (1, 4000001) in place of fibrange. I really have no idea what else to try. This is like my 4th or 5th program ever.

D M
  • 3
  • 1
  • 1
    The simple answer is "because a ``for`` loop is not a ``while`` loop". Are you familiar with how they differ? What makes you expect the provide the same behaviour? In short, a ``for`` loop does not care if you modify its iteration variable – the iterable (``fibrange``) decides the next next step, not the loop body. – MisterMiyagi Jul 14 '20 at 13:09
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ - use `print` or a debugger to analyse your for loop - especially how n and n2 develop. – Patrick Artner Jul 14 '20 at 13:10
  • `for n2 in fibrange` is assigning new values to `n2` each go, which is not what you want. – Peter Meisrimel Jul 14 '20 at 13:10
  • Does this answer your question? [How does a Python for loop with iterable work?](https://stackoverflow.com/questions/1292189/how-does-a-python-for-loop-with-iterable-work-for-party-in-feed-entry) – MisterMiyagi Jul 14 '20 at 13:14
  • @MisterMiyagi I just became aware of loops a few days ago, I'll read into more. Thanks for the link. Does that mean that the expressions for n2 and n in the loop body are not doing anything at all? Edit: Would a while loop be more suitable for something like this than a for loop? – D M Jul 14 '20 at 13:46

2 Answers2

1

Using for loop in range function, The Value of the variable n2 changes according only to range function.You cannot manually change the variable in for loop while using range function.

Srikeshram
  • 87
  • 1
  • 5
  • So the `n2 = n2 + n` and `n = n2 - n` parts of the for loop are not working? It is only assigning a new value to n2 as the range function is iterated? – D M Jul 14 '20 at 13:42
  • I tried already but it won't register because I'm new and my reputation is below 15. – D M Jul 14 '20 at 14:00
0

for and while and are different types of loops.

while: Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.

for: Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

With while you can do something like this:

a = 0
b = 1
target = 4000000
result = 0

while a <= target:
    if a % 2 == 0:
        result +=a
    a, b = b, a + b
print(result)

With for:

a, b = 0, 1
result = 0
target = 4000000
fib_sequence = 35  # -> the length of fibonacci sequence

for _ in range(fib_sequence):
    if a % 2 == 0:
        result +=a
    a, b = b, a + b
    if a >= target: break
print(result)