algorithm-in-python/math/numberTheory/euler.py

37 lines
974 B
Python
Raw Normal View History

#coding: utf-8
''' mbinary
#######################################################################
# File : euler.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
2019-01-31 12:09:46 +08:00
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-12-16 10:53
# Description:
euler function: phi(n)
perfect num: \sigma (n) = 2n, \sigma (n) is the sum of all factors of n
eg \sigma (9) = 3+3+9 = 15
#######################################################################
'''
from factor import factor
from collections import Counter
from functools import reduce
from operator import mul
2020-04-15 12:28:20 +08:00
def phi(n):
2020-04-15 12:28:20 +08:00
st = set(factor(n))
return round(reduce(mul, (1-1/p for p in st), n))
def sigma(n):
ct = Counter(factor(n))
2020-04-15 12:28:20 +08:00
return reduce(mul, (round((p**(ct[p]+1)-1)/(p-1)) for p in ct), 1)
2020-04-15 12:28:20 +08:00
if __name__ == '__main__':
while 1:
n = int(input('n: '))
2020-04-15 12:28:20 +08:00
print('phi(n):', phi(n))
print('sigma(n):', sigma(n))