Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Use WheatWizard's shorter input method
Source Link
Ørjan Johansen
  • 7.5k
  • 1
  • 23
  • 39

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

Try it online! Try it online!

  • Ignoring type mismatch, the fractional part of n/d (assuming d positive) is mod n d/d, so unless mod n d==0, f! recurses with a representation of d/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

Try it online!

  • Ignoring type mismatch, the fractional part of n/d (assuming d positive) is mod n d/d, so unless mod n d==0, f recurses with a representation of d/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

Try it online!

  • Ignoring type mismatch, the fractional part of n/d (assuming d positive) is mod n d/d, so unless mod n d==0, ! recurses with a representation of d/mod n d.
Source Link
Ørjan Johansen
  • 7.5k
  • 1
  • 23
  • 39

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

Try it online!

  • Ignoring type mismatch, the fractional part of n/d (assuming d positive) is mod n d/d, so unless mod n d==0, f recurses with a representation of d/mod n d.

AltStyle によって変換されたページ (->オリジナル) /