DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Efficient way to find highest common denominator? (For reducing fractions.)  (Read 7,458 times)

Hirudin

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 543
I'm making some AutoHotkey scripts and need to reduce a fraction.

Can anyone help me find the most efficient way to find the highest common denominator between 2 numbers?

I figure I can do a brute force type thing where I make each number "numerator1" and "numerator2".
Take each numerator and divide it by a counter.
- If the results for each division are integers then I'd make the results the new numerators.
- If one of the results is not an integer then increase the counter over and over until the counter = the smaller of the numerators
But I figure there's probably a better way...

Hirudin

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 543
Welp, I made a function that will do it. It uses a list of prime numbers (primes.txt) that must be in the same directory as the "Func_GreatestCommonDenominator.ahk" script.

Also included is an example ahk script that uses the function to reduce a fraction and report by how much the original was reduced.

GCD 1.1.rar (23.0 KB)
« Last Edit: July 13, 2007, 03:50 PM by Hirudin »

Eóin

  • O'Callaghan
  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
The Euclidean algorithm is considered to be quite efficient :)
Interviewer: Is there anything you don't like?
Bjarne Stroustrup: Marketing hype as a substitute for technical argument. Thoughtless adherence to dogma. Pride in ignorance.

Hirudin

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 543
The Euclidean algorithm is considered to be quite efficient :)
Whoa! Looks pretty efficient to me. Thanks Eóin!

Original address: https://www.donationcoder.com/forum/index.php?topic=9200.0