blob: ecae51885aae54a34c8d4dace679d35112f17f13 (
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
|
#A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
#Find the largest palindrome made from the product of two 3-digit numbers.
describe 'problem four' do
class Palindrome
def largest(digits:1)
min = ([1] + ([0] * (digits - 1))).flatten.join('').to_i
max = ([9] * digits).flatten.join('').to_i
puts [min, max].inspect
results = []
max.downto(min) do |m|
max.downto(min) do |n|
result = m * n
results.push(result) if palindrome?(result)
end
end
results.max
end
private
def palindrome?(number)
number.to_s == number.to_s.reverse
end
end
subject { Palindrome.new }
it 'can find the largest palindrome that is the product of two one digit numbers' do
expect(subject.largest(digits: 1)).to eql(9)
end
it 'can find the largest palindrome that is the product of two digits' do
expect(subject.largest(digits: 2)).to eql(9009)
end
it 'can find the largest palindrome that is the product of two three digit numbers' do
expect(subject.largest(digits: 3)).to eql(906609)
end
end
|