Largest palindrome product: Difference between revisions
m
→{{header|Ring}}: removed library dependence, improved performance by checking hi to low and terminating the inner loop once it's obvious the result won't be improved
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: 'reverse' suffices) |
m (→{{header|Ring}}: removed library dependence, improved performance by checking hi to low and terminating the inner loop once it's obvious the result won't be improved) |
||
Line 138:
=={{header|Ring}}==
<lang ring>? "working..."
bestProd = 0
limitStart = 100▼
// maximum 3 digit number
// both factors must be >100 for a 6 digit product
// one factor must be divisible by 11
limitEnd = 11 * floor(max / 11)
iters = 0
// loop from hi to low to find the best result in the fewest steps
▲for n = limitStart to limitEnd
// with n falling, the lower limit of m can rise with
prodNew = n*m▼
// the best-found-so-far second number. Doing this
// lowers the iteration count by a lot.
prodOld = prodNew▼
for m = max to second step
prod = n
if isPal(prod)
iters++
// exit when the product stops increasing
if bestProd > prod
exit
ok
// maintain the best-found-so-far result
bestProd = prod
ok
next
next
? "Found in " + iters + " iterations"
func isPal n
x = string(n)
l = len(x) + 1
i = 0
while i < l
if x[i++] != x[l--]
return false
ok
end
return true</lang>
{{out}}
<pre>working...
The largest palindrome is: 906609 = 913 * 993▼
Found in 6 iterations
▲The largest palindrome is:
done...</pre>▼
▲done...
=={{header|Wren}}==
|