A
A
Alexander Pavlov2021-11-18 17:06:20
Python
Alexander Pavlov, 2021-11-18 17:06:20

How can I find a prime number that has only one divisor?

Hello.

There is a function:

def is_prime(random_number):
    number = 0
    for i in range(2, random_number // 2 + 1):
        if random_number % i == 0:
            number = 1
    if number <= 0:
        return True
    else:
        return False


So that True is returned when the first divisor is found, do I understand correctly that you just need to remove +1 in the condition ? for i in range(2, random_number // 2):

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexSeley, 2021-12-08
@AlexSeley

def is_prime(random_number):
number = 0
for i in range(2, random_number // 2 + 1):
if random_number % i == 0:
number = 1
if number <= 0:
return True
Read below
else:
return False
We we can write exit(0) - If True, then it will exit the code immediately
We can write break - If True, then it will exit the function (loop)
As for me, it's better like this:
m = (float(input("Enter your number: ")))
number = 0
if m % 2 == 0:
print("Complex")
return True
exit(0)
for i in range(3, random_number // 2, 2):#It starts with a 3 (taking it into account) and goes further through a 2 because we checked again by looping whether the number is divisible by 2 and so it will be 2 times faster because it does not go through numbers that are divisible by 2.
if m % i = = 0:
print("Complex")
exit(0)
else:
print("Simple")
exit(0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question