Largest palindrome product: Difference between revisions
Content added Content deleted
(→{{header|FreeBASIC}}: added a fast secound version) |
|||
Line 268: | Line 268: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
===Version 1=== |
|||
<lang freebasic>function make_pal( n as ulongint ) as ulongint |
<lang freebasic>function make_pal( n as ulongint ) as ulongint |
||
'turn a number into a palindrom with twice as many digits |
'turn a number into a palindrom with twice as many digits |
||
Line 301: | Line 302: | ||
nextd: 'yes, I used a goto. sue me. |
nextd: 'yes, I used a goto. sue me. |
||
next d</lang> |
next d</lang> |
||
{{out}} |
{{out}} |
||
99 * 91 = 9009 |
<pre>99 * 91 = 9009 |
||
993 * 913 = 906609 |
|||
9999 * 9901 = 99000099 |
|||
99979 * 99681 = 9966006699 |
|||
999999 * 999001 = 999000000999 |
|||
9998017 * 9997647 = 99956644665999</pre> |
|||
===Version 2=== |
|||
This version is based on Version 1 with only a few changes and a option for using goto, exit or continue. It fast enough to go for n = 9 (highest possible for unsigned 64bit integers). |
|||
<lang freebasic>' version 06-10-2021 |
|||
' compile with: fbc -s console |
|||
' Now you can choice, no speed changes for all 3 |
|||
' 1: use goto |
|||
' 2: use exit |
|||
' 3: use continue |
|||
#Define Option_ 1 ' set option_ to 1, 2 or 3. for all other value's uses 1 |
|||
Function make_pal( n As UInteger ) As ULongInt |
|||
'turn a number into a palindrom with twice as many digits |
|||
Dim As String ns = Str(n), ret = ns |
|||
For i As UInteger = Len(ns) To 1 Step -1 |
|||
ret += Mid(ns, i, 1) |
|||
Next i |
|||
Return ValULng(ret) |
|||
End Function |
|||
Dim As ULongInt np |
|||
Dim As Double t1 =Timer |
|||
For d As UInteger = 2 To 9 |
|||
For n As UInteger = 10^d -1 To 10^(d -1) Step -1 'count down from 999... |
|||
'since the first good number we encounter |
|||
'must be the highest |
|||
np = make_pal( n ) 'produce a 2d-digit palindrome from it |
|||
For f As uinteger = 10^d -1 To Sqr(np) Step -2 '10^(d-1) step -1 'look for highest d-digit factor |
|||
If np Mod f = 0 Then |
|||
Print f; " * "; np \ f; " = "; np |
|||
#If (option_ = 2) |
|||
Exit For, For |
|||
#ElseIf (option_ = 3) |
|||
Continue For, For, For |
|||
#Else |
|||
GoTo nextd |
|||
#EndIf |
|||
End If |
|||
Next f |
|||
Next n |
|||
#If (option_ <> 2 Or option_ <> 3) |
|||
nextd: |
|||
#EndIf |
|||
Next d |
|||
' empty keyboard buffer |
|||
While InKey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</lang> |
|||
{{out}} |
|||
<pre>99 * 91 = 9009 |
|||
993 * 913 = 906609 |
993 * 913 = 906609 |
||
9999 * 9901 = 99000099 |
9999 * 9901 = 99000099 |
||
Line 308: | Line 366: | ||
999999 * 999001 = 999000000999 |
999999 * 999001 = 999000000999 |
||
9998017 * 9997647 = 99956644665999 |
9998017 * 9997647 = 99956644665999 |
||
99999999 * 99990001 = 9999000000009999 |
|||
</pre> |
|||
999980347 * 999920317 = 999900665566009999</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |