Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Last active January 10, 2020 18:25
Show Gist options
  • Save anmolj7/e931ac1ebbc99690df07e486dbbb428f to your computer and use it in GitHub Desktop.
Save anmolj7/e931ac1ebbc99690df07e486dbbb428f to your computer and use it in GitHub Desktop.
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