I recommend you read through this as it makes helping you easier for others: About the Python Help category
Anyway, I’ve renamed the values to be more descriptive in your code:
potential_prime_squared = 3 # q
potential_prime_divisor = 2 # w
while 0 == 0: # Why not just use 'True'?
potential_prime = potential_prime_squared ** 0.5 # t
if potential_prime_squared % potential_prime_divisor == 0: # interstingly enough this works because pontential_prime_divisor only reaches (at max.) the value of potential_prime-1 meaning that the square of a prime is not divisible by any number in the range of 2 to prime-1, which I find to be quite interesting, see proof of concept
potential_prime_squared += 1
potential_prime_divisor = 2
else:
potential_prime_divisor += 1
if potential_prime_divisor == potential_prime:
print(potential_prime_divisor)
potential_prime_divisor = 2
potential_prime_squared += 1
So the reason why printing q
(potential_prime_squared
) will give a wrong answer is that you are comparing it to the square root of it meaning that the value it holds will be higher and might not even be a prime number.
This also highlights why giving your variables good names is very important as it will be way easier to think through your code, improve it, and understand it again.
Proof of concept:
prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 364565471, 364565503] # I've tested this with a list of 1000 prime_numbers with the same result, so I shortened this list
for prime_number in prime_numbers:
prime_squared = prime_number**2
divisible = False
for i in range(2, prime_number):
if prime_squared % i == 0:
divisible = True
break
print(divisible)
Even more fun fact: a square of a prime can never be a prime as it will always have 3 divisors: 1, the prime it’s the square from, and itself. Other than that it would appear that they are also not divisible by any other values, so tho they are no primes they certainly are kind of special too.