Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

SAFWAN.K's blog

By SAFWAN.K, history, 9 years ago, In English

Hi all how to take gcd between rational number

a/b,c/d

thank all

  • Vote: I like it
  • +11
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
Rev. 2   Vote: I like it +16 Vote: I do not like it

in python you can do

from fractions import *
print gcd(Fraction(9,13), Fraction(6,13))
»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

You can calculate it using integer gcd: gcd(a * d, b * c) / (b * d). Don't forget about possible integer overflow.

»
9 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Something like this? (C++)

double fgcd(double x, double y) {
  if (fabs(y) <= EPS) return x;
  return fgcd(y, fmod(y, x));
}