Largest palindrome product: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Optimized inner loop - run time now less than 0.7 seconds.)
(→‎{{header|Go}}: Optimized as per Wren - cuts run time in half.)
Line 19: Line 19:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{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.
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 15 seconds on my machine.
<lang go>package main
<lang go>package main


Line 44: Line 44:
j := reverse(i)
j := reverse(i)
p := i*pow + j
p := i*pow + j
// all palindromic numbers with an even number of digits are divisible by 11
// we assume here the palindromes will end in 9
// and we assume here will end in 9
if p%10 != 9 {
if p%11 != 0 || p%10 != 9 {
continue
continue
}
}
for k := high; k >= low+1; k-- {
// k can't be even nor end in 5 to produce a product ending in 9
for k := high; k > low; k -= 2 {
if k % 10 == 5 {
continue
}
l := p / k
l := p / k
if l > high {
if l > high {