Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,52 +1,91 @@
|
|||
print "Part 1: Create a routine to generate the hailstone sequence for a number."
|
||||
print ""
|
||||
while hailstone < 1 or hailstone <> int(hailstone)
|
||||
input "Please enter a positive integer: "; hailstone
|
||||
wend
|
||||
print ""
|
||||
print "The following is the 'Hailstone Sequence' for your number..."
|
||||
print ""
|
||||
print hailstone
|
||||
while hailstone <> 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
print hailstone
|
||||
wend
|
||||
print ""
|
||||
input "Hit 'Enter' to continue to part 2...";dummy$
|
||||
cls
|
||||
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."
|
||||
print ""
|
||||
print "No. in Seq.","Hailstone Sequence Number for 27"
|
||||
print ""
|
||||
c = 1: hailstone = 27
|
||||
print c, hailstone
|
||||
while hailstone <> 1
|
||||
c = c + 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
print c, hailstone
|
||||
wend
|
||||
print ""
|
||||
input "Hit 'Enter' to continue to part 3...";dummy$
|
||||
cls
|
||||
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 ""
|
||||
print "Calculating result... Please wait... This could take a little while..."
|
||||
print ""
|
||||
print "Percent Done", "Start Number", "Seq. Length", "Maximum Sequence So Far"
|
||||
print ""
|
||||
for cc = 1 to 99999
|
||||
hailstone = cc: c = 1
|
||||
while hailstone <> 1
|
||||
c = c + 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
wend
|
||||
if c > max then max = c: largesthailstone = cc
|
||||
locate 1, 7
|
||||
print " "
|
||||
locate 1, 7
|
||||
print using("###.###", cc / 99999 * 100);"%", cc, c, max
|
||||
scan
|
||||
next cc
|
||||
print ""
|
||||
print "The number less than 100,000 with the longest 'Hailstone Sequence' is "; largesthailstone;". It's sequence length is "; max;"."
|
||||
end
|
||||
' version 17-06-2015
|
||||
' compile with: fbc -s console
|
||||
|
||||
Function hailstone_fast(number As ULongInt) As ULongInt
|
||||
' faster version
|
||||
' only counts the sequence
|
||||
|
||||
Dim As ULongInt count = 1
|
||||
|
||||
While number <> 1
|
||||
If (number And 1) = 1 Then
|
||||
number += number Shr 1 + 1 ' 3*n+1 and n/2 in one
|
||||
count += 2
|
||||
Else
|
||||
number Shr= 1 ' divide number by 2
|
||||
count += 1
|
||||
End If
|
||||
Wend
|
||||
|
||||
Return count
|
||||
|
||||
End Function
|
||||
|
||||
Sub hailstone_print(number As ULongInt)
|
||||
' print the number and sequence
|
||||
|
||||
Dim As ULongInt count = 1
|
||||
|
||||
Print "sequence for number "; number
|
||||
Print Using "########"; number; 'starting number
|
||||
|
||||
While number <> 1
|
||||
If (number And 1) = 1 Then
|
||||
number = number * 3 + 1 ' n * 3 + 1
|
||||
count += 1
|
||||
Else
|
||||
number = number \ 2 ' n \ 2
|
||||
count += 1
|
||||
End If
|
||||
Print Using "########"; number;
|
||||
Wend
|
||||
|
||||
Print : Print
|
||||
Print "sequence length = "; count
|
||||
Print
|
||||
Print String(79,"-")
|
||||
|
||||
End Sub
|
||||
|
||||
Function hailstone(number As ULongInt) As ULongInt
|
||||
' normal version
|
||||
' only counts the sequence
|
||||
|
||||
Dim As ULongInt count = 1
|
||||
|
||||
While number <> 1
|
||||
If (number And 1) = 1 Then
|
||||
number = number * 3 + 1 ' n * 3 + 1
|
||||
count += 1
|
||||
End If
|
||||
number = number \ 2 ' divide number by 2
|
||||
count += 1
|
||||
Wend
|
||||
|
||||
Return count
|
||||
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As ULongInt number
|
||||
Dim As UInteger x, max_x, max_seq
|
||||
|
||||
hailstone_print(27)
|
||||
Print
|
||||
|
||||
For x As UInteger = 1 To 100000
|
||||
number = hailstone(x)
|
||||
If number > max_seq Then
|
||||
max_x = x
|
||||
max_seq = number
|
||||
End If
|
||||
Next
|
||||
|
||||
Print "The longest sequence is for "; max_x; ", it has a sequence length of "; max_seq
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Var _key_ = Inkey : Wend
|
||||
Print : Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
|
|||
|
|
@ -1,37 +1,52 @@
|
|||
function Hailstone(sys *n)
|
||||
'=========================
|
||||
if n and 1
|
||||
n=n*3+1
|
||||
else
|
||||
n=n>>1
|
||||
end if
|
||||
end function
|
||||
|
||||
function HailstoneSequence(sys n) as sys
|
||||
'=======================================
|
||||
count=1
|
||||
do
|
||||
Hailstone n
|
||||
Count++
|
||||
if n=1 then exit do
|
||||
end do
|
||||
return count
|
||||
end function
|
||||
|
||||
'MAIN
|
||||
'====
|
||||
|
||||
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 e ", " maxn ", " maxc
|
||||
|
||||
'result 100000, 77031, 351
|
||||
print "Part 1: Create a routine to generate the hailstone sequence for a number."
|
||||
print ""
|
||||
while hailstone < 1 or hailstone <> int(hailstone)
|
||||
input "Please enter a positive integer: "; hailstone
|
||||
wend
|
||||
print ""
|
||||
print "The following is the 'Hailstone Sequence' for your number..."
|
||||
print ""
|
||||
print hailstone
|
||||
while hailstone <> 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
print hailstone
|
||||
wend
|
||||
print ""
|
||||
input "Hit 'Enter' to continue to part 2...";dummy$
|
||||
cls
|
||||
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."
|
||||
print ""
|
||||
print "No. in Seq.","Hailstone Sequence Number for 27"
|
||||
print ""
|
||||
c = 1: hailstone = 27
|
||||
print c, hailstone
|
||||
while hailstone <> 1
|
||||
c = c + 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
print c, hailstone
|
||||
wend
|
||||
print ""
|
||||
input "Hit 'Enter' to continue to part 3...";dummy$
|
||||
cls
|
||||
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 ""
|
||||
print "Calculating result... Please wait... This could take a little while..."
|
||||
print ""
|
||||
print "Percent Done", "Start Number", "Seq. Length", "Maximum Sequence So Far"
|
||||
print ""
|
||||
for cc = 1 to 99999
|
||||
hailstone = cc: c = 1
|
||||
while hailstone <> 1
|
||||
c = c + 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
wend
|
||||
if c > max then max = c: largesthailstone = cc
|
||||
locate 1, 7
|
||||
print " "
|
||||
locate 1, 7
|
||||
print using("###.###", cc / 99999 * 100);"%", cc, c, max
|
||||
scan
|
||||
next cc
|
||||
print ""
|
||||
print "The number less than 100,000 with the longest 'Hailstone Sequence' is "; largesthailstone;". It's sequence length is "; max;"."
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,42 +1,40 @@
|
|||
Module HailstoneSequence
|
||||
Sub Main()
|
||||
' Checking sequence of 27.
|
||||
print "Part 1: Create a routine to generate the hailstone sequence for a number."
|
||||
print ""
|
||||
|
||||
Dim l As List(Of Long) = HailstoneSequence(27)
|
||||
Console.WriteLine("27 has {0} elements in sequence:", l.Count())
|
||||
while hailstone < 1 or hailstone <> int(hailstone)
|
||||
input "Please enter a positive integer: "; hailstone
|
||||
wend
|
||||
count = doHailstone(hailstone,"Y")
|
||||
|
||||
For i As Integer = 0 To 3 : Console.Write("{0}, ", l(i)) : Next
|
||||
Console.Write("... ")
|
||||
For i As Integer = l.Count - 4 To l.Count - 1 : Console.Write(", {0}", l(i)) : Next
|
||||
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")
|
||||
|
||||
Console.WriteLine()
|
||||
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
|
||||
|
||||
' Finding longest sequence for numbers below 100000.
|
||||
|
||||
Dim max As Integer = 0
|
||||
Dim maxCount As Integer = 0
|
||||
|
||||
For i = 1 To 99999
|
||||
l = HailstoneSequence(i)
|
||||
If l.Count > maxCount Then
|
||||
max = i
|
||||
maxCount = l.Count
|
||||
End If
|
||||
Next
|
||||
Console.WriteLine("Max elements in sequence for number below 100k: {0} with {1} elements.", max, maxCount)
|
||||
Console.ReadLine()
|
||||
End Sub
|
||||
|
||||
Private Function HailstoneSequence(ByVal n As Long) As List(Of Long)
|
||||
Dim valList As New List(Of Long)()
|
||||
valList.Add(n)
|
||||
|
||||
Do Until n = 1
|
||||
n = IIf(n Mod 2 = 0, n / 2, (3 * n) + 1)
|
||||
valList.Add(n)
|
||||
Loop
|
||||
|
||||
Return valList
|
||||
End Function
|
||||
|
||||
End Module
|
||||
'---------------------------------------------
|
||||
' 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue