Euler Problem 72

Just add up the totient functions between 2 and 10E6.

#Euler72
class Algorithms:
    def totient(self, n):
        result = float(n)
        i = 2
        while i*i <= n:
            if n % i == 0:
                result -= result/i
            while n % i == 0:
                n /= i
            i += 1
        if n > 1:
            result -= result/n
        return result
class Problem72:
    def __init__(self, limit):
        self.limit = limit
        self.Euler = Algorithms()
    def Solution(self):
        count = 0
        for n in range (2, self.limit+1):
            count += int(self.Euler.totient(n))
            if n % 50000 == 0:
                print n
        return "Answer:"+str(count)
if __name__ == '__main__':
    P = Problem72(1000000)
    print P.Solution()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s