Reputation: 81
I'll start off by saying that I am terrible when it comes to numbers. Anyways, I'm writing a function 'testPrime' that accepts a # as a parameter and returns true if the number is prime or false if it ain't.
Code:
function testPrime(n) {
if (n % 2 !== 0 && n % 3 !== 0) {
return true;
} else {
return false;
}
};
So I'm not too sure if this right (am I?) but when I tested out a gazillion numbers on console by typing in testPrime(insert number here), it seems like only 2 and 3 are getting returned as false, when they should be getting returned as true. (2 and 3 are prime ... right?) Did I leave something out in my if statement? Can't seem to find the issue. Help would be appreciated. Thanks.
Upvotes: 1
Views: 248
Reputation: 768
Uh...I have way too much time on my hands. Anyway, I'd probably go ahead and collect the numbers the item is divisible by, though this adds lots of extra calculation(If you just want prime, you can stop after the first).
html:
<div class="primeTest">
<label>Enter a number</label><input type='text' id="number" class="input"/>
<div class="results"></div>
<div class="divisibleBy"></div>
</div>
script:
var test = function(n){
var max = Math.sqrt(n),
t = 2,
divBy = [],
str;
if (!(n > 1 && n < 4)) {
while (t <= max) {
if (n % t === 0) divBy.push([t,n/t]);
t++;
}
}
if (divBy.length) {
var str = '';
divBy.forEach( function(d){
str += '<div>' + d[0] + ' * ' + d[1] + '</div>';
});
$('.results').html( n + ' is NOT a prime number.');
$('.divisibleBy').html( 'It is a product of each of the following:' + str );
} else {
$('.results')
.html(n + ' IS a prime number.')
.addClass('prime');
}
}
$('.input').keyup(function() { test( this.value ) });
And here's a fiddle: https://jsfiddle.net/mckinleymedia/upvjuh51/3/
Upvotes: 1
Reputation: 346
Also you can use recursion, Try with this function:
var x = 2;
function testPrime(n) {
if(n == x){
return true;
}
else if((n > x) && (n%x == 0) ){
return false;
}
else if((n > x) && (n%x != 0)){
x++;
testPrime(n);
}
}
Upvotes: 0
Reputation: 507
Hello you are dividing number only by 2 and 3 that is not a proper way to find a prime number. as there are numbers which are divisible only by prime numbers. for example you 49 is not a prime number as it is divisible by 7 only. so we have to consider all the prime number less than half the input number. you can have a look in following code for reference. it'll check if the number is prime or not.
var n = 29;
var flag = true;
for(var i=2; i<n/2;i++){
if(n%i == 0){
flag = false;
break;
}
}
var docElem = document.getElementById("test");
if(flag){
docElem.innerHTML = n + " is Prime number";
}else{
docElem.innerHTML = n + " is not a Prime number";
}
<div id="test"><div>
Upvotes: 3
Reputation: 47
In the given code you tested prime only for 2 and 3. So if n=25 than your program return true even though 25 is not a prime number , because 25 will be checked for only 2 and 3 not 5.
You should write your code as follow by adding a loop:
for (i=2 ; i<n ;)
if (n%i == 0) {
return false;
i++;
}
else {
return true;
}
Upvotes: 1