/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/.ɪᴏ
* ᶜᵒᵖʸʳᶦᵍʰᵗ ᵇʸ ᵛᵃᵛʳ ⁻ ˡᶦᶜᵉⁿˢᵉᵈ ᵘⁿᵈᵉʳ ᵗʰᵉ ᵃᵖᵃᶜʰᵉ ˡᶦᶜᵉⁿˢᵉ ᵛᵉʳˢᶦᵒⁿ ᵗʷᵒ ᵈᵒᵗ ᶻᵉʳᵒ
*/
package io.vavr.collection.euler;
import io.vavr.collection.List;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class Euler04Test {
/**
* <strong>Problem 4: Largest palindrome product</strong>
* <p>
* A palindromic number reads the same both ways. The largest palindrome made
* from the product of two 2-digit numbers is 9009 = 91 × 99.
* <p>
* Find the largest palindrome made from the product of two 3-digit numbers.
* <p>
* See also <a href="https://projecteuler.net/problem=4">projecteuler.net problem 4</a>.
*/
@Test
public void shouldSolveProblem4() {
assertThat(largestPalindromeOfProductsFromFactorsInRange(10, 99)).isEqualTo(9009);
assertThat(largestPalindromeOfProductsFromFactorsInRange(100, 999)).isEqualTo(906609);
}
private static int largestPalindromeOfProductsFromFactorsInRange(final int min, final int max) {
return List.rangeClosed(min, max)
.crossProduct()
.filter(t -> t._1 <= t._2)
.map(t -> t._1 * t._2)
.filter(Utils::isPalindrome)
.max().get();
}
}