mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
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:
committed by
Christian Clauss
parent
f8e97aa597
commit
53ff735701
@@ -67,10 +67,8 @@ def euler_phi(n: int) -> int:
|
||||
>>> euler_phi(100)
|
||||
40
|
||||
"""
|
||||
l = prime_factors(n)
|
||||
l = set(l)
|
||||
s = n
|
||||
for x in l:
|
||||
for x in set(prime_factors(n)):
|
||||
s *= (x - 1) / x
|
||||
return int(s)
|
||||
|
||||
|
||||
18
maths/factors.py
Normal file
18
maths/factors.py
Normal 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)}")
|
||||
@@ -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)
|
||||
[2, 3, 5, 7]
|
||||
>>> primes(11)
|
||||
|
||||
Reference in New Issue
Block a user