Skip to content

Commit b7309f0

Browse files
📦 NEW: Added solution for ProjectEuler-007
1 parent 8461271 commit b7309f0

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Project-Euler/Problem007.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { PrimeCheck } from '../Maths/PrimeCheck.js'
2+
3+
/**
4+
* Find nth Prime Number
5+
*
6+
* P.S.(Project Euler - 007):
7+
* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
8+
* What is the 10001st prime number?
9+
*
10+
* @param {Number} n
11+
* @returns {Number} returns the nth prime number
12+
*/
13+
export const nthPrime = (n) => {
14+
if (n < 1) {
15+
return 'Invalid Input'
16+
}
17+
18+
let count = 0
19+
let inc = 2
20+
while (count < n) {
21+
if (PrimeCheck(inc)) {
22+
count++
23+
}
24+
inc++
25+
}
26+
return inc - 1
27+
}

Project-Euler/test/Problem007.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { nthPrime } from '../Problem007.js'
2+
3+
describe('checking nth prime number', () => {
4+
it('should be invalid input if number is negative', () => {
5+
expect(nthPrime(-3)).toBe('Invalid Input')
6+
})
7+
it('should be invalid input if number is 0', () => {
8+
expect(nthPrime(0)).toBe('Invalid Input')
9+
})
10+
test('if the number is greather than 0', () => {
11+
expect(nthPrime(10)).toBe(29)
12+
})
13+
// Project Euler Condition Check
14+
test('if the number is 10001', () => {
15+
expect(nthPrime(10001)).toBe(104743)
16+
})
17+
})

0 commit comments

Comments
 (0)