Write a method that returns the ‘n’th prime number. Recall that only numbers greater than 1 can be prime.
nth_prime(1) == 2
nth_prime(2) == 3
nth_prime(3) == 5
nth_prime(4) == 7
nth_prime(5) == 11
Use previous ‘is_prime?’ solution.
- First, start with i = 2 and check to see if it’s a prime number.
- If it’s a prime number, which 2 is, increment the nth_num to 1. (This sets nth_prime(1) == 2.)
- If it’s not a prime number, nothing will happen and it won’t be given an nth_number. Instead, the program will increment i to check if the next number is a prime number.
- Finally, if nth_num is the same as the input argument (n), then the program will spit out i, which is the prime number set for that nth.