Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

2 of 7
fixed assignment to f; added TIO link
Misha Lavrov
  • 5.3k
  • 14
  • 27

Mathematica, (削除) 175 (削除ここまで) 177 bytes

##&@@Table[#,Abs@#2]&@@@#&/@GatherBy[r@#,Sign@*Last]&@*(If[#==1,1,{p,e}=(f=First)@MaximalBy[(r=FactorInteger)@#,f];q=1~Max~NextPrime[p,-1];(p/q)^e#0[#(1##&@@Range[q+1,p])^-e]]&)

Try it online!

How it works

This is the composition of two functions. The first, which ungolfs to

If[#==1,
 1,
 {p,e}=f@MaximalBy[(r=FactorInteger)@#,f=First];
 q=1~Max~NextPrime[p,-1];
 (p/q)^e #0[#(1##&@@Range[q+1,p])^-e]
]&

is a recursive function for actually computing the desired numbers. Given input x, we find the largest prime factor p appearing in x (to the power e), let q be the prime before p, and recurse on x*(p!/q!)^-e. (So, for example, given x=10/9 we'd pull out a factor of 5!/3! and recurse on 1/18.)

This function computes the result, but (to cancel like factors) it writes the result as a rational number. For example, 10/9 gets mapped to 2*5/3*3*3 = 10/27, which we want to convert to the list {2,5},{3,3,3}. That's what the second part, ungolfing to

##& @@
 Table[#,Abs@#2]& @@@ #& /@
 GatherBy[r@#,Sign@*Last]&

does: we factor 10/27, gather prime factors by the sign of their powers, and un-Tally the lists.


+2 bytes: the assignment f=First has to be done in the right place to keep Mathematica from getting upset.

Misha Lavrov
  • 5.3k
  • 14
  • 27

AltStyle によって変換されたページ (->オリジナル) /