30
\$\begingroup\$

Sandbox

Definition: A positive integer n is almost-prime, if it can be written in the form n=p^k where p is a prime and k is also a positive integers. In other words, the prime factorization of n contains only the same number.

Input: A positive integer 2<=n<=2^31-1

Output: a truthy value, if n is almost-prime, and a falsy value, if not.

Truthy Test Cases:

2
3
4
8
9
16
25
27
32
49
64
81
1331
2401
4913
6859
279841
531441
1173481
7890481
40353607
7528289

Falsy Test Cases

6
12
36
54
1938
5814
175560
9999999
17294403

Please do not use standard loopholes. This is so the shortest answer in bytes wins!

\$\endgroup\$
9
  • \$\begingroup\$ To clarify: the truthy and falsy values need not be consistent, right? \$\endgroup\$ – Luis Mendo Aug 26 '20 at 0:42
  • 6
    \$\begingroup\$ This is A000961 in the OEIS. \$\endgroup\$ – Giuseppe Aug 26 '20 at 13:21
  • 21
    \$\begingroup\$ The usual name for this kind of number is "prime power". \$\endgroup\$ – Andreas Rejbrand Aug 26 '20 at 17:39
  • 8
    \$\begingroup\$ It feels odd to me that you include prime numbers as being "almost prime," but this is still a good challenge! :) \$\endgroup\$ – Captain Man Aug 26 '20 at 18:26
  • 9
    \$\begingroup\$ This should use the terminology "prime power". en.wikipedia.org/wiki/Almost_prime already has a definition. \$\endgroup\$ – qwr Aug 28 '20 at 5:27

31 Answers 31

1
2
0
\$\begingroup\$

05AB1E, 2 bytes

fg

Try it online!

fg  # full program
 g  # number of...
f   # prime factors of...
    # implicit input
    # implicit output
\$\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.