main
 1require 'prime'
 2
 3#The prime factors of 13195 are 5, 7, 13 and 29.
 4
 5#What is the largest prime factor of the number 600851475143 ?
 6class OptimusPrime
 7  def factors_of(number)
 8    return [] if number == 1
 9    lowest_prime = 1
10    each do |prime|
11      if number % prime == 0
12        lowest_prime = prime
13        break
14      end
15    end
16    ([lowest_prime] + factors_of(number / lowest_prime)).flatten
17  end
18
19  private
20
21  def each(&block)
22    Prime.each(&block)
23  end
24end
25describe 'problem three' do
26  subject { OptimusPrime.new }
27
28  it 'returns prime factorization' do
29    expect(subject.factors_of(13195)).to match_array([5, 7, 13, 29])
30  end
31
32  it 'returns the answer' do
33    expect(subject.factors_of(600851475143)).to match_array([71, 839, 1471, 6857])
34  end
35
36  it 'returns the answer to 65' do
37    expect(subject.factors_of(65)).to match_array([5, 13])
38  end
39
40  it 'returns the answer to 38' do
41    expect(subject.factors_of(38)).to match_array([2, 19])
42  end
43
44  it 'returns the answer to 48' do
45    expect(subject.factors_of(48)).to match_array([2, 2, 2, 2, 3])
46  end
47
48  it 'returns the answer to 64' do
49    expect(subject.factors_of(64)).to match_array([2, 2, 2, 2, 2, 2])
50  end
51end