Largest palindrome product: Difference between revisions
Content added Content deleted
(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: | Line 8: | ||
=={{header|ALGOL 68}}== |
=={{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 n := v; |
|||
WHILE n > 0 DO |
|||
⚫ | |||
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}} |
{{Trans|Ring}} |
||
Also showing the maximum for 2 and 4 digit numbers. Tests for a better product before testing for palindromicity. |
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 |
<lang algol68>BEGIN # find the highest palindromic multiple of various sizes of numbers # |
||
PROC is pal = ( LONG INT n )BOOL: |
PROC is pal = ( LONG INT n )BOOL: |
||
BEGIN |
BEGIN |
||
Line 24: | Line 82: | ||
# maximum 2 digit number # |
# maximum 2 digit number # |
||
INT max := 99; |
LONG INT max := 99; |
||
# both factors must be >= 10for a 4 digit product # |
# both factors must be >= 10for a 4 digit product # |
||
INT limit start := 10; |
LONG INT limit start := 10; |
||
FOR w FROM 2 TO |
FOR w FROM 2 TO 7 DO |
||
INT best prod := 0; |
LONG INT best prod := 0; |
||
# one factor must be divisible by 11 # |
# one factor must be divisible by 11 # |
||
INT limit end = 11 * ( max OVER 11 ); |
LONG INT limit end = 11 * ( max OVER 11 ); |
||
INT second := limit start; |
LONG INT second := limit start; |
||
INT first := 1; |
LONG INT first := 1; |
||
# loop from hi to low to find the best result in the fewest steps # |
# loop from hi to low to find the best result in the fewest steps # |
||
LONG INT n := limit end + 11; |
|||
WHILE n -:= 11; |
|||
n >= limit start |
|||
DO |
|||
# with n falling, the lower limit of m can rise with # |
# with n falling, the lower limit of m can rise with # |
||
# the best-found-so-far second number. Doing this # |
# the best-found-so-far second number. Doing this # |
||
# lowers the iteration count by a lot. # |
# lowers the iteration count by a lot. # |
||
LONG INT m := max + 2; |
|||
WHILE |
WHILE m -:= 2; |
||
IF m < second |
|||
THEN FALSE |
|||
ELIF LONG INT prod = n * m; |
|||
best prod > prod |
|||
THEN FALSE |
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 |
DO SKIP OD |
||
OD; |
OD; |
||
print( ( " |
print( ( "Largest palindromic product of two ", whole( w, 0 ) |
||
, "-digit numbers |
, "-digit numbers: ", whole( first, 0 ), " * ", whole( second, 0 ) |
||
, " = ", whole( |
, " = ", whole( best prod, 0 ) |
||
, newline |
, newline |
||
) |
) |
||
Line 66: | Line 129: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Largest palindromic product of two 2-digit numbers: 99 * 91 = 9009 |
|||
Largest palindromic product of two 3-digit numbers: 913 * 993 = 906609 |
|||
Largest palindromic product of two 4-digit numbers: 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> |
</pre> |
||