forked from course-work/week1
40 lines
860 B
Python
40 lines
860 B
Python
import fractions
|
|
|
|
def compute_divisors(num):
|
|
divisors = [1]
|
|
for i in range( 2, num//2+1 ): # if num is even, one needs to be added
|
|
if num % i == 0:
|
|
divisors.append(i)
|
|
|
|
divisors.append( num )
|
|
|
|
return divisors
|
|
|
|
def sum_of_divisors(num):
|
|
sumOf = 0
|
|
for i in compute_divisors( num ):
|
|
sumOf += i
|
|
|
|
return sumOf
|
|
|
|
def divisor_count(num):
|
|
return len( compute_divisors(num) )
|
|
|
|
def get_totatives(num):
|
|
totatives = []
|
|
for i in range(0, num):
|
|
gcd = fractions.gcd( i, num )
|
|
if gcd == 1:
|
|
totatives.append( i )
|
|
|
|
return totatives
|
|
|
|
def totient(num):
|
|
return len( get_totatives(num) )
|
|
|
|
|
|
assert( compute_divisors(60) == [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60] )
|
|
assert( sum_of_divisors(60) == 168 )
|
|
assert( divisor_count(60) == 12 )
|
|
assert( get_totatives(30) == [1, 7, 11, 13, 17, 19, 23, 29] )
|
|
assert( totient(30) == 8 ) |