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

45 Answers 45

1
2
1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ Submissions must be programs or functions by default, and we don't consider this a function. Both rimf, (full program) and {mf,} (function) would be valid. \$\endgroup\$
    – Dennis
    Jun 21, 2018 at 20:49
  • \$\begingroup\$ @Dennis Yeah, I think I'm kind of confused on that. I also looked at allowed stardard io before and wondered about what I should actually submit... I actually wanted to ask a question on meta about that. But you confirmed that, so thanks! \$\endgroup\$
    – Chromium
    Jun 22, 2018 at 3:19
1
\$\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.

\$\endgroup\$
1
\$\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.

\$\endgroup\$
1
\$\begingroup\$

Julia, 19 bytes

port of Xi'an's answer in R

n->sum(n.%(2:n).<1)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Factor + math.primes.factors, 18 bytes

[ factors length ]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Excel, 34 bytes

=SUM((MOD(A1,SEQUENCE(A1-1))=0)*1)

Link to Spreadsheet

Counts the factors. Works up to 2 ^ 20.

\$\endgroup\$
1
\$\begingroup\$

Husk, 2 bytes

Lp

Try it online!

Explanation

Lp
L  length of
 p prime factors
\$\endgroup\$
1
\$\begingroup\$

BQN, 7 bytesSBCS

+´0=↕|⊢

Run online!

Translates directly to Dyalog APL:

+/0=⍳|⊢

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Gaia, 2 bytes

ḍl

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 37 bytes

f=(n,k=2)=>n%k?n>1&&f(n,k+1):1+f(n/k)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

Racket, 31 bytes

(car(cdr(perfect-power(read))))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 18 bytes

{+grep($_%%*,^$_)}

Try it online!

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

\$\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.