Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d0e61e1

Browse files
pomkarnath98omkarnathparida
andauthored
solution: ProjectEuler-007 (TheAlgorithms#1142)
* 📦 NEW: Added solution for ProjectEuler-007 * 🐛 FIX: Spelling mistake fixes * 👌 IMPROVE: changed variable name from `inc` to `candidateValue` and thrown error in case of invalid input * 👌 IMPROVE: Modified the code Co-authored-by: Omkarnath Parida <omkarnath.parida@yocket.in>
1 parent 109e4a6 commit d0e61e1

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+
throw new Error('Invalid Input')
16+
}
17+
18+
let count = 0
19+
let candidateValue = 1
20+
while (count < n) {
21+
candidateValue++
22+
if (PrimeCheck(candidateValue)) {
23+
count++
24+
}
25+
}
26+
return candidateValue
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)).toThrowError('Invalid Input')
6+
})
7+
it('should be invalid input if number is 0', () => {
8+
expect(() => nthPrime(0)).toThrowError('Invalid Input')
9+
})
10+
test('if the number is greater 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)

AltStyle によって変換されたページ (->オリジナル) /