September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,40 +1,48 @@
|
|||
print "Part 1: Create a routine to generate the hailstone sequence for a number."
|
||||
print ""
|
||||
NewList Hailstones.i() ; Make a linked list to use as we do not know the numbers of elements needed for an Array
|
||||
|
||||
while hailstone < 1 or hailstone <> int(hailstone)
|
||||
input "Please enter a positive integer: "; hailstone
|
||||
wend
|
||||
count = doHailstone(hailstone,"Y")
|
||||
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
|
||||
|
||||
print: print "Part 2: Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1."
|
||||
count = doHailstone(27,"Y")
|
||||
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
|
||||
|
||||
print: print "Part 3: Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length.(But don't show the actual sequence)!"
|
||||
print "Calculating result... Please wait... This could take a little while..."
|
||||
print "Stone Percent Count"
|
||||
for i = 1 to 99999
|
||||
count = doHailstone(i,"N")
|
||||
if count > maxCount then
|
||||
theBigStone = i
|
||||
maxCount = count
|
||||
print using("#####",i);" ";using("###.#", i / 99999 * 100);"% ";using("####",count)
|
||||
end if
|
||||
next i
|
||||
end
|
||||
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.")
|
||||
|
||||
'---------------------------------------------
|
||||
' pass number and print (Y/N)
|
||||
FUNCTION doHailstone(hailstone,prnt$)
|
||||
if prnt$ = "Y" then
|
||||
print
|
||||
print "The following is the 'Hailstone Sequence' for number:";hailstone
|
||||
end if
|
||||
while hailstone <> 1
|
||||
if (hailstone and 1) then hailstone = (hailstone * 3) + 1 else hailstone = hailstone / 2
|
||||
doHailstone = doHailstone + 1
|
||||
if prnt$ = "Y" then
|
||||
print hailstone;chr$(9);
|
||||
if (doHailstone mod 10) = 0 then print
|
||||
end if
|
||||
wend
|
||||
END FUNCTION
|
||||
Print(#CRLF$+#CRLF$+"Press ENTER to exit."): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue