A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

New answers tagged

6

Python, 67 bytes g=lambda z,w:(z/w).real>0and'T'+g(z-w,w)or z+w*w and'S'+g(w,-z)or'' Try it online! Thanks to Bubbler for cutting a byte Takes input as two complex numbers for the rows. The idea is simple and easy-to-see on a less-golfed version: def f(a,b,c,d): if (a,b,c,d)==(1,0,0,1): return '' elif a*c+b*d>0: return 'T'+f(a-c,b-d,c,d) else: ...


0

Python 3, 193 192 191 bytes Takes in a 2D numpy array and returns a string of S and T. Uses the algorithm in the question. import numpy def g(m,x=0): if m[1,0]==0:y=m[0,1];return['T','SSSTSTS'][y<0]*abs(y) while(m[:,0]<[1,0]).any():x+=1;m=[[0,1],[-1,0]]@m q=-m[0,0]//m[1,0];return'S'*x+'T'*-q+g([[1,q],[0,1]]@m) Try it online!


2

Jelly,  24  22 bytes ⁽¬Pb3’s2$⁺⁸ṃæ×/⁼ 0ç1#B A monadic Link accepting the target matrix which outputs a list containing a single list of 1s (\$S\$) and 0s (\$T\$). As a full program a Jelly representation of this list is printed. Always yields a resulting equation with a leading \$S\$ (except for an input of \$T\$ itself). Note that since \$S^4=I\...


2

Python 3, 301 \$\cdots\$ 139 129 bytes def f(a,b,c,d,l=''): while c: while 0>a or b<0:a,b,c,d=c,d,-a,-b;l+='S' if c:q=-a//c;a+=q*c;b+=q*d;l+='T'*-q return l+'T'*b Try it online! Saved 18 bytes thanks to Arnauld!!! Takes the 4 matrix elements as separate numbers as input and outputs a string of 'S's and 'T's.


0

Ruby, 96 bytes ->m{z=[['',1,0,0,1]];z.find{|a,q,w,e,r|z<<[a+?S,w,-q,r,-e]<<[a+?T,q,q+w,e,e+r];m==[q,w,e,r]}[0]} Try it online! Use the (brute) force.


2

MATL, 42 38 32 bytes `@B"@?FT!tqh}IlhB]]Nq:"Y*]G-z}@B The code enumerates all finite sequences that begin with S, and stops when a solution is found. It is sufficient to test sequences beginning with S because S*S*S*S equals the identity matrix (inspiration from @Adam's APL solution). Outputs a binary sequence, where 1 corresponds to matrix S and 0 to ...


0

Charcoal, 64 bytes W∨§η⁰⊖§θ⁰¿‹⁰קθ⁰§η⁰«TUMθ⁻κ§ηλ»«S≔±θι≔ηθ≔ιη»×TΠθ¿›⁰Πθ«SSS×TST±ΠθS Try it online! Link is to verbose version of code. Based on the provided algorithm. Explanation: W∨§η⁰⊖§θ⁰ Repeat while the first column is not [1, 0]. ¿‹⁰קθ⁰§η⁰ If the product of the first column is positive... «TUMθ⁻κ§ηλ» ... then print a T and subtract the second ...


2

05AB1E, 48  44  43 40 bytes Crossed out &nbsp;44&nbsp; is no longer 44 :) 9bS[)DðýIk©d#¼vyDÁ0T·Sǝ+y2Å€(}2ôí˜]T¾ã®è 9bS and DÁ0T·Sǝ+ could alternatively be 9Yв/т1ª/тĆS/TºS and D2ôO13Sǝ/DÁ2Å€0}+ for the same byte-count. Brute-force approach with pretty bad performance (it times out for the last three test cases - the previous 48;44;43 ...


3

APL (Dyalog Extended), 47 bytesSBCS Anonymous tacit prefix function. Gives Boolean list with 0 and 1 meaning \$S\$ and \$T\$ respectively. {⍺≡⊃+.×/(r←⊤⍵)⊇2 2∘⍴¨(0 ¯1 1)(1 1 0):r⋄⍺∇1+⍵}∘1 Try it online! (Faster edition emulating Extended in Unicode.) {…}∘1 apply the following function with 1 and the given matrix as right and left arguments:  (0 ¯1 1)(1 1 ...


7

JavaScript (ES6), 68 bytes This is using xnor's method -- but obviously without complex numbers. Takes the input matrix \$\begin{pmatrix}a&b\\c&d\end{pmatrix}\$ as 4 distinct parameters a,b,c,d. Returns a string of digits (\$0\$ for \$S\$, \$1\$ for \$T\$). f=(a,b,c,d)=>b|c|a-1|d-1?a*c>-b*d?1+f(a-c,b-d,c,d):0+f(c,d,-a,-b):'' Try it online! ...


Top 50 recent answers are included