Project Euler Problem 7

2014年04月09日

Q:

###

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?

#

R:

First Version

'prime' <- function(x)
{
  result <- TRUE
  i <-2
  while(x>2 & i<=ceiling(sqrt(x)))
  {
    if(x %% i==0) 
    {result <-FALSE
     break
    }
    i <-i+1
  }
  return(result)
}
k <- 0
Num <- 0
while(k<1001)
{
  Num <-Num+1
  if(prime(Num)) k<-k+1    
}
print(Num)
[1] 1999