ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Jan 02 2021
·
0 comments

Instructions

Test suite

Solution

Detect palindrome products in a given range.

A palindromic number is a number that remains the same when its digits are
reversed. For example, `121`

is a palindromic number but `112`

is not.

Given a range of numbers, find the largest and smallest palindromes which are products of numbers within that range.

Your solution should return the largest and smallest palindromes, along with the factors of each within the range. If the largest or smallest palindrome has more than one pair of factors within the range, then return all the pairs.

Given the range `[1, 9]`

(both inclusive)...

And given the list of all possible products within this range:
`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]`

The palindrome products are all single digit numbers (in this case):
`[1, 2, 3, 4, 5, 6, 7, 8, 9]`

The smallest palindrome product is `1`

. Its factors are `(1, 1)`

.
The largest palindrome product is `9`

. Its factors are `(1, 9)`

and `(3, 3)`

.

Given the range `[10, 99]`

(both inclusive)...

The smallest palindrome product is `121`

. Its factors are `(11, 11)`

.
The largest palindrome product is `9009`

. Its factors are `(91, 99)`

.

For installation and learning resources, refer to the Ruby resources 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 palindrome_products_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride palindrome_products_test.rb
```

Problem 4 at Project Euler http://projecteuler.net/problem=4

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

```
require 'minitest/autorun'
require_relative 'palindrome_products'
class PalindromesTest < Minitest::Test
def test_largest_palindrome_from_single_digit_factors
palindromes = Palindromes.new(max_factor: 9)
palindromes.generate
largest = palindromes.largest
assert_equal 9, largest.value
assert_includes [[[3, 3], [1, 9]], [[1, 9], [3, 3]]], largest.factors
end
def test_largest_palindrome_from_double_digit_factors
skip
palindromes = Palindromes.new(max_factor: 99, min_factor: 10)
palindromes.generate
largest = palindromes.largest
assert_equal 9009, largest.value
assert_equal [[91, 99]], largest.factors
end
def test_smallest_palindrome_from_double_digit_factors
skip
palindromes = Palindromes.new(max_factor: 99, min_factor: 10)
palindromes.generate
smallest = palindromes.smallest
assert_equal 121, smallest.value
assert_equal [[11, 11]], smallest.factors
end
def test_largest_palindrome_from_triple_digit_factors
skip
palindromes = Palindromes.new(max_factor: 999, min_factor: 100)
palindromes.generate
largest = palindromes.largest
assert_equal 906_609, largest.value
assert_equal [[913, 993]], largest.factors
end
def test_smallest_palindrome_from_triple_digit_factors
skip
palindromes = Palindromes.new(max_factor: 999, min_factor: 100)
palindromes.generate
smallest = palindromes.smallest
assert_equal 10_201, smallest.value
assert_equal [[101, 101]], smallest.factors
end
end
```

```
require 'matrix'
class Palindromes
def initialize (min_factor: 1, max_factor:)
@min_factor = min_factor
@max_factor = max_factor
@palindromes ||= generate
end
def generate
range = (@min_factor..@max_factor).to_a
Matrix
.build(range.count) { |i, j| range[i] * range[j] }
.flat_map { |x| x }
.uniq
.select(&:palindrome?)
end
def largest
Largest.new(@palindromes, @min_factor, @max_factor)
end
def smallest
Smallest.new(@palindromes, @min_factor, @max_factor)
end
end
class Integer
def palindrome?
c = to_s
c.reverse.eql? c
end
def factors(min, max)
factor_range = (min..max)
factor_candidates = factor_range.select do |i|
(self % i).zero?
end
factor_candidates = factor_candidates.inject([]) do |f, i|
f << i
f << self / i unless i == self / i
f
end
factor_candidates.sort
end
# https://stackoverflow.com/a/21165002/4341322
def paired_up_factors(min, max)
factor_range = (min..max)
a = factors(min, max)
l = a.length
factor_pairs = if (l % 2).zero?
a[0, l / 2].zip(a[-l / 2, l / 2].reverse)
else
a[0, l / 2].zip(a[-l / 2 + 1, l / 2].reverse) + [[a[l / 2], a[l / 2]]]
end
factor_pairs
.uniq
.select do |p|
factor_range.include? p[0] and factor_range.include? p[1]
end
end
end
class Factor
def initialize(palindromes, min_factor, max_factor)
@palindromes = palindromes
@min_factor = min_factor
@max_factor = max_factor
end
def factors
value.paired_up_factors(@min_factor, @max_factor)
end
end
class Largest < Factor
def value
@palindromes.max
end
end
class Smallest < Factor
def value
@palindromes.min
end
end
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments