-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem7.js
45 lines (39 loc) · 919 Bytes
/
problem7.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function* primeGenerator(limit = 10001) {
let primes = [2]
let next = 3
let nexiIsAPrime, limitReached = false
while (!limitReached) {
nexiIsAPrime = true
for (let i = 0; i < primes.length; i++) {
if (next % primes[i] == 0) {
nexiIsAPrime = false
break
}
}
if (nexiIsAPrime) {
primes.push(next)
yield next
}
if (primes.length === limit) {
limitReached = true
continue
}
next += 2
}
return `Prime number at position ${limit} is ${next}`
}
function problem7() {
const generator = primeGenerator()
let next
do {
next = generator.next().value
}
while (typeof(next) === 'number')
return next
}
const startTime = Date.now()
const number = problem7()
const endTime = Date.now()
console.log('number: ', number)
const runningTime = endTime - startTime
console.log(`Running time (s): ${runningTime / 1000}`)