summaryrefslogtreecommitdiff
path: root/spec/euler/problem_seven_spec.rb
blob: 3cc7ac5a89403f66c574ac04e4d53ba41a9ed68b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require "spec_helper"
require 'prime'

describe "problem seven" do
  #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?

  class Primes
    include Enumerable

    def [](index)
      each_with_index do |n, current|
        return n if current == (index - 1)
      end
    end

    def each(&block)
      prime.each(&block)
    end

    private

    def prime
      Prime
    end
  end

  subject { Primes.new }

  it "returns the first 6 primes" do
    expect(subject.take(6)).to eql([2, 3, 5, 7, 11, 13])
  end

  it "returns 13" do
    expect(subject[6]).to eql(13)
  end

  it "returns the 10_001 prime" do
    expect(subject[10_001]).to eql(104743)
  end
end