Like Tree1Likes

Is a number Prime or not prime?

This is a discussion on Is a number Prime or not prime? within the C Programming forums, part of the General Programming Boards category; Originally Posted by ImaCnoob actually, everything works now except for 25. Notice that 25 is a square. Check that your ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    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)
    Last edited by laserlight; 10-08-2012 at 09:49 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,512
    sign changed from > to <

    Notice that 25 is a square

    Are you looping through the for loop while
    Code:
    i<sqrt(n) or i<=sqrt(n)
    Fact - Beethoven wrote his first symphony in C

  3. #18
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    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!

  4. #19
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,512
    Looks good to me - Good work
    Fact - Beethoven wrote his first symphony in C

  5. #20
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Could you explain what changes you made in order to make 25 show as a non-prime number?

  6. #21
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    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?

  7. #22
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Perfect! Works out great.

    Now, I don't suppose you've done the second part of this, have you?

  8. #23
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    haha no, not yet but I am starting now

  9. #24
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    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.
    Last edited by NextName; 10-08-2012 at 10:53 PM.

  10. #25
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    I cant figure out how to scan in the specified range, could you post some of the code for me?

  11. #26
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    I had to add this in after declaring the first variables:

    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++)

  12. #27
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    should the n in your code be an i? I cannot get this to work

    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;
    }
    Last edited by ImaCnoob; 10-08-2012 at 11:21 PM.

  13. #28
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it prime number or not
    By CruelSoulz in forum C Programming
    Replies: 6
    Last Post: 07-25-2012, 10:11 AM
  2. prime number
    By bvsa in forum C Programming
    Replies: 4
    Last Post: 08-08-2011, 01:43 PM
  3. Replies: 3
    Last Post: 03-29-2005, 03:24 PM
  4. prime number.
    By tdoctasuess in forum C Programming
    Replies: 13
    Last Post: 05-13-2004, 08:03 AM
  5. prime number
    By sg786 in forum C# Programming
    Replies: 2
    Last Post: 10-18-2002, 12:27 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21