James B
James B

Reputation: 1

Python to list all primes 50 - 1000 , then squaring and checking if prime

I would like to write a program to list all prime numbers 50 - 1000 and then square each of the identified primes and perform a check if any of the numbers are prime or composite.

I have the below code working to list all primes, I am not entirely sure where to start to perform the second check to square the prime numbers and check if prime or composite. Any pointers in the right direction would be much appreciated.

lower = 50
upper = 1000

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):
   # all prime numbers are greater than 1
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           print(num)

Upvotes: 0

Views: 352

Answers (1)

Aleksander Ikleiw
Aleksander Ikleiw

Reputation: 2685

By definition, a square can not be prime. However, if you want to test it by your own, I have rewritten your by creating a function called check_prime() which checks whether the number is prime.

def check_prime(number):
    temp = []
    for i in range(1, number):
        if len(temp) > 1:
            return False
        if number % i == 0:
            temp.append(i)
    return True
prime = []
for i in range(50, 101):
    if check_prime(i):
        prime.append(i)
prime_2 = []
for i in prime:
    if check_prime(i ** 2):
        prime_2.append(i ** 2)
print(prime_2)

Upvotes: 1

Related Questions