Largest palindrome product: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Sidef}}: slightly faster)
m (syntax highlighting fixup automation)
Line 17:
{{trans|Wren}}
 
<langsyntaxhighlight lang="11l">F reverse(=n)
V r = Int64(0)
L n > 0
Line 45:
k -= 2
I nextN
L.break</langsyntaxhighlight>
 
{{out}}
Line 59:
=={{header|ALGOL 68}}==
{{Trans|Wren}}
<langsyntaxhighlight lang="algol68">BEGIN # find the highest palindromic multiple of various sizes of numbers #
# returns n with the digits reversed #
PROC reverse = ( LONG INT v )LONG INT:
Line 104:
OD
OD
END</langsyntaxhighlight>
 
{{out}}
Line 118:
{{Trans|Ring}}
Also showing the maximum for 2 and 4 .. 7 digit numbers. Tests for a better product before testing for palindromicity.
<langsyntaxhighlight lang="algol68">BEGIN # find the highest palindromic multiple of various sizes of numbers #
PROC is pal = ( LONG INT n )BOOL:
BEGIN
Line 176:
limit start *:= 10
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 188:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LARGEST_PALINDROME_PRODUCT.AWK
BEGIN {
Line 220:
return(rts)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 230:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 279:
 
print "Largest palindrome product of two 3-digit factors = ${MAXPPROD}"
</syntaxhighlight>
</lang>
{{out}}<pre>
Largest palindrome product of two 3-digit factors = 906609</pre>
Line 286:
===Main Task===
{{trans|Paper & Pencil}}
<langsyntaxhighlight lang="csharp">using System;
class Program {
 
Line 323:
Console.Write("{0} {1} μs", bs, sw.Elapsed.TotalMilliseconds * 1000.0);
}
}</langsyntaxhighlight>
{{out|Output @ Tio.run}}
<pre>913 x 993 = 906609 245.2 μs</pre>
 
===Stretch===
<langsyntaxhighlight lang="csharp">using System;
 
class Program {
Line 379:
Console.Write("{0} sec", sw.Elapsed.TotalSeconds);
}
}</langsyntaxhighlight>
{{out|Output @ Tio.run}}
Showing results for 2 through 10 digit factors.
Line 395:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Largest palindrome product. Nigel Galloway: November 3rd., 2021
let fN g=let rec fN g=[yield g%10; if g>=10 then yield! fN(g/10)] in let n=fN g in n=List.rev n
printfn "%d" ([for n in 100..999 do for g in n..999->n*g]|>List.filter fN|>List.max)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 407:
=={{header|FreeBASIC}}==
===Version 1===
<langsyntaxhighlight lang="freebasic">function make_pal( n as ulongint ) as ulongint
'turn a number into a palindrom with twice as many digits
dim as string ns, ret
Line 439:
next n
nextd: 'yes, I used a goto. sue me.
next d</langsyntaxhighlight>
{{out}}
<pre>99 * 91 = 9009
Line 449:
===Version 2===
This version is based on Version 1 with only a few changes and some extra code based on the fact that one divisor can be divided by 11, this speeds it even more up and a option for using goto, exit or continue. highest n is 9, (highest possible for unsigned 64bit integers).
<langsyntaxhighlight lang="freebasic">' version 07-10-2021
' compile with: fbc -s console
 
Line 500:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>99 * 91 = 9009
Line 514:
{{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 15 seconds on my machine.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 554:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 572:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def reverseNumber:
tostring|explode|reverse|implode|tonumber;
Line 608:
.emit ) ;
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 621:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function twoprodpal(factorlength)
Line 654:
twoprodpal(i)
end
</langsyntaxhighlight>{{out}}
<pre>
For factor length 2, 91 * 99 = 9009
Line 671:
=== Faster version ===
{{trans|Python}}
<langsyntaxhighlight lang="julia">""" taken from https://leetcode.com/problems/largest-palindrome-product/discuss/150954/Fast-algorithm-by-constrains-on-tail-digits """
 
const T = [Set([(0, 0)])]
Line 740:
largestpalindrome(k)
end
</langsyntaxhighlight>{{out}}
<pre>
1 9 (9, 1) 9
Line 762:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">palindromeQ[n_] := (* faster than built in test PalindromeQ *)
Block[{digits = IntegerDigits@n}, digits == Reverse[digits]]
 
Line 785:
 
Grid[Join[{{"factors", "largest palindrome"}}, {#, Times @@ #} & /@
Table[search[n], {n, 2, 7}]], Alignment -> {Left, Baseline}]</langsyntaxhighlight>
 
{{out}}<pre>
Line 798:
 
=={{header|Paper & Pencil}}==
<syntaxhighlight lang="text">find two 3-digit factors, that when multiplied together, yield the highest 6-digit palindrome.
 
lowest possible 6 digit palindrome starting with 9 is 900009
Line 825:
979 x 931 = 911449‬, not a palindrome, so continue
979 x 921 = 901659‬, not a palindrome, and less than the best found so far, so stop
done because 979 + 22 = 1001</langsyntaxhighlight>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 841:
say "Largest palindromic product of two @{[$l]}-digit integers: $f[1] × $f[0] = $p" and last LOOP;
}
}</langsyntaxhighlight>
{{out}}
<pre>Largest palindromic product of two 2-digit integers: 91 × 99 = 9009
Line 855:
and further optimised as per the C# comments (on this very rosettacode page).
You can run this online [http://phix.x10.mx/p2js/Largest_palindrome_product.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Largest_palindrome_product.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 960:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sy</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 985:
=={{header|Python}}==
Original author credit to user peijunz at Leetcode.
<langsyntaxhighlight lang="python">""" taken from https://leetcode.com/problems/largest-palindrome-product/discuss/150954/Fast-algorithm-by-constrains-on-tail-digits """
 
T=[set([(0, 0)])]
Line 1,042:
for k in range(1, 14):
largestPalindrome(k)
</langsyntaxhighlight>{{out}}
<pre>
1 9 (9, 1) 9
Line 1,061:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Inline::Perl5;
my $p5 = Inline::Perl5.new();
$p5.use: 'ntheory';
Line 1,086:
}
$p
}</langsyntaxhighlight>
<pre>Largest palindromic product of two 2-digit integers: 91 × 99 = 9009
Largest palindromic product of two 3-digit integers: 913 × 993 = 906609
Line 1,100:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">? "working..."
 
prod = 1
Line 1,148:
ok
end
return true</langsyntaxhighlight>
{{out}}
<pre>working...
Line 1,156:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func largest_palindrome_product (n) {
 
for k in ((10**n - 1) `downto` 10**(n-1)) {
Line 1,172:
var (a,b) = largest_palindrome_product(n)
say "Largest palindromic product of two #{n}-digit integers: #{a} * #{b} = #{a*b}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,187:
=={{header|Wren}}==
The approach here is to manufacture palindromic numbers of length 2n in decreasing order and then see if they're products of two n-digit numbers.
<langsyntaxhighlight lang="ecmascript">var reverse = Fn.new { |n|
var r = 0
while (n > 0) {
Line 1,222:
if (nextN) break
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,235:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Rev(A); \Reverse digits
int A, B;
[B:= 0;
Line 1,253:
];
IntOut(0, Max);
]</langsyntaxhighlight>
 
{{out}}
10,327

edits