1
def is_prime(num):

    if num > 1:

        for i in range(2,num):

return False if (num % i) == 0:

            if (num % i) == 0:
                return False

return True if prime

        else:
            return True

return False if not prime

    else:
        return False

def is_semiprime():
        n = int(input("Enter a number to find out if its semiprime or not\n>>> "))
        for d1 in range(2, int(n**.5)):
            if n % d1 == 0:
                d2 = int(n / d1)

is_prime(d1) and is_prime(d2) go through and don't tell me there true or false they just send blank lines.

               return is_prime(d1) and is_prime(d2)
            return False

is_semiprime()

when it Returns is_prime(d1) and is_prime(d2) if they are both True then return True

MarshallCodeX
  • 43
  • 2
  • 7
  • Is your problem (a) that you don't know what semiprimes are, (b) that you don't know an algorithm to test for semiprimes, or (c) that you do, but you're stuck on some particular step in implementing it? If it's (c), show us the code you _do_ know how to write, and point out the gap (like `count += 1 # I know I need to do count += n if i**n divides num but I don't know how to get n`). – abarnert Apr 25 '18 at 21:49
  • 1
    Write a function that just checks whether a number is prime. Write another function that gets the two factors of a number, and calls the `isPrime()` function on both of them. If they're both true, the number is semiprime. – Barmar Apr 25 '18 at 22:40
  • @Barmar What are “the two factors” of 60? Or 5? – abarnert Apr 25 '18 at 23:05
  • @abarnert If the number is not prime, you should be able to find a factor. Divide the number by that factor to get the other factor. Then check whether both of these are prime. – Barmar Apr 25 '18 at 23:06
  • @Barmar (b) I Don't know an algorithm to test for semi primes. Also a semi prime number is a number made up of two primes so how do i code it to check for that – MarshallCodeX Apr 27 '18 at 20:43
  • I just told you the algorithm to test for semi primes. Find one divisor of the number. Divide the number by that to get a second divisor. Test whether both divisors are prime. That's all there is to it. – Barmar Apr 27 '18 at 20:56
  • oh ok sorry thanks didnt see that – MarshallCodeX Apr 27 '18 at 21:03

3 Answers3

6

A semi-prime number is a number that's the product of two prime numbers. So the algorithm is simple:

  1. Find one divisor of the number, call it d1.
  2. Divide the number by d1 to get a second divisor, d2.
  3. Test whether both d1 and d2 are prime. If they are, then the original number is semi-prime.

Code:

def is_semiprime(n):
    for d1 in range(2, int(n**.5)+1):
        if n % d1 == 0:
            d2 = n / d1
            return is_prime(d1) and is_prime(d2)
    return False

def is_prime(n):
  if n == 2 or n == 3: return True
  if n < 2 or n%2 == 0: return False
  if n < 9: return True
  if n%3 == 0: return False
  r = int(n**0.5)
  f = 5
  while f <= r:
    if n%f == 0: return False
    if n%(f+2) == 0: return False
    f +=6
  return True    

def ask_semiprime():
    num = int(input("What number would you like to check?"))
    if is_semiprime(num):
        print(num, " is semiprime")
    else:
        print(num, " is not semiprime")

ask_semiprime()

I copied the is_prime function from isPrime Function for Python Language

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • yep i have a prime function – MarshallCodeX May 01 '18 at 20:47
  • so my code wont report to me if the imputed number is semi prime. – MarshallCodeX May 08 '18 at 21:23
  • `if is_semiprime(number): print(number, " is semiprime")` – Barmar May 08 '18 at 21:28
  • my code above was changed though that wont work. well idk were to put it in my code lol – MarshallCodeX May 09 '18 at 20:42
  • You need to learn to separate functions that perform input and output from functions that compute results. – Barmar May 09 '18 at 20:45
  • oh well i just dont get why it passed P and adds 2 sometimes not all the time – MarshallCodeX May 09 '18 at 20:45
  • What is `P`, and why are you testing it in your `elif` statement? – Barmar May 09 '18 at 20:53
  • `is_prime()` needs to return `True` or `False` to indicate whether `num` is prime or not. It shouldn't print anything. – Barmar May 09 '18 at 20:55
  • im testing it in elif statement because when is_prime(d1), is_prime(d2) go through to the els statment in is_prime it is suposed to add up to 4 which tells me that the inputed number is semiprime. – MarshallCodeX May 09 '18 at 20:59
  • I don't understand what you're doing at all. Stop using global variables, use parameters and return values. `is_semiprime()` calls `is_prime()` twice, they'll each set `P` differently. You only initialize `P` once, at the beginning of the script, so each time you call `is_prime()` it increments it from where it left off before. – Barmar May 09 '18 at 21:03
  • But your version of `is_semiprime()` doesn't even use the result of `is_prime()`. It needs to test if it's true of both `d1` and `d2`. – Barmar May 09 '18 at 21:04
  • What does adding up to 4 have to do with whether a number is semiprime? I think you're very confused about what "semiprime" means. – Barmar May 09 '18 at 21:05
  • You need to write a self-contained function called `is_prime(num)` that just returns `True` or `False`. The function you've written doesn't work like that. Once you've done that, my `is_semiprime()` function will work. If you're having trouble with that, it's a separate question. – Barmar May 09 '18 at 21:08
  • A code that will factor any number inputted and report to the user if the number was, semi prime. this i what i have to do and it is for school. No one has completed it – MarshallCodeX May 09 '18 at 21:26
  • I know what a semiprime number is. the problem is that i cant make it tell me the number inputed is semi prime, returning true or false sends a blank line lol – MarshallCodeX May 10 '18 at 20:56
  • I've added all the other functions you need. – Barmar May 10 '18 at 21:00
  • btw what version of python are you because it tells me 49 is not semi prime when it is – MarshallCodeX May 10 '18 at 21:24
  • Sorry, thought the factors had to be different, so I wasn't including `n**.5` in the range checked. I changed it to `range(2, int(n**.5)+1)` and now it works. – Barmar May 10 '18 at 21:36
  • ah ok cool I also changed the false in is_semiprime(n) to Capital False – MarshallCodeX May 10 '18 at 21:39
1

number=int(input())

s=number

x=[]

for i in range(2,number+1):

if s%i==0:

    x.append(i)

    s=s//I

    if s==i or (i>s and s!=1):

        x.append(s)

if len(x)==2:

print(f"{number} is a semiprime number because it is the product of Tow primes: ",end="")

else:

print(f"{number} is not a semiprime number because it is the product of {len(x)} primes: ",end="")

for i in x:

if i==x[-1]:

    print(i)

else:

    print(str(i)+"*",end="")
1
def check_prime(n): #for checking wheather prime or not
    if n > 1:
        if n == 2: return True
        for i in range(2,n):
            if n % i == 0:
                return False
                break
        return True
    return False
def primeproduct(m): #for checking wheather prime product or not 
    if m >= 0:
      for i in range(1,m):
        if m%i == 0 and check_prime(i) and check_prime(m//i):
            return True
            break
    return False
  

this may help