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 10,001st prime number?
Code
functionPrime(n)#' @@name Prime#' #' @@description #' #' This function finds the nth prime numbers#'#' @@arg n: A positive integer n#'#' @@return The nth prime number#'#' @@examples#'#' n = 13#' Prime(n)functionis_prime(m)# This function checks if m is prime bool =true k =2if m ==0 bool =falseelseif m ==1 bool =falseelseif m ==2 bool =trueendwhile k < mif m%k ==0 bool =falsebreakend k = k +1endreturn boolend prime_vector = [] i =0whiletrueifis_prime(i) prime_vector =push!(prime_vector, i)endiflength(prime_vector) == nbreakend i = i +1end prime = prime_vector[n]return primeend
Prime (generic function with 1 method)
Let’s now test our code with the given example
Code
Prime(6)
13
Therefore we can see that the 6th prime number is 13 which is correct.
Let’s now find the 10,001st prime number
Code
Prime(10001)
104743
Therefore we have that the 10,001st prime number is 104,743.