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.
Powershell Solution:
- function Reverse-Text([string] $text)
- {
- if($text.Length -eq 1)
- {
- return $text
- }
- else
- {
- return (Reverse-Text($text.Substring(1, $text.Length-1))) + $text[0]
- }
- }
- $start = 999
- $end = 100
- $max_palindrome = 0
- for($num1 = $start; $num1 -ge $end; $num1--)
- {
- for($num2 = $num1; $num2 -ge $end; $num2--)
- {
- $product = $num1*$num2
- if($product.ToString() -eq (Reverse-Text($product)))
- {
- if($product -gt $max_palindrome)
- {
- $max_palindrome = $product
- }
- }
- }
- }
- echo $max_palindrome
No comments:
Post a Comment