Thread: Greatest Common Factor

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    23

    Greatest Common Factor

    I'm trying to reduce a fraction and I need to find the GCF. 20/8 would be reduced to 5/2. How would I find the 4?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You should thank me: I coded this by hand for you in 3 minutes!


    Code:
    int greatest_common_factor( int a, int b )
    {
     int i;
     int result = 0;
    
     for(i = 1; i <= a && i <= b; i++)
     {
      if( a % i == 0 && b % i == 0)
      result = i;
     }
    return result;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    there is actually a much more elegant algorithm-look up Euclid's Algorithm
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  4. #4
    Unregistered
    Guest

    Greatest Common Factor

    Hi idiots,

    there is a recrsive function you may wish to use....
    {in pascal code... haha, C-style syntax sucks ass!}

    function GCF (a,b: integer): integer;
    begin
    if b<> 0
    then
    GCF:= GCF(b, a mod b)
    else
    GCF:= a;
    end;

    Computer science is the study of problem-solving, not of learning how to code in fifteen-thousand languages, you damn turds!

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Computer science is the study of problem-solving, not of
    >learning how to code in fifteen-thousand languages, you damn
    >turds!

    This is the C-board, one of which goals is to learn C.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    2
    Here is how it is in C:

    int GCF(int a,int b)
    {
    int gcf;

    if(b!=0)
    gcf=GCF(b, a%b);
    else gcf=a;
    return gcf;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Greatest Common Factor
    By cppdungeon in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 11:06 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Lowest Common Factor
    By PJYelton in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2002, 09:30 AM
  4. reducing a fraction/greatest common factor
    By dbaryl in forum C Programming
    Replies: 2
    Last Post: 07-21-2002, 03:05 PM
  5. Greatest Common Factor problem
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 03:29 PM