Largest palindrome product: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Wren)
Line 54: Line 54:
913 * 993 = 906609
913 * 993 = 906609
done...
done...
</pre>

=={{header|Wren}}==
<lang ecmascript>var pow = 10
for (n in 2..7) {
var low = pow * 9 + 1
pow = pow * 10
var high = pow - 1
System.write("Largest palindromic product of two %(n)-digit integers: ")
var nextN = false
for (i in high..low) {
for (j in high..low) {
var p = i * j
if (p%10 != 9) continue
var s = p.toString
if (s == s[-1..0]) {
System.print("%(i) x %(j) = %(p)")
nextN = true
break
}
}
if (nextN) break
}
}</lang>

{{out}}
<pre>
Largest palindromic product of two 2-digit integers: 99 x 91 = 9009
Largest palindromic product of two 3-digit integers: 993 x 913 = 906609
Largest palindromic product of two 4-digit integers: 9999 x 9901 = 99000099
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: 9999979 x 9467731 = 94677111177649
</pre>
</pre>

Revision as of 10:29, 3 November 2021

Largest palindrome product is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task


Task description is taken from Project Euler (https://projecteuler.net/problem=4)
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

Raku

<lang perl6>use Prime::Factor; (^6).map: -> $oom {

   LOOP: for (+(1 ~ (0 x $oom)) .. +(9 ~ (9 x $oom))).reverse.map({ +($_ ~ .flip) }) -> $pal {
       for my @factors = $pal.&divisors.grep: {.chars == ($oom + 1)} {
           next unless $pal div $_ ∈ @factors;
           say "Largest palindromic product of two {$oom + 1}-digit integers: $_ × {$pal div $_} = $pal" and last LOOP;
       }
   }

}</lang>

Largest palindromic product of two 2-digit integers: 91 × 99 = 9009
Largest palindromic product of two 3-digit integers: 993 × 913 = 906609
Largest palindromic product of two 4-digit integers: 9901 × 9999 = 99000099
Largest palindromic product of two 5-digit integers: 99681 × 99979 = 9966006699
Largest palindromic product of two 6-digit integers: 999999 × 999001 = 999000000999


Ring

<lang ring> load "stdlib.ring" see "working..." + nl

prodOld = 0 limitStart = 100 limitEnd = 999

for n = limitStart to limitEnd

   for m = limitStart to limitEnd
       prodNew = n*m
       if prodNew > prodOld and palindrome(string(prodNew))
          prodOld = prodNew
          first = n
          second = m
       ok
   next

next

see "The largest palindrome is:" + nl see "" + first + " * " + second + " = " + prodOld + nl see "done..." + nl </lang>

Output:
working...
The largest palindrome is:
913 * 993 = 906609
done...

Wren

<lang ecmascript>var pow = 10 for (n in 2..7) {

   var low = pow * 9 + 1
   pow = pow * 10
   var high = pow - 1
   System.write("Largest palindromic product of two %(n)-digit integers: ")
   var nextN = false
   for (i in high..low) {
       for (j in high..low) {
           var p = i * j
           if (p%10 != 9) continue
           var s = p.toString
           if (s == s[-1..0]) {
               System.print("%(i) x %(j) = %(p)")
               nextN = true
               break                   
           }
       }
       if (nextN) break
   }

}</lang>

Output:
Largest palindromic product of two 2-digit integers: 99 x 91 = 9009
Largest palindromic product of two 3-digit integers: 993 x 913 = 906609
Largest palindromic product of two 4-digit integers: 9999 x 9901 = 99000099
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: 9999979 x 9467731 = 94677111177649