Factors of a number (#1493)

* Factors of a number

* Update factors.py

* Fix mypy issue in basic_maths.py

* Fix mypy error in perceptron.py

* def primes(max: int) -> List[int]:

* Update binomial_heap.py

* Add a space

* Remove a space

* Add a space
This commit is contained in:
himanshujain171 2019-10-30 04:24:31 +05:30 committed by Christian Clauss
parent f8e97aa597
commit 53ff735701
5 changed files with 105 additions and 95 deletions

View File

@ -1,6 +1,5 @@
""" """
Binomial Heap Binomial Heap
Reference: Advanced Data Structures, Peter Brass Reference: Advanced Data Structures, Peter Brass
""" """
@ -47,10 +46,9 @@ class Node:
class BinomialHeap: class BinomialHeap:
""" r"""
Min-oriented priority queue implemented with the Binomial Heap data Min-oriented priority queue implemented with the Binomial Heap data
structure implemented with the BinomialHeap class. It supports: structure implemented with the BinomialHeap class. It supports:
- Insert element in a heap with n elemnts: Guaranteed logn, amoratized 1 - Insert element in a heap with n elemnts: Guaranteed logn, amoratized 1
- Merge (meld) heaps of size m and n: O(logn + logm) - Merge (meld) heaps of size m and n: O(logn + logm)
- Delete Min: O(logn) - Delete Min: O(logn)
@ -58,13 +56,11 @@ class BinomialHeap:
Example: Example:
Create a random permutation of 30 integers to be inserted and Create a random permutation of 30 integers to be inserted and 19 of them deleted
19 of them deleted
>>> import numpy as np >>> import numpy as np
>>> permutation = np.random.permutation(list(range(30))) >>> permutation = np.random.permutation(list(range(30)))
Create a Heap and insert the 30 integers Create a Heap and insert the 30 integers
__init__() test __init__() test
>>> first_heap = BinomialHeap() >>> first_heap = BinomialHeap()
@ -123,7 +119,6 @@ class BinomialHeap:
>>> while not first_heap.isEmpty(): >>> while not first_heap.isEmpty():
... print(first_heap.deleteMin(), end=" ") ... print(first_heap.deleteMin(), end=" ")
17 20 25 26 27 28 29 31 34 17 20 25 26 27 28 29 31 34
""" """
def __init__(self, bottom_root=None, min_node=None, heap_size=0): def __init__(self, bottom_root=None, min_node=None, heap_size=0):

View File

@ -67,10 +67,8 @@ def euler_phi(n: int) -> int:
>>> euler_phi(100) >>> euler_phi(100)
40 40
""" """
l = prime_factors(n)
l = set(l)
s = n s = n
for x in l: for x in set(prime_factors(n)):
s *= (x - 1) / x s *= (x - 1) / x
return int(s) return int(s)

18
maths/factors.py Normal file
View File

@ -0,0 +1,18 @@
def factors_of_a_number(num: int) -> list:
"""
>>> factors_of_a_number(1)
[1]
>>> factors_of_a_number(5)
[1, 5]
>>> factors_of_a_number(24)
[1, 2, 3, 4, 6, 8, 12, 24]
>>> factors_of_a_number(-24)
[]
"""
return [i for i in range(1, num + 1) if num % i == 0]
if __name__ == "__main__":
num = int(input("Enter a number to find its factors: "))
factors = factors_of_a_number(num)
print(f"{num} has {len(factors)} factors: {', '.join(str(f) for f in factors)}")

View File

@ -1,9 +1,9 @@
"""Prime numbers calculation.""" from typing import List
def primes(max: int) -> int: def primes(max: int) -> List[int]:
""" """
Return a list of all primes up to max. Return a list of all primes numbers up to max.
>>> primes(10) >>> primes(10)
[2, 3, 5, 7] [2, 3, 5, 7]
>>> primes(11) >>> primes(11)

View File

@ -95,8 +95,7 @@ class Perceptron:
... ...
>>> perceptron.sort([-0.6508, 0.1097, 4.0009]) # doctest: +ELLIPSIS >>> perceptron.sort([-0.6508, 0.1097, 4.0009]) # doctest: +ELLIPSIS
('Sample: ', ...) ('Sample: ', ...)
classification: P1 classification: P...
""" """
if len(self.sample) == 0: if len(self.sample) == 0:
raise AttributeError("Sample data can not be empty") raise AttributeError("Sample data can not be empty")