# 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. Jun 20 '18 at 23:55
• Can we output True instead of 1? Alternatively, float instead of ints?
– Jo King
Jun 21 '18 at 0:33
• @JoKing yes, yes. Jun 21 '18 at 0:34
• @EriktheOutgolfer Challenge accepted :D Jun 22 '18 at 4:46

# 05AB1E, 2 bytes

Òg


Try it online!

# Python 2, 37 bytes

f=lambda n,i=2:i/n or(n%i<1)+f(n,i+1)


Try it online!

Counts factors. Apparently I wrote the same golf in 2015.

Narrowly beats out the non-recursive

Python 2, 38 bytes

lambda n:sum(n%i<1for i in range(1,n))


Try it online!

# Python 3, 49 bytes

f=lambda n,x=2:n%x and f(n,x+1)or n/x<2or-~f(n/x)


Try it online!

Outputs True instead of 1 (as allowed by OP). Recursive function that repeatedly finds the lowest factor and then calls the function again with the next lowest power until it reaches 1. This is an extension of my answer to the previous question.

# Pyth, 2

Count prime factors:

lP


# face, 86 bytes

(%d@)$*,c',io>Av"[""mN*c?*m1*mp*m%*s1"pN1p:~+p1p%%Np?%~:=/NNp+?1?-%N1?%=p%'i?w1'%>  Hooray, longer than Java! Try it online! I am particularly fond of the trick of using the return value of sscanf. Normally the return value would be discarded, but here it will always be 1, because we're always reading a single number as input. We can take advantage of this by assigning its return value to the variable 1, saving the 2 bytes that would otherwise be required to assign 1 to 1 explicitly. (%d@)$*,c'$,io> ( setup - assign$ to "%d", * to a number, o to stdout )
Av"[""mN*    ( set " to input and allocate space for N for int conversion )
c?*          ( calloc ?, starting it at zero - this will be the output )
m1*          ( allocate variable "1", which gets the value 1 eventually )
mp*m%*       ( p is the prime, % will be used to store N mod p )

s1"$pN ( scan " into N with$ as format; also assigns 1 to 1 )

1p:~         ( begin loop, starting p at 1 )
+p1p       ( increment p )
%%Np       ( set % to N mod p )
?%~          ( repeat if the result is nonzero, so that we reach the factor )

:=           ( another loop to repeatedly divide N by p )
/NNp       ( divide N by p in-place )
+?1?       ( increment the counter )
-%N1       ( reuse % as a temp variable to store N-1 )
?%=          ( repeat while N-1 is not 0 -- i.e. break when N = 1 )

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