Largest palindrome product: Difference between revisions
Content added Content deleted
m (Int128) |
Catskill549 (talk | contribs) (added AWK) |
||
Line 143: | Line 143: | ||
Largest palindromic product of two 7-digit numbers: 9997647 * 9998017 = 99956644665999 |
Largest palindromic product of two 7-digit numbers: 9997647 * 9998017 = 99956644665999 |
||
</pre> |
</pre> |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f LARGEST_PALINDROME_PRODUCT.AWK |
|||
BEGIN { |
|||
main(9) |
|||
main(99) |
|||
main(999) |
|||
main(9999) |
|||
exit(0) |
|||
} |
|||
function main(n, i,j,max_i,max_j,max_product,product) { |
|||
for (i=1; i<=n; i++) { |
|||
for (j=1; j<=n; j++) { |
|||
product = i * j |
|||
if (product > max_product) { |
|||
if (product ~ /^9/ && product ~ /9$/) { |
|||
if (product == reverse(product)) { |
|||
max_product = product |
|||
max_i = i |
|||
max_j = j |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
printf("%1d: %4s * %-4s = %d\n",length(n),max_i,max_j,max_product) |
|||
} |
|||
function reverse(str, i,rts) { |
|||
for (i=length(str); i>=1; i--) { |
|||
rts = rts substr(str,i,1) |
|||
} |
|||
return(rts) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
1: 1 * 9 = 9 |
|||
2: 91 * 99 = 9009 |
|||
3: 913 * 993 = 906609 |
|||
4: 9901 * 9999 = 99000099 |
|||
</pre> |
|||
=={{header|C#|CSharp}}== |
=={{header|C#|CSharp}}== |