I am writing a python program to find and return a list of Twin primes between two numbers.
This is my code:
#prime selector
def is_prime(num):
if num < 2 or num % 2 == 0:
return False
if num == 2:
return True
upr_lmt = int((num ** 0.5) + 1)
for number in range(3, upr_lmt, 2):
if num % number == 0:
return False
return True
def primeLister(end_point, st_point = 2):
primeList = []
for i in range(st_point, end_point + 1):
if is_prime(i):
primeList.append(i)
return primeList
It is saved as prime_selector.py in a folder.
This is the testcase that I have written:
# Test case
from prime_selector import primeLister
def log_newline(file_writer):
file_writer.write("\n\n")
def log_testcase(case_num, case_desc, result, file_writer):
file_writer.write(f"================= Test Case {case_num + 1} =================\n")
print((f"================= Test Case {case_num + 1} ================="))
file_writer.write(case_desc)
print(case_desc, end = "")
file_writer.write(result)
print(result)
num_list = [99991,999999937]
with open("./primeLister_testcase","w") as file_writer:
for case_num, i in enumerate(num_list):
case_desc = f"Listing all prime numbers till {i}:\n"
result = "".join([f" > {prime}\n" for prime in primeLister(i)])
log_testcase(case_num, case_desc, result, file_writer)
if case_num + 1 != len(num_list):
log_newline(file_writer)
The testcase is also saved in the same folder locally.
The program stops abruptly during the execution of primeLister testcase after writing the primes up to 99991 in the file.
I guess that it stops during the process of listing primes between 99991 and 999999937. I've got no idea why. My computer has an Intel i5-12450H processor with 16GB RAM. If that's relevant.
I have already tried implementing an infinite loop, threading and also using exception handling. Nothing has worked so far. What am I doing wrong?
I was expecting the program to keep executing until it found all the primes within the provided range.
while Trueloop to repeat everything from scratch again? Why creating a thread? How does that help to test your code? Please remove everything that is not contributing to showcase the problem. Only run with one input, and only once, and only testing what fails.99991apparently runs fine, so you can leave it out. Only one failing case is needed. The File I/O seems unrelated. Can you confirm whether you get the problem also when you remove all file I/O? Making one long string as output: is that related to your problem? Does the problem also occur when you don't create that string? If not, please remove that. This way continue to reduce the code to only get the minimal code that still produces the issue.