944,165 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 775
  • C RSS
Nov 7th, 2009
-1

check 1 number is a prime or not

Expand Post »
[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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
thebluestar is offline Offline
55 posts
since Apr 2009
Nov 7th, 2009
0
Re: check 1 number is a prime or not
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;
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Nov 8th, 2009
-1
Re: check 1 number is a prime or not
Click to Expand / Collapse  Quote originally posted by invisal ...
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
thebluestar is offline Offline
55 posts
since Apr 2009
Nov 8th, 2009
0
Re: check 1 number is a prime or not
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; Nov 8th, 2009 at 3:36 am.
Reputation Points: 34
Solved Threads: 16
Junior Poster
Chilton is offline Offline
106 posts
since Oct 2009
Nov 8th, 2009
0
Re: check 1 number is a prime or not
Click to Expand / Collapse  Quote originally posted by Chilton ...
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
thebluestar is offline Offline
55 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC