Thanks Charles ( from the past I remember FORTH which was very fast doing such things .. but the language seems vanished ?!)
That's seriously fast, John ... maybe you can reach the 5th perfect number within hours(?) it's somewhere between 33 000 000 and 34 000 000 (iirc).
The other languages tested are : (on a slow computer)
CLisp interpreted 57sec - compiled 5 sec (but these are big (unlimited) integers )
Racket Scheme 6 sec (bytecode + GNU lightning JIT)
Steel Bank Common Lisp (native x86) 1 sec
thinBasic script 28 sec
the fastest I get : thinBasic O2 (however, O2 checks the condition and tB builds the output - O2 runs till a perfect number is found, tB prints and O2 is called again .., so almost all iterations , be it interupted a few times are done in oxygen ) pre-JIT (the O2 function are built on forehand )
;----------------------------------
' Empty thinBasic CONSOLE file template
Uses "Console" , "oxygen"
Dim As Long pnext
Dim As Long pFinish
Dim t As Long
Dim src As String
src="
Function nextperfect( Long a, Long b) As Long link #pnext
dim i , j , sumdiv as long
for i=a to b
sumdiv=0
for j=1 to i/2
if mod(i,j) = 0 then
sumdiv += j
end if
next
if sumdiv = i then
return i
endif
next
End Function
Sub finish() link #pFinish
terminate
End Sub
"
O2_Basic src
If O2_Error Then
MsgBox 0,O2_Error
Stop
Else
O2_Exec
End If
Declare Function nextperfect(ByVal a As Long, ByVal b As Long) As Long At pnext
Declare Sub Finish() At pFinish
Function printperfect ( n As Long )
Dim i As Long
For i = n/2 To 2 Step -1
If Mod(n,i)=0 Then
Print Str$(i) & "+"
EndIf
Next
PrintL " 1 "
End Function
Function findthenumbers ( x As Long)
Dim i , n As Long
i=1
Do
n=nextperfect(i,x)
If n=0 Then Exit Do
Print Str$(n) & "="
printperfect(n)
i=n+1
Loop
End Function
PrintL "Perfect numbers tB+O2"
PrintL "=====================" & $CRLF
t=GetTickCount
findthenumbers(10000)
PrintL
PrintL Str$(GetTickCount -t ) & " mSec"
finish()
WaitKey
;-----------------------------------------------------------
around 0.55 sec (an up-to-date computer could be +1.5x faster? ).
The code is not similar with the Lisp memory manipulations - I have an idea how to do it with dynamic arrays , but I don't think it makes any sense. (?)
best, Rob
.