Good evening I have the last few days been trying to get an assignment about Prime Numbers in Java Script to work, so far not so good. This is my assignment,
The Sieve of Eratosthenes
An integer is a prime number if it is evenly divisible by only itself and 1. There are several ways to find all prime numbers within a given range of integers, and the Sieve of Eratosthenes is an algorithm for to do just that. It operates as follows:
Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain as 1. All other array elements will eventually be set to zero.
Starting with array subscript 2 (subscript 1 must be prime), every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. For array subscript 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, etc.); for array subscript 3, all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc.); and so on.
When this process is complete, the array elements that are still set to 1 indicate that the subscript is a prime number. These subscripts can then be printed. Write a script that uses an array of 1000 elements to determine and print the prime numbers between 1 and 999. Ignore element 0 of the array.
_______
This is the coding I came up with but its not what i am supposed to do as i was just trying to get the prime numbers to work,
here is my code
<HTML>
<head>
<title>Find the Prime Numbers</title>
</head>
<body>
<p><center>
<!--
A number is a prime number if it's an integer greater than zero and it can not be divided exactly by other integers except 1 and itself.
So number 1, 2 and 3 are prime numbers.
Beyond number 3, If a number is not a prime number, it can be divided exactly by one of the integers between 2 and the square root of this number.
-->
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function isPrimeNumber(theTestNumber) {
var SQRT_theTestNumber=Math.floor(Math.sqrt(theTestNumber));
var isPRIME=1;
for (var i=2;i<=SQRT_theTestNumber;i++){
if ((theTestNumber%i)==0) {
isPRIME=0;
break;
}
}
if(isPRIME==1){
window.document.write("<b><font color='#0000FF'>"+theTestNumber+"</font></b> is a prime number!<br>");
}
}
var StartingNumber=parseInt(window.prompt("Please input the starting number:","1"));
var StoppingNumber=parseInt(window.prompt("Please input the stopping number:","1000"));
if (StartingNumber<=0 || StoppingNumber<=0){
window.alert("The starting number or the stopping number should be greater than zero!");
}
else if (isNaN(StartingNumber) || isNaN(StoppingNumber)){
window.alert("You didn't input a right value for the starting number or the stopping number!");
}
else if (StartingNumber>StoppingNumber){
window.alert("The starting number should be less than or equal to the stopping number!");
}
else {
for (var j=StartingNumber;j<=StoppingNumber;j++){
if(j<=3){
window.document.write("<b><font color='#0000FF'>"+j+"</font></b> is a prime number!<br>");
}
else {
isPrimeNumber(j);
}
}
}
// End -->
</SCRIPT>
</p></center>
</body>
</HTML>
_________
What do i need to do to make this proper to the assignment, i was totally confused and on my last resort on this. thanks all