Forum Moderators: open

Message Too Old, No Replies

Prime Numbers for Javascript

javascript help

         

GoldTraveller321

2:16 am on Nov 5, 2010 (gmt 0)

10+ Year Member



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

daveVk

4:58 am on Nov 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The instruction seem clear
- set up array
- run algorithm
- print results (non zero array entries)

I don't see that in your code ?

GoldTraveller321

5:38 am on Nov 5, 2010 (gmt 0)

10+ Year Member



Then what type of code is the solution to this problem then?

GoldTraveller321

5:43 am on Nov 5, 2010 (gmt 0)

10+ Year Member



i found this to be quite confusing, what would you all write for this type of program?

daveVk

6:57 am on Nov 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// set up array
var arr = [];
for (var i=1;i<=1000;i++){
arr[ i ] = 1;
}
// run algorithm
for (var i=2;i<=1000;i++){
if ( arr[ i ] === 1 ) { ... update arr per algorithm ... }
}
// print results (non zero array entries)
for (var i=1;i<=1000;i++){
if ( arr[ i ] === 1 ) { window.document.write( " " + i ); }
}

Fotiman

1:41 pm on Nov 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, 1 is not a prime number.

Fotiman

2:24 pm on Nov 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



JavaScript arrays are indexed starting at zero. I presume that "subscript" as you've used it means "index".


Array Values: [1 1 1 1 1 ... 1]
Array Indexes: 0 1 2 3 4 ... 999



Starting with array subscript 2 (subscript 1 must be prime)



Array Values: [1 1*1*1 1 ... 1]
Array Indexes: 0 1*2*3 4 ... 999




<html>
<head>
<title>Calculating Primes</title>
</head>
<body>
<div id="results"></div>
<script type="text/javascript">
var arr = [],
arrSize = 1000,
i,
results = document.getElementById('results'),
htmlStr = '';
// Create an array with all elements initialized to 1 (true).
for (i = 0; i < arrSize; i++) {
arr[i] = 1;
}
// Iterate through the array starting with array subscript 2
for (i = 2; i < arrSize; i++) {
// every time an array element is found whose value is 1
if (arr[i] === 1) {
// set to zero every element whose subscript is a multiple
for (j = 2; i * j < arrSize; j++) {
arr[i * j] = 0;
}
}
}
// the array elements that are still set to 1 indicate that the subscript is a
// prime number. These subscripts can then be printed.
// print the prime numbers between 1 and 999. Ignore element 0 of the array.
for (i = 1; i < arrSize; i++) {
// Note, 1 is not actually a prime number, so this should really start
// with i = 2 instead. But leaving it incorrect as per the directions.
if (arr[i] === 1) {
htmlStr += ', ' + i;
}
}
// Strip off the first ', ' from the htmlStr
if (htmlStr.length > 2) {
htmlStr = htmlStr.substr(2);
}
// Print the results
results.innerHTML = htmlStr;
</script>
</body>
</html>

Fotiman

2:28 pm on Nov 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, if this is a homework assignment, I'm not trying to do your homework for you. Please make sure that you fully grasp what I've done, and ask questions here.

GoldTraveller321

10:46 pm on Nov 5, 2010 (gmt 0)

10+ Year Member



Thanks a lot for the coding. I can work on it.
Just curious the above code you provided, would that be exactly how the question is saying? altogether.

Thanks again

Fotiman

1:06 am on Nov 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, and I tried to comment it well so you could understand exactly how it follows the question. But as I said, please feel free to ask questions on any of it. :)

GoldTraveller321

1:33 am on Nov 6, 2010 (gmt 0)

10+ Year Member



is arrSize the same as saying Array size? i was writing array when i was trying to make the codes.

Lots of it makes sense since u wrote all the comments in the line.

Fotiman

3:15 pm on Nov 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In my example, arrSize is a variable which holds the number of items you want in the array. For example, if you wanted to increase the number of items in the array to 2000, you simply change that value to 2000. It is not getting the size of the array, but rather it's the number that is used to create the array. After the code that creates the array, you could instead use arr.length to get the same value.

GoldTraveller321

10:54 pm on Nov 6, 2010 (gmt 0)

10+ Year Member



sounds good