Hi,
I have written a program to display all twin primes less than 1,000. Could someone please read my code and give me ways to improve, for example: the spacing between code blocks, my choice of variable names, insert comments anywhere??, structure of the program, etc. Please don’t hold anything back. Anything that bothers you, please tell me. I’d really appreciate the advice and improvements. Thanks!
So, my program starts with prime1 which I assign 2 to. Then it enters a while loop and calls the function find_the_next_prime to find the next prime. So it starts with prime1 + 1 to check if it’s prime and keeps adding 1’s until it’s prime and returns that number. Then the rest I think is clear.
def is_twin_prime(prime1, prime2):
return abs(prime1 - prime2) == 2
def find_next_prime(number):
while True:
is_prime = True
for divisor in range(2, (number - 1) + 1):
if number % divisor == 0:
number += 1
is_prime = False
break
if is_prime:
return number
def main():
prime_1 = 2
while prime_1 < 1000:
prime_2 = find_next_prime(prime_1 + 1)
if is_twin_prime(prime_1, prime_2):
print("({0:d},{1:d})".format(prime_1, prime_2))
prime_1 = prime_2
main()