5 functions - divisor, sum of divisors, num. devisors, totatives, totient

By brbcoding on Jun 13, 2012

Python functions that will find the divisor, number of divisors, sum of divisors, totatives, and totient... I'm new to python, this snippet is my reponse to the problem posted HERE: http://www.reddit.com/r/dailyprogrammer/comments/uzx8b/6132012_challenge_64_easy/

div(n) returns a list of divisors for div(n)
num_divs(n) returns the number of elements returned by the div(n) function
sum_divs(n) returns the sum of the elements returned by the div(n) function
totative(n) returns a list of totatives
totient(n) returns the number of elements in the list of totatives (totient is the number of totatives a number has.)

This is my first post here, hopefully it's up to snuff.

import fractions
def div(n):
    i = 1
    list = []
    while i <= n:
        if n % i == 0:
            list.append(i)
        i = i + 1
    return list
def num_divs(n):
    return len(div(n))
def sum_divs(n):
    return sum(div(n))
def totative(n):
    i = 0
    tot_list = []
    while i < n:
        gcd = fractions.gcd(i,n)
        if gcd == 1:
            tot_list.append(i)
        i = i + 1
    return tot_list
def totient(n):
    return len(totative(n))

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.