Largest palindrome product: Difference between revisions

Added Easylang
(Added Easylang)
 
(3 intermediate revisions by 3 users not shown)
Line 513:
3 913 993 906609
4 9901 9999 99000099
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc rev n .
while n > 0
r = r * 10 + n mod 10
n = n div 10
.
return r
.
for i = 100 to 999
for j = i to 999
p = i * j
if p > max and p = rev p
max = p
.
.
.
</syntaxhighlight>
{{out}}
<pre>
906609
</pre>
 
Line 1,178 ⟶ 1,201:
12 378 (999999000001, 999999999999) 999999000000000000999999
13 1097 (9999999993349, 9999996340851) 99999963342000024336999999
</pre>
 
=={{header|Quackery}}==
 
The largest product of two 3 digit numbers is 999*999 = 998001.
The smallest product of two 3 digit numbers is 100*100 = 10000.
Therefore we need only consider 6 and 5 digit palindromic numbers.
 
Considering 6 digit palindromic numbers:
 
The largest is 999999.
The smallest is 100001.
These can be costructed from the numbers 100 to 999.
Therefore there are 899 6 digit palindromic numbers.
 
The same applies to 5 digit palindromic numbers:
The largest is 99999.
The smallest is 10001.
These can be costructed from the numbers 100 to 999.
Therefore there are 899 5 digit palindromic numbers.
 
'''Method:'''
 
* Construct the 6 digit palindromic numbers in reverse numerical order.
* Test each one to see if it is divisible by a 3 digit number with a 3 digit result, starting with 999.
* If it is, the solution has been found.
* If no solution found, consider 5 digit palindromes.
 
A six digit solution was found, and as it was found virtually instantaneously I did not feel that any optimisations were necessary.
 
I went on to find the largest five digit soultion, even though the task did not call for it, as it was a trivial exercise.
 
<syntaxhighlight lang="Quackery"> [ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is ->digits ( n --> [ )
 
[ behead swap
witheach
[ swap 10 * + ] ] is ->number ( [ --> n )
 
[ ->digits
dup reverse join
->number ] is evenpal ( n --> n )
 
[ ->digits
dup reverse
behead drop join
->number ] is oddpal ( n --> n )
 
[ 2dup mod 0 != iff
[ 2drop false ]
done
/ 100 1000 within ] is solution ( n n --> b )
 
false
899 times
[ i 100 + evenpal
899 times
[ dup i 100 +
solution if
[ dip not
conclude ] ]
over iff
[ nip conclude ]
else drop ]
dup iff
[ say "Six digit solution found: " echo ]
else
[ drop say "No six digit solution found." ]
cr
false
899 times
[ i 100 + oddpal
899 times
[ dup i 100 +
solution if
[ dip not
conclude ] ]
over iff
[ nip conclude ]
else drop ]
dup iff
[ say "Five digit solution found: " echo ]
else
[ drop say "No five digit solution found." ]
</syntaxhighlight>
 
{{out}}
 
<pre>Six digit solution found: 906609
Five digit solution found: 99899
</pre>
 
Line 1,275 ⟶ 1,391:
Found in 6 iterations
done...</pre>
 
=={{header|RPL}}==
≪ "" OVER SIZE 1 '''FOR''' j
OVER j DUP SUB +
-1 '''STEP''' NIP
≫ '<span style="color:blue">REVSTR</span>' STO
≪ 1 CF
'''DO'''
1 -
DUP →STR DUP <span style="color:blue">REVSTR</span> + STR→
DUP DIVIS SORT
1 OVER SIZE '''FOR''' j
DUP j GET
DUP XPON 2
'''CASE'''
DUP2 < '''THEN''' 3 DROPN '''END'''
> '''THEN''' DROP DUP SIZE 'j' STO '''END'''
PICK3 SWAP /
'''IF''' XPON 2 == '''THEN''' 1 SF '''END'''
'''END'''
'''NEXT''' DROP2
'''UNTIL''' 1 FS? '''END'''
→STR DUP <span style="color:blue">REVSTR</span> + STR→
≫ '<span style="color:blue">P004</span>' STO
 
1000 <span style="color:blue">P004</span>
{{out}}
<pre>
1: 906609
</pre>
 
=={{header|Sidef}}==
Line 1,308 ⟶ 1,455:
=={{header|Wren}}==
The approach here is to manufacture palindromic numbers of length 2n in decreasing order and then see if they're products of two n-digit numbers.
<syntaxhighlight lang="ecmascriptwren">var reverse = Fn.new { |n|
var r = 0
while (n > 0) {
2,016

edits