[First Commit]

Primenumber generator
This commit is contained in:
xoy 2023-10-16 19:06:54 +02:00
commit ac0f5a1011
7 changed files with 69 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

1
cache/primenumbers/0 vendored Normal file
View File

@ -0,0 +1 @@
1

1
cache/primenumbers/1 vendored Normal file
View File

@ -0,0 +1 @@
2

1
cache/primenumbers/last_index vendored Normal file
View File

@ -0,0 +1 @@
1

6
main.py Normal file
View File

@ -0,0 +1,6 @@
from primenumbers import primenumbers as pn
if __name__ == "__main__":
P = pn()
P.generate(10000)

46
primenumbers.py Normal file
View File

@ -0,0 +1,46 @@
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

13
util.py Normal file
View File

@ -0,0 +1,13 @@
def read_file(filepath: str) -> list[str]:
file = open(filepath, "r")
return file.readlines()
def write_file(filepath: str, insert_string: str) -> bool:
file = open(filepath, "w")
file.write(insert_string)
file.close()
def append_file(filepath: str, append_string: str) -> bool:
file = open(filepath, "a")
file.write(append_string)
file.close()