#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Tue, 19 Nov 2019 14:12:45 +0100
changeset 5259 fa49d09cad7b
parent 5258 18c5f6630c12
child 5260 2cc9d375be98
#FEATURE by cg class: RecursiveSeriesStream added: #startValue: class: RecursiveSeriesStream class comment/format in: #example #example2
RecursiveSeriesStream.st
--- a/RecursiveSeriesStream.st	Tue Nov 19 11:23:27 2019 +0100
+++ b/RecursiveSeriesStream.st	Tue Nov 19 14:12:45 2019 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "{ Package: 'stx:libbasic2' }"
 
 "{ NameSpace: Smalltalk }"
@@ -47,26 +49,36 @@
 
 example
 "
-        |s|
+        |collatz|
 
-        s := RecursiveSeriesStream on:[:n | n/2] startValue:1.
-        s next:10.
-        s next:10.
-
-        s := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:10.
-        s next:10.
-        s next:10.
+        collatz := [:n | RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:n].
+        (collatz[20]) next:20.  
+        (collatz[24]) next:20.  
+        (collatz[25]) next:30.   
 "
 !
 
 example2
 "
-        |s|
+        |max bits collatz newNums|
 
-        1 to:100 collect:[:n0 |
-                s := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:n0.
-                s next:10.   
-        ]
+        newNums := OrderedCollection new.        
+        bits := SortedCollection new.
+        max := 2.
+        collatz := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:1.
+        1 to:100000 do:[:n0 |
+            newNums removeAll.    
+            collatz startValue:n0.
+            [
+                |n|
+                n := collatz next.
+                newNums add:n.
+                n <= max
+            ] whileFalse.
+            bits addAll:newNums.
+            [bits notEmpty and:[bits first <= (max+1)]] whileTrue:[max := max max:(bits first). bits removeFirst].
+        ].
+        self halt.
 "
 ! !
 
@@ -81,6 +93,10 @@
 iterator:aOneArgBlock startValue:startValue
     iterator := aOneArgBlock.
     lastValue := startValue.
+!
+
+startValue:startValue
+    lastValue := startValue.
 ! !
 
 !RecursiveSeriesStream methodsFor:'reading'!