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

2. 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.

3. Originally Posted by grumpy
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. 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. But how do you use the isprime function within the for loop

6. Originally Posted by yankee34
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. Originally Posted by yankee34
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".

8. 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;
}```