Largest palindrome product: Difference between revisions

Added Paper & Pencil method, C# version
(→‎{{header|ALGOL 68}}: Extended to 7 digit numbers, format output as per Wren sample and added translation of the Wren sample)
(Added Paper & Pencil method, C# version)
Line 136:
Largest palindromic product of two 7-digit numbers: 9997647 * 9998017 = 99956644665999
</pre>
 
 
=={{header|C#|CSharp}}==
{{trans|Paper & Pencil}}
<lang csharp>using System;
class Program {
 
static bool isPal(int n) {
int rev = 0, lr = -1, rem;
while (n > rev) {
n = Math.DivRem(n, 10, out rem);
if (lr < 0 && rem == 0) return false;
lr = rev; rev = 10 * rev + rem;
if (n == rev || n == lr) return true;
} return false; }
 
static void Main(string[] args) {
var sw = System.Diagnostics.Stopwatch.StartNew();
int x = 900009, y = (int)Math.Sqrt(x), y10, max = 999, max9 = max - 9, z, p, bp = x, ld, c;
var a = new int[]{ 0,9,0,3,0,0,0,7,0,1 }; string bs = "";
y /= 11;
if ((y & 1) == 0) y--;
if (y % 5 == 0) y -= 2;
y *= 11;
while (y <= max) {
c = 0;
y10 = y * 10;
z = max9 + a[ld = y % 10];
p = y * z;
while (p >= bp) {
if (isPal(p)) {
if (p > bp) bp = p;
bs = string.Format("{0} x {1} = {2}", y, z - c, bp);
}
p -= y10; c += 10;
}
y += ld == 3 ? 44 : 22;
}
sw.Stop();
Console.Write("{0} {1} μs", bs, sw.Elapsed.TotalMilliseconds * 1000.0);
}
}</lang>
{{out|Output @ Tio.run}}
<pre>913 x 993 = 906609 245.2 μs</pre>
 
=={{header|F_Sharp|F#}}==
Line 253 ⟶ 297:
For factor length 12, 999999000001 * 999999999999 = 999999000000000000999999
</pre>
 
=={{header|Paper & Pencil}}==
<lang>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
floor(sqrt(900009)) = 948
one factor must be an odd multiple of 11
floor(948 / 11) = 86
it must not be even or a multiple of 5, so use 83
83 * 11 = 913 <- this is the lowest possible first factor
the last digit of the second factor must multiply with the last digit of the first factor to get 9
the highest suitable second factor (for 913) is 993
913 x 993 = 906609, a palindrome, now check suitable higher first factors
913 + 22 = 935, an unsuitable multiple of 5, so skip it and use 913 + 44 = 957
957 x 997 = ‭954129‬, not a palindrome, so continue (just subtract 9570)
957 x 987 = 944559‬‬, not a palindrome, so continue
957 x 977 = ‭934989‬, not a palindrome, so continue
957 x 967 = ‭925429‬, not a palindrome, so continue
957 x 957 = ‭915849‬, not a palindrome, so continue
957 x 947 = ‭906279‬, not a palindrome, and less than the best found so far, so stop and
continue to the next suitible first number, 957 + 22 = 979
979 x 991 = 970189‬‬, not a palindrome, so continue (just subtract 9790)
979 x 981 = 960399‬, not a palindrome, so continue
979 x 971 = 950609‬, not a palindrome, so continue
979 x 961 = 940819‬, not a palindrome, so continue
979 x 951 = 931029‬, not a palindrome, so continue
979 x 941 = 921239‬, not a palindrome, so continue
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</lang>
 
=={{header|Perl}}==