diff --git a/examples/prime_sequence_generator.gr b/examples/prime_sequence_generator.gr index 1cffc975..fe70c3fb 100644 --- a/examples/prime_sequence_generator.gr +++ b/examples/prime_sequence_generator.gr @@ -2,42 +2,35 @@ func primesGen(maxSieve) { sieve = [true] * maxSieve - num = 2 - currentPrime = 2 + prime = 2 nextPrime = () => { - for currentPrime < maxSieve { - if sieve[currentPrime] { - prime = currentPrime - currentPrime++ - mult = prime * prime + for prime < maxSieve { + if sieve[prime] { + p = prime++ + mult = p * p for max(maxSieve - mult, 0) { - if mult % prime == 0 { + if mult % p == 0 { sieve[mult] = false } mult++ } - return prime + return p } else { - currentPrime++ + prime++ } } - return nil + nil } } maxS = 1000 // use 100_000 for profiling. primeIter = primesGen(maxS) -p = primeIter() -printf("| %4d", p) -n = 1 -for p <= maxS { - p = primeIter() - if p != nil { - printf(" %4d", p) - if ++n % 15 == 0 { - print("\n|") - } +n = 0 +for (p = primeIter()) != nil { + if n++ % 15 == 0 { + print("\n|") } + printf(" %4d", p) } println() diff --git a/go.mod b/go.mod index 382248fb..191f58ae 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( fortio.org/sets v1.2.0 fortio.org/struct2env v0.4.1 fortio.org/terminal v0.8.2 - fortio.org/testscript v0.3.1 // only for tests + fortio.org/testscript v0.3.2 // only for tests fortio.org/version v1.0.4 github.com/rivo/uniseg v0.4.7 ) diff --git a/go.sum b/go.sum index 3562ee27..4df7a762 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,8 @@ fortio.org/term v0.23.0-fortio-6 h1:pKrUX0tKOxyEhkhLV50oJYucTVx94rzFrXc24lIuLvk= fortio.org/term v0.23.0-fortio-6/go.mod h1:7buBfn81wEJUGWiVjFNiUE/vxWs5FdM9c7PyZpZRS30= fortio.org/terminal v0.8.2 h1:kluLHjxsuflyRpkp9HzVM5Df8mbiX1tdDRN9Jdlp2M4= fortio.org/terminal v0.8.2/go.mod h1:4mFl6U7FmnQ+D/NZuxq05QDX/guBTwCRb2+DxTOj4Tg= -fortio.org/testscript v0.3.1 h1:MmRO64AsmzaU1KlYMzAbotJIMKRGxD1XXssJnBRiMGQ= -fortio.org/testscript v0.3.1/go.mod h1:7OJ+U4avooRNqc7p/VHKJadYgj9fA6+N0SbGU8FVWGs= +fortio.org/testscript v0.3.2 h1:ks5V+Y6H6nmeGqnVlZuLdiFwpqXemDkEnyGgCZa/ZNA= +fortio.org/testscript v0.3.2/go.mod h1:Z2kUvEDHYETV8FLxsdA6zwSZ8sZUiTNJh2Dw5c4a3Pg= fortio.org/version v1.0.4 h1:FWUMpJ+hVTNc4RhvvOJzb0xesrlRmG/a+D6bjbQ4+5U= fortio.org/version v1.0.4/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0= github.com/kortschak/goroutine v1.1.2 h1:lhllcCuERxMIK5cYr8yohZZScL1na+JM5JYPRclWjck=