🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of remcopeereboom

remcopeereboom's solution

to Nth Prime in the Ruby Track

Published at Jul 13 2018 · 3 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a number n, determine what the nth prime is.

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

If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.


For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby nth_prime_test.rb

To include color from the command line:

ruby -r minitest/pride nth_prime_test.rb

Source

A variation on Problem 7 at Project Euler http://projecteuler.net/problem=7

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

nth_prime_test.rb

require 'minitest/autorun'
require_relative 'nth_prime'

# Common test data version: 1.0.0 016d65b
class NthPrimeTest < Minitest::Test
  def test_first_prime
    # skip
    assert_equal 2, Prime.nth(1)
  end

  def test_second_prime
    skip
    assert_equal 3, Prime.nth(2)
  end

  def test_sixth_prime
    skip
    assert_equal 13, Prime.nth(6)
  end

  def test_big_prime
    skip
    assert_equal 104743, Prime.nth(10001)
  end

  def test_there_is_no_zeroth_prime
    skip
    assert_raises(ArgumentError) { Prime.nth(0) }
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 1, BookKeeping::VERSION
  end
end
class Prime
  @primes ||= [2, 3]

  def self.nth(n)
    fail ArgumentError, "No #{n}th prime." if n < 1

    i = @primes.last + 2
    while @primes.length < n
      @primes << i if prime? i
      i += 2
    end

    @primes[n - 1]
  end

  def self.prime?(candidate)
    sqrt = Math.sqrt(candidate)
    @primes.all? do |prime|
      return true if prime > sqrt
      candidate % prime != 0
    end
  end
  private_class_method :prime?
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of remcopeereboom

This is a speedy solution, even if it is particularly ugly.

I tried some things that didn't work. Inlining the prime test actually made the code run a little slower (though within the variability). Using plain old loops offered no significant speed up either. Changing the loop counter to avoid the indirection through @primes was also not effective. All this goes to show that it is really only the algorithms that matter when you are doing ruby.

Avatar of paulbaker3

Nice error message ;)

Avatar of remcopeereboom

@paulbaker3 Thanks, but it could have been better. It doesn't tell me what possible good values are (though it's probably not hard to infer). I try to write my error messages in the form of: Condition of failure - (actual value for good values). Much like a unit test :)

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?