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 ...
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 ...
23
votes
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 ...
20
votes
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 ...
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 ...
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 ...
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\$.
...
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 (...
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. ...
14
votes
Simplify a continued fraction
Haskell, 37 36 18 bytes
foldr1$(.(1/)).(+)
This function expects Haskell's Ratio type as input. Usage example:
...
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
...
14
votes
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?
...
14
votes
Phony fractions
J, 22 bytes
%&".e.=/#&,%/&(1".\.])
Try it online!
quick explanation for now:
take the input as strings
...
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 ...
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 ...
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?
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 ...
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!
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 ...
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
...
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...
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 ...
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 ...
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}\$
12
votes
Delete the first periodic digit
Wolfram Language (Mathematica), 59 bytes
FromDigits@MapAt[RotateLeft@*List@@#&,RealDigits@#,{1,-1}]&
Try it online!
Explanation
...
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 ...
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
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
rational-numbers × 104code-golf × 99
math × 57
number × 20
sequence × 12
number-theory × 9
arithmetic × 7
parsing × 4
probability-theory × 4
code-challenge × 3
integer × 3
primes × 3
binary × 3
open-ended-function × 3
string × 2
decision-problem × 2
combinatorics × 2
counting × 2
restricted-time × 2
polynomials × 2
fractal × 2
pi × 2
approximation × 2
electrical-engineering × 2
kolmogorov-complexity × 1