Largest palindrome product: Difference between revisions

Added Algol 68
(julia example)
(Added Algol 68)
Line 6:
<br>Find the largest palindrome made from the product of two 3-digit numbers.
<br><br>
 
=={{header|ALGOL 68}}==
{{Trans|Ring}}
Also showing the maximum for 2 and 4 digit numbers. Tests for a better product before testing for palindromicity.
<lang algol68>BEGIN # find the highest palindromic multiple of 2, 3 nd 4 digit numbers #
PROC is pal = ( LONG INT n )BOOL:
BEGIN
STRING x = whole( n, 0 );
INT l := UPB x + 1;
BOOL result := TRUE;
FOR i FROM LWB x WHILE i < l AND result DO
l -:= 1;
result := x[ i ] = x[ l ]
OD;
result
END # is pal # ;
 
# maximum 2 digit number #
INT max := 99;
# both factors must be >= 10for a 4 digit product #
INT limit start := 10;
FOR w FROM 2 TO 4 DO
INT best prod := 0;
# one factor must be divisible by 11 #
INT limit end = 11 * ( max OVER 11 );
INT second := limit start;
INT first := 1;
# loop from hi to low to find the best result in the fewest steps #
FOR n FROM limit end BY -11 TO 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. #
FOR m FROM max BY -2 TO second
WHILE IF m < second
THEN FALSE
ELSE INT prod = n * m;
IF best prod > prod THEN FALSE
ELIF NOT is pal( prod ) THEN TRUE
ELSE # maintain the best-found-so-far result #
first := n;
second := m;
best prod := prod;
TRUE
FI
FI
DO SKIP OD
OD;
print( ( "The largest palindromic product of two ", whole( w, 0 )
, "-digit numbers is: ", whole( best prod, 0 )
, " = ", whole( first, 0 ), " * ", whole( second, 0 )
, newline
)
);
max *:= 10;
max +:= 9;
limit start *:= 10
OD
END</lang>
{{out}}
<pre>
The largest palindromic product of two 2-digit numbers is: 9009 = 99 * 91
The largest palindromic product of two 3-digit numbers is: 906609 = 913 * 993
The largest palindromic product of two 4-digit numbers is: 99000099 = 9999 * 9901
</pre>
 
=={{header|F_Sharp|F#}}==
Line 17 ⟶ 81:
906609
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
3,021

edits