Thread: Question regarding using euclidean algorithm to simply fractions in program

  1. #1
    Registered User
    Join Date
    Jun 2014
    Location
    Tampa, Florida, United States
    Posts
    28

    Question regarding using euclidean algorithm to simply fractions in program

    In my assignment, I'm asked to be able to take a fraction in as input, and then output it in simplified form if the fraction can be reduced down. Here is the problem i'm facing in my head about this, how do I make sure that the program knows the difference between a fraction that can be simplified, such as : 45/ 210

    and a simplified fraction such as 3/4.

    I was planning on using a function to find the GDF of the two numbers using modulo and then dividing it by each one to simplify (Euclidean algorithm) , but how can I make it so that the function can distinguish between a simplified fraction and a non-simplified fraction?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Presumably, if the GCF is 1, then the fraction is as simple as it's going to get.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Unrelated Hint: use the 'Extended Euclid's Algorithm' and you can skip the division.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    This is a math problem actually. If the numerator and denominator are mutually prime (coprime) then it is already in it's simplest form. The best algorithm to figure out if they are mutually prime is the Euclidean algorithm you are already familiar with. It computes the greatest common divisor, which would be one if they are mutually prime. It should be easy enough to code via a loop or a recursive function that algorithm.

    Thus if gcd(a,b)=1 , the fraction can not be reduced since there is no common factor other than 1. It's that simple. You can write a function to compute that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simply question about Pthread
    By mario++ in forum C Programming
    Replies: 1
    Last Post: 06-09-2011, 06:39 AM
  2. Help with my euclidean algorithm program?
    By miwashi in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2011, 07:26 AM
  3. The Euclidean algorithm
    By the_storm in forum General AI Programming
    Replies: 1
    Last Post: 10-28-2009, 03:41 AM
  4. simply question, structures
    By kiddprogrammer in forum C Programming
    Replies: 7
    Last Post: 04-09-2003, 11:09 AM
  5. fastest sort algorithm ever??? it's just been found!!! simply amazing!
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-24-2002, 10:13 PM