17
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Erik the Outgolfer Jun 20 '18 at 23:55
  • \$\begingroup\$ Can we output True instead of 1? Alternatively, float instead of ints? \$\endgroup\$ – Jo King Jun 21 '18 at 0:33
  • 1
    \$\begingroup\$ @JoKing yes, yes. \$\endgroup\$ – Leaky Nun Jun 21 '18 at 0:34
  • \$\begingroup\$ @EriktheOutgolfer Challenge accepted :D \$\endgroup\$ – DLosc Jun 22 '18 at 4:46

32 Answers 32

1
2
0
\$\begingroup\$

JavaScript (Node.js), 29 bytes

f=(n,k=n)=>--k&&!(n%k)+f(n,k)

Try it online! Note: Stack overflows for larger inputs.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.