46 lines
1.9 KiB
Python
46 lines
1.9 KiB
Python
|
from util import read_file, write_file, append_file
|
||
|
from sys import maxsize
|
||
|
|
||
|
class primenumbers:
|
||
|
def __init__(self, cache: str = "./cache/primenumbers/") -> None:
|
||
|
self.p_last_index : int
|
||
|
self.p_current_index : int
|
||
|
self.p_current_number: int
|
||
|
|
||
|
self.p_cache = cache
|
||
|
|
||
|
self.p_last_index = int(read_file(self.p_cache+"last_index")[0])
|
||
|
self.p_current_index = self.p_last_index + 1
|
||
|
self.p_current_number = int(read_file(self.p_cache+str(self.p_last_index))[0])
|
||
|
if self.p_current_number == maxsize:
|
||
|
print("Max int size reached!")
|
||
|
exit()
|
||
|
self.p_current_number += 1
|
||
|
|
||
|
print(f"Last index : {self.p_last_index}")
|
||
|
print(f"Current index : {self.p_current_index}")
|
||
|
print(f"Current number : {self.p_current_number}")
|
||
|
|
||
|
def generate(self, iterations: int = -1):
|
||
|
print(f"Generating {iterations} primenumbers:")
|
||
|
iteration: int = 0
|
||
|
while(iterations == -1 or iteration < iterations):
|
||
|
is_primenumber = True
|
||
|
for i in range(1, self.p_current_index):
|
||
|
if self.check_modulo(self.p_current_number, int(read_file(self.p_cache+str(i))[0])):
|
||
|
is_primenumber = False
|
||
|
break
|
||
|
if is_primenumber:
|
||
|
print(f"\nPrimenumber found: {self.p_current_number}\n")
|
||
|
write_file(self.p_cache+str(self.p_current_index), str(self.p_current_number))
|
||
|
self.p_last_index = self.p_current_index
|
||
|
write_file(self.p_cache+"last_index", str(self.p_last_index))
|
||
|
self.p_current_index += 1
|
||
|
if self.p_current_number == maxsize:
|
||
|
print("Max int size reached!")
|
||
|
break
|
||
|
self.p_current_number += 1
|
||
|
iteration += 1
|
||
|
|
||
|
def check_modulo(self, a: int, b: int) -> bool:
|
||
|
return a % b == 0
|