nth prime

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.



Thought process:

  • 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.
nth prime

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s