in reply to Re: Sieve of Eratosthenes Golf?
in thread Sieve of Eratosthenes Golf?

I don't understand those at all really, but the second one did a million in about a minute, so not bad at all. It did tell me that a million was a prime number though...


($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re^3: Sieve of Eratosthenes Golf?
by BrowserUk (Patriarch) on Apr 07, 2005 at 07:52 UTC
    It did tell me that a million was a prime number though...

    Due to golfing it, it doesn't handle the very last number correctly.

    I don't understand those at all really,

    They both work in essentially the same way. The first one creates a string of $limit '1's. It then uses a regex to scan the string substituting '0's for '1's, for all the multiples. It then scans the string, incrementing a counter, looking for the remaining '1's and prints out the counter when it finds a '1'. It just uses the big string as a tally stick and 'crosses off' all the multiples.

    The second one does the same thing, but uses vec, and bits instead of bytes, to represent the tally stick.

    By avoiding building big lists/arrays, it allows you to go 30/250 or so times higher.

    If you're not golfing, it is easy to omit the even bytes/bits and therebye double the range again.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?