# Is a number Prime or not prime?

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 10-08-2012
laserlight
Quote:

Originally Posted by ImaCnoob
actually, everything works now except for 25.

Notice that 25 is a square. Check that your loop condition accounts for this. Also, it may be more prudent to use ceil instead of round (EDIT: or just add 1), or use an algorithm that specifically computes the square root of integers instead of sqrt (due to floating point inaccuracy)
• 10-08-2012
Click_here
Quote:

sign changed from > to <

Quote:

Notice that 25 is a square

Are you looping through the for loop while
Code:

`i<sqrt(n) or i<=sqrt(n)`
• 10-08-2012
ImaCnoob
ok, i made those changes and here is the output:

8 = 2 x 4
8: NOT A PRIME Number

25 = 5 x 5
25: NOT A PRIME Number

101 = 1 x 101
101: PRIME Number

5001 = 3 x 1667
5001: NOT A PRIME Number

10213 = 7 x 1459
10213: NOT A PRIME Number

56777 = 7 x 8111
56777: NOT A PRIME Number

765321 = 3 x 255107
765321: NOT A PRIME Number

317971 = 1 x 317971
317971: PRIME Number

1028743 = 131 x 7853
1028743: NOT A PRIME Number

23498271 = 3 x 7832757
23498271 = 9 x 2610919
23498271: NOT A PRIME Number

987278023 = 149 x 6626027
987278023 = 761 x 1297343
987278023 = 8707 x 113389
987278023: NOT A PRIME Number

1768374681 = 3 x 589458227
1768374681 = 157 x 11263533
1768374681 = 471 x 3754511
1768374681: NOT A PRIME Number

It looks good to me, I have checked a few and everything is correct.

Thanks for the help everyone!!! Your speed and helpfulness without just giving me the code is why I use this forum!
• 10-08-2012
Click_here
Looks good to me - Good work
• 10-08-2012
NextName
Could you explain what changes you made in order to make 25 show as a non-prime number?
• 10-08-2012
ImaCnoob
this is in my code, the <= helped me

Code:

`        for(i = Start; i <= Stop; i++)`
i forget if that was it or not, does it help?
• 10-08-2012
NextName
Perfect! Works out great.

Now, I don't suppose you've done the second part of this, have you? ;)
• 10-08-2012
ImaCnoob
haha :wink: no, not yet but I am starting now
• 10-08-2012
NextName
I've done the simple part and displayed only the numbers that are prime without showing factors, but I'm having trouble scanning in a range of numbers - would an array be used here?

Edit: Never mind that. You can do this with a simple if loop and a counter.

Final edit: Got the second part to work. Let me know if you run into any trouble or want me to post my code.
• 10-08-2012
ImaCnoob
I cant figure out how to scan in the specified range, could you post some of the code for me?
• 10-08-2012
NextName

Code:

```printf("Enter start of range: ");     scanf("%d", &Range1);     printf("Enter end of range: ");     scanf("%d", &Range2);```
Then changed the while loop to:
Code:

```    if (Range1 != Range2)     {         for(n = Range1; n <= Range2; n++)```
• 10-08-2012
ImaCnoob
should the n in your code be an i? I cannot get this to work :confused:

EDIT: nevermind, this somewhat works, I have it print out that all numbers in my range are prime. here is code:

Code:

```#include <stdio.h> #include <stdlib.h> #include <math.h> int main (void) {     //declare variables     int n, is_prime, Start, Stop, display_factor, i, Lower, Upper;     printf("Enter lower range:");     scanf("%d", &Lower);     printf("Enter upper range:");     scanf("%d", &Upper);     //loops until it reaches the End Of File     if (Lower != Upper)     {         is_prime = 1;         for(n = Lower; n <= Upper; n++)         {             if ((n%i) == 0)             {                 is_prime = 0;             }             else             {                 printf("%d: PRIME Number\n\n", n);             }     }     }     return 0; }```
• 10-08-2012
NextName
This board seems a bit messed up right now.

I think you changed up too much of your code. You trashed a lot of the stuff in your first for loop when you should have left it in. There is only supposed to be a minor change from the first part to the second, which is mostly just modifying that while loop call and getting rid of the options for when a number isn't prime.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12