Last active
January 10, 2020 18:25
-
-
Save anmolj7/e931ac1ebbc99690df07e486dbbb428f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
def return_factors(n): | |
p = 2 | |
Factors = {} | |
while p * p <= n: | |
count = 0 | |
while n % p == 0: | |
n //= p | |
count += 1 | |
if count > 0: | |
Factors[p] = count | |
p += 1 | |
if n != 1: | |
Factors[n] = 1 | |
return Factors | |
def Total_factors(n): | |
""" | |
Given that the prime factors of a given number are | |
2^p, 3^q, ... prime^n | |
The number of divisors = (p+1)*(q+1)* .... (n+1) | |
""" | |
if n%2: #If the number isn't divisible by 2, i.e odd. | |
return 0 | |
factors = return_factors(n) | |
temp = [factors[factor]+1 for factor in factors] #Adding 1 to the count of every factor | |
total_factors = reduce(lambda x, y: x*y, temp) | |
return total_factors | |
def main(): | |
num = int(input("Enter a number: ")) | |
assert num > 0, "A number bigger than zero." | |
print(f"The number of even factors: {Total_factors(num)}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment