Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6
Thread: I want to make my script beta

03182004, 08:55 PM #1
 Join Date
 Mar 2004
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
I want to make my script beta
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.
03182004, 09:08 PM
#2
 Join Date
 Jun 2002
 Location
 Wichita
 Posts
 3,880
 Thanks
 0
 Thanked 0 Times in 0 Posts
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.
Check out the Forum Search. It's the short path to getting great results from this forum.
03182004, 09:17 PM
#3
 Join Date
 Feb 2003
 Posts
 638
 Thanks
 0
 Thanked 0 Times in 0 Posts
If you just want to differentiate between even & odd #s, this might be useful:
Code: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." ); }
hmm... ?
03192004, 12:39 AM
#4
 Join Date
 Jul 2002
 Posts
 698
 Thanks
 0
 Thanked 0 Times in 0 Posts
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 23seconds
Code:function isPrimeTest(aNr) { if(aNr==1aNr==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; }
Last edited by Garadon; 03192004 at 12:47 AM.
03192004, 02:12 AM
#5
 Join Date
 Feb 2004
 Location
 WA
 Posts
 213
 Thanks
 0
 Thanked 0 Times in 0 Posts
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
PHP Code:
<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>
Nobody is Perfect. I am Nobody.
03192004, 09:07 PM
#6
 Join Date
 Mar 2004
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
WOW
You are all so brainy Thankyou