Thread: C code to find the prime numbers from given set of numbers?

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    30

    Question C code to find the prime numbers from given set of numbers?

    I am struggling to write the code to find the prime numbers from given set of numbers.
    Can someone help me out with the logic to find the prime numbers say from 1 to 100! I have seen many codes but I do not quite understand their logic.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What algorithm do you have in mind, or least what algorithms do you know of to determine prime numbers or find prime numbers in a range?
    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

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by laserlight View Post
    What algorithm do you have in mind, or least what algorithms do you know of to determine prime numbers or find prime numbers in a range?
    I am just trying to apply the basic mathematical concepts of the prime numbers i.e., divisible only by the number and one. But it just doesn't seem possible???

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shan2014
    I am just trying to apply the basic mathematical concepts of the prime numbersn i.e., divisible only by the number and one. But it just doesn't seem possible?
    It certainly is possible. Read up on trial division. With this approach, you would check each number in the range for primality.

    Another method may be more applicable to your particular scenario: read up on the sieve of Eratosthenes.
    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

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by laserlight View Post
    It certainly is possible. Read up on trial division. With this approach, you would check each number in the range for primality.

    Another method may be more applicable to your particular scenario: read up on the sieve of Eratosthenes.
    I actually tried the Sieve of Eratoshthenes.. but led to a dead end??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shan2014
    I actually tried the Sieve of Eratoshthenes.. but led to a dead end?
    Why do you say that it led you to a dead end?

    What is the code that you wrote to implement it and how does it not work?
    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

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by laserlight View Post
    Why do you say that it led you to a dead end?

    What is the code that you wrote to implement it and how does it not work?
    Dead end as in I couldn't get me to incorporate the logic in the code? My little drops of knowledge in C is a hurdle!!
    So as of now I am stuck here
    Code:
    
    main()
    {
        float a,b,n,i,j,k;
        printf("Enter two positive numbers a and b");// these will be my set of numbers
        scanf("%f %f",&a,&b);
        for(i=a;i<=b;i++)
            {

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shan2014
    So as of now I am stuck here
    Are you stuck with writing the code, or stuck with understanding the algorithm?
    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

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by laserlight View Post
    Are you stuck with writing the code, or stuck with understanding the algorithm?
    i am stuck with the code.. on how to implement the algo!!

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shan2014
    i am stuck with the code.. on how to implement the algo!!
    Simplify: for example, assume that a = 1 and b = 30, so as to skip the input step. Next, implement the sieve on paper using those example input, that is, write down the integers in the range [1, 30], and cross them out according to the algorithm, and finally extract the prime numbers in the range. Having done that, what data structure would you need to implement the sieve as a program?

    By the way, you are dealing with integers here, so why use the float data type?
    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

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look up the modulus operator "%".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by laserlight View Post
    Simplify: for example, assume that a = 1 and b = 30, so as to skip the input step. Next, implement the sieve on paper using those example input, that is, write down the integers in the range [1, 30], and cross them out according to the algorithm, and finally extract the prime numbers in the range. Having done that, what data structure would you need to implement the sieve as a program?

    By the way, you are dealing with integers here, so why use the float data type?
    Oh! I was trying something that's why i was using float.

    Anyway this is my interpretation of the Sieve . but am getting the multiples of 5?
    Code:
    int a,b,i,j,flag;
        printf("Enter two positive numbers:");
        scanf("%d%d", &a,&b);
        for(i=a;i<=b;i++)
        {    flag=0;
        for(j=2;j<=i/2;j++)
            {if(i%j==0) 
                flag=1;
            break;}
        if(i!=1 && i==2 && flag==0)
            printf("%d ",i);
        else if(i!=1 && i==3 && flag==0)
            printf("%d ",i);
        else if(i!=1 && i==5 && flag==0)
            printf("%d ",i);
        else if(i!=1 && flag==0)
            printf("%d ",i);
        }
        getch();
        return 0;

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That's not even close to an interpretation of the Sieve of Eratosthenes. It's actually more like a broken implementation of trial division.

    The sieve works by recording prime values that have been found, using them to test subsequent values, and (when a new prime is found - i.e. a value is found for which no previous prime is a factor) adding it to the list of recorded primes. Nowhere in your code is there some means of keeping track of what primes have been found, let alone using them.

    What you need to do is read the description of the sieve, and then work through a few examples by hand BEFORE trying to write code.

    Also, it is only necessary to check for factors up to square root of a value, not to half that value (if there is any prime factor of a value n, one is always less than the square root of n). Caution though: a common beginner mistake, when told that, is to use floating point functions (sqrt(), pow()) to compute the square root. Instead, look up code for "integer square root" (which does everything using integers, and is not subject to rounding errors or other problems associated with floating point).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by grumpy View Post
    That's not even close to an interpretation of the Sieve of Eratosthenes. It's actually more like a broken implementation of trial division.

    The sieve works by recording prime values that have been found, using them to test subsequent values, and (when a new prime is found - i.e. a value is found for which no previous prime is a factor) adding it to the list of recorded primes. Nowhere in your code is there some means of keeping track of what primes have been found, let alone using them.

    What you need to do is read the description of the sieve, and then work through a few examples by hand BEFORE trying to write code.

    Also, it is only necessary to check for factors up to square root of a value, not to half that value (if there is any prime factor of a value n, one is always less than the square root of n). Caution though: a common beginner mistake, when told that, is to use floating point functions (sqrt(), pow()) to compute the square root. Instead, look up code for "integer square root" (which does everything using integers, and is not subject to rounding errors or other problems associated with floating point).
    hahaha .. thank you for the correction. My limited knowledge of C programming leads to such a outcome! Anyway inorder to record the primes that have been found I would need to make an array right!

  15. #15
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Hodor hardcodes all prime numbers into his source

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-09-2013, 07:41 PM
  2. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  3. for loop to find prime numbers
    By lsecrease in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:58 AM
  4. for loop to find prime numbers
    By 1rwhites in forum C Programming
    Replies: 11
    Last Post: 10-21-2005, 10:37 AM

Tags for this Thread