Largest palindrome product: Difference between revisions

→‎{{header|Go}}: Updated in line with Wren example.
(→‎{{header|Wren}}: Much better approach.)
(→‎{{header|Go}}: Updated in line with Wren example.)
Line 9:
=={{header|Go}}==
{{trans|Wren}}
18 digit integers are within the range of Go's uint64 type though finding the result for 9-digit number products takes a while - around 30 seconds on my machine.
Note also that it's a lot quicker in Go to reverse a number arithmetically than to reverse its string equivalent but finding the result for 6-digit numbers is still very slow.
<lang go>package main
 
Line 26:
pow := uint64(10)
nextN:
for n := 2; n < 710; n++ {
low := pow *9 + 19
pow *= 10
high := pow - 1
fmt.Printf("Largest palindromic product of two %d-digit integers: ", n)
for pi := high * high; pi >= low*low; pi-- {
ifj p%10 !:= 9 {reverse(i)
p := i*pow + }j
// all palindromic numbers are divisible by 11 and we assume here will end in 9
if p%11 != 0 || p%10 != 9 {
continue
}
for ik := high; ik >= low+1; ik-- {
var jl := p / ik
if jl > high {
break
}
if p%ik == 0 {
iffmt.Printf("%d px %d == reverse(%d\n", k, l, p) {
continue fmt.Printf("%d x %d = %d\n", i, j, p)nextN
continue nextN
}
}
}
Line 58 ⟶ 59:
Largest palindromic product of two 5-digit integers: 99979 x 99681 = 9966006699
Largest palindromic product of two 6-digit integers: 999999 x 999001 = 999000000999
Largest palindromic product of two 7-digit integers: 9998017 x 9997647 = 99956644665999
Largest palindromic product of two 8-digit integers: 99999999 x 99990001 = 9999000000009999
Largest palindromic product of two 9-digit integers: 999980347 x 999920317 = 999900665566009999
</pre>
 
9,485

edits