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

# IlyaLykov's solution

## to Palindrome Products in the C# Track

Published at Sep 02 2020 · 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.

## Example 1

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)`.

## Example 2

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)`.

## Hints

For this exercise, you will need to create a set of factors using tuples. For more information on tuples, see this link.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit PalindromeProducts.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

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

### PalindromeProductsTests.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using System;
using Xunit;

public class PalindromeProductsTests
{
[Fact]
public void Finds_the_smallest_palindrome_from_single_digit_factors()
{
var actual = PalindromeProducts.Smallest(1, 9);
var expected = (1, new[] { (1, 1) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Finds_the_largest_palindrome_from_single_digit_factors()
{
var actual = PalindromeProducts.Largest(1, 9);
var expected = (9, new[] { (1, 9), (3, 3) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_the_smallest_palindrome_from_double_digit_factors()
{
var actual = PalindromeProducts.Smallest(10, 99);
var expected = (121, new[] { (11, 11) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_the_largest_palindrome_from_double_digit_factors()
{
var actual = PalindromeProducts.Largest(10, 99);
var expected = (9009, new[] { (91, 99) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_smallest_palindrome_from_triple_digit_factors()
{
var actual = PalindromeProducts.Smallest(100, 999);
var expected = (10201, new[] { (101, 101) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_the_largest_palindrome_from_triple_digit_factors()
{
var actual = PalindromeProducts.Largest(100, 999);
var expected = (906609, new[] { (913, 993) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_smallest_palindrome_from_four_digit_factors()
{
var actual = PalindromeProducts.Smallest(1000, 9999);
var expected = (1002001, new[] { (1001, 1001) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_the_largest_palindrome_from_four_digit_factors()
{
var actual = PalindromeProducts.Largest(1000, 9999);
var expected = (99000099, new[] { (9901, 9999) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Empty_result_for_smallest_if_no_palindrome_in_the_range()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Smallest(1002, 1003));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Empty_result_for_largest_if_no_palindrome_in_the_range()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Largest(15, 15));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Error_result_for_smallest_if_min_is_more_than_max()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Smallest(10000, 1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Error_result_for_largest_if_min_is_more_than_max()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Largest(2, 1));
}
}``````
``````ï»¿using System;
using System.Collections.Generic;
using System.Linq;

public static class PalindromeProducts
{
public static (int, IEnumerable<(int, int)>) Largest(int minFactor, int maxFactor)
{
int maxPalindrome = FindNextBiggestPalindrome(maxFactor * maxFactor);

while (maxPalindrome >= minFactor * minFactor
&& IsFactorsInRange(maxPalindrome, minFactor, maxFactor) == false)
{
maxPalindrome = FindNextBiggestPalindrome(--maxPalindrome);
}

List<(int, int)> pairs = GetFactorPairs(maxPalindrome, minFactor, maxFactor);

return pairs.Count == 0 ? throw new ArgumentException() : ((int, IEnumerable<(int, int)>))(maxPalindrome, pairs);
}

public static (int, IEnumerable<(int, int)>) Smallest(int minFactor, int maxFactor)
{
int minPalindrome = FindNextLowestPalindrome(minFactor * minFactor);

while (minPalindrome <= maxFactor * maxFactor
&& IsFactorsInRange(minPalindrome, minFactor, maxFactor) == false)
{
minPalindrome = FindNextLowestPalindrome(++minPalindrome);
}

List<(int, int)> pairs = GetFactorPairs(minPalindrome, minFactor, maxFactor);

return pairs.Count == 0 ? throw new ArgumentException() : ((int, IEnumerable<(int, int)>))(minPalindrome, pairs);
}

private static List<(int, int)> GetFactorPairs(int num, int minF, int maxF)
{
List<(int, int)> pairs = new List<(int, int)>();

for (int minFactor = minF; minFactor <= num / minFactor; minFactor++)
{
if (num % minFactor == 0 && num / minFactor >= minFactor && num / minFactor <= maxF)
{
}
}

return pairs;
}

private static bool IsFactorsInRange(int num, int minF, int maxF)
{
while ((num / minF >= minF && num / minF <= maxF) || (num / maxF >= minF && num / maxF <= maxF))
{
if (num % minF == 0 && num / minF >= minF && num / minF <= maxF)
{
return true;
}
else
{
minF++;
}
}

return false;
}

private static int FindNextLowestPalindrome(int num)
{
int smallestPalindrome = num;

while (IsPalindrome(smallestPalindrome) == false)
{
smallestPalindrome++;
};

return smallestPalindrome;
}

private static int FindNextBiggestPalindrome(int num)
{
int biggestPalindrome = num;

while (IsPalindrome(biggestPalindrome) == false)
{
biggestPalindrome--;
}

return biggestPalindrome;
}

private static bool IsPalindrome(int num)
{
if (num <= 9)
{
return true;
}

int executNum = num;
int reversedNum = 0;

while (executNum != 0)
{
reversedNum = reversedNum * 10 + executNum % 10;
executNum /= 10;
}

return num == reversedNum;
}
}``````

### What can you learn from this solution?

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?