check 1 number is a prime or not

Thread Solved

Join Date: Apr 2009
Posts: 28
Reputation: thebluestar is an unknown quantity at this point 
Solved Threads: 0
thebluestar thebluestar is offline Offline
Light Poster

check 1 number is a prime or not

 
-1
  #1
16 Days Ago
[CODE =C]
#include "conio.h"
#include "stdio.h"
#include "math.h"

int isPrime(int n)
{
int i,isTrue=1;
for(i=2; i<(int)sqrt(n); i++)
if(n%i==0)
return 0;
else return 1;
}

int main()
{
int N;
printf("Enter n: ");
scanf("%d",&N);
if(isPrime(N))
printf("prime");
else
printf("Not prime\n");
getch();
return 0;
}

[/CODE]
here is my source code, but when i test , sometimes it's true sometimes it's false. I don't know what is my mistake
Please help me solve it
Thanks a lot!
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training
 
0
  #2
16 Days Ago
Your isPrime function will return 0 for even-number and return 1 for odd-number because your loop always end at the first loop (because you always return a value at the first loop.)

Try this one instead:
  1. int i;
  2. for(i=2; i<(int)sqrt(n); i++) {
  3. if(n%i==0) return 0;
  4. }
  5. return 1;

You can also further optimize your code by:
int i;
int k = (int)sqrt(n);

for(i=2; i<k; i++) {
  if(n%i==0) return 0;
}
return 1;
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 28
Reputation: thebluestar is an unknown quantity at this point 
Solved Threads: 0
thebluestar thebluestar is offline Offline
Light Poster
 
-1
  #3
16 Days Ago
Originally Posted by invisal View Post
Your isPrime function will return 0 for even-number and return 1 for odd-number because your loop always end at the first loop (because you always return a value at the first loop.)

Try this one instead:
[code]
int i;
for(i=2; i<(int)sqrt(n); i++) {
if(n%i==0) return 0;
}
return 1;
[/c
You can also further optimize your code by:
int i;
int k = (int)sqrt(n);

for(i=2; i<k; i++) {
  if(n%i==0) return 0;
}
return 1;
ode]
I try it again but the results sometimes true sometimes wrong
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Chilton is an unknown quantity at this point 
Solved Threads: 2
Chilton Chilton is offline Offline
Newbie Poster
 
0
  #4
15 Days Ago
It should actually be "<= k" rather than less than.

Use 9 as an example and you'll see. The for loop ends and fails to mod 9 against 3, hence returning a value of 1.
Last edited by Chilton; 15 Days Ago at 3:36 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 28
Reputation: thebluestar is an unknown quantity at this point 
Solved Threads: 0
thebluestar thebluestar is offline Offline
Light Poster
 
0
  #5
15 Days Ago
Originally Posted by Chilton View Post
It should actually be "<= k" rather than less than.

Use 9 as an example and you'll see. The for loop ends and fails to mod 9 against 3, hence returning a value of 1.
Ukie! Thank you very much!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC