Click to See Complete Forum and Search --> : Array's and primes.


David Harrison
05-31-2003, 02:54 PM
Just a couple of questions, first of all does it matter if I don't define how many numbers, (in my case), there are in an array. If it does then does it matter if I tell it that there are, for example 1000 numbers and the only fill it with say, 100 and leave the others blank.

Now for the next question how can I make this prime number finder faster.

<html>

<head>

<title>Primes</title>

</head>

<body>

<script language="javascript">

l=1000;n=3;p=1;

primes = new Array()

primes[0]=2;

while(n<=l){q=1;

if((n-1)/1000==Math.round((n-1)/1000)){window.status="Numbers checked so far: "+(n-1);}

while(primes[q]<=Math.floor(Math.sqrt(n)) && n/primes[q]!=Math.round(n/primes[q])){q++;}
if(n/primes[q]!=Math.round(n/primes[q])){primes[p]=n;p++;}

n+=2;}

window.status="Up to and including "+l+" there are "+p+" prime numbers.";

document.write("The following numbers are all prime numbers:<br>"+primes.join(", "));

</script>

</body>

</html>

because although it only takes a blink of an eye to run this script even with the 395MHz pentium 2 that I'm using now, I need to find much bigger (100's of millions bigger) prime numbers.

If you've any ideas, then please let me know.

David Harrison
05-31-2003, 03:57 PM
Well in that case why does anyone bother to define the length of an array.

Also, just anoth plea, do you know how to make my script run any faster with any js trick you may have.

Oh and also, now that I think about it, how do you get that sometimes useful, but in my case very annoying message from popping up that says, "There is a script on this page that is making it run slow...", I leav it going to find primes up to say, 10,000,000 and I come back and it's got that message on the screen and it's only done 250,000 or so.

Charles
05-31-2003, 05:43 PM
Originally posted by Dave Clark
myArray[myArray.length] = "new element"; Or, more simply:

myArray.push('new element');

David Harrison
06-01-2003, 03:33 PM
Originally the window.status message was not designed as a progress report but as an attempt to stop that message, so is there perhaps something that can be shown in the main body of the page that will stop it?