<#
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.
#>
# 998001 - so what's the largest palindrome number less than this one - then check if it's a product of 3-digit numbers
function ispalindrome {
[cmdletbinding()]
param(
[int] $number
)
process{
$digits = @()
$next_int = $number
while ($next_int -gt 9) {
$digit = $next_int % 10
$digits = $digits + $digit
$next_int = ($next_int / 10) - (($next_int % 10)/10)
#(99099 / 10) - ((99099 % 10)/10)
}
$digits = $digits + $next_int
#$digits
#$digits.Equals([array]::Reverse($digits))
#$digits.Equals($digits[($digits.Length - 1)..0])
for($i = 0; $i -ile $digits.count; $i += 1) {
#Write-Host $digits[$i]
#Write-Host $digits[-($i+1)]
if ($digits[$i] -ne $digits[-($i+1)]) {
#Write-Host "Value $($digits[$i]) at position $i does not match value $($digits[-($i-1)]) at position -$($i+1) [NB: Going through the array backwards]"
return $false
}
}
return $true
}
}
#ispalindrome 9909
#ispalindrome 99099
# largest product of 3-digit numbers
$max = 999 * 999
for ($i = $max; $i -igt 0; $i -= 1) {
if (ispalindrome $i) {
Write-Verbose "Checking the palindrome $i"
# is it divisable by a 3 digit number
for($j = 999; $j -igt 100 ; $j -= 1){
Write-Verbose "Checking $i % $j ($($i % $j))"
if ($i % $j -eq 0) {
Write-Verbose "Checking the product $j"
# it is divisable by 3 digit number - if it's a 3 digit number return $i and $j
if (($i/$j -gt 99) -and ($i/$j -lt 1000)) {
Write-host $i
Write-Host $j
return $true
}
}
}
}
}