# Help! Math functions

This is a discussion on Help! Math functions within the C Programming forums, part of the General Programming Boards category; Here is the assignment and then I wil post what I have. For ea ch pair of primes between 2 ...

1. ## Help! Math functions

Here is the assignment and then I wil post what I have.

For each pair of primes between 2 and 100 inclusive (EVERYCOMBINATION – NO DUPLICATES) check if the sum of the pair plus one is prime.Below is an example of what the results of the calculations should be, NOT theactual sample output. DO NOT PRINT THIS IN YOUR PROGRAM:

The sum of 2 and3 plus 1 is 6, which is NOT a prime number
The sum of 2 and5 plus 1 is 8, which is NOT a prime number
[SIZE=2][COLOR=#000000] The sum of 2 and7 plus 1 is 10, which is NOT a prime number
and then

[SIZE=3][FONT=Times New Roman] For every number from 500 to 600 inclusive, use pullDigitto obtain the first and second digits of the number. Combine these two digitsback into a single two digit number and sum it with the third digit of theoriginal number. Check if this result is prime.
I have to use 5 functions in the program for some reason
sumofextremes,productofextremes, printResults, isPrime, and pulldigit.

Heres what I have so far
Code:
```#include <stdio.h>
#include <math.h>
#include #include <stdio.h>
#include <math.h>
#include <limits.h>
int flag;
int productOfExtremes(int,int,int);
int sumOfExtremes(int,int,int);
int minimum(int,int,int);
int maximum(int,int,int);
int isPrime(int);
int primecombo(int);

int main()
{

return 0;
}
int sumOfExtremes(int a, int b, int c)
{
int sumOF;
sumOF=maximum(a,b,c)+minimum(a,b,c);
return sumOF;
}
int maximum(int a, int b, int c)
{
int maximum;
maximum=INT_MIN;
if(maximum<a)
maximum=a;
if(maximum<b)
maximum=b;
if(maximum<c)
maximum=c;
return maximum;
}
int minimum(int a, int b, int c)
{
int minimum;
minimum=INT_MAX;
if(minimum>a)
minimum=a;
if(minimum>b)
minimum=b;
if(minimum>c)
minimum=c;
return minimum;
}
int productOfExtremes(int a, int b, int c)
{
int productOF;
product=maximum (a,b,c)*minimum (a,b,c);
return productOF;
}
int isPrime(int a)
{
int isPrime;
if(a==1 || a==0 || a==3)
return 1;
else
{
if(!(a&1))
return 0;
if(!(a&5))
return 0;
/*for loop to check for prime numbers*/
for (int i=3,i<a&&<100,i+2=i)
isPrime=a%i
if (isPrime==0)
return 0;

}
return 1;
}
int primecombo(int a)
{
int i=1,beforecheck,firstprime,secondprime;
a=2;
for(i=1,i<=100,i++)
{
if(isPrime(a)==1)
firstprime=a;
else if(isPrime(a+i)==1)```

2. In isprime:
Code:
` for (int i=3,i<a&&<100,i+2=i)`
There is a lot out there for isprime functions.

All you need to do is find one divisor which is not '1' or the number that that is being tested. Also note that '1' is not a prime number, but '2' is - see Prime number - Wikipedia, the free encyclopedia - You should study this page, because your algorithm will be better.

For a larger domain of input numbers, you need to use a sieve, but for this small domain, brute force (what you are doing) is probably a better solution. The reason for this is that the divide takes a long time to do when compared with other operations.

Code:
```if(!(a&5))
return 0;```
This does not do what you expect. You should be doing something more like "if ((a%5) == 0)". This is redundant though, because your 'for' loop takes care of that anyway...

When I was first making a version of "isprime" the test that I used was -
Code:
```for (i=1; i<=20; i++)
{
if (isprime(i)) printf("%d", i);
}```
This should print
2, 3, 5, 7, 11, 13, 17, 19

3. This does not do what you expect. You should be doing something more like "if ((a%5) == 0)".
For any integer a, !(a % 5) and a % 5 == 0 will yield the same result. If he is looking for factors of 5, it will work.

4. Originally Posted by whiteflags
For any integer a, !(a % 5) and a % 5 == 0 will yield the same result. If he is looking for factors of 5, it will work.
True, but they are using the "&" operator instead of the "%" operator - So it will not work.

Popular pages Recent additions