Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Link to original description
Source code examples on Github
Erlang version 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14 # !/ usr / bin / env escript
%% -*- erlang -*-
%%! -smp enable -sname p4
% vim:syn=erlang
- mode ( compile ).
main (_) ->
Answer = lists : max ([ X * Y || X <- lists : seq ( 999 , 100 , - 1 ), Y <- lists : seq ( 999 , 100 , - 1 ), is_palindrome ( X * Y )]),
io : format ( "Answer ~p ~n " ,[ Answer ]).
is_palindrome ( N ) -> S = integer_to_list ( N ), S =:= lists : reverse ( S ).
Erlang version 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 # !/ usr / bin / env escript
%% -*- erlang -*-
%%! -smp enable -sname p4
% vim:syn=erlang
- mode ( compile ).
main (_) ->
Answer = lists : max ([ p ( X , X ) || X <- lists : seq ( 999 , 100 , - 1 )]),
io : format ( "Answer ~p ~n " ,[ Answer ]).
p ( X , Y ) when Y > 99 ->
P = X * Y ,
case is_palindrome ( P ) of
true -> P
;_ -> p ( X , Y - 1 )
end ;
p (_,_) -> 0 .
is_palindrome ( N ) -> S = integer_to_list ( N ), S =:= lists : reverse ( S ).
Perl version
1
2
3
4
5
6
7
8
9
10
11
12 #!/usr/bin/perl -w
use strict ;
my $answer = 0 ;
for my $x ( - 999 ..- 100 ){
for my $y ( $x ..- 100 ){
my $n = $x * $y ;
$answer = $n if $n > $answer && $n == reverse ( split ( // , $n ))
}
}
print "Answer: $answer \n" ;
Python version 1
1
2
3
4
5
6
7
8
9
10
11 #!/usr/bin/python
n = 0
for a in xrange ( 999 , 100 , - 1 ):
for b in xrange ( a , 100 , - 1 ):
x = a * b
if x > n :
s = str ( x )
if s == s [:: - 1 ]: n = x
print n
Please enable JavaScript to view the comments powered by Disqus.