Skip to content

Commit 148ebd6

Browse files
authored
algorithm: Project Euler Problem 44 (#1188)
* [CREATE] Problem 044 from Project Euler * [UPDATE] Code styling update * [UPDATE] Change return condition, added an input for main function, added tests for problem 44 * [UPDATE] minor styling fixes to standard javascript * [UPDATE] Fix parentheses in main function return
1 parent bd7de6a commit 148ebd6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Project-Euler/Problem044.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Problem 44 - Pentagon numbers
3+
*
4+
* @see {@link https://projecteuler.net/problem=44}
5+
*
6+
* Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are:
7+
* 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
8+
* It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 − 22 = 48, is not pentagonal.
9+
* Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk − Pj| is minimised; what is the value of D?
10+
*
11+
* @author ddaniel27
12+
*/
13+
14+
function problem44 (k) {
15+
if (k < 1) {
16+
throw new Error('Invalid Input')
17+
}
18+
19+
while (true) {
20+
k++
21+
const n = k * (3 * k - 1) / 2 // calculate Pk
22+
23+
for (let j = k - 1; j > 0; j--) {
24+
const m = j * (3 * j - 1) / 2 // calculate all Pj < Pk
25+
if (isPentagonal(n - m) && isPentagonal(n + m)) { // Check sum and difference
26+
return n - m // return D
27+
}
28+
}
29+
}
30+
}
31+
32+
/**
33+
* Function to check if a number is pentagonal or not
34+
* This function solves n
35+
* applying the solution for a quadratic function
36+
* @see {@link https://en.wikipedia.org/wiki/Quadratic_function}
37+
*/
38+
39+
function isPentagonal (n) {
40+
const pent = (Math.sqrt(24 * n + 1) + 1) / 6
41+
return pent === Math.floor(pent)
42+
}
43+
44+
export { problem44 }

Project-Euler/test/Problem044.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { problem44 } from '../Problem044.js'
2+
3+
describe('checking nth prime number', () => {
4+
it('should be invalid input if number is negative', () => {
5+
expect(() => problem44(-3)).toThrowError('Invalid Input')
6+
})
7+
it('should be invalid input if number is 0', () => {
8+
expect(() => problem44(0)).toThrowError('Invalid Input')
9+
})
10+
// Project Euler Condition Check
11+
test('if the number is greater or equal to 1', () => {
12+
expect(problem44(1)).toBe(5482660)
13+
})
14+
// Project Euler Second Value for Condition Check
15+
test('if the number is greater or equal to 2167', () => {
16+
expect(problem44(2167)).toBe(8476206790)
17+
})
18+
})

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