...

View Full Version : I want to make my script beta



shockingu
03-18-2004, 08:55 PM
i am learning from a good book called javascript in easy steps.
and I am only on chapter 3. i thought that i could make a program that is for my friend and i have most of the knowlege on how to do it.

i am wanting my program to tell which numbers are prime or not so when i have entered them in a prompt then the code can tell the user if the number is prime or not. i have done some of the code but how i have done it i will need thousands of lines.

var num = prompt( "Type in a number to see if it is a prime number or not.", "");
if (num == 1)
{
document.writeln( "The number " + num + " is a prime number." );
}
else
if (num == 3)
{
document.writeln( "The number " + num + " is a prime number." );
}
else
if (num == 5)
{
document.writeln( "The number " + num + " is a prime number." );
}
else
if (num == 2)
{
document.writeln("The number " + num + " is not a prime number." );
}
else
if (num == 4)
{
document.writeln("The number " + num + " is not a prime number." );
}
else
if (num == 6)
{
document.writeln("The number " + num + " is not a prime number." );
}

the above is some of the code that i have done but as you can see it will take thousands of lines.
i want the code so that it looks somthing like this

var num = prompt( "Type in a number to see if it is a prime number or not.", "");
if (num == 1,3,5,7,11,13)
{
document.writeln( "The number " + num + " is a prime number." );
}
else
// even numbers
if (num == 2,4,6,8,10,12)
{
document.writeln("The number " + num + " is not a prime number." );
}

where the 1,3,5,7,11 prime numbers are the numbers that the code understands are prime and the code understands that the 2,4,6,8,10,12 is the non prime numbers.

i dont mind that i am going to get a big list of prime numbers like this 1, 3, 5, 7, 11, 13, 17, 19 but going all the way up to the number 100 in prime and a list of non primes going up to 100.

i want to know if this is posible to do it this way or if i would have to make thousands of lines of code. This is in a .js file.

Roy Sinclair
03-18-2004, 09:08 PM
What you need is a function you can call that'll test a number to see if it's prime. In fact you've already got a bug in your current script because the number 2 is a prime number and you're saying it isn't.

swmr
03-18-2004, 09:17 PM
If you just want to differentiate between even & odd #s, this might be useful:



var num = prompt( "Type in a number to see if it is a prime number or not.", "");
if (num && !isNaN(num)){
(num % 2) == 1 ? document.writeln( "The number " + num + " is an odd number." ) :
document.writeln("The number " + num + " is an even number." );
}

Garadon
03-19-2004, 12:39 AM
This runs by a logic I made up myself so it migth not stick:
it return false to non primes and true to primes,

its logic:
is that any number moduled with every prime before it excluding 1 that give a result that are not 0 must be a prime any that gives 0 ain't a prime.

It tests out to correctly claim that 7919 is a prime after 2-3seconds


function isPrimeTest(aNr)
{
if(aNr==1||aNr==2)return true;
if((aNr%2)==0)return false;
var J,I,P;
I=3;
P=0;
var PrimTals=new Array();
PrimTals[P]=I;
I=5;
while(I<aNr)
{
if((aNr%PrimTals[P])==0)return false;
J=0;
isPrime=true;
while(J<=P)
{
if((I%PrimTals[J])==0)
{
isPrime=false;
break;
}
J++;
}
if(isPrime)
{
P+=1;
PrimTals[P]=I;
}
I+=2;
}
if((aNr%PrimTals[P])==0)return false;
return true;
}

Unit
03-19-2004, 02:12 AM
While the mathematical definition goes by your implementation, its often much simpler to use shortcuts when trying to figure out a certain thing. like avoiding loops inside loops. The function below achieves the same thing making use of a few facts to our advantage. You might find this as a much simpler approach when you need to write this again in future :)



<script>
function isPrimeTest(aNr)
{
if(aNr == 2) return true; // 2 is prime.
if(aNr%2 == 0) return false; // if it is divisible by 2 its not prime.
var n=aNr/2; // we only need to check if it has a factor less than half.
var i=3; // start with 3 as a possible factor.
while(i<n)
{
if(aNr%i == 0) return false; // we found a factor, so the number is not prime!
i+=2; // no need to test with even numbers, so skip over them.
}
return true; // no factors found.
}

var count=0;

for(i=1;i<10000;i++)
if(isPrimeTest(i)) count++;

alert(count);
</script>

shockingu
03-19-2004, 09:07 PM
You are all so brainy Thankyou



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum