main
1#A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
2
3#Find the largest palindrome made from the product of two 3-digit numbers.
4describe 'problem four' do
5 class Palindrome
6 def largest(digits:1)
7 min = ([1] + ([0] * (digits - 1))).flatten.join('').to_i
8 max = ([9] * digits).flatten.join('').to_i
9 puts [min, max].inspect
10 results = []
11
12 max.downto(min) do |m|
13 max.downto(min) do |n|
14 result = m * n
15 results.push(result) if palindrome?(result)
16 end
17 end
18 results.max
19 end
20
21 private
22
23 def palindrome?(number)
24 number.to_s == number.to_s.reverse
25 end
26 end
27
28 subject { Palindrome.new }
29
30 it 'can find the largest palindrome that is the product of two one digit numbers' do
31 expect(subject.largest(digits: 1)).to eql(9)
32 end
33
34 it 'can find the largest palindrome that is the product of two digits' do
35 expect(subject.largest(digits: 2)).to eql(9009)
36 end
37
38 it 'can find the largest palindrome that is the product of two three digit numbers' do
39 expect(subject.largest(digits: 3)).to eql(906609)
40 end
41end