JavaScript Source Code 3000: Calculators: Prime Number
Prime Number
Enter a number and JavaScript can instantly tell you if it is a prime number or not. I wish I had this little gem to help me in my math classes years ago!
Prime Number Calculator
Please enter a number:
JavaScript Source Code 3000: Calculator: Prime Number
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 Apple-s). The script is yours!!!
<!-- TWO STEPS TO INSTALL PRIME NUMBER CALCULATOR:
1. Paste the coding into the HEAD of your HTML document
2. Put the last code into the BODY of your HTML document -->
<!-- STEP ONE: Copy this code into the HEAD of your HTML document -->
<SCRIPT LANGUAGE="JavaScript">
<! >
<! >
<!-- Begin
function calculate(form) {
var num=parseInt(form.number.value);
if (isNaN(num) || num < 0) {
form.result.value=(form.number.value + " is not a valid number! Try again!");
}
if (num == 1 || num == 2) {
form.result.value=(num + " is prime!");
}
for (var i=2;i<num;i++) {
if (num % i == 0) {
var prime="yes";
form.result.value=(num + " is not prime. It is divisible by " + i + ".");
break;
}
if (num % i != 0) var prime="no";
}
if (prime == "no") form.result.value=(num + " is prime!");
}
// End -->
</SCRIPT>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<center>
<form name=form>
<h2>Prime Number Calculator</h2><p>
Please enter a number:<br>
<input type=text name=number size=7>
<input type=button value="Calculate" onClick="calculate(this.form)">
<P>
<input type=text name=result size=45 value="">
</form>
</center>
<!-- Script Size: 0.88 KB -->
Wyszukiwarka
Podobne podstrony:
AiSD Relatively prime numbersNumberAndDateFormatting csproj FileListAbsolutedocs Nebula Fix NumbersNumberFormatProviderDIXELL Prime DRegardie I Enochian NumbersEC vocabulary numbers 0 20 E with KEYNumberUpnumberofassessorsmath Complex Numbers and Complex ArithmeticnumberingtypeNumberOfInterveningJobswięcej podobnych podstron