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
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..."
load "stdlib.ring"
see "working..." + nl
 
prodOldprod = 01
bestProd = 0
limitStart = 100
// maximum 3 digit number
limitEndmax = 999
// both factors must be >100 for a 6 digit product
limitStart = 100101
// one factor must be divisible by 11
limitEnd = 11 * floor(max / 11)
for nsecond = limitStart to limitEnd
iters = 0
 
// loop from hi to low to find the best result in the fewest steps
for n = limitStart to limitEnd
for mn = limitStartlimitEnd to limitEndlimitStart step -11
// with n falling, the lower limit of m can rise with
prodNew = n*m
// the best-found-so-far second number. Doing this
if prodNew > prodOld and palindrome(string(prodNew))
// lowers the iteration count by a lot.
prodOld = prodNew
for m = max to second step first = n-1
prod = n second =* m
if isPal(prod)
iters++
// exit when the product stops increasing
if bestProd > prod
exit
ok
// maintain the best-found-so-far result
prodNew first = n*m
prodOld second = prodNewm
bestProd = prod
ok
next
next
 
seeput "The largest palindrome is: " + nl
see? "" + firstbestProd + " *= " + secondfirst + " =* " + prodOld + nlsecond
? "Found in " + iters + " iterations"
seeput "done..." + nl
</lang>
 
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
working...
Found in 6 iterations
The largest palindrome is:
done...</pre>
913 * 993 = 906609
done...
</pre>
 
=={{header|Wren}}==