Prime Number Searcher
Bas Rosier Jul 25, 2005
This script will find and display all the prime numbers up to a user-determined number.
The JavaScript Source: Math Related: Prime Number Searcher
Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Command-s). The script is yours!!!
<!-- TWO STEPS TO INSTALL PRIME NUMBER SEARCHER:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<style type="text/css">
<!--
textarea {
border: none;
color: red;
background-color: white;
}
-->
</style>
<script type="text/javascript">
<!-- Begin
/* This script and many more are available free online at
The JavaScript Source!! http://www.javascriptsource.com
Created by: Bas Rosier :: http://www.geocities.com/bas5432 */
var n = 3; // number of which is investigated if it is a prime number
var i; // number of division
var out; // this equals the division n/i or prm/dlf
var isPriem; // true when a number is a prime number, otherwise false
function askMax() {
document.output.outputNumbers.value="";
max = prompt("Up to what number do you want me to search for prime numbers?","");
Priem();
}
function Priem() {
i=n;
Search(n,i);
if(isPriem==true) {document.output.outputText.value+="\n"+n+" is een priemgetal"; document.output.outputNumbers.value+=n+", "; }
if(isPriem==false) {document.output.outputText.value+="\n"+n+" is geen priemgetal"; }
document.output.outputText.value="";
n++;
if(n<=max)
{Priem();}
}
function Search(prm,dlf) {
dlf--;
out = prm/dlf;
document.output.outputText.value+="\n"+prm+"/"+dlf+"="+out;
if((out!=Math.round(out)) && (dlf>2)) {
document.output.outputText.value+="\ndlf is nog groter dan twee en er is nog geen ronde uitkomst gevonden";
Search(prm,dlf);
}
else if(out==Math.round(out)) {
document.output.outputText.value+="\nronde uitkomst gevonden";
isPriem=false;
}
else if((dlf==2) && (out!=Math.round(out))) {
document.output.outputText.value+="\ndlf is 2 en er is nog geen ronde uitkomst gevonden";
isPriem=true;
}
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<div align="center">
<form name="output">
<p>
"1" is not a prime number, even if it's only divisible by 1 and itself. "0" is not a prime number because every result of a division with 0 is 0. Negative numbers behave in the same way positive numbers do. (If you enter a number that is too high, you might get an out of memory error.)</p>
<input type="button" value="Click to Enter a Prime Number" onclick="javascript: askMax();">
<br><br>
<input type="hidden" name="outputText">The prime numbers will be displayed below.<br><br>
<textarea cols="40" rows="5" name="outputNumbers" readonly></textarea>
</form>
</div>
<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 2.83 KB --> Did you use this script? Do you like this site? Please link to us!
Leave a Response
(0 comments)