# Recover the power from the prime power

It seems that many people would like to have this, so it's now a sequel to this challenge!

Definition: a prime power is a natural number that can be expressed in the form pn where p is a prime and n is a natural number.

Task: Given a prime power pn > 1, return the power n.

Testcases:

input output
9     2
16    4
343   3
2687  1
59049 10


Scoring: This is . Shortest answer in bytes wins.

• Note: This challenge might be trivial in some golfing languages, but it's not so trivial for some mainstream languages, as well as the language of June 2018, QBasic. Commented Jun 20, 2018 at 23:55
• Can we output True instead of 1? Alternatively, float instead of ints?
– Jo King
Commented Jun 21, 2018 at 0:33
• @JoKing yes, yes. Commented Jun 21, 2018 at 0:34
• @EriktheOutgolfer Challenge accepted :D Commented Jun 22, 2018 at 4:46

f n=sum$(0^).mod n<$>[2..n]


Try it online!

Counts factors. Compare:

f n=sum[1|0<-mod n<$>[2..n]]  Try it online! Haskell, 28 bytes f n=sum[0^mod n i|i<-[2..n]]  Try it online! Haskell, 30 bytes f n=sum[1|i<-[2..n],mod n i<1]  Try it online! # Octave, 18 bytes @(x)nnz(factor(x))  Try it online! Does what it says on the tin: Number of non-zero elements in the prime factorization of the input. # Cjam, 5 bytes rimf,  Try it Online! Explanation: ri take the input and convert it to an int mf factors the input , take the length of the list  Builtins are great! # JavaScript (Node.js), 29 bytes f=(n,k=n)=>--k&&!(n%k)+f(n,k)  Try it online! Note: Stack overflows for larger inputs. # F#, 91 bytes let rec d n c v=if v=n then c else d(n/v)(c+1)v let p n=d n 1(Seq.find(fun x->n%x=0){2..n})  Try it online! p gets the prime factor. d recursively divides the target value until it's equal to the prime factor and returns the count from that. # Julia, 19 bytes port of Xi'an's answer in R n->sum(n.%(2:n).<1)  Try it online! # Retina 0.8.2, 30 bytes .+$*
((?=(1+)(\2+)$)\3)+1$#1


Try it online! Link includes test cases. Explanation:

.+
$*  Convert the input to unary. ((?=(1+)(\2+)$)\3)+1


Repeatedly find the largest factor of the current value. Eventually this becomes 1, which is then matched at the end outside of the loop.

$#1  Output the resulting number of factors, which for a prime power will be the power. # Factor + math.primes.factors, 18 bytes [ factors length ]  Try it online! # Excel, 34 bytes =SUM((MOD(A1,SEQUENCE(A1-1))=0)*1)  Link to Spreadsheet Counts the factors. Works up to 2 ^ 20. # Husk, 2 bytes Lp  Try it online! ### Explanation Lp L length of p prime factors  # BQN, 7 bytesSBCS +´0=↕|⊢  Run online! Translates directly to Dyalog APL: +/0=⍳|⊢  Try it online! # Gaia, 2 bytes ḍl  Try it online! # JavaScript (ES6), 37 bytes f=(n,k=2)=>n%k?n>1&&f(n,k+1):1+f(n/k)  Try it online! # Perl 6, 36 bytes {round .log/log (2..*).first:$_%%*}


Looks for the first factor (2..*).first: $_%%*, then from there calculates the approximate value (logs won't get it exact) and rounds it. Try it online! # Pari/GP, 8 bytes bigomega  Try it online! bigomega(x): number of prime divisors of x, counted with multiplicity. # Pari/GP, 14 bytes n->numdiv(n)-1  Try it online! # Racket, 31 bytes (car(cdr(perfect-power(read))))  Try it online! # Perl 6, 18 bytes {+grep($_%%*,^\$_)}


Try it online!

Anonymous code block that gets a list of factors and coerces it to a number.