diff options
| -rw-r--r-- | challenge-155/lubos-kolouch/python/ch-1.py | 43 | ||||
| -rw-r--r-- | challenge-155/lubos-kolouch/python/ch-2.py | 46 |
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-155/lubos-kolouch/python/ch-1.py b/challenge-155/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..83820190de --- /dev/null +++ b/challenge-155/lubos-kolouch/python/ch-1.py @@ -0,0 +1,43 @@ +""" Challenge 155 LK Python Task 1""" +from sympy import isprime, nextprime + + +def get_pn(what: int) -> int: + """Get the pn needed for the formula""" + last_num = 1 + pn = 1 + + for _ in range(1, what + 1): + last_num = nextprime(last_num) + + pn *= last_num + return pn + + +def find_fortunate_num(what: int) -> int: + """Find the respective fortunate number""" + + num = 2 + while not isprime(what + num): + num += 1 + + return num + + +def get_n_fortunate_nums(what: int) -> list: + """Find the task solution""" + + fortunate_nums = [] + + # * 2 number should be more than enough + for i in range(1, what * 2): + pn = get_pn(i) + + fortunate = find_fortunate_num(pn) + fortunate_nums.append(fortunate) + + fortunate_nums = list(sorted(set(fortunate_nums))) + return fortunate_nums[0 : what + 1] + + +assert get_n_fortunate_nums(7) == [3, 5, 7, 13, 17, 19, 23, 37] diff --git a/challenge-155/lubos-kolouch/python/ch-2.py b/challenge-155/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..a681ea5e70 --- /dev/null +++ b/challenge-155/lubos-kolouch/python/ch-2.py @@ -0,0 +1,46 @@ +""" Challenge 155 Task 2 """ + +import re + + +class PisanoPeriod: + """Class for the challenge""" + + def __init__(self) -> None: + self.fibs: list[int] = [] + self.fib_hash: dict[int, int] = {} + + self.fibs.append(0) + self.fibs.append(1) + self.fib_hash[0] = 1 + self.fib_hash[1] = 1 + + def gen_more_fibs(self) -> None: + """Generate Fibonacci numbers of the fly""" + self.fibs.append(self.fibs[-1] + self.fibs[-2]) + + def get_pisano_period(self, what) -> int: + """Find out the period for the given what""" + + all_nums = str(self.fibs[0]) + str(self.fibs[1]) + + period_len = 2 + while 1: + period_len += 1 + self.gen_more_fibs() + all_nums += str(self.fibs[-1] % what) + + if re.search(r"^(.*)\1$", all_nums): + break + return period_len // 2 + + +def main() -> None: + """The main method""" + + pisano_period = PisanoPeriod() + assert pisano_period.get_pisano_period(3) == 8 + + +if __name__ == "__main__": + main() |
