Skip to main content
43 votes

Round towards zero

Python 3, 3 bytes int Try it online! Truncates the digits after the decimal point. NOTE: This is a trivial answer. Please take a look at the other answers ...
MilkyWay90's user avatar
  • 2,722
30 votes

Egyptian fraction representations of 1

Python 2, 42 bytes f=lambda n,x=1:1/n*[x]or[x+1]+f(n-1,x*-~x) Try it online! The key thing to observe is that ...
dingledooper's user avatar
  • 22.8k
23 votes

Round towards zero

Python 2, 3 bytes int Try it online!
Chas Brown's user avatar
  • 9,767
22 votes

Half, Half Half, and, Half

Haskell, 25 bytes pred.until(<2)(/2).(+0.5) Try it online! Outputs decimals, one-indexed without the initial zero term. Adds 0.5 to the input, then halves ...
xnor's user avatar
  • 146k
20 votes

Rational Number RNG

JavaScript (ES6), 81 bytes Returns \$p/q\$ as [p,q]. ...
Arnauld's user avatar
  • 195k
16 votes
Accepted

Parse a Unicode vulgar fraction

Raku, 17 bytes {S!\⅟!1/!.EVAL} Try it online! Returns a rational number type. Raku actually natively supports unicode literals and operators, though it doesn't ...
Jo King's user avatar
  • 47.4k
16 votes

Next digit of rational number

Regex (ECMAScript or better), 38 34 20 bytes ((.).*),?(.*)\1,?\3$ Uses , as the decimal separator. Returns its output as ...
Deadcode's user avatar
  • 11.7k
15 votes
Accepted

Simplify a continued fraction

J, 8 5 bytes Same as this, but uses a build-in for rationals. Argument is {a0,a1,a2,a3,...} as a list of J extended precision rational numbers. Result is the fraction as a J extended precision ...
Adám's user avatar
  • 30.9k
15 votes

Find Index of Rational Number in Calkin-Wilf Sequence

JavaScript (ES6), 33 bytes Expects a pair of integers (p,q) representing the fraction \$p/q\$. ...
Arnauld's user avatar
  • 195k
15 votes

Rabinowitz-Wagon \$\pi\$ formula

Python, 71 70 60 57 bytes f=lambda n,q=1,p=1:n and f(n-1,q:=q*2*n+q,p*n+q)or(2*p,q) Attempt This Online! Outputs the (0-indexed) nth term of the sequence as a (...
shape warrior t's user avatar
14 votes

Simplify a continued fraction

GolfScript, 13 bytes ~]-1%{\-1?+}* Try it online! Yay for GolfScript's hidden rationals. :) Explanation GolfScript's only "official" number type is integers. ...
Martin Ender's user avatar
14 votes

Simplify a continued fraction

Haskell, 37 36 18 bytes foldr1$(.(1/)).(+) This function expects Haskell's Ratio type as input. Usage example: ...
nimi's user avatar
  • 35.9k
14 votes
Accepted

Infinitely Print Zeno's Dichotomy Paradox (1/(2^n))

05AB1E, 10 9 bytes Saved 1 byte thanks to Erik the Outgolfer [No…+1/J? Try it online! Explanation ...
Emigna's user avatar
  • 53k
14 votes

Round towards zero

Perl 5 -p056l15, 2 bytes <> Try it online! How does that work? ...
Xcali's user avatar
  • 15.7k
14 votes
Accepted

Round towards zero

Jelly, 1 byte r A full program (as a monadic Link it returns a list of length one). Try it online! How? ...
Jonathan Allan's user avatar
14 votes

Phony fractions

J, 22 bytes %&".e.=/#&,%/&(1".\.]) Try it online! quick explanation for now: take the input as strings ...
Jonah's user avatar
  • 33.7k
14 votes

Repetend length in 1/n

Jelly, 6 bytes R⁵*%i1 Try it online! Basically compute 10**[1..n] % n and get the 1-based index of ...
Bubbler's user avatar
  • 78.2k
14 votes

Quoted rational numbers

J, 25 bytes (%-.)~/@(10^#&>)#.10#.&>] Try it online! Takes a boxed 2-item vector containing the digit vectors of \$x\$ and \$y\$, and returns a ...
Bubbler's user avatar
  • 78.2k
13 votes

Simplify a continued fraction

LabVIEW, 36 equivalent bytes Fairly straight forward naive implementation using OP's algorithm. Is there a nicer way to do this?
ijustlovemath's user avatar
13 votes

Is it within the Cantor set?

Mathematica, 54 bytes If[Last@#===1,Most@#,#]&@RealDigits[#,3][[1]]~FreeQ~1& Unnamed function taking a fraction x/y ...
Greg Martin's user avatar
  • 15.9k
13 votes

Infinitely Print Zeno's Dichotomy Paradox (1/(2^n))

Python 2, 30 bytes -5 thanks to Erik the Outgolfer i=1 while 1:print i,'+1/';i*=2 Try it online!
Felipe Nardi Batista's user avatar
13 votes

I want my book to be away from this table

Octave, 41 40 33 bytes 1 byte saved thanks to @Dennis @(n)find(cumsum(.5./(1:9^n))>n,1) Try it online! Explanation This uses the fact that harmonic numbers can ...
Luis Mendo's user avatar
  • 105k
13 votes

Ryley's Theorem

PARI/GP, 35 bytes r->[x=r+1/3,-x+r/3/d=x^2-r,1/9/d-1] Attempt This Online! PARI/GP, 40 bytes ...
alephalpha's user avatar
  • 49.3k
13 votes

Iterate your way to a fraction

Python, 54 bytes (-1 @Jonathan Allan, -1 me) f=lambda n,d:n and'gf'[p:=n*d>0]+f(p*n-d,[n,d][p])or'' Attempt This Online! Old Python, 56 bytes (@tsh) f=lambda n,d:n*d<0and'g&#...
loopy walt's user avatar
  • 16.7k
13 votes

Egyptian fraction representations of 1

Vyxal, 10 bytes ¡ɾṗ'Ė∑1=;t Try it Online! Bruteforcer. \$O\left(2^{n!}\right)\$ time complexity, searches for fractions with reciprocals \$ n! \$ which seems to be ...
allxy's user avatar
  • 510
13 votes

Infinite Candle Sequence

Python, 31 bytes f=lambda a:a and(f(a//2)+a%2)/2 Attempt This Online! Returns the nth term (0-based) as a float. Python, 54 bytes a=b=0 while[print(int(bin(a)[:1:-1],2)/-~b)]:a+=1;b|=a Attempt This ...
loopy walt's user avatar
  • 16.7k
13 votes

Find Index of Rational Number in Calkin-Wilf Sequence

PARI/GP, 29 bytes f(x)=if(x,1+f(-1\-x*2-1-1/x)) Attempt This Online! \$\begin{align}f(0)&=0\\f(x)&=1+f(2\lceil1/x\rceil-1-1/x)\end{align}\$
alephalpha's user avatar
  • 49.3k
12 votes

Delete the first periodic digit

Wolfram Language (Mathematica), 59 bytes FromDigits@MapAt[RotateLeft@*List@@#&,RealDigits@#,{1,-1}]& Try it online! Explanation ...
JungHwan Min's user avatar
  • 13.8k
12 votes

Phony fractions

Python 3, 86 bytes lambda a,b:g(a)&g(b) g=lambda s:{(int(s[:i]+s[i+1:])/int(s),x)for i,x in enumerate(s)} Try it online! -8 bytes thanks to ovs Making use ...
Jitse's user avatar
  • 7,684
12 votes

Egyptian fraction representations of 1

Wolfram Language (Mathematica), 32 bytes Using Sylvester's sequence A000058 Nest[##&[#^2+#,1+#,##2]&,1,#-1]& Try it online! -8 bytes from @att
ZaMoC's user avatar
  • 24.9k

Only top scored, non community-wiki answers of a minimum length are eligible