Skip to content

Commit 6693f9c

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents b7584fd + e652112 commit 6693f9c

File tree

7 files changed

+137
-2
lines changed

7 files changed

+137
-2
lines changed

DIRECTORY.md

+1
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)
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @function fastFibonacci
3+
* @description fastFibonacci is same as fibonacci algorithm by calculating the sum of previous two fibonacci numbers but in O(log(n)).
4+
* @param {Integer} N - The input integer
5+
* @return {Integer} fibonacci of N.
6+
* @see [Fast_Fibonacci_Numbers](https://www.geeksforgeeks.org/fast-doubling-method-to-find-the-nth-fibonacci-number/)
7+
*/
8+
9+
// recursive function that returns (F(n), F(n-1))
10+
const fib = (N) => {
11+
if (N === 0) return [0, 1]
12+
const [a, b] = fib(Math.trunc(N / 2))
13+
const c = a * (b * 2 - a)
14+
const d = a * a + b * b
15+
return N % 2 ? [d, c + d] : [c, d]
16+
}
17+
18+
const fastFibonacci = (N) => {
19+
if (!Number.isInteger(N)) {
20+
throw new TypeError('Input should be integer')
21+
}
22+
return fib(N)[0]
23+
}
24+
25+
export { fastFibonacci }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { fastFibonacci } from '../FastFibonacciNumber'
2+
3+
describe('Testing FibonacciNumber', () => {
4+
const errorCases = ['0', '12', true]
5+
6+
test.each(errorCases)('throws an error if %p is invalid', (input) => {
7+
expect(() => {
8+
fastFibonacci(input)
9+
}).toThrow()
10+
})
11+
12+
const testCases = [
13+
[0, 0],
14+
[1, 1],
15+
[10, 55],
16+
[25, 75025],
17+
[40, 102334155]
18+
]
19+
20+
test.each(testCases)('if input is %i it returns %i', (input, expected) => {
21+
expect(fastFibonacci(input)).toBe(expected)
22+
})
23+
})

Project-Euler/Problem002.js

+4-2
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/Problem028.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Problem 28 - Number spiral diagonals
3+
*
4+
* @see {@link https://projecteuler.net/problem=28}
5+
*
6+
* Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
7+
*
8+
* 21 22 23 24 25
9+
* 20 07 08 09 10
10+
* 19 06 01 02 11
11+
* 18 05 04 03 12
12+
* 17 16 15 14 13
13+
*
14+
* It can be verified that the sum of the numbers on the diagonals is 101.
15+
* What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
16+
*
17+
* @author ddaniel27
18+
*/
19+
20+
function problem28 (dim) {
21+
if (dim % 2 === 0) {
22+
throw new Error('Dimension must be odd')
23+
}
24+
if (dim < 1) {
25+
throw new Error('Dimension must be positive')
26+
}
27+
28+
let result = 1
29+
for (let i = 3; i <= dim; i += 2) {
30+
/**
31+
* Adding more dimensions to the matrix, we will find at the top-right corner the follow sequence:
32+
* 01, 09, 25, 49, 81, 121, 169, ...
33+
* So this can be expressed as:
34+
* i^2, where i is all odd numbers
35+
*
36+
* Also, we can know which numbers are in each corner dimension
37+
* Just develop the sequence counter clockwise from top-right corner like this:
38+
* First corner: i^2
39+
* Second corner: i^2 - (i - 1) | The "i - 1" is the distance between corners in each dimension
40+
* Third corner: i^2 - 2 * (i - 1)
41+
* Fourth corner: i^2 - 3 * (i - 1)
42+
*
43+
* Doing the sum of each corner and simplifing, we found that the result for each dimension is:
44+
* sumDim = 4 * i^2 + 6 * (1 - i)
45+
*
46+
* In this case I skip the 1x1 dim matrix because is trivial, that's why I start in a 3x3 matrix
47+
*/
48+
result += (4 * i * i) + 6 * (1 - i) // Calculate sum of each dimension corner
49+
}
50+
return result
51+
}
52+
53+
export { problem28 }

Project-Euler/test/Problem002.test.js

+14
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+
})

Project-Euler/test/Problem028.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { problem28 } from '../Problem028.js'
2+
3+
describe('checking number spiral diagonals', () => {
4+
it('should be invalid input if number is negative', () => {
5+
expect(() => problem28(-3)).toThrowError('Dimension must be positive')
6+
})
7+
it('should be invalid input if number is not odd', () => {
8+
expect(() => problem28(4)).toThrowError('Dimension must be odd')
9+
})
10+
test('if the number is equal to 5 result should be 101', () => {
11+
expect(problem28(5)).toBe(101)
12+
})
13+
// Project Euler Condition Check
14+
test('if the number is equal to 1001 result should be 669171001', () => {
15+
expect(problem28(1001)).toBe(669171001)
16+
})
17+
})

0 commit comments

Comments
 (0)