# Tag Info

40

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 before upvoting

22

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 until the results is below 2, then subtracts 1. Using a pointfree expression saves 1 bytes over f n=until(<2)(/2)(n+0.5)-1

21

Python 2, 3 bytes int Try it online!

20

JavaScript (ES6), 81 bytes Returns $p/q$ as [p,q]. f=(p=0,q=1)=>Math.random()<(g=(a,b)=>b?g(b,a%b):a)(p,q)/2?f(++p-q?p:!++q,q):[p,q] Try it online! Or Try some statistics! How? We recursively build the fractions $p/q$ in the relevant order, but without any explicit deduplication, until a random number picked in $[0,1[$ is greater than or ...

19

CJam, 1.17 × 10678 bytes ",,,",KK#b:c~ Well, the string should actually contain 1,167,015,602,222,203,651,546,923,533,233,456,645,527,427,020,625,754,322,603,554,937,551,735,592,092,356,520,085,507,613,447,896,812,875,213,856,544,974,386,642,866,232,121,069,637,599,975,236,272,634,227,913,998,493,360,693,139,149,236,571,503,883,331,020,249,908,672,008,574,...

15

Perl 6 (93 101 100 80 68 66 bytes) $/=split ".",get;say ($0+($1+$2/(9 x$2.comb||1))/10**$1.comb).nude The size was increased to handle nothing, instead of just failing. Mouq proposed to use $/, so it's now being used, and the code is 20 bytes shorter. Ayiko proposed replacing / with , so the code is even shorter (by 12 bytes). Then Mouq proposed replacing ... 15 T-SQL 254 While T-SQL isn't really suited to this sort of thing, it wa fun to try. The performance gets real bad the higher the denominator. It is limited to a denominator of 1000. Input is a float variable @ WITH e AS(SELECT *FROM(VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(0))n(n)),t AS(SELECT ROW_NUMBER()OVER(ORDER BY(SELECT \))N FROM e a,e b,e c,e d)... 15 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 rational number. (+%)/ (+%) the plus-the-reciprocal-of / reduction over Try it online! -3 thanks to miles. 14 Python, 176 bytes Not a bowler, but... import sys,fractions def f(t): c=0 for i in range(t+10): for j in range(1,i): if fractions.gcd(j,i)==1:c+=1 if c==t:return str(j)+'/'+str(i) print f(int(sys.argv[1])) 14 05AB1E, 10 9 bytes Saved 1 byte thanks to Erik the Outgolfer [No…+1/J? Try it online! Explanation [ # loop over N infinitely [0 ...] No # calculate 2^N …+1/J # join with the string "+1/" ? # print without newline 14 J, 22 bytes %&".e.=/#&,%/&(1".\.]) Try it online! quick explanation for now: take the input as strings %/&(1".\.]) creates a function table %/ whose axes are the integer ". lists formed by the 1-outfixes \. (remove 1 digit at a time) of both args, and whose cells are the quotients of those numbers =/ forms a corresponding function table ... 13 Mathematica, 54 bytes If[Last@#===1,Most@#,#]&@RealDigits[#,3][[1]]~FreeQ~1& Unnamed function taking a fraction x/y as input, where y > 0 and 0 ≤ x ≤ y, and returning True or False. A real number between 0 and 1 is in the Cantor set precisely when none of the digits in its base-3 expansion equals 1; the exception is that a fraction whose ... 13 Wolfram Language (Mathematica), 59 bytes FromDigits@MapAt[RotateLeft@*List@@#&,RealDigits@#,{1,-1}]& Try it online! Explanation RealDigits@# Find the decimal digits of the input. MapAt[RotateLeft@*List@@#&, ..., {1,-1}] If there are repeating digits, RotateLeft them. (List@@# prevents the code from attempting to rotate the last decimal ... 13 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 be lower-bounded by a logarithmic function. Also, the >= comparison can be replaced by > because harmonic numbers cannot be even integers (thanks, @Dennis!). @(n) ... 13 Jelly, 1 byte r A full program (as a monadic Link it returns a list of length one). Try it online! How? r - Main Link: number, X e.g. -7.999 r - inclusive range between left (X) and right (X) (implicit cast to integer of inputs) - = [int(X):int(X)] = [int(X)] [-7] - implicit (smashing) print -7 12 Python 2.7 - 138 x,y=n,d=map(int,raw_input().split('/')) while y:x,y=y,x%y def f(p,a=d): while(a*n+p)%d:a-=1 print(a*n+p)/d+('/'+a)*(a>1), f(-x);f(x) I started with the obvious brute-force solution, but I realized that since the OP wanted to be able to solve instances with six digit numerators and denominators in under a minute, I need a better ... 12 CJam, 9.44 × 10284 "ddd",'iib:c~ The string actually contains 943,611,762,745,112,544,157,801,937,289,871,933,621,396,073,807,297,328,579,826,246,436,861,144,651,900,144,172,793,266,430,374,467,343,433,363,000,182,294,622,535,895,774,344,720,689,882,873,880,571,351,234,260,849,874,055,687,224,065,790,608,381,303,357,434,711,286,607,328,858,338,155,948,406,... 12 Haskell, 62 59 if only the names weren't so long... import Data.Ratio f s=approxRational(read s)$50/10^length s this is a function returning a Rational value. explanation: the function approxRational is a function which takes a float number and a float epsilon and returns the most simple rational which is in distance epsilon of the input. basically, ...

12

Haskell, 37 36 18 bytes foldr1$(.(1/)).(+) This function expects Haskell's Ratio type as input. Usage example: Prelude Data.Ratio> ( foldr1$(.(1/)).(+) ) [4%1,2,1,3,1,2] 170 % 39 Note: one explicit Ratio in the input list (4%1) is enough, the type systems figures out that the others have to be Ratios, too. Edit: @Lynn saved a byte. Thanks! Edit ...

12

Python 2, 30 bytes -5 thanks to Erik the Outgolfer i=1 while 1:print i,'+1/';i*=2 Try it online!

12

Perl 5 -p056l15, 2 bytes <> Try it online! How does that work? -056 # (CLI) Make "." the input record separator -l15 # (CLI) Make "\n" the output record separator # (otherwise it would use the input separator) -p # (CLI) Implicitly read $_ from STDIN <> # Read the second input field and do nothing with it -p # (CLI) ... 11 Python, 61 bytes lambda a,b,l:[i for i,(x,y)in enumerate(l)if x/y in[a/b,b/a]] Yes, I'm using spending 9 chars to write enumerate. Takes input like 1, 2, [(1, 9), (3,6), (2, 5), (16, 8)]. For Python 2, input values need to be written as floats. One char longer (62) in Python 3: def f(a,b,l,i=0): for x,y in l:b/a!=x/y!=a/b or print(i);i+=1 11 Mathematica, 16 bytes Range@##⋃{#2}& An unnamed function that takes two integers and a rational number and returns a list of numbers, e.g.: Range@##⋃{#2}&[-2, 3, 1/2] (* {-2, -(3/2), -1, -(1/2), 0, 1/2, 1, 3/2, 2, 5/2, 3} *) Mathematica's Range does exactly what the challenge asks, except that it omits the upper bound if the difference between ... 11 Haskell, 78 77 65 58 bytes Shamelessly stealing the optimized approach gives us: (s#t)0=show s++'/':show t (s#t)n=t#(s+t-2*mod s t)$n-1 1#1 Thanks to @nimi for golfing a few bytes using an infix function! (Still) uses 0-based indexing. The old approach: s=(!!)(1:1:f 0) f n=s n+s(n+1):s(n+1):(f$n+1) r n=show(s n)++'/':(show.s$n+1) Damn the output ...

11

CJam (20 bytes) 1_{_@_2\$%2*-+}ri*'/\ Online demo. Note that this is 0-indexed. To make it 1-indexed, replace the initial 1_ by T1. Dissection This uses the characterisation due to Moshe Newman that the fraction a(n+1)/a(n+2) can be generated from the previous fraction a(n)/a(n+1) = x by 1/(2*floor(x) + 1 - x) If x = s/t then we get 1 / (2 * floor(...

11

GolfScript, 13 bytes ~]-1%{\-1?+}* Try it online! Yay for GolfScript's hidden rationals. :) Explanation GolfScript's only "official" number type is integers. But the exponentiation operator doesn't cast its result to integer and conveniently the native result of an integer exponentiation in Ruby (the language of GolfScript's interpreter) is a rational ...

11

Mathematica, 23 22 bytes Fold[#2+1/#&]@*Reverse Essentially a port of my GolfScript answer. Here are some alternatives: For 24 bytes, we can write a recursive variadic function: f@n_=n n_~f~m__:=n+1/f@m For 21 bytes, we can even define a "variadic operator", but its calling convention would be so weird, that I'm reluctant to count this one: ±n_=n ...

11

Python 3, 35 bytes f=lambda n,k=2:n>0and-~f(n-1/k,k+2) Try it online!

11

Java 10, 68 64 bytes First try at code golf! Option 1: find the n-th element (1-indexed) -4 bytes thanks to @Kevin Cruijssen n->{int x=0;for(;n>>++x!=1;);return((~(1<<x)&n)*2.+1)/(1<<x+1);} This is an anonymous method that finds the n-th term by removing the most significant bit from n, doubling it and adding one, then dividing ...

11

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 of the fact that the boolean value for a0/b0==a/b is equivalent to a0/a==b0/b. The helper function g generates all ratios a0/a and keeps track of the removed digit. Then it does the same for b0/b. ...

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