Hot answers tagged

26

Haskell, 35 bytes x?y|isNaN$x/y=tanh x*tanh y|0<1=x/y Try it online! The relevant function is (?), which takes two Doubles as input and returns a Double as output. How? The hyperbolic tangent is an amazing function (two bytes shorter than signum!). x tanh x 0.0 0.0 -0.0 -0.0 Infinity 1.0 -Infinity -1.0 Haskell, 30 29 bytes x?y|x/y<=1/0=x/y|0<...


19

Haskell, 320…236 230 bytes length<$>iterate(\h->nub[r$x:y:u|u<-h,t<-u,x<-n t\\u,y<-n x\\u,([x,x,y]\\(u>>=n))/=[x,y]])[[l,0<$l]] r u=minimum[sort$(!!i).p.map(*j).m a<$>u|a<-u,j<-l,i<-[0..5]] n=(<$>p l).m m=zipWith(-) p=permutations l=[-1..1] import Data.List Try it online! An infinite list whose \$n\$-th ...


13

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 rational number. The digits must be given in extended precision. \$y\$ may be an empty vector. I guess it's pretty well golfed when Jelly is at 20 bytes :) How it works Uses the formula at the end of ...


12

Scratch 3.0, 45 blocks/254 bytes As SB Syntax: define(n)(d) set[A v]to(0 set[D v]to(d set[i v]to(0 repeat((n)+(1 set[j v]to(0 repeat((n)+(1 set[r v]to((i)/(j if<(D)>([abs v]of((r)-(D)))>then set[A v]to(join(i)(join(/)(j set[D v]to([abs v]of((r)-(D end set[j v]to((j)+(1 end set[i v]to((i)+(1 end say(A Try it on Scratch! Just wouldn't be right if I ...


12

JavaScript (ES6), 45 bytes Expects (a)(b), as Numbers. Returns a Number. a=>b=>a/b||+(b?1/a?a/b:a*b>0||-1:1/a/b<0&&-0) Try it online! How? Let \$\mathbb{R}^*\$ be \$\{x\in\mathbb{R}\mid x\neq0\}\$. Direct answers when a/b is truthy Whenever \$a/b\$ is neither NaN nor \$0\$, we know for sure that it is the expected answer. The cases that ...


11

Julia 0.7, 26 bytes imaginary brownie points for using tanh instead of sign (thanks Delfad0r) a$b=a/b<=Inf?a/b:tanh(a*b) Try it online!


10

Wolfram Language (Mathematica), 22 bytes #!~IntegerExponent~#2& Try it online!


9

Husk, 12 10 bytes ◄oa-⁰´×/ŀ→ Try it online! ≠ is absolute difference, but doesn't seem to work for floats. So oa- is used. -2 bytes from Leo. Explanation ◄oa-⁰´×/ŀ→ Inputs: decimal → ⁰, max denominator → implicit ŀ→ range 0..denominator ´×/ convert all possible pairs to fractions ◄o min by a-⁰ absolute difference with the ...


9

Wolfram Language (Mathematica), 25 bytes $$n! \div \left( \sum ^n _{k=0} k!(n-k)! \right)$$ #!/Sum[k!(#-k)!,{k,0,#}]& Try it online! -4 bytes from @ovs


9

J, 14 13 bytes <[:>./2^/@p:] Try it online! -1 byte thanks to @Jonah A dyadic function. The right argument is \$n\$, the left argument is \$B\$. Returns 0 if \$n\$ is \$B\$-powersmooth, 1 otherwise. How it works <[:>./2^/@p:] NB. left arg = B, right arg = n 2 p:] NB. Prime factorization of n, NB. given as a 2-row matrix ...


9

C++, Not competing After spending so long on the golfing side of this challenge, I was curious to see what I could do from a fastest-code perspective. Here is my C++ attempt, which should be able to run up to \$n=8\$ on every computer, and \$n=9\$ if you have enough RAM and patience. The approach is the same as my other answer, but optimized for speed ...


9

J, 17 15 bytes 1#.0&(]|.##\)&2 Try it online! -2 thanks to FrownyFrog! On TIO, handles up to the 11th term 6837625106787 (where term 1 is 2). how J's "Copy" verb # is tailor made for this challenge: 1 2 3 # 'abc' abbccc The rest is just mechanics needed to reverse and iterate: 0&(...)&2 Iterate ... as many times as the input. ...


9

Wolfram Language (Mathematica), 37 33 bytes d[#/d[0#-9]]~d~#2& d=Fold[9#+##&] Try it online! Input two lists of digits. Fold[9#+##&] is longer than FromDigits, but has more flexibility with its input.


9

R, 98 95 89 88 bytes -6 bytes thanks to Giuseppe function(x,y=abs(scan(t=x)))cat("-"[!!sd(grepl("-",x))],c(z<-y[1]/y[2],!!y)[1+is.na(z)]) Try it online! Expects a vector of strings, as R cannot handle signed 0. Infinity will be input and output as Inf. x contains the input as 2 strings, and y is the conversion to numeric in absolute ...


8

M, 7 6 bytes cRİS‘İ Try it online! M is Jelly's more mathematical (and out-of-date) sibling. While Jelly uses floating point maths, M uses rational numbers where possible. Similarly to J42161217's Mathematica answer, we use the binomial coefficient. This calculates the exact value of $$1 \div \left(1 + \sum ^n _{k=1} \frac 1 {\binom n k}\right)$$ How it ...


8

JavaScript (ES6),  72 68   67 bytes Expects (max_denominator)(seq). Returns [numerator, denominator]. m=>g=([v,...a],n=0,d=1,N=1,D=0)=>(d+=D*v)<=m?g(a,N,D,N*v+n,d):[N,D] Try it online! Commented m => // outer function taking m = maximum denominator g = ( // inner recursive function taking: [v, // v = next ...


8

Husk, 10 bytes ►ȯLU¡oΠd↔ŀ Try it online! Explanation ►ȯLU¡oΠd↔ŀ ↔ŀ reverse the range 0..n-1 ►ȯ max-by, returning the last maximal element for the following: ¡o create an infinite list using: Πd product of digits. U longest unique prefix L take its length


7

Factor + math.combinatorics math.unicode, 45 43 bytes [ dup [0,b] [ nCk -1 ^ ] with map Σ -1 ^ ] Try it online! This uses the formula graciously offered in the following M answer. Edit: use -1 ^ instead of recip since it's 1 byte shorter. Explanation: It's a quotation (anonymous function) that takes an (0-indexed) integer as input and returns a number (...


7

Regex (ECMAScript), 61 bytes ^(?=(|x*)\1*(?=\1\b)(?!(((x+)x+)(?=\3+,)\3*\4)\2+,)(x*)).*,\5 Try it online! Takes \$n\$ and \$B\$ from input in unary as strings of xs whose length represents the number, separated by a ,. Uses my prime power regex, and the same expression for cycling through divisors of N from largest to smallest as used in this problem. # ...


7

JavaScript (Node.js), 63 bytes f=(a,t=1)=>a.some(v=>t%v[1])?f(a,1+t):a.map(([p,q])=>[p*t/q,t]) Try it online! JavaScript (Node.js), 63 bytes f=(a,t,b=a.map(([p,q])=>[m=p*t/q,o&=m%1||t],o=t))=>o?b:f(a,-~t) Try it online! m=p*t/q cannot be changed to p*=t/q due to floating point errors. For example, 11*15/11 is 15, but 11*(15/11) is 14....


7

Vyxal, 15 bytes L⁾₁I*₁L⁾⨪/₂I$-ƒ Try it Online! I spent roughly an hour on this, trying to arrange it to work properly. Took me a few re-reads of the specs, but I got there in the end. Input taken as: y x where y and x are strings (done by surrounding them with ""s), and if y isn't provided, [] is used. Explained L⁾₁I*₁L⁾⨪/₂I$-ƒ L⁾ # ...


7

R, 118 116 108 103 98 92 bytes function(x,y,`+`=nchar,B=10^+x-1,A=el(max(y,0):0)*B-x*10^+y,q=1:x)c(A,B)/max(q[!A%%q&!B%%q]) Try it online! Takes first input as integer and second as string (empty if no \$y\$). GCD implementation borrowed from here: https://codegolf.stackexchange.com/a/48845/55372 -8 bytes thanks to @Kirill & @Dominic; -5 and ...


7

R, 114 110 109 107 104 bytes Edit: -4 and then -2 more bytes thanks to Giuseppe's comments on pajonk's answer, and then -3 bytes after copying a trick from Robin Ryder's answer here function(s,z=10^nchar(s)-1:0,n=scan(t=pmax(s,0)),a=c(diff(n*z[2:1]),z[1]),c=1:z)a/max(c[!a%%c&!a[2]%%c]) Try it online! Input is a vector of two strings representing x and ...


7

Scratch, 310 bytes Try it online! In scratchblocks4: when gf clicked ask()and wait set[N v]to(answer ask()and wait set[D v]to(answer set[Q v]to((N)/(D if<(Q)=[NaN]>then if<(A)contains(0)?>then set[Q v]to(0 else set[Q v]to(1 end end if<<(join(N)(D))contains(-)?>and<not<<(N)contains(-)?>and<(D)contains(-)?>>>>...


6

Factor + math.primes math.unicode, 38 33 bytes [ dup length nprimes swap v^ Π ] Try it online! Explanation: dup duplicate the input (e.g.) { 0 1 2 3 } { 0 1 2 3 } length find the length of a sequence { 0 1 2 3 } 4 nprimes generate primes given the number on top of the data stack { 0 1 2 3 } { 2 3 5 7 } swap swap the top two objects on the data stack { 2 3 ...


6

Brachylog, 30 bytes {s{h∧1|~cṪ↺₁hʰb=h↰ᶠ⌉+₁}ᵘ}ᶠcọtᵐ Try it online! {s … }ᶠ for every substring: {h∧1| …} on every non-empty string, return 1. Also try … ~c input split into groups, so that … Ṫ↺₁…b= it unifies with [A,B,A] (this works, too, but is sadly longer). hʰ B is non-empty h↰ᶠ take A and call the predicate recursively, finding all results ⌉+₁ get the ...


6

05AB1E, 10 bytes ÝD¦âΣ`/α}¬ Try it online! Explanation Ý # [0 .. input] D # Duplicate ¦ # Tail -> [1 .. input] â # Cartesian product Σ } # Sort by α # Absolute difference between # Input 2 `/ # and numerator / denominator ¬ # Head


6

APL (Dyalog Unicode), 41 bytes (|40+⎕)+.×(|÷≢)1-1.8*1(-,+)⍨⍣(⎕-2)⊢1-3|⍳4 Try it online! A full program. Takes the number of people, then the initial temperature. TIO version is wrapped in a function for easier testing. As I pointed out in a comment, the initial and final units do not matter as long as they are equal. The math The code exploits the fact ...


6

J, 39 32 30 27 bytes {.@".@]-".@[*(%<:)~&(10x^#) Try it online! -5 thanks to Bubbler. Also go upvote his j answer, which is more creative and has delightful application of base #. The wikipedia formula translated into J. The only part that might be of some interest is the calculation of z/w: (%&x:<:)~&(10^#) &(1>.10^#) ...


6

JavaScript (ES7),  80  76 bytes Expects (x)(y) as strings (y may be an empty string) and returns [numerator, denominator]. x=>y=>(G=(a,b)=>b?G(b,a%b):[p/a,q/a])(q=1-10**x.length,p=y*q+x*10**y.length) Try it online! Commented x => // outer function taking x y => // inner function taking y ( G = (a,...


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