Haskell, 40(削除) 40 (削除ここまで) 34 bytes
Edit:
- -6 bytes: @WheatWizard pointed out the fraction can probably be given as two separate arguments.
(Couldn't resist posting this after seeing Haskell answers with verbose imports – now I see some other language answers are also essentially using this method.)
f! takes a pair of integerstwo integer arguments (numerator and denominator of the fraction; they don't need to be in smallest terms but the denominator must be positive) and returns an integer. Call as 314!100.
f(n,d)|m<!d|m<-mod n d,m>0=f(d,m)|0<1=divm>0=d!m|0<1=div n d
- Ignoring type mismatch, the fractional part of
n/d(assumingdpositive) ismod n d/d, so unlessmod n d==0,f!recurses with a representation ofd/mod n d.
Haskell, 40 bytes
(Couldn't resist posting this after seeing Haskell answers with verbose imports – now I see some other language answers are also essentially using this method.)
f takes a pair of integers (numerator and denominator of the fraction; they don't need to be in smallest terms but the denominator must be positive) and returns an integer.
f(n,d)|m<-mod n d,m>0=f(d,m)|0<1=div n d
- Ignoring type mismatch, the fractional part of
n/d(assumingdpositive) ismod n d/d, so unlessmod n d==0,frecurses with a representation ofd/mod n d.
Haskell, (削除) 40 (削除ここまで) 34 bytes
Edit:
- -6 bytes: @WheatWizard pointed out the fraction can probably be given as two separate arguments.
(Couldn't resist posting this after seeing Haskell answers with verbose imports – now I see some other language answers are also essentially using this method.)
! takes two integer arguments (numerator and denominator of the fraction; they don't need to be in smallest terms but the denominator must be positive) and returns an integer. Call as 314!100.
n!d|m<-mod n d,m>0=d!m|0<1=div n d
- Ignoring type mismatch, the fractional part of
n/d(assumingdpositive) ismod n d/d, so unlessmod n d==0,!recurses with a representation ofd/mod n d.
Haskell, 40 bytes
(Couldn't resist posting this after seeing Haskell answers with verbose imports – now I see some other language answers are also essentially using this method.)
f takes a pair of integers (numerator and denominator of the fraction; they don't need to be in smallest terms but the denominator must be positive) and returns an integer.
f(n,d)|m<-mod n d,m>0=f(d,m)|0<1=div n d
- Ignoring type mismatch, the fractional part of
n/d(assumingdpositive) ismod n d/d, so unlessmod n d==0,frecurses with a representation ofd/mod n d.