New answers tagged

1

Sledgehammer, 11 bytes Alternatively, 83 bits. I ended up forgetting to actually answer this with Sledgehammer for a while... ⣜⢍⡪⡱⣰⡵⠅⣧⣼⣅⣸ Compressed from this Wolfram Language code: If[NumberQ[x1 = Log[Input[], Input[]]], x1, -1] , where NumberQ is a built-in that checks whether Log has returned an actual number and not a symbolic expression, i.e. ...


2

Charcoal, 27 bytes ⊙θ⊙η∧⁼ιλ⁼×IΦη⁻ξμIθ×IΦθ⁻ξκIη Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for phony, nothing otherwise. Explanation: θ First input as a string ⊙ Any character satisfies η Second input as a string ⊙ ...


2

Excel (Ver. 1911), 64 Bytes Fraction entered as a row literal of 2 strings e.x. ={"16","64"} A1 'Input: row literal of 2 strings -> ={num,den} C1:D9 {=SUBSTITUTE(A$1#,ROW(),,1)} 'Array formula (entered with <C-S-Enter>) E1 =SUM((C1:C9/D1:D9=A1/B1)*(A1#<>C1#)) 'Output (truthy/falsy int) Test Sample


3

C (gcc), 128 126 bytes Thanks to ceilingcat for saving two bytes. P,h,o,n;i(e,s){for(n=P=1;e/P;P*=10)for(h=1;s/h;h*=10)e/P%10&&e/P%10==s/h%10&&(o=s/h/10*h+s%h,n*=!o|(e/P/10*P+e%P)*s-e*o);e=n;} Try it online!


3

Jelly,  18  17 bytes DḌ-Ƥż$€÷þ/Ẏċ÷/,1Ɗ A monadic Link accepting a list, [numerator, denominator] which yields zero (falsey) if not reducible, or a positive integer (truthy) if reducible. Try it online! Or see the test-suite. How? DḌ-Ƥż$€÷þ/Ẏċ÷/,1Ɗ - Link: [n, d] D - decimal digits (vectorises) $€ - last two links ...


4

Jelly, 22 bytes DżḌ-Ƥ$€ŒpḢ€E$Ƈ÷/€F=÷/Ẹ Try it online! A monadic link taking a list of [num, den] and returning 1 for phony and 0 for non-phony. Explanation D | Convert to decimal digits $€ | For each list of decimal digits: ż | - Zip with: Ḍ-Ƥ | - A list of lists of digits ...


4

Perl 6, 58 bytes {[(&)] .map:{m:g/./>>.&{(.prematch~.postmatch)/.orig~$_}}} Try it online! Same approach as in Jitse's Python answer. Alternative, 75 bytes {?grep {[==] $_ Z*$^m[(3,4),(0,2)]>>.join},m:ex/^(.*)(.)(.*)\s(.*)$1(.*)$/} Try it online! Same regex-based approach as in Neil's Retina answer.


3

Ruby, 109 86 81 78 bytes ->n,d{g=->n,d{w=1;n.digits.map{|s|[s,d*(n%w+w*(n/w*=10))]}};g[n,d]&g[d,n]!=[]} Try it online! Saved some bytes by using multiplication instead of division: if a/b==a0/b0, then a*b0==a0*b. Then stole some ideas from Jitse's excellent Python answer (upvote him!) to trim a couple of bytes off the corners.


2

Retina, 69 bytes L$w`(.)(.*)/(.*)\1 $($`$2)*$($3$1$')*_/$($`$1$2)*$($3$')* \b(_+)/\1\b Try it online! Link includes test suite. Outputs the number of phony pairs of digit cancellations. Explanation: L$w`(.)(.*)/(.*)\1 List all matching pairs of digits in the numerator and denominator, including overlaps. $($`$2)*$($3$1$')*_/$($`$1$2)*$($3$')* Cross-...


6

05AB1E, 26 23 22 bytes €æ`âεR*Ë9ݺIJySõ.;å*}à -3 bytes thanks to @Grimmy. Input as a pair [numerator, denominator]. Try it online or verify all test cases. Explanation: €æ # Get the powerset of each number in the (implicit) input-pair ` # Push both lists separated to the stack â # Create all possible pairs by taking the ...


4

JavaScript (ES6),  100  93 bytes Saved 7 bytes by using @Jitse's method Takes input as ('numerator')('denominator'). Returns a Boolean value. n=>d=>(g=n=>[...n].map((x,i)=>x+-(n.slice(0,i)+n.slice(i+1))/n))(n).some(v=>g(d).includes(v)) Try it online!


4

T-SQL, 192 bytes Returns -1 for true, 0 for false WITH x as(SELECT substring(@n,number,1)b,substring(@,number,1)a,number n FROM spt_values)SELECT~(1/~count(*))FROM x,x y WHERE x.b=y.a AND x.b>0and 1*stuff(@,x.n,1,'')*@n=@*1*stuff(@n,y.n,1,'') Try it online


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. ...


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 ...


2

JavaScript (Node.js),  72  68 bytes Takes input as (a)(b), where \$a\$ and \$b\$ are BigInts. Returns either [numerator, denominator] or \$-1\$. a=>g=(b,p=0n,q=1n)=>p>b?-1:(d=a**p-b**q)?g(b,d>0?++q&&p:-~p,q):[p,q] Try it online! How? We start with \$p=0\$ and \$q=1\$. We iteratively increment \$q\$ if \$a^p>b^q\$ or ...


1

Ruby, 72 bytes ->a,b{n,d=[*1..a*b].product([*1..a]).find{|n,d|a**n==b**d};n ?n/1r/d:-1} Try it online!


2

bc, 110 bytes define f(a,b){ c=0 d=1 while(0!=e=a^c-b^d){if(a<2^d){print"-1";return} if(e>1)d=d+1 else c=c+1} print c,"/",d} Try it online! As with a number of my solutions, it outputs its answer and returns 0, which the test code ignores. The use of scale= in the provided input is only so the expected value can be passed in. (You can't have a ...


4

Python 3, 94 79 76 bytes Nothing clever here; this is the obvious brute force approach. Edit: Unified return point and switched to lambda syntax. Edit 2: Shaved off three bytes due to @xnor's next trick and rules clarification. lambda a,b:next(((i,j)for i in range(b)for j in range(1,a)if a**i==b**j),-1) Try it online!


5

05AB1E, 18 17 bytes -1 byte thanks to Kevin Cruijssen Ó0ζD€¿÷ʒOĀ}DËiнë® Try it online! Ó # prime factorize each input 0ζ # zip the factorizations together with filler 0 # for each pair of exponents: D€¿ # get the gcd ÷ # divide both by the gcd (reducing the ...


5

Charcoal, 88 82 bytes ≔Eθ⟦⟧ζ≔²ηW⊖⌈θ¿⌊﹪θη≦⊕ηUMθ⎇﹪κηκ∧⊞O§ζλη÷κηFζFι⊞υκUMυEζ№λιUMυ÷ι⊟Φ⊕⌈ι∧λ¬⌈﹪ιλ¿›⌈υ⌊υ-1I⊟υ Try it online! Link is to verbose version of code. Takes input as a list in [value, base] order and outputs numerator and denominator. Edit: Saved 6 bytes by simplifying my check for exclusive factors. Explanation: ≔Eθ⟦⟧ζ≔²ηW⊖⌈θ¿⌊﹪θη≦⊕ηUMθ⎇﹪κηκ∧⊞O§ζλη÷...


5

Python 3, 103 101 97 bytes Saved 2 bytes thanks to Kevin Cruijssen!!! Saved 4 bytes thanks to Value Ink!!! from math import* def f(a,b): l=log(b,a) n,m=l.as_integer_ratio() return -1if a**n-b**m else l Try it online!


8

Mathematica, 47 45 43 29 bytes SBCS If[NumberQ[c=#~Log~#2],c,-1]& You can try it online! Thanks to @J42161217 for saving me a total of 16 bytes!!


Top 50 recent answers are included