Largest palindrome product: Difference between revisions
Content added Content deleted
(Added Quackery.) |
|||
Line 1,178: | Line 1,178: | ||
12 378 (999999000001, 999999999999) 999999000000000000999999 |
12 378 (999999000001, 999999999999) 999999000000000000999999 |
||
13 1097 (9999999993349, 9999996340851) 99999963342000024336999999 |
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> |
</pre> |
||