Skip to content

Commit

Permalink
Improve prime_sequence_generator.gr. update testscript (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly authored Sep 6, 2024
1 parent 98400d6 commit 462c40c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
35 changes: 14 additions & 21 deletions examples/prime_sequence_generator.gr
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 462c40c

Please sign in to comment.