Skip to content

Update basic_maths.py #1517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions maths/basic_maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,81 @@
import math


def prime_factors(n):
"""Find Prime Factors."""
def prime_factors(n: int) -> list:
"""Find Prime Factors.
>>> prime_factors(100)
[2, 2, 5, 5]
"""
pf = []
while n % 2 == 0:
pf.append(2)
n = int(n / 2)

for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
pf.append(i)
n = int(n / i)

if n > 2:
pf.append(n)

return pf


def number_of_divisors(n):
"""Calculate Number of Divisors of an Integer."""
def number_of_divisors(n: int) -> int:
"""Calculate Number of Divisors of an Integer.
>>> number_of_divisors(100)
9
"""
div = 1

temp = 1
while n % 2 == 0:
temp += 1
n = int(n / 2)
div = div * (temp)

div *= temp
for i in range(3, int(math.sqrt(n)) + 1, 2):
temp = 1
while n % i == 0:
temp += 1
n = int(n / i)
div = div * (temp)

div *= temp
return div


def sum_of_divisors(n):
"""Calculate Sum of Divisors."""
def sum_of_divisors(n: int) -> int:
"""Calculate Sum of Divisors.
>>> sum_of_divisors(100)
217
"""
s = 1

temp = 1
while n % 2 == 0:
temp += 1
n = int(n / 2)
if temp > 1:
s *= (2 ** temp - 1) / (2 - 1)

for i in range(3, int(math.sqrt(n)) + 1, 2):
temp = 1
while n % i == 0:
temp += 1
n = int(n / i)
if temp > 1:
s *= (i ** temp - 1) / (i - 1)

return s
return int(s)


def euler_phi(n):
"""Calculte Euler's Phi Function."""
def euler_phi(n: int) -> int:
"""Calculte Euler's Phi Function.
>>> euler_phi(100)
40
"""
l = prime_factors(n)
l = set(l)
s = n
for x in l:
s *= (x - 1) / x
return s
return int(s)


def main():
"""Print the Results of Basic Math Operations."""
if __name__ == "__main__":
print(prime_factors(100))
print(number_of_divisors(100))
print(sum_of_divisors(100))
print(euler_phi(100))


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion neural_network/perceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def sort(self, sample) -> None:
>>> data = [[2.0149, 0.6192, 10.9263]]
>>> targets = [-1]
>>> perceptron = Perceptron(data,targets)
>>> perceptron.training() # doctest:+ELLIPSIS
>>> perceptron.training() # doctest: +ELLIPSIS
('\\nEpoch:\\n', ...)
...
>>> perceptron.sort([-0.6508, 0.1097, 4.0009]) # doctest: +ELLIPSIS
Expand Down