using math functions

This is a discussion on using math functions within the C Programming forums, part of the General Programming Boards category; I am in a intro class for c programming and I have an assignment that involves functions. I don't want ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    using math functions

    I am in a intro class for c programming and I have an assignment that involves functions. I don't want an answer or anything to the problem but I just need a kick start because our professor never really covered this yet. Here is the problem For each pair of primes between 2 and 100 inclusive (EVERYCOMBINATION – NO DUPLICATES) check if the sum of the pair plus one is prime.
    We need to use the isPrime function and printResults. I know I have to use a for loop and if statement. But I just need help getting started because I dont know how to use these functions.
    Last edited by Salem; 09-27-2012 at 10:53 PM. Reason: font sanity

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    Have you tried writing down on paper how you would go about determining if a value is prime?

    If you can't describe how you would work out, using pen and paper, if a value is prime, you can't expect to write code to do it.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    1
    Quote Originally Posted by grumpy View Post
    Have you tried writing down on paper how you would go about determining if a value is prime?

    If you can't describe how you would work out, using pen and paper, if a value is prime, you can't expect to write code to do it.
    obviously use the % operator to catch x % i if i==0, but how do you go about looping this for 2 to 100 inclusive? and then adding 1 and checking isPrime?

  4. #4
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    I can't just give you the answer, but I'll give you a hint. Nested loops are one loop inside of another. Compile and run this code and take a look at the output. Then modify it some and post your new code.
    Code:
    int main(void)
    {
            int i,j;
            for (i = 7; i < 11 ; i++)
            {
                    for (j = i ; j < 13 ; j++)
                    {
                            printf("i=%i j=%i \n",i,j);
                    }
            }
            return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    But how do you use the isprime function within the for loop

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    Quote Originally Posted by yankee34 View Post
    For each pair of primes between 2 and 100 inclusive (EVERYCOMBINATION NO DUPLICATES) check if the sum of the pair plus one is prime.
    Start by finding each prime between 2 and 100, inclusive.

    Next, find all unique pairs of those primes. The standard method of processing all unique pairs of values is a nested loop. The outer loop loops from first up to next-to-last item, and the inner loop from the item following the outer loop iterator up to the last item:
    Code:
        Outer:  0      Inner: 1 2 .. N-3 N-2 N-1
        Outer:  1      Inner: 2 .. N-3 N-2 N-1
                :
        Outer:  N-3    Inner: N-2 N-1
        Outer:  N-2    Inner: N-1
    Note: the above excludes self-pairs. To get all combinations, you need to include self-pairs (0 0, 1 1, 2 2, .., N-2 N-2, and N-1 N-1); just start the inner loop at the outer loop value.

    Finally, check whether the pair fulfills the condition (if i1 * i2 + 1 is prime), and output (or save in an array) those that are. (I believe you were given a function you should call for each such pair you find?)

    Basically, you have two options: You either create an array of the desired primes, or loop over the desired integer range, and exclude non-primes as you go (for example by adding "if this number is not a prime, then continue with the next one right away" test at the beginning of each loop).

    Both work equally well, although the array solution will be much faster, because isPrime() is likely to be relatively slow. Using an array you'll be calling isPrime() the least number of times. I do not know if speed is relevant here, or if the difference is even noticeable in real life.

    (You also have two options to handle the results -- either print the pairs found immediately, or save them in an array -- but the approach you need to take depends on whether printResults() takes one pair, or an array of pairs/results as input.)

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    Quote Originally Posted by yankee34 View Post
    But how do you use the isprime function within the for loop
    Aww, cmon. This is not a "do my homework for free" site. In fact, there is a site policy here about homework (here which essentially says that folks will help if you get into trouble but only IF you have shown some effort of your own. Effort does not equate to begging in a forum.

    Instead of the printf() call in javaeyes' post, try putting an if() statement that does something like (in your words) "obviously use the % operator to catch x % i if i==0".
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Sweet Juniper berries. This is absolutely the last help you'll get without showing some effort (read: code) of your own. Look at this code for a similar function isEven(). Play around with it, make some changes and an honest effort and post back with your modified code.
    Code:
     int isEven(int number)
    {
            if (number % 2 == 0)
            {
                    return 1;
            }
            return 0;
    }
    
    
    int main(void)
    {
            int i,j;
            for (i = 7; i < 11 ; i++)
            {
                    for (j = i ; j < 13 ; j++)
                    {
                            if ( isEven(i+j) == 1 )
                            {
                                    printf("i=%i j=%i and i+j is even\n",i,j);
                            }
                    }
            }
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use math functions in g++
    By mukeshshah in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2008, 12:12 AM
  2. math function vs functions...
    By niceguy in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 10:38 PM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 02:14 AM
  4. Math Functions
    By glider_pilot123 in forum C Programming
    Replies: 1
    Last Post: 10-14-2004, 02:22 PM
  5. math functions
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2002, 06:14 PM

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