From ac0f5a1011276f5209b38e3ac182985cf5997f6a Mon Sep 17 00:00:00 2001 From: xoy Date: Mon, 16 Oct 2023 19:06:54 +0200 Subject: [PATCH] [First Commit] Primenumber generator --- .gitignore | 1 + cache/primenumbers/0 | 1 + cache/primenumbers/1 | 1 + cache/primenumbers/last_index | 1 + main.py | 6 +++++ primenumbers.py | 46 +++++++++++++++++++++++++++++++++++ util.py | 13 ++++++++++ 7 files changed, 69 insertions(+) create mode 100644 .gitignore create mode 100644 cache/primenumbers/0 create mode 100644 cache/primenumbers/1 create mode 100644 cache/primenumbers/last_index create mode 100644 main.py create mode 100644 primenumbers.py create mode 100644 util.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/cache/primenumbers/0 b/cache/primenumbers/0 new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/cache/primenumbers/0 @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/cache/primenumbers/1 b/cache/primenumbers/1 new file mode 100644 index 0000000..d8263ee --- /dev/null +++ b/cache/primenumbers/1 @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/cache/primenumbers/last_index b/cache/primenumbers/last_index new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/cache/primenumbers/last_index @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..aa91d51 --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +from primenumbers import primenumbers as pn + + +if __name__ == "__main__": + P = pn() + P.generate(10000) \ No newline at end of file diff --git a/primenumbers.py b/primenumbers.py new file mode 100644 index 0000000..558e099 --- /dev/null +++ b/primenumbers.py @@ -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 \ No newline at end of file diff --git a/util.py b/util.py new file mode 100644 index 0000000..ac36626 --- /dev/null +++ b/util.py @@ -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()