From 51a7bc8f9228fb1bea79044f0783c45e73620b83 Mon Sep 17 00:00:00 2001 From: Warlock001 Date: Sun, 13 Oct 2019 20:53:46 +0530 Subject: [PATCH 1/6] Implementation of Hardy Ramanujan Algorithm --- maths/hardy_ramanujanalgo.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 maths/hardy_ramanujanalgo.py diff --git a/maths/hardy_ramanujanalgo.py b/maths/hardy_ramanujanalgo.py new file mode 100644 index 000000000000..be550bb34a0b --- /dev/null +++ b/maths/hardy_ramanujanalgo.py @@ -0,0 +1,28 @@ +import math + +def exactPrimeFactorCount(n) : + count = 0 + if (n % 2 == 0) : + count = count + 1 + while (n % 2 == 0) : + n = int(n / 2) + + i = 3 + + while (i <= int(math.sqrt(n))) : + if (n % i == 0) : + count = count + 1 + while (n % i == 0) : + n = int(n / i) + i = i + 2 + + if (n > 2) : + count = count + 1 + return count + +n = 51242183 +print ("The number of distinct prime factors is/are {}". + format(exactPrimeFactorCount(n), end = "\n")) +print ("The value of log(log(n)) is {0:.4f}" + .format(math.log(math.log(n)))) + From 8b5078284fc38648e1e546100c17a17926fda09a Mon Sep 17 00:00:00 2001 From: Warlock001 Date: Fri, 18 Oct 2019 19:52:23 +0530 Subject: [PATCH 2/6] added docstrings --- maths/hardy_ramanujanalgo.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/maths/hardy_ramanujanalgo.py b/maths/hardy_ramanujanalgo.py index be550bb34a0b..4ea2d6273011 100644 --- a/maths/hardy_ramanujanalgo.py +++ b/maths/hardy_ramanujanalgo.py @@ -1,11 +1,19 @@ + +#It's a theorem that states the number of prime factors +# of n will approximately be log(log(n)) for most +#natural numbers n + import math + def exactPrimeFactorCount(n) : count = 0 if (n % 2 == 0) : count = count + 1 while (n % 2 == 0) : n = int(n / 2) + #the n input value must be odd so that + #we can skip one element ( ie i = i + 2) i = 3 @@ -16,11 +24,15 @@ def exactPrimeFactorCount(n) : n = int(n / i) i = i + 2 + #this condition checks the prime + #number n is greater than 2 + if (n > 2) : count = count + 1 return count n = 51242183 +#explicitly define value of n here print ("The number of distinct prime factors is/are {}". format(exactPrimeFactorCount(n), end = "\n")) print ("The value of log(log(n)) is {0:.4f}" From 138bc151d4ca949538da0b592c1e8c6b4fe126c5 Mon Sep 17 00:00:00 2001 From: Warlock001 Date: Fri, 18 Oct 2019 19:56:11 +0530 Subject: [PATCH 3/6] added doctests --- maths/hardy_ramanujanalgo.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/maths/hardy_ramanujanalgo.py b/maths/hardy_ramanujanalgo.py index 4ea2d6273011..073b875f7264 100644 --- a/maths/hardy_ramanujanalgo.py +++ b/maths/hardy_ramanujanalgo.py @@ -38,3 +38,9 @@ def exactPrimeFactorCount(n) : print ("The value of log(log(n)) is {0:.4f}" .format(math.log(math.log(n)))) +""" +n = 51242183 +The number of distinct prime factors is/are 3 +The value of log(log(n)) is 2.8765 +""" + From b528a5f0a70d614d4965558a15d9f1054a70f965 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 19 Oct 2019 05:57:57 +0200 Subject: [PATCH 4/6] Run Python black on the code --- maths/hardy_ramanujanalgo.py | 91 ++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/maths/hardy_ramanujanalgo.py b/maths/hardy_ramanujanalgo.py index 073b875f7264..bb31a1be49fb 100644 --- a/maths/hardy_ramanujanalgo.py +++ b/maths/hardy_ramanujanalgo.py @@ -1,46 +1,45 @@ - -#It's a theorem that states the number of prime factors -# of n will approximately be log(log(n)) for most -#natural numbers n - -import math - - -def exactPrimeFactorCount(n) : - count = 0 - if (n % 2 == 0) : - count = count + 1 - while (n % 2 == 0) : - n = int(n / 2) - #the n input value must be odd so that - #we can skip one element ( ie i = i + 2) - - i = 3 - - while (i <= int(math.sqrt(n))) : - if (n % i == 0) : - count = count + 1 - while (n % i == 0) : - n = int(n / i) - i = i + 2 - - #this condition checks the prime - #number n is greater than 2 - - if (n > 2) : - count = count + 1 - return count - -n = 51242183 -#explicitly define value of n here -print ("The number of distinct prime factors is/are {}". - format(exactPrimeFactorCount(n), end = "\n")) -print ("The value of log(log(n)) is {0:.4f}" - .format(math.log(math.log(n)))) - -""" -n = 51242183 -The number of distinct prime factors is/are 3 -The value of log(log(n)) is 2.8765 -""" - +# This theorem states that the number of prime factors of n +# will be approximately log(log(n)) for most natural numbers n + +import math + + +def exactPrimeFactorCount(n): + """ + >>> exactPrimeFactorCount(51242183) + 3 + """ + count = 0 + if n % 2 == 0: + count += 1 + while n % 2 == 0: + n = int(n / 2) + # the n input value must be odd so that + # we can skip one element (ie i += 2) + + i = 3 + + while i <= int(math.sqrt(n)): + if n % i == 0: + count += 1 + while n % i == 0: + n = int(n / i) + i = i + 2 + + # this condition checks the prime + # number n is greater than 2 + + if n > 2: + count += 1 + return count + + +if __name__ == "__main__": + n = 51242183 + print(f"The number of distinct prime factors is/are {exactPrimeFactorCount(n)}") + print("The value of log(log(n)) is {0:.4f}".format(math.log(math.log(n)))) + + """ + The number of distinct prime factors is/are 3 + The value of log(log(n)) is 2.8765 + """ From cbefb89e170df9aa9509d565746e7f3321abcc76 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 19 Oct 2019 05:59:00 +0200 Subject: [PATCH 5/6] Travis CI: Upgrade to Python 3.8 --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index be227df1fdbd..cd1b9eb16599 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: python -dist: xenial # required for Python >= 3.7 -python: 3.7 +python: 3.8 cache: pip before_install: pip install --upgrade pip setuptools install: pip install -r requirements.txt From 759ff2267b92651c3301168f761631ee77935bfa Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 19 Oct 2019 06:01:48 +0200 Subject: [PATCH 6/6] Revert to Python 3.7 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cd1b9eb16599..877dbee9ade2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: python -python: 3.8 +python: 3.7 cache: pip before_install: pip install --upgrade pip setuptools install: pip install -r requirements.txt