Thread: Help! Math functions

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

    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)
    Last edited by Salem; 10-01-2012 at 12:04 AM. Reason: demunged the code

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by whiteflags View Post
    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.
    Last edited by Click_here; 09-30-2012 at 07:30 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using math functions
    By yankee34 in forum C Programming
    Replies: 7
    Last Post: 10-01-2012, 10:14 AM
  2. how to use math functions in g++
    By mukeshshah in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2008, 12:12 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03: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