Surprisingly, I don't think we have a question for determining if a number is semiprime.

A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers.

Simple enough, but a remarkably important concept.

Given a positive integer, determine if it is a semiprime. Your output can be in any form so long as it gives the same output for any truthy or falsey value. You may also assume your input is reasonably small enough that performance or overflow aren't an issue.

Test cases:

input -> output
1     -> false
2     -> false
3     -> false
4     -> true
6     -> true
8     -> false
30    -> false   (5 * 3 * 2), note it must be EXACTLY 2 (non-distinct) primes
49    -> true    (7 * 7)      still technically 2 primes
95    -> true
      -> true, and go call someone, you just cracked RSA-2048

This is , so standard rules apply!

  • 4
    \$\begingroup\$ @WheatWizard There's a slightly difference in that that one asks for 3 primes (not a big difference for almost all languages) and it's for distinct primes only (fairly different for some languages). I can discuss it with you in chat if you'd like to continue a more detailed discussion. \$\endgroup\$ – HyperNeutrino Aug 7 '17 at 18:30
  • 2
    \$\begingroup\$ @WheatWizard You raise a good point, but similarly, we already have a bunch of many types of questions, and although, in contrast to what you express, most of them do add significant contribution to their area, this question has enough of a difference that I would believe that it warrants a separate question/post. \$\endgroup\$ – HyperNeutrino Aug 7 '17 at 18:33
  • 2
    \$\begingroup\$ @hyperneutrino looking at the answers on both challenges, it looks like the difference is a single number in the source code, 2 vs 3. I would hardly call that a big difference. \$\endgroup\$ – Post Rock Garf Hunter Aug 7 '17 at 18:37
  • 2
    \$\begingroup\$ @WheatWizard There is also "distinct" vs "not distinct"... \$\endgroup\$ – HyperNeutrino Aug 7 '17 at 18:40
  • 3
    \$\begingroup\$ @LordFarquaad Just because its a duplicate doesn't mean it is bad. In my mind being a duplicate is a good thing, it means that you are asking a thing that the community finds interesting enough to have already asked about. \$\endgroup\$ – Post Rock Garf Hunter Aug 7 '17 at 18:50

33 Answers 33


PHP, 75 bytes

<?php for($c=$t=2;$n=&$argv[1]>1;$t++)while($n%$t<1){$c--;$n/=$t;}echo+!$c;

Try it online!

This script is called from the command line with the number being tested as its parameter. It prints out a 1 if the number was a semi-prime, else a 0.



Work through each integer, $t, from 2 up, to see if it's a factor. We need to keep going, dividing by the factors until $n (an alias for $argv[1], the first CLI parameter) reaches 1. (And as there is a 2 to hand, we take the chance to initialize the counter, $c, to 2.)

for ($c = $t = 2; $n = &$argv[1] > 1; $t++)

When we find a factor, we keep using it for as long as we can…

    while ($n % $t < 1) {

…each time deccrementing the factor counter…


…and recalculating our number.

        $n /= $t;

When we reach the end, the counter can only be 0 if there were 2 factors. We ‘not’ the number, but this converts it to a boolean, so we use + to make it into 1 (the number was a semi-prime) or 0 (it wasn’t).

echo +!$c;

Braingolf, 8 bytes


Try it online!


APL(NARS), 7 chars, 14 bytes



  f¨1 2 3 4 6 8 30 49 95
0 0 0 1 1 0 0 1 1 

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.