Jump to content

Largest palindrome product: Difference between revisions

→‎{{header|ALGOL 68}}: Extended to 7 digit numbers, format output as per Wren sample and added translation of the Wren sample
(Added Algol 68)
(→‎{{header|ALGOL 68}}: Extended to 7 digit numbers, format output as per Wren sample and added translation of the Wren sample)
Line 8:
 
=={{header|ALGOL 68}}==
{{Trans|Wren}}
<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:
BEGIN
LONG INT r := 0 FI;
LONG INT n := v;
WHILE n > 0 DO
r *:= 10; r +:= n MOD TRUE10;
n OVERAB 10
OD;
r
END # reverse # ;
LONG INT pow := 10;
FOR n FROM 2 TO 7 DO
LONG INT low := pow * 9;
pow *:= 10;
LONG INT high := pow - 1;
print( ( "Largest palindromic product of two ", whole( n, 0 ), "-digit integers: " ) );
BOOL next n := FALSE;
LONG INT i := high + 1;
WHILE i -:= 1;
i >= low AND NOT next n
DO
LONG INT j = reverse( i );
LONG INT p = ( i * pow ) + j;
# k can't be even nor end in 5 to produce a product ending in 9 #
LONG INT k := high + 2;
WHILE k -:= 2;
IF k < low
THEN FALSE
ELIF k MOD 10 = 5
THEN TRUE
ELIF LONG INT l = p OVER k;
l > high
THEN FALSE
ELIF p MOD k = 0 THEN
print( ( whole( k, 0 ), " x ", whole( l, 0 ), " = ", whole( p, 0 ), newline ) );
next n := TRUE;
FALSE
ELSE TRUE
FI
DO SKIP OD
OD
OD
END</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: 9998017 x 9997647 = 99956644665999
</pre>
 
{{Trans|Ring}}
Also showing the maximum for 2 and 4 .. 7 digit numbers. Tests for a better product before testing for palindromicity.
<lang algol68>BEGIN # find the highest palindromic multiple of 2,various 3sizes nd 4 digitof numbers #
PROC is pal = ( LONG INT n )BOOL:
BEGIN
Line 24 ⟶ 82:
 
# maximum 2 digit number #
LONG INT max := 99;
# both factors must be >= 10for a 4 digit product #
LONG INT limit start := 10;
FOR w FROM 2 TO 47 DO
LONG INT best prod := 0;
# one factor must be divisible by 11 #
LONG INT limit end = 11 * ( max OVER 11 );
LONG INT second := limit start;
LONG INT first := 1;
# loop from hi to low to find the best result in the fewest steps #
FORLONG INT n FROM limit end BY -11 TO := limit startend DO+ 11;
WHILE n -:= 11;
n >= limit start
DO
# with n falling, the lower limit of m can rise with #
# the best-found-so-far second number. Doing this #
# lowers the iteration count by a lot. #
FORLONG INT m FROM:= max BY+ -2 TO second;
WHILE IF m <-:= second2;
IF m < second
THEN FALSE
ELIF LONG INT prod = n * m;
best prod > prod
THEN FALSE
ELSEELIF INTNOT prodis =pal( n *prod m;)
IF best prod > prod THEN FALSETRUE
ELSE # maintain the best-found-so-far ELIFresult NOT is pal( prod ) THEN TRUE#
ELSEfirst # maintain the best-found-so-far result:= #n;
first second := nm;
best second prod := mprod;
best prod := prod;TRUE
TRUE
FI
FI
DO SKIP OD
OD;
print( ( "The largestLargest palindromic product of two ", whole( w, 0 )
, "-digit numbers is: ", whole( bestfirst, 0 ), " * ", whole( prodsecond, 0 )
, " = ", whole( first,best 0 ), " * ", whole( secondprod, 0 )
, newline
)
Line 66 ⟶ 129:
{{out}}
<pre>
The largestLargest palindromic product of two 2-digit numbers is: 9009 = 99 * 91 = 9009
The largestLargest palindromic product of two 3-digit numbers is: 906609 = 913 * 993 = 906609
The largestLargest palindromic product of two 4-digit numbers is: 99000099 = 9999 * 9901 = 99000099
Largest palindromic product of two 5-digit numbers: 99979 * 99681 = 9966006699
Largest palindromic product of two 6-digit numbers: 999999 * 999001 = 999000000999
Largest palindromic product of two 7-digit numbers: 9997647 * 9998017 = 99956644665999
</pre>
 
3,037

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.