Created
May 27, 2019 14:57
-
-
Save bartcis/53178bddc9b133db51d56fbd75890f42 to your computer and use it in GitHub Desktop.
Sum up all odd Fibbonaci numbers - solution 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // All odd Fibbonaci's | |
| function sumOddFibsBasic(number) { | |
| // 1. Create variables storing two latest values from the Fibbonaci numbers | |
| // and value of a final sum off only odd numbers | |
| let previousNum = 0; | |
| let currentNum = 1; | |
| let result = 0; | |
| // 2. While loop iterates as long as current value is smaller that function parameter | |
| while (currentNum <= number) { | |
| // 3. If Fibbonaci number is odd, add it to the sum | |
| if (currentNum % 2 !== 0) { | |
| result += currentNum; | |
| } | |
| // 4. Update two latest values from Fibbonaci sequence | |
| currentNum += previousNum; | |
| previousNum = currentNum - previousNum; | |
| } | |
| return result; | |
| } | |
| console.time('Start Algo 1'); | |
| console.log(sumOddFibsBasic(467)); | |
| console.timeEnd('Start Algo 1'); // Start Algo 1: 0.460693359375ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment