Thread: Returning prime number

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    6

    Returning prime number

    Hi,
    I have a task where I have to write a function with return value int, but without parameters, something like this:
    Code:
    int prime_number () {
    . . .
    }
    I have to write a function which will every time return a new prime number, so I have to use static variable. The first time I "call" the function it has to return 2, the second time 3, the third 5, and etc.
    I have to use this function to calculate the sum of numbers. I know how to calculate that sum, but I don't know to write that function.I and can't use any other function that will return or determine prime number.
    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Do you know the rules for determining if a number is prime? If not, read up on that.

    Can you express this procedure as a series of steps? If not, work out the logic on paper.

    Can you convert this series of steps to code? If not, try your best and post your attempt.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    I know how to determine if a number is prime.
    Code:
     int is_prime (int n){
        int temp=1,i;
        for (i=2;i<=n/2;i++)
        {
            if (n%i==0)
            {
                temp=0;
                break;
            }
        }
        return temp;
    }
    I wrote this as a seperate function but I can't use it as separate. I tried to use this function, but I don't know to return every time the next prime.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Perhaps you can wrap the existing loop in an outer loop.

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    Yeah, but this program doesn't really work with what I need. It determines if a number between two integers is prime. I don't need those barriers, just to return number. That's problem

  6. #6
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Your is_prime function is very naive.

    You should have a test for n==2 returning true. Your loop should start with i=3 and the increment should be i+=2. You should end the loop at i<= sqrt(n).

    Do you know how static variables work inside functions?

  7. #7
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    I have more complicated function, but this works how it shoud. I know that static variables remeber the previous value when it was called.

  8. #8
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Your function tests every even number from 2 to n/2 for primeness when no even number except 2 is prime. Might not make much difference at low prime numbers but it makes a huge difference further up the scale.

  9. #9
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    This function isn't that important, I can change it. But, still I don't how to return numbers with this order 2,3,5,7,11...

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It sounds like you need a static number inside of is_prime() that remembers the last prime you found and then later returned. The function will have to be reworked to start from that position in the sequence (so it finds 2, then starts from 2 to find 3, then starts from 3 to find 5 and so on).

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Ana_1996 View Post
    This function isn't that important, I can change it. But, still I don't how to return numbers with this order 2,3,5,7,11...
    The naive implementation would be to initialize your static variable to 1. On each call, add one, check if it's prime, if not add 1 and test again. Return it when it's prime.

    Give it a try. Post your entire program.

  12. #12
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Work out the pseudocode.

    Static variable to store last prime number.
    Use that variable to work out next prime number.
    Store prime in static variable.
    return that variable.

    What's so hard?

    You know how function level statics work. You know how to work out prime numbers. Make a good attempt now and we'll help you fix it. All the information you need is already in this thread. Good luck!

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your current function tests to see if "n" is prime.

    Code:
    check if value is prime
    if value is prime
        return 1
    Instead of testing it with one value of "n", you need to test many values of "n".

    Code:
    set n
        check if value is prime
        if value is prime
            return n

  14. #14
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    I done it. So if admin (or someone else) could either delete it or lock the thread.
    Thank you all.
    Last edited by Ana_1996; 12-05-2016 at 01:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-02-2014, 10:11 AM
  2. Is a number Prime or not prime?
    By ImaCnoob in forum C Programming
    Replies: 27
    Last Post: 10-08-2012, 11:47 PM
  3. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  4. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  5. Prime Number?
    By cprogramnewbie in forum C Programming
    Replies: 8
    Last Post: 03-16-2002, 12:18 AM

Tags for this Thread