September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,48 +1,37 @@
|
|||
NewList Hailstones.i() ; Make a linked list to use as we do not know the numbers of elements needed for an Array
|
||||
function Hailstone(sys *n)
|
||||
'=========================
|
||||
if n and 1
|
||||
n=n*3+1
|
||||
else
|
||||
n=n>>1
|
||||
end if
|
||||
end function
|
||||
|
||||
Procedure.i FillHailstones(n) ; Fills the list & returns the amount of elements in the list
|
||||
Shared Hailstones() ; Get access to the Hailstones-List
|
||||
ClearList(Hailstones()) ; Remove old data
|
||||
Repeat
|
||||
AddElement(Hailstones()) ; Add an element to the list
|
||||
Hailstones()=n ; Fill current value in the new list element
|
||||
If n=1
|
||||
ProcedureReturn ListSize(Hailstones())
|
||||
ElseIf n%2=0
|
||||
n/2
|
||||
Else
|
||||
n=(3*n)+1
|
||||
EndIf
|
||||
ForEver
|
||||
EndProcedure
|
||||
function HailstoneSequence(sys n) as sys
|
||||
'=======================================
|
||||
count=1
|
||||
do
|
||||
Hailstone n
|
||||
Count++
|
||||
if n=1 then exit do
|
||||
end do
|
||||
return count
|
||||
end function
|
||||
|
||||
If OpenConsole()
|
||||
Define i, l, maxl, maxi
|
||||
l=FillHailstones(27)
|
||||
Print("#27 has "+Str(l)+" elements and the sequence is: "+#CRLF$)
|
||||
ForEach Hailstones()
|
||||
If i=6
|
||||
Print(#CRLF$)
|
||||
i=0
|
||||
EndIf
|
||||
i+1
|
||||
Print(RSet(Str(Hailstones()),5))
|
||||
If Hailstones()<>1
|
||||
Print(", ")
|
||||
EndIf
|
||||
Next
|
||||
'MAIN
|
||||
'====
|
||||
|
||||
i=1
|
||||
Repeat
|
||||
l=FillHailstones(i)
|
||||
If l>maxl
|
||||
maxl=l
|
||||
maxi=i
|
||||
EndIf
|
||||
i+1
|
||||
Until i>=100000
|
||||
Print(#CRLF$+#CRLF$+"The longest sequence below 100000 is #"+Str(maxi)+", and it has "+Str(maxl)+" elements.")
|
||||
maxc=0
|
||||
maxn=0
|
||||
e=100000
|
||||
for n=1 to e
|
||||
c=HailstoneSequence n
|
||||
if c>maxc
|
||||
maxc=c
|
||||
maxn=n
|
||||
end if
|
||||
next
|
||||
|
||||
Print(#CRLF$+#CRLF$+"Press ENTER to exit."): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
print e ", " maxn ", " maxc
|
||||
|
||||
'result 100000, 77031, 351
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue