-
Notifications
You must be signed in to change notification settings - Fork 578
Open
Description
Hi! Wren is a great little language! (And I say this despite my strong background in Lua...)
I've had some fun trying out various approaches of the Sieve of Eratosthenes to find prime numbers, when I noticed something odd.
Most sieve implementations tend to speed up as they fill up the range. One Example is the following list-based code:
var sieve = List.filled(99999998, true)
for (i in 2...sieve.count) {
if (sieve[i]) {
System.print(i)
var mult = i
while (mult < sieve.count) {
sieve[mult] = false
mult = mult + i
}
}
}
When you run this, you'll notice how the first few primes take a while to print, before execution speeds up and the numbers come faster and faster.
With the following sequence-based implementation, it's the other way around however:
var seq = 2..99999997
while (!seq.isEmpty) {
//don't know a better way to peek at the first element in a sequence
var elem=seq.take(1).toList[0]
System.print(elem)
seq = seq.where{ |n| n % elem != 0 }
}
Something is very wrong with the output speed of that code. It starts printing quite quickly but then it slows to a crawl. Any ideas?
I'm using wren 0.4.0 on x86_64 Linux.
Metadata
Metadata
Assignees
Labels
No labels