Code Pyre

All Code Dies and Burns in Time

Fork me on GitHub

Project Euler: Problem 4 in PowerShell

| Comments

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Solve-Problem4 {
  (100..999 | % {
    $lhs = $_
    100..999 | % {
      $rhs = $_
      $product = $lhs * $rhs
      $value = $product.ToString()
      if($value -eq ($value[$value.Length..0] -join "")) {$product}
    }
  } | Measure-Object -Maximum).Maximum
}

Write-Host "Elapsed Time (s): " (Measure-Command {Solve-Problem4}).TotalSeconds
Write-Host "Solution: " (Solve-Problem4)

Elapsed Time (s):  87.4686333
Solution:  906609

Comments