Skip to content

Commit 2a62eab

Browse files
authored
tests: Project Euler Problem 2 (TheAlgorithms#1200)
1 parent 4ce3dbe commit 2a62eab

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@
268268
* **Sorts**
269269
* [AlphaNumericalSort](Sorts/AlphaNumericalSort.js)
270270
* [BeadSort](Sorts/BeadSort.js)
271+
* [BinaryInsertionSort](Sorts/BinaryInsertionSort.js)
271272
* [BogoSort](Sorts/BogoSort.js)
272273
* [BubbleSort](Sorts/BubbleSort.js)
273274
* [BucketSort](Sorts/BucketSort.js)

Project-Euler/Problem002.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const PHI = (1 + SQ5) / 2 // definition of PHI
55
// theoretically it should take O(1) constant amount of time as long
66
// arithmetic calculations are considered to be in constant amount of time
77
export const EvenFibonacci = (limit) => {
8+
if (limit < 1) throw new Error('Fibonacci sequence limit can\'t be less than 1')
9+
810
const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI))
911
const n = Math.floor(highestIndex / 3)
10-
return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) -
11-
((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5
12+
return Math.floor(((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) -
13+
((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5)
1214
}

Project-Euler/test/Problem002.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { EvenFibonacci } from '../Problem002'
2+
3+
describe('Even Fibonacci numbers', () => {
4+
it('should throw error when limit is less than 1', () => {
5+
expect(() => EvenFibonacci(-1)).toThrowError('Fibonacci sequence limit can\'t be less than 1')
6+
})
7+
test('when limit is greater than 0', () => {
8+
expect(EvenFibonacci(40)).toBe(44)
9+
})
10+
// Project Euler Condition Check
11+
test('when limit is 4 million', () => {
12+
expect(EvenFibonacci(4e6)).toBe(4613732)
13+
})
14+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy