mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
tests: Project Euler Problem 2 (#1200)
This commit is contained in:
@ -268,6 +268,7 @@
|
|||||||
* **Sorts**
|
* **Sorts**
|
||||||
* [AlphaNumericalSort](Sorts/AlphaNumericalSort.js)
|
* [AlphaNumericalSort](Sorts/AlphaNumericalSort.js)
|
||||||
* [BeadSort](Sorts/BeadSort.js)
|
* [BeadSort](Sorts/BeadSort.js)
|
||||||
|
* [BinaryInsertionSort](Sorts/BinaryInsertionSort.js)
|
||||||
* [BogoSort](Sorts/BogoSort.js)
|
* [BogoSort](Sorts/BogoSort.js)
|
||||||
* [BubbleSort](Sorts/BubbleSort.js)
|
* [BubbleSort](Sorts/BubbleSort.js)
|
||||||
* [BucketSort](Sorts/BucketSort.js)
|
* [BucketSort](Sorts/BucketSort.js)
|
||||||
|
@ -5,8 +5,10 @@ const PHI = (1 + SQ5) / 2 // definition of PHI
|
|||||||
// theoretically it should take O(1) constant amount of time as long
|
// theoretically it should take O(1) constant amount of time as long
|
||||||
// arithmetic calculations are considered to be in constant amount of time
|
// arithmetic calculations are considered to be in constant amount of time
|
||||||
export const EvenFibonacci = (limit) => {
|
export const EvenFibonacci = (limit) => {
|
||||||
|
if (limit < 1) throw new Error('Fibonacci sequence limit can\'t be less than 1')
|
||||||
|
|
||||||
const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI))
|
const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI))
|
||||||
const n = Math.floor(highestIndex / 3)
|
const n = Math.floor(highestIndex / 3)
|
||||||
return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) -
|
return Math.floor(((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) -
|
||||||
((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5
|
((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5)
|
||||||
}
|
}
|
||||||
|
14
Project-Euler/test/Problem002.test.js
Normal file
14
Project-Euler/test/Problem002.test.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { EvenFibonacci } from '../Problem002'
|
||||||
|
|
||||||
|
describe('Even Fibonacci numbers', () => {
|
||||||
|
it('should throw error when limit is less than 1', () => {
|
||||||
|
expect(() => EvenFibonacci(-1)).toThrowError('Fibonacci sequence limit can\'t be less than 1')
|
||||||
|
})
|
||||||
|
test('when limit is greater than 0', () => {
|
||||||
|
expect(EvenFibonacci(40)).toBe(44)
|
||||||
|
})
|
||||||
|
// Project Euler Condition Check
|
||||||
|
test('when limit is 4 million', () => {
|
||||||
|
expect(EvenFibonacci(4e6)).toBe(4613732)
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user