-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheuler7compiled.R
More file actions
40 lines (35 loc) · 801 Bytes
/
euler7compiled.R
File metadata and controls
40 lines (35 loc) · 801 Bytes
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
library(compiler)
isPrime <- function(x){
sqrtX = sqrt(x)
prime = TRUE
testValue = 2
while (testValue <= sqrtX & prime == TRUE){
if (x %% testValue == 0) {
prime = FALSE
}
else {
if (testValue == 2) {
testValue = testValue + 1
}
else {
testValue = testValue + 2
}
}
}
return(prime)
}
getNthPrime <- function(n){
counter = 1
testValue = 1
while (counter < n){
testValue = testValue + 2
currentPrime = isPrimeCmp(testValue)
if (currentPrime) {
counter = counter + 1
}
}
return(testValue)
}
isPrimeCmp <- cmpfun(isPrime)
getNthPrimeCmp <- cmpfun(getNthPrime)
print(getNthPrimeCmp(10001))