Skip to main content
deleted 103 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Optimising code for finding Finding the Nth prime

I'm going through some of the problems over on exercism and we were asked to write a program that finds the nth prime. I know Ruby has a Prime class that handles that but the exercise doesn't allow us to use that.

My code works fine but it's very, VERY slow when testing very large numbers (ege.g.: the 10,001st prime number). I'm not entirely sure of this is because of inefficient math or bad code but if anyone could point me in the right direction it would be much appreciated.

Here's my solution:

 def nth(nth_prime) raise ArgumentError, 'Invalid Input' if nth_prime == 0 list_of_primes = [] results = [] (1..2000).each do |i| (2..(i-1)).each do |j| results << i%j end list_of_primes << i unless results.include? 0 results.clear end list_of_primes[nth_prime] end 

Note: I've limited it to listing the primes from 2 to 2000 for now because that's fast enough for testing purposes. I've also deliberately included 1 in list_of_primes because the tests (written and provided by exercism.io) test that asking for the 1st prime will give you the number 2 so I needed 2 to be at list_of_primes[1].

Optimising code for finding the Nth prime

I'm going through some of the problems over on exercism and we were asked to write a program that finds the nth prime. I know Ruby has a Prime class that handles that but the exercise doesn't allow us to use that.

My code works fine but it's very, VERY slow when testing very large numbers (eg: the 10,001st prime number). I'm not entirely sure of this is because of inefficient math or bad code but if anyone could point me in the right direction it would be much appreciated.

Here's my solution:

 def nth(nth_prime) raise ArgumentError, 'Invalid Input' if nth_prime == 0 list_of_primes = [] results = [] (1..2000).each do |i| (2..(i-1)).each do |j| results << i%j end list_of_primes << i unless results.include? 0 results.clear end list_of_primes[nth_prime] end 

Note: I've limited it to listing the primes from 2 to 2000 for now because that's fast enough for testing purposes. I've also deliberately included 1 in list_of_primes because the tests (written and provided by exercism.io) test that asking for the 1st prime will give you the number 2 so I needed 2 to be at list_of_primes[1]

Finding the Nth prime

I'm going through some of the problems over on exercism and we were asked to write a program that finds the nth prime. I know Ruby has a Prime class that handles that but the exercise doesn't allow us to use that.

My code works fine but it's very, VERY slow when testing very large numbers (e.g.: the 10,001st prime number). I'm not entirely sure of this is because of inefficient math or bad code.

 def nth(nth_prime) raise ArgumentError, 'Invalid Input' if nth_prime == 0 list_of_primes = [] results = [] (1..2000).each do |i| (2..(i-1)).each do |j| results << i%j end list_of_primes << i unless results.include? 0 results.clear end list_of_primes[nth_prime] end 

Note: I've limited it to listing the primes from 2 to 2000 for now because that's fast enough for testing purposes. I've also deliberately included 1 in list_of_primes because the tests (written and provided by exercism.io) test that asking for the 1st prime will give you the number 2 so I needed 2 to be at list_of_primes[1].

edited tags
Link
RubberDuck
  • 31.2k
  • 6
  • 74
  • 177
Source Link
SoSimple
  • 237
  • 1
  • 5

Optimising code for finding the Nth prime

I'm going through some of the problems over on exercism and we were asked to write a program that finds the nth prime. I know Ruby has a Prime class that handles that but the exercise doesn't allow us to use that.

My code works fine but it's very, VERY slow when testing very large numbers (eg: the 10,001st prime number). I'm not entirely sure of this is because of inefficient math or bad code but if anyone could point me in the right direction it would be much appreciated.

Here's my solution:

 def nth(nth_prime) raise ArgumentError, 'Invalid Input' if nth_prime == 0 list_of_primes = [] results = [] (1..2000).each do |i| (2..(i-1)).each do |j| results << i%j end list_of_primes << i unless results.include? 0 results.clear end list_of_primes[nth_prime] end 

Note: I've limited it to listing the primes from 2 to 2000 for now because that's fast enough for testing purposes. I've also deliberately included 1 in list_of_primes because the tests (written and provided by exercism.io) test that asking for the 1st prime will give you the number 2 so I needed 2 to be at list_of_primes[1]