The sieve of eratosthenes

JavaScript/HTML

<html>
<!--
        Written by Jeff Mikels (http://jeff.mikels.cc),
        borrowing heavily from the Python version at
        http://badcomputer.org/code/eratos/python1.bot
-->
<script language="javascript">

function sieve(x) {
        y = Math.floor(Math.sqrt(x));
        var a = new Array();

        a.push(2);
        for (i = 3; i <= x; i+=2) {
                a.push(i)
        }
        a.reverse();
        var primes = new Array();
        while ((current_prime = a.pop()) <= y) {
                primes.push(current_prime);
                for (index in a) {
                        if (a[index] % current_prime == 0) {
                                a.splice(index,1);
                        }
                }
        }
        primes.push(current_prime);
        a.reverse();
        primes = primes.concat(a);
        return primes.join(' ');
}

function dosieve() {
        input_value = document.getElementById('input').value;
        document.getElementById('output').innerHTML = sieve(input_value);
}
</script>
<!-- INPUT AND OUTPUT -->
<p>I will find all primes up to the number you enter here...
<br />
        <input type=text id=input name=input value="" onchange="dosieve();"/>
        <button onclick="dosieve();"/>click here</button>
<p>Your primes...
<br /><div id="output" style="width:100%"></div>
</html>

Run it!

I will find all primes up to the number you enter here...

Your primes...

back to Eratosthenes page

stats

It is Friday December 05, 2008 10:22 am
This page served 2068 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: 10:22:40 up 24 days, 14:32, 2 users, load average: 0.00, 0.01, 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