From 788f8f2d58eb352c8378ec572c90f212889be236 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:16:44 +0000 Subject: [PATCH 1/3] updating DIRECTORY.md --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1d9e6eff75c6..64e9d5333a2f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -9,6 +9,7 @@ * [Newton Forward Interpolation](arithmetic_analysis/newton_forward_interpolation.py) * [Newton Method](arithmetic_analysis/newton_method.py) * [Newton Raphson](arithmetic_analysis/newton_raphson.py) + * [Newton Raphson New](arithmetic_analysis/newton_raphson_new.py) * [Secant Method](arithmetic_analysis/secant_method.py) ## Audio Filters @@ -107,6 +108,7 @@ * [Lempel Ziv](compression/lempel_ziv.py) * [Lempel Ziv Decompress](compression/lempel_ziv_decompress.py) * [Peak Signal To Noise Ratio](compression/peak_signal_to_noise_ratio.py) + * [Run Length Encoding](compression/run_length_encoding.py) ## Computer Vision * [Cnn Classification](computer_vision/cnn_classification.py) @@ -621,6 +623,7 @@ * [Linear Congruential Generator](other/linear_congruential_generator.py) * [Lru Cache](other/lru_cache.py) * [Magicdiamondpattern](other/magicdiamondpattern.py) + * [Maximum Subarray](other/maximum_subarray.py) * [Nested Brackets](other/nested_brackets.py) * [Password Generator](other/password_generator.py) * [Scoring Algorithm](other/scoring_algorithm.py) @@ -1053,6 +1056,7 @@ * [Fetch Bbc News](web_programming/fetch_bbc_news.py) * [Fetch Github Info](web_programming/fetch_github_info.py) * [Fetch Jobs](web_programming/fetch_jobs.py) + * [Fetch Quotes](web_programming/fetch_quotes.py) * [Fetch Well Rx Price](web_programming/fetch_well_rx_price.py) * [Get Imdb Top 250 Movies Csv](web_programming/get_imdb_top_250_movies_csv.py) * [Get Imdbtop](web_programming/get_imdbtop.py) From 65d64d1c71752c9d4556b6f714a8886f99bb1f9e Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Sun, 9 Oct 2022 01:50:16 +0530 Subject: [PATCH 2/3] added chakravala method --- maths/chakravala.py | 120 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 maths/chakravala.py diff --git a/maths/chakravala.py b/maths/chakravala.py new file mode 100644 index 000000000000..2c51ce385d5d --- /dev/null +++ b/maths/chakravala.py @@ -0,0 +1,120 @@ +""" +Implementing Chakravala method using python + +https://en.wikipedia.org/wiki/Chakravala_method +https://kappadath-gopal.blogspot.com/2013/04/ancient-medieval-indian-mathematics.html + +The chakravala method is a cyclic algorithm to solve indeterminate quadratic equations, +including Pell's equation. + +""" + +import math + + +def is_perfect_square(num: int) -> bool: + """ + Check if a number is perfect square number or not + :param num: the number to be checked + :return: True if number is square number, otherwise False + + >>> is_perfect_square(9) + True + >>> is_perfect_square(16) + True + >>> is_perfect_square(1) + True + >>> is_perfect_square(0) + True + >>> is_perfect_square(10) + False + """ + + sr = int(math.sqrt(num)) + return sr * sr == num + + +def chakravala_method(num: int) -> (tuple[int, int] | tuple): + """ + This method takes in the value of N in the equation + + x^2 = N*y^2 + 1 + :param num: the number N equals to + :return: empty tuple if N is perfect square else tuple(x,y) + + >>> chakravala_method(1) + () + >>> chakravala_method(2) + (3, 2) + >>> chakravala_method(4) + () + >>> chakravala_method(5) + (9, 4) + >>> chakravala_method(7) + (8, 3) + + """ + + if is_perfect_square(num): + return () + + # Takes b = 1 and finds a and k accordingly, refer to algorithm link + # variable naming is used as same as algorithm, (a,b,k,m) except N = num + + b = 1 + + min_diff = num + a = 0 + while True: + diff = abs(num - (a + 1) ** 2) + if min_diff > diff: + min_diff = diff + a += 1 + continue + break + + k = a**2 - num + + while True: + + kabs = abs(k) + + if k == 1: + return (a, b) + + if k == -1 or kabs == 2 or (kabs == 4 and (a % 2 == 0 or b % 2 == 0)): + return (abs((a**2 + num * b**2) // k), abs(2 * a * b // k)) + + min_diff = num + n = 1 # loop variable + n2 = n # stores the correct value of n + while True: + if kabs * n <= a: + n += 1 + continue + if (kabs * n - a) % b == 0: + m = (kabs * n - a) // b + else: + n += 1 + continue + + diff = abs(m**2 - num) + if min_diff > diff: + min_diff = diff + n2 = n + n += 1 + continue + break + m = (kabs * n2 - a) // b + + a, b = abs((a * m + num * b) // k), abs((a + b * m) // k) + k = (m**2 - num) // k + + +if __name__ == "__main__": + + import doctest + + doctest.testmod() + + print("X and Y for the equation X^2 - 13Y^2 = 1 is: ", chakravala_method(13)) From a00ee876e53f9e750912bff6c2628f19ce896716 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:20:59 +0000 Subject: [PATCH 3/3] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 64e9d5333a2f..7ffb222d5246 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -476,6 +476,7 @@ * [Binomial Distribution](maths/binomial_distribution.py) * [Bisection](maths/bisection.py) * [Ceil](maths/ceil.py) + * [Chakravala](maths/chakravala.py) * [Check Polygon](maths/check_polygon.py) * [Chudnovsky Algorithm](maths/chudnovsky_algorithm.py) * [Collatz Sequence](maths/collatz_sequence.py)