Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
32
Task/Hailstone-sequence/BASIC/hailstone-sequence-1.basic
Normal file
32
Task/Hailstone-sequence/BASIC/hailstone-sequence-1.basic
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
10 HOME
|
||||
|
||||
100 N = 27
|
||||
110 GOSUB 400"HAILSTONE
|
||||
120 DEF FN L(I) = E(I + 4 * (I < 0))
|
||||
130IFL=112AND(S(0)=27ANDS(1)=82ANDS(2)=41ANDS(3)=124)AND(FNL(M-3)=8ANDFNL(M-2)=4ANDFNL(M-1)=2ANDFNL(M)=1)THENPRINT"THE HAILSTONE SEQUENCE FOR THE NUMBER 27 HAS 112 ELEMENTS STARTING WITH 27, 82, 41, 124 AND ENDING WITH 8, 4, 2, 1"
|
||||
140 PRINT
|
||||
150 V = PEEK(37) + 1
|
||||
|
||||
200 N = 1
|
||||
210 GOSUB 400"HAILSTONE
|
||||
220 MN = 1
|
||||
230 ML = L
|
||||
240 FOR I = 2 TO 99999
|
||||
250 N = I
|
||||
260 GOSUB 400"HAILSTONE
|
||||
270 IFL>MLTHENMN=I:ML=L:VTABV:HTAB1:PRINT "THE NUMBER " MN " HAS A HAILSTONE SEQUENCE LENGTH OF "L" WHICH IS THE LONGEST HAILSTONE SEQUENCE OF NUMBERS LESS THAN ";:Y=PEEK(37)+1:X=PEEK(36)+1
|
||||
280 IF Y THEN VTAB Y : HTAB X : PRINTI+1;
|
||||
290 NEXT I
|
||||
|
||||
300 END
|
||||
|
||||
400 M = 0
|
||||
410 FOR L = 1 TO 1E38
|
||||
420 IF L < 5 THEN S(L-1) = N
|
||||
430 M = (M + 1) * (M < 3)
|
||||
440 E(M) = N
|
||||
450 IF N = 1 THEN RETURN
|
||||
460 EVEN = INT(N/2)=N/2
|
||||
470 IF EVEN THEN N=N/2
|
||||
480 IF NOT EVEN THEN N = (3 * N) + 1
|
||||
490 NEXT L : STOP
|
||||
23
Task/Hailstone-sequence/BASIC/hailstone-sequence-2.basic
Normal file
23
Task/Hailstone-sequence/BASIC/hailstone-sequence-2.basic
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
seqlen% = FNhailstone(27, TRUE)
|
||||
PRINT '"Sequence length = "; seqlen%
|
||||
maxlen% = 0
|
||||
FOR number% = 2 TO 100000
|
||||
seqlen% = FNhailstone(number%, FALSE)
|
||||
IF seqlen% > maxlen% THEN
|
||||
maxlen% = seqlen%
|
||||
maxnum% = number%
|
||||
ENDIF
|
||||
NEXT
|
||||
PRINT "The number with the longest hailstone sequence is " ; maxnum%
|
||||
PRINT "Its sequence length is " ; maxlen%
|
||||
END
|
||||
|
||||
DEF FNhailstone(N%, S%)
|
||||
LOCAL L%
|
||||
IF S% THEN PRINT N%;
|
||||
WHILE N% <> 1
|
||||
IF N% AND 1 THEN N% = 3 * N% + 1 ELSE N% DIV= 2
|
||||
IF S% THEN PRINT N%;
|
||||
L% += 1
|
||||
ENDWHILE
|
||||
= L% + 1
|
||||
52
Task/Hailstone-sequence/BASIC/hailstone-sequence-3.basic
Normal file
52
Task/Hailstone-sequence/BASIC/hailstone-sequence-3.basic
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
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
|
||||
37
Task/Hailstone-sequence/BASIC/hailstone-sequence-4.basic
Normal file
37
Task/Hailstone-sequence/BASIC/hailstone-sequence-4.basic
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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
|
||||
48
Task/Hailstone-sequence/BASIC/hailstone-sequence-5.basic
Normal file
48
Task/Hailstone-sequence/BASIC/hailstone-sequence-5.basic
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
NewList Hailstones.i() ; Make a linked list to use as we do not know the numbers of elements needed for an Array
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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.")
|
||||
|
||||
Print(#CRLF$+#CRLF$+"Press ENTER to exit."): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
40
Task/Hailstone-sequence/BASIC/hailstone-sequence-6.basic
Normal file
40
Task/Hailstone-sequence/BASIC/hailstone-sequence-6.basic
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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
|
||||
count = doHailstone(hailstone,"Y")
|
||||
|
||||
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")
|
||||
|
||||
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
|
||||
|
||||
'---------------------------------------------
|
||||
' 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
|
||||
42
Task/Hailstone-sequence/BASIC/hailstone-sequence-7.basic
Normal file
42
Task/Hailstone-sequence/BASIC/hailstone-sequence-7.basic
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
Module HailstoneSequence
|
||||
Sub Main()
|
||||
' Checking sequence of 27.
|
||||
|
||||
Dim l As List(Of Long) = HailstoneSequence(27)
|
||||
Console.WriteLine("27 has {0} elements in sequence:", l.Count())
|
||||
|
||||
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
|
||||
|
||||
Console.WriteLine()
|
||||
|
||||
' 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
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
hailstone:
|
||||
local hailstone:
|
||||
swap [ over ]
|
||||
while < 1 dup:
|
||||
if % over 2:
|
||||
|
|
@ -10,25 +10,27 @@ hailstone:
|
|||
swap push-through rot dup
|
||||
drop
|
||||
|
||||
local :h27 hailstone 27
|
||||
. = 112 len h27
|
||||
. = 27 h27! 0
|
||||
. = 82 h27! 1
|
||||
. = 41 h27! 2
|
||||
. = 124 h27! 3
|
||||
. = 8 h27! 108
|
||||
. = 4 h27! 109
|
||||
. = 2 h27! 110
|
||||
. = 1 h27! 111
|
||||
if = (name) :(main):
|
||||
local :h27 hailstone 27
|
||||
!. = 112 len h27
|
||||
!. = 27 h27! 0
|
||||
!. = 82 h27! 1
|
||||
!. = 41 h27! 2
|
||||
!. = 124 h27! 3
|
||||
!. = 8 h27! 108
|
||||
!. = 4 h27! 109
|
||||
!. = 2 h27! 110
|
||||
!. = 1 h27! 111
|
||||
|
||||
local :max 0
|
||||
local :maxlen 0
|
||||
for i range 1 99999:
|
||||
dup len hailstone i
|
||||
if < maxlen:
|
||||
set :maxlen
|
||||
set :max i
|
||||
else:
|
||||
drop
|
||||
|
||||
print( "number: " max ", length: " maxlen )
|
||||
local :max 0
|
||||
local :maxlen 0
|
||||
for i range 1 99999:
|
||||
dup len hailstone i
|
||||
if < maxlen:
|
||||
set :maxlen
|
||||
set :max i
|
||||
else:
|
||||
drop
|
||||
!print( "number: " to-str max ", length: " to-str maxlen )
|
||||
else:
|
||||
@hailstone
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@ package main
|
|||
|
||||
import "fmt"
|
||||
|
||||
func hs(n int, seq *[]int) {
|
||||
s := append((*seq)[:0], n)
|
||||
// 1st arg is the number to generate the sequence for.
|
||||
// 2nd arg is a slice to recycle, to reduce garbage.
|
||||
func hs(n int, recycle []int) []int {
|
||||
s := append(recycle[:0], n)
|
||||
for n > 1 {
|
||||
if n&1 == 0 {
|
||||
n = n / 2
|
||||
|
|
@ -12,21 +14,18 @@ func hs(n int, seq *[]int) {
|
|||
}
|
||||
s = append(s, n)
|
||||
}
|
||||
*seq = s
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
var seq []int
|
||||
|
||||
hs(27, &seq)
|
||||
seq := hs(27, nil)
|
||||
fmt.Printf("hs(27): %d elements: [%d %d %d %d ... %d %d %d %d]\n",
|
||||
len(seq), seq[0], seq[1], seq[2], seq[3],
|
||||
seq[len(seq)-4], seq[len(seq)-3], seq[len(seq)-2], seq[len(seq)-1])
|
||||
|
||||
var maxN, maxLen int
|
||||
for n := 1; n < 100000; n++ {
|
||||
// reusing seq reduces garbage
|
||||
hs(n, &seq)
|
||||
seq = hs(n, seq)
|
||||
if len(seq) > maxLen {
|
||||
maxN = n
|
||||
maxLen = len(seq)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
function hailstone(n)
|
||||
seq = [n]
|
||||
while n>1
|
||||
n = n % 2 == 0 ? n/2 : 3n + 1
|
||||
n = n % 2 == 0 ? n >> 1 : 3n + 1
|
||||
push!(seq,n)
|
||||
end
|
||||
return seq
|
||||
|
|
|
|||
52
Task/Hailstone-sequence/Limbo/hailstone-sequence.limbo
Normal file
52
Task/Hailstone-sequence/Limbo/hailstone-sequence.limbo
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
implement Hailstone;
|
||||
|
||||
include "sys.m"; sys: Sys;
|
||||
include "draw.m";
|
||||
|
||||
Hailstone: module {
|
||||
init: fn(ctxt: ref Draw->Context, args: list of string);
|
||||
};
|
||||
|
||||
init(nil: ref Draw->Context, nil: list of string)
|
||||
{
|
||||
sys = load Sys Sys->PATH;
|
||||
|
||||
seq := hailstone(big 27);
|
||||
l := len seq;
|
||||
|
||||
sys->print("hailstone(27): ");
|
||||
for(i := 0; i < 4; i++) {
|
||||
sys->print("%bd, ", hd seq);
|
||||
seq = tl seq;
|
||||
}
|
||||
sys->print("⋯");
|
||||
|
||||
while(len seq > 4)
|
||||
seq = tl seq;
|
||||
|
||||
while(seq != nil) {
|
||||
sys->print(", %bd", hd seq);
|
||||
seq = tl seq;
|
||||
}
|
||||
sys->print(" (length %d)\n", l);
|
||||
|
||||
max := 1;
|
||||
maxn := big 1;
|
||||
for(n := big 2; n < big 100000; n++) {
|
||||
cur := len hailstone(n);
|
||||
if(cur > max) {
|
||||
max = cur;
|
||||
maxn = n;
|
||||
}
|
||||
}
|
||||
sys->print("hailstone(%bd) has length %d\n", maxn, max);
|
||||
}
|
||||
|
||||
hailstone(i: big): list of big
|
||||
{
|
||||
if(i == big 1)
|
||||
return big 1 :: nil;
|
||||
if(i % big 2 == big 0)
|
||||
return i :: hailstone(i / big 2);
|
||||
return i :: hailstone((big 3 * i) + big 1);
|
||||
}
|
||||
|
|
@ -26,6 +26,5 @@ hailstone: procedure expose @.; parse arg n 1 s 1 o /*N,S,O = 1st arg.*/
|
|||
else n=n%2 /* " " " even, perform fast ÷ */
|
||||
s=s n /*build a sequence list (append).*/
|
||||
end /*forever*/
|
||||
@.o=s /*memoization for this hailstone.*/
|
||||
@.o=subword(s,2)
|
||||
@.o=subword(s,2) /*memoization for this hailstone.*/
|
||||
return s
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue