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; I need to do this with a couple conditions: -need to use flags (display_factor) for checking if prime -need to ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    15

    Is a number Prime or not prime?

    I need to do this with a couple conditions:
    -need to use flags (display_factor) for checking if prime
    -need to print out all possibilities with no repeats if not prime
    (see below)
    333 = 1 x 333
    333 = 3 x 111
    333 = 9 x 37
    333: NOT A PRIME Number
    -just print out xxx: PRIME for prime numbers

    When the program runs it does not print out the Prime numbers, it only prints out the not-primes. I am reading from a file with around 10 numbers in it. Any help with the PRIME error? Also it runs slow, but that isn't a big deal.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main (void)
    {
        //declare variables
        FILE *fpin;
        int n, is_prime, Start, Stop, i, display_factor;
    
    
        //check to see if file exists and open
        if ((fpin=fopen("ints.txt", "r"))==NULL)
        {
            printf("File ints.txt does not exist");
            exit(1);
        }
    
    
        //loops until it reaches the End Of File
        while (fscanf(fpin, "%d", &n) != EOF)
        {
            fscanf(fpin, "%d", &n);
            is_prime = 1;
    
    
            Start = n;
            Stop = round(sqrt(n));
            display_factor = 1;
    
    
            for(i = Start; i > Stop; i++)
            {
                if ((n%i) == 0)
                {
                    is_prime = 0;
                    if(display_factor == 1)
                    {
                        //print out immediate factors
                        printf("%d = %d x %d\n", n, i, (n/i));
                    }
                }
            }
            if(is_prime == 1)
            {
                printf("%d: PRIME Number\n", n);
            }
            else
            {
                printf("%d: NOT A PRIME Number\n", n);
            }
    
    
        }
    
    
        fclose(fpin);
    
    
        return 0;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    I think that the best way to approach this problem is to make a function called "is_prime" that returns either a 1 for yes or 0 for no.

    Why are you starting at n? Did you mean to start at 2 and check up to the sqrt(n)?
    stahta01 likes this.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You only need the first one of your fscanf's (the one in the while condition). Having two of them as you do will skip every 2nd number (starting with the first).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    thanks oogabooga for that, and Click, I'm not good at functions so I'll pass on that but thanks for the suggestion. Now, all numbers it says they are not prime. I am thinking somehow since the first is not prime, the flag is_prime is set to 0 and it doesnt reset itself for the next n...

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,542
    N will almost always be greater that the square root of N.


    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    Can you post your newest version of your code?
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    here it is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main (void)
    {
        //declare variables
        FILE *fpin;
        int n, is_prime, Start, Stop, i, display_factor;
    
    
        //check to see if file exists and open
        if ((fpin=fopen("ints.txt", "r"))==NULL)
        {
            printf("File ints.txt does not exist");
            exit(1);
        }
    
    
        //loops until it reaches the End Of File
        while (fscanf(fpin, "%d", &n) != EOF)
        {
            is_prime = 1;
    
    
            Start = n;
            Stop = round(sqrt(n));
            display_factor = 1;
    
    
            for(i = Start; i > Stop; i++)
            {
                if ((n%i) == 0)
                {
                    is_prime = 0;
                    if(display_factor == 1)
                    {
                        //print out immediate factors
                        printf("%d = %d x %d\n", n, i, (n/i));
                    }
                }//after for loop
    
    
            }
            if(is_prime == 1)
            {
                printf("%d: PRIME Number\n", n);
            }
            else
            {
                printf("%d: NOT A PRIME Number\n", n);
            }
    
    
        }
    
    
        fclose(fpin);
    
    
        return 0;
    }

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    I want you to think about this line
    Code:
    Start = n; // did you mean 2 instead of n?...
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,542
    Quote Originally Posted by Click_here View Post
    I want you to think about this line
    Code:
    Start = n; // did you mean 2 instead of n?...
    And think about my follow on comment.

    Quote Originally Posted by stahta01 View Post
    N will almost always be greater that the square root of N.


    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    well if I try the Start = 2, then it says all numbers are prime. But I understand the logic of starting with 2

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    What is your code when you try Start = 2?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    Durrh! Now I see what you are saying about n > sqrt(n). I changed this to say i < Stop (changed the sign from > to <). This results in some pretty weird results this is what I get from it:

    8: NOT A PRIME Number
    25: PRIME Number
    101: PRIME Number
    5001: NOT A PRIME Number
    10213: NOT A PRIME Number
    56777: NOT A PRIME Number
    765321: NOT A PRIME Number
    317971: PRIME Number
    1028743: NOT A PRIME Number
    23498271: NOT A PRIME Number
    987278023: NOT A PRIME Number
    1768374681: NOT A PRIME Number

  13. #13
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    laserlight: the start = 2 actually is the only way to run it correctly now with the sign changed from > to < in the if statement.

    actually, everything works now except for 25. Also, the output does not print out the "333 = 3 x 111" and such which I need

  14. #14
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    actually I fixed it a bit and here is the output:

    8 = 2 x 4
    8: NOT A PRIME Number


    25: PRIME Number


    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: 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

  15. #15
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    and i just changed this code:

    Code:
            if(is_prime == 1)
            {
                printf("%d = 1 x %d\n", n, n); //ADDED THIS LINE
                printf("%d: PRIME Number\n\n", n);
            }

Page 1 of 2 12 LastLast
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