The sieve of eratosthenes

polyglot page

indexing
  description: "The Sieve of Eratosthenes in Eiffel"
  author: "Rob Hoelz"

class SIEVE create
    make
feature
    primes: ARRAY [INTEGER]

    make is
    local
        index: INTEGER
        args: ARGUMENTS
    do
        !!args
        if args.argument_count > 0 then
            !!primes.make(2, args.argument(1).to_integer)
            from
                index := primes.lower
            until
                index > primes.upper
            loop
                primes.put(1, index)
                index := index + 1
            end

            generate
            print_primes
        end
    end



feature { NONE }
    generate is
    local
        curr_prime: INTEGER
    do
        from
            curr_prime := 2
        until
            (curr_prime * curr_prime) > primes.upper
        loop
            remove_multiples(curr_prime)
            curr_prime := next_prime(curr_prime)
        end
    end

    print_primes is
    local
        index: INTEGER
    do
        from
            index := primes.lower
        until
            index > primes.upper
        loop
            if (primes @ index) = 1 then
                io.put_integer(index)
                io.put_string(" ")
            end
            index := index + 1
        end
        io.put_new_line
    end

    remove_multiples(base: INTEGER) is
    local
        index: INTEGER
    do

        from
            index := base * 2
        until
            index > primes.upper
        loop
            primes.put(0, index)
            index := index + base
        end
    end

    next_prime(curr_prime: INTEGER): INTEGER is
    do
        from
            Result := curr_prime + 1
        until
            (Result > primes.upper) or ((primes @ Result).is_equal(1))
        loop
            Result := Result + 1
        end
    end
end

back to Eratosthenes page

stats

It is Monday September 08, 2008 1:11 am
This page served 726 times
This page last modified: April 14, 2008 11:28 am
Your IP address is: 38.103.63.61
You are browsing using: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
You are browsing from: United States.
badcomputer.org's uptime: 01:11:11 up 39 days, 16:17, 2 users, load average: 0.00, 0.03, 0.00

local

home | unix stuff | dir2ogg | sneetchalizer | wmainfo | q&d guide to permissions | q&d guide to tar and gzip | code | MS rant | browser shootout | linux & iAudio X5 | photos | music | programming poetry | sieve of Eratosthenes | plea | rain | suffer | archive | about | recipes | compaqr3000 | sitemap

search

Google

credits

hacker emblem

This page, and all pages on this site were created and are maintained by Darren Kirby using valid XHTML 1.0 and CSS, and are ©copyright 2002 - 2008. The Penguin image was created by Tukka, and is used by permission. Inspiration for the look of this site was provided by Eric A. Meyer's CSS gallery. This website runs on Gentoo Linux. It is served by Apache. PHP and MySQL hold together the backend.

advertisement