2018-12-16 17:09:54 +08:00
|
|
|
#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
|
2018-12-16 17:09:54 +08:00
|
|
|
# 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
|
|
|
|
|
|
|
|
2018-12-16 17:09:54 +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))
|
|
|
|
|
2018-12-16 17:09:54 +08:00
|
|
|
|
|
|
|
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)
|
2018-12-16 17:09:54 +08:00
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-12-16 17:09:54 +08:00
|
|
|
while 1:
|
|
|
|
n = int(input('n: '))
|
2020-04-15 12:28:20 +08:00
|
|
|
print('phi(n):', phi(n))
|
|
|
|
print('sigma(n):', sigma(n))
|