Find the sum of all odd Fibonacci numbers less than 2 million

TrueNaijaKing

Hello I have been trying to do the opposite of project Euler question 2 (which is to sum of all even Fibonacci numbers less than 4,000,000). I am trying to print the sum of all odd Fibonacci numbers less than 2,000,000 however I cannot seem to get the correct answer. This is the code I have so far

fib1 = 1
fib2 = 2
fibholder = 0 #place holder for the new value
Sum = 0
while fibholder<2000000:
    fibholder = fib1 + fib2
    if fibholder%2==1:
        Sum+=fibholder
    fib1 = fib2
    fib2 = fibholder
print(Sum)
benvc

You are skipping the first two odd numbers in the sequence and including the first odd value greater than 2,000,000 in your sum because you calculate the next fibonacci number and increment your total before your while loop checks whether or not the value is less than your limit. You could correct your existing approach by initializing your variables to include the beginning of the sequence and by moving your calculation of the next number in the sequence to the end of your while loop.

total = 1
fib1 = 0
fib2 = 1
fibholder = fib1 + fib2
while fibholder < 2000000:
    if fibholder % 2:
        total += fibholder
    fib1 = fib2
    fib2 = fibholder
    fibholder = fib1 + fib2

print(total)
# 2435423

That said, the logic might be easier to follow if you just generate the odd fibonacci numbers and then handle the sum.

def odd_fibonacci(limit):
    a, b = 0, 1    
    while a < limit:
        if a % 2:
            yield a

        a, b = b, a + b

x = sum(n for n in odd_fibonacci(2000000))
print(x)
# 2435423

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

find all possible 3 number combinations where sum is less than a given number using R

分類Dev

TSQL: Find odd and even numbers in each column for all rows

分類Dev

I'm trying to find the sum of primes below 2 million in Java

分類Dev

The sum of all odd digits of an input

分類Dev

Need to generate 9 unique integers less than 100 such that no two has sum equal to another one/two numbers

分類Dev

Find SUM of child table column is less than parent child total column

分類Dev

Given two even numbers, find the sum of the squares of all even numbers between them

分類Dev

Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

分類Dev

Get even / odd / all numbers between two numbers

分類Dev

Get even / odd / all numbers between two numbers

分類Dev

Given 4 numbers of array of 1 to 10 elements. Find 3 numbers whose sum can generate all the four numbers?

分類Dev

Finding numbers in an array that are less than or equal to numbers in another array?

分類Dev

Script to find files less permissive than 750?

分類Dev

"ls -lh" reports total size less than sum of individual sizes

分類Dev

How to find characters other than numbers, alphabetical and $ _ #?

分類Dev

Sum of all numbers in the first or second place in an array

分類Dev

How to return the sum of all the numbers in a string?

分類Dev

Sum of all numbers in a multi-dimensional array

分類Dev

Fibonacci series sum

分類Dev

Find all subarray with sum equal to number?

分類Dev

Showing numbers with 2 odd numbers that also contain 4 and 5 up to 10 000 in Python

分類Dev

How to find all the ranges in an array of numbers

分類Dev

Find all telephone numbers in a string by regular expression

分類Dev

PowerTOP: why sum of "Power est" values is much less than discharge rate?

分類Dev

solve_ivp error: 'Required step size is less than spacing between numbers.'

分類Dev

Python 101 and Math Logic - Listing square root numbers less than n

分類Dev

Removing odd numbers from an array

分類Dev

How to use MongoDb specific operator $size to find array size greater than, less than, between and not equal to a value in Laravel / jenssegers?

分類Dev

Find all files & directories with group different than owner

Related 関連記事

  1. 1

    find all possible 3 number combinations where sum is less than a given number using R

  2. 2

    TSQL: Find odd and even numbers in each column for all rows

  3. 3

    I'm trying to find the sum of primes below 2 million in Java

  4. 4

    The sum of all odd digits of an input

  5. 5

    Need to generate 9 unique integers less than 100 such that no two has sum equal to another one/two numbers

  6. 6

    Find SUM of child table column is less than parent child total column

  7. 7

    Given two even numbers, find the sum of the squares of all even numbers between them

  8. 8

    Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

  9. 9

    Get even / odd / all numbers between two numbers

  10. 10

    Get even / odd / all numbers between two numbers

  11. 11

    Given 4 numbers of array of 1 to 10 elements. Find 3 numbers whose sum can generate all the four numbers?

  12. 12

    Finding numbers in an array that are less than or equal to numbers in another array?

  13. 13

    Script to find files less permissive than 750?

  14. 14

    "ls -lh" reports total size less than sum of individual sizes

  15. 15

    How to find characters other than numbers, alphabetical and $ _ #?

  16. 16

    Sum of all numbers in the first or second place in an array

  17. 17

    How to return the sum of all the numbers in a string?

  18. 18

    Sum of all numbers in a multi-dimensional array

  19. 19

    Fibonacci series sum

  20. 20

    Find all subarray with sum equal to number?

  21. 21

    Showing numbers with 2 odd numbers that also contain 4 and 5 up to 10 000 in Python

  22. 22

    How to find all the ranges in an array of numbers

  23. 23

    Find all telephone numbers in a string by regular expression

  24. 24

    PowerTOP: why sum of "Power est" values is much less than discharge rate?

  25. 25

    solve_ivp error: 'Required step size is less than spacing between numbers.'

  26. 26

    Python 101 and Math Logic - Listing square root numbers less than n

  27. 27

    Removing odd numbers from an array

  28. 28

    How to use MongoDb specific operator $size to find array size greater than, less than, between and not equal to a value in Laravel / jenssegers?

  29. 29

    Find all files & directories with group different than owner

ホットタグ

アーカイブ