Sieve of Eratosthenes

Size
2,341 Kb
Views
18,216

How do I make an sieve of eratosthenes?

I implement a popular prime number generator.. What is a sieve of eratosthenes? How do you make a sieve of eratosthenes? This script and codes were developed by Mei Weng Brough-Smyth on 06 November 2022, Sunday.

Sieve of Eratosthenes Previews

Sieve of Eratosthenes - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Sieve of Eratosthenes</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <html>
<head> <title>Primes!</title> <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
</head>
<body id="bohday">	<h1>	Sieve of Eratosthenes :)	</h1> <h4>A quick and simple algorithm for finding all primes under a number.</b> </h4>	<input id="input" placeholder="Type integer here" onkeydown="validInteger()"/>	<br> Primes output: <p id = "junk"></p>
</body> <script src="js/index.js"></script>
</body>
</html>

Sieve of Eratosthenes - Script Codes CSS Codes

#bohday {	font-family: 'Open Sans', 'sans-serif';	background: skyBlue;	color: white;
}

Sieve of Eratosthenes - Script Codes JS Codes

// Mei Weng Brough-Smyth
// beepboop.com.au
var limit;
// Take input and ensure it is an integer.
function validInteger(){ document.getElementById("junk").innerHTML = ""; var input = document.getElementById('input').value; limit = input.match(/^\d+$/); limit++; // Add 1 to compensate for place 0 doSieve(limit);
}
// Take limit and output using sieve of eratosthenes
function doSieve(limit) { // Create array with limit amount of elements var sieve= new Array(limit); // set 1 and 2 to false sieve[0] = false; sieve[1] = false; // Set elements that divide equally by 2 to false and the rest to true for (var i=2; i<limit; i++) { i%2===0 ? sieve[i] = false : sieve[i] = true; } // Get square root of limit var squareLimit = Math.sqrt(limit); // Get all primes from 2 to the square limit for (i=2; i<squareLimit; i++) { if (sieve[i]) { // Get all multiples of these primes and set them to false for (var j=i*2; j<=limit; j+=i) { sieve[j] = false; } } } // Print out prime result for (i=1; i<limit; i++) { if (sieve[i]) { document.getElementById("junk").innerHTML += i + ", "; } }
}
Sieve of Eratosthenes - Script Codes
Sieve of Eratosthenes - Script Codes
Home Page Home
Developer Mei Weng Brough-Smyth
Username melatonind
Uploaded November 06, 2022
Rating 3
Size 2,341 Kb
Views 18,216
Do you need developer help for Sieve of Eratosthenes?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Mei Weng Brough-Smyth (melatonind) Script Codes
Create amazing sales emails with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!