-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem4.ps1
45 lines (32 loc) · 938 Bytes
/
Problem4.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<#
<p>A palindromic number reads the same both ways. The largest palindrome made from the product of two $2$-digit numbers is $9009 = 91 \times 99$.</p>
<p>Find the largest palindrome made from the product of two $3$-digit numbers.</p>
#>
$length = 3
$start = ""
$end = ""
for ($i = 0; $i -lt $length; $i++) {
$start = "$($start)1"
$end = "$($end)9"
}
$start = [int]$start
$end = [int]$end
$res = @()
Function Palindrome ([int]$in) {
$arr = ([string]($in)).ToCharArray()
for ($i = 0; $i -lt $arr.Length / 2; $i++) {
if ($arr[$i] -ne $arr[(-1 - $i)]) { return $false }
}
return $true
}
for ($i = $end; $i -ge $start; $i--) {
for ($j = $end; $j -ge $start; $j--) {
$k = $i * $j
if (Palindrome $k) {
$res += $k
Write-Host "$i * $j = $k" -ForegroundColor Green
break
}
}
}
$res | Sort-Object -Descending | Select-Object -First 1