Skip to content

Commit 5c351d8

Browse files
alfinwilliamcclauss
authored andcommitted
Implementation of Hardy Ramanujan Algorithm in /maths (TheAlgorithms#1355)
* Implementation of Hardy Ramanujan Algorithm * added docstrings * added doctests * Run Python black on the code * Travis CI: Upgrade to Python 3.8 * Revert to Python 3.7
1 parent acd962b commit 5c351d8

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: python
2-
dist: xenial # required for Python >= 3.7
32
python: 3.7
43
cache: pip
54
before_install: pip install --upgrade pip setuptools

maths/hardy_ramanujanalgo.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This theorem states that the number of prime factors of n
2+
# will be approximately log(log(n)) for most natural numbers n
3+
4+
import math
5+
6+
7+
def exactPrimeFactorCount(n):
8+
"""
9+
>>> exactPrimeFactorCount(51242183)
10+
3
11+
"""
12+
count = 0
13+
if n % 2 == 0:
14+
count += 1
15+
while n % 2 == 0:
16+
n = int(n / 2)
17+
# the n input value must be odd so that
18+
# we can skip one element (ie i += 2)
19+
20+
i = 3
21+
22+
while i <= int(math.sqrt(n)):
23+
if n % i == 0:
24+
count += 1
25+
while n % i == 0:
26+
n = int(n / i)
27+
i = i + 2
28+
29+
# this condition checks the prime
30+
# number n is greater than 2
31+
32+
if n > 2:
33+
count += 1
34+
return count
35+
36+
37+
if __name__ == "__main__":
38+
n = 51242183
39+
print(f"The number of distinct prime factors is/are {exactPrimeFactorCount(n)}")
40+
print("The value of log(log(n)) is {0:.4f}".format(math.log(math.log(n))))
41+
42+
"""
43+
The number of distinct prime factors is/are 3
44+
The value of log(log(n)) is 2.8765
45+
"""

0 commit comments

Comments
 (0)