File tree 3 files changed +19
-2
lines changed
3 files changed +19
-2
lines changed Original file line number Diff line number Diff line change 268
268
* ** Sorts**
269
269
* [ AlphaNumericalSort] ( Sorts/AlphaNumericalSort.js )
270
270
* [ BeadSort] ( Sorts/BeadSort.js )
271
+ * [ BinaryInsertionSort] ( Sorts/BinaryInsertionSort.js )
271
272
* [ BogoSort] ( Sorts/BogoSort.js )
272
273
* [ BubbleSort] ( Sorts/BubbleSort.js )
273
274
* [ BucketSort] ( Sorts/BucketSort.js )
Original file line number Diff line number Diff line change @@ -5,8 +5,10 @@ const PHI = (1 + SQ5) / 2 // definition of PHI
5
5
// theoretically it should take O(1) constant amount of time as long
6
6
// arithmetic calculations are considered to be in constant amount of time
7
7
export const EvenFibonacci = ( limit ) => {
8
+ if ( limit < 1 ) throw new Error ( 'Fibonacci sequence limit can\'t be less than 1' )
9
+
8
10
const highestIndex = Math . floor ( Math . log ( limit * SQ5 ) / Math . log ( PHI ) )
9
11
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 )
12
14
}
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments