Skip to main content

Project Euler Problem 69 Solution with Python

Totient maximum

Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9)=6.

n Relatively Prime φ(n) n/φ(n)
2 1 1 2
3 1,2 2 1.5
4 1,3 2 2
5 1,2,3,4 4 1.25
6 1,5 2 3
7 1,2,3,4,5,6 6 1.1666...
8 1,3,5,7 4 2
9 1,2,4,5,7,8 6 1.5
10 1,3,7,9 4 2.5

It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10.

Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum.

As we know that to find the value of Euler Totient Function $\phi(n)$ we can use the following formula: $$\phi(n) = n\prod_{i = 1}^{k}\left(1 - \frac{1}{p^k}\right)$$

Where $p_1, p_2, p_3.....p_k$ are the prime factors of a given number $n$.

But if we had to generate prime factors for each and every number using brute force method would increase time and space complexity. So I wrote an algorithm taking inspiration from Sieve of Eratosthenes.

I will explain my code in bits so that it will be very easy for you to understand.

First thing is to generate prime factors for all the numbers upto 100. We will assume that each and every number until 100 are prime. This assumptions tells us that the prime factors of the numbers are themseleves. For example, if we assume that 20 is a prime number, then the prime factors of 20 will be [20].

In [1]:
# we are using 101, so that we can also
# include 0 for the sake of simplicity
# original_code: prime_factors = [[i] for i in xrange(n+1)]
prime_factors = [[i] for i in xrange(101)]
print(prime_factors)
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31], [32], [33], [34], [35], [36], [37], [38], [39], [40], [41], [42], [43], [44], [45], [46], [47], [48], [49], [50], [51], [52], [53], [54], [55], [56], [57], [58], [59], [60], [61], [62], [63], [64], [65], [66], [67], [68], [69], [70], [71], [72], [73], [74], [75], [76], [77], [78], [79], [80], [81], [82], [83], [84], [85], [86], [87], [88], [89], [90], [91], [92], [93], [94], [95], [96], [97], [98], [99], [100]]

If you have observed, our assumption is completely flawed. We will correct our mistake by finding prime factors for each and every composite number. We will also make a Boolean list which will tell us if the given number is a prime or composite. The list(composite) will have the following properties.

  • For a given number, if the value is False then the number is prime.
  • If the value is True for a given number, then the number is a composite number(not a prime number).
In [2]:
# Boolean list for composite numbers
# 101 is to make indexing easy.
composite = [False]*(101)

We will further break down the problem of finding the prime factors of all the numbers. We will first find the prime factors of all the odd composite numbers and later on we will find the prime factors of all the even composite numbers. Remember, the largest prime factor for a given number will be less than or equal to the square root of the number if the number is composite. So we can limit our loop till $\sqrt{100} = 10$.

In [3]:
for i in range(3, int(100**0.5)+1, 2):
    if not composite[i]:
        for j in range(i, 100/i+1, 2):
            mul = i*j
            prime_factors[mul] = [i] + prime_factors[j]
            composite[mul] = True

print prime_factors
print composite
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [3, 3], [10], [11], [12], [13], [14], [3, 5], [16], [17], [18], [19], [20], [3, 7], [22], [23], [24], [5, 5], [26], [3, 3, 3], [28], [29], [30], [31], [32], [3, 11], [34], [5, 7], [36], [37], [38], [3, 13], [40], [41], [42], [43], [44], [5, 3, 3], [46], [47], [48], [7, 7], [50], [3, 17], [52], [53], [54], [5, 11], [56], [3, 19], [58], [59], [60], [61], [62], [7, 3, 3], [64], [5, 13], [66], [67], [68], [3, 23], [70], [71], [72], [73], [74], [5, 3, 5], [76], [7, 11], [78], [79], [80], [3, 3, 3, 3], [82], [83], [84], [5, 17], [86], [3, 29], [88], [89], [90], [7, 13], [92], [3, 31], [94], [5, 19], [96], [97], [98], [3, 3, 11], [100]]
[False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, True, False, False, False, False, False, True, False, False, False, True, False, True, False, False, False, False, False, True, False, True, False, False, False, True, False, False, False, False, False, True, False, False, False, True, False, True, False, False, False, True, False, True, False, False, False, False, False, True, False, True, False, False, False, True, False, False, False, False, False, True, False, True, False, False, False, True, False, False, False, True, False, True, False, False, False, True, False, True, False, True, False, False, False, True, False]

To make it simple, we will take an example,

i = 3
Now composite[3] = False, so we pass the if condition. Moving on for first iteration in the nested for loop

    j = 3
    mul = 9
    prime_factors[9] = [3, 3] # we are reassigning the value
    composite[9] = True
Moving on to second iteration in the nested for loop,

    j = 5
    mul = 15
    prime_factors[15] = [3, 5] # value of prime_factors[5] = [5]
    composite[15] = True
For third iteration,

    j = 7
    mul = 21
    prime_factors[21] = [3, 7] # value of prime_factors[7] = [7]
    composite[21] = True
For fourth iteration,

    j = 9
    mul = 27
    prime_factors[27] = [3, 3, 3] # value of prime_factors[9] = [3, 3]
    composite[15] = True
And these iterations will continue...

As you can see, we have only a part of the flawed assumption. Only the odd composite number values are correct, but all the even numbers still seem to follow the assumption. We will correct that mistake.

In [4]:
for j in range(2, 100/2+1):
    mul = 2*j
    prime_factors[mul] = [2] + prime_factors[j]
    composite[mul] = True

print prime_factors

print composite
[[0], [1], [2], [3], [2, 2], [5], [2, 3], [7], [2, 2, 2], [3, 3], [2, 5], [11], [2, 2, 3], [13], [2, 7], [3, 5], [2, 2, 2, 2], [17], [2, 3, 3], [19], [2, 2, 5], [3, 7], [2, 11], [23], [2, 2, 2, 3], [5, 5], [2, 13], [3, 3, 3], [2, 2, 7], [29], [2, 3, 5], [31], [2, 2, 2, 2, 2], [3, 11], [2, 17], [5, 7], [2, 2, 3, 3], [37], [2, 19], [3, 13], [2, 2, 2, 5], [41], [2, 3, 7], [43], [2, 2, 11], [5, 3, 3], [2, 23], [47], [2, 2, 2, 2, 3], [7, 7], [2, 5, 5], [3, 17], [2, 2, 13], [53], [2, 3, 3, 3], [5, 11], [2, 2, 2, 7], [3, 19], [2, 29], [59], [2, 2, 3, 5], [61], [2, 31], [7, 3, 3], [2, 2, 2, 2, 2, 2], [5, 13], [2, 3, 11], [67], [2, 2, 17], [3, 23], [2, 5, 7], [71], [2, 2, 2, 3, 3], [73], [2, 37], [5, 3, 5], [2, 2, 19], [7, 11], [2, 3, 13], [79], [2, 2, 2, 2, 5], [3, 3, 3, 3], [2, 41], [83], [2, 2, 3, 7], [5, 17], [2, 43], [3, 29], [2, 2, 2, 11], [89], [2, 5, 3, 3], [7, 13], [2, 2, 23], [3, 31], [2, 47], [5, 19], [2, 2, 2, 2, 2, 3], [97], [2, 7, 7], [3, 3, 11], [2, 2, 5, 5]]
[False, False, False, False, True, False, True, False, True, True, True, False, True, False, True, True, True, False, True, False, True, True, True, False, True, True, True, True, True, False, True, False, True, True, True, True, True, False, True, True, True, False, True, False, True, True, True, False, True, True, True, True, True, False, True, True, True, True, True, False, True, False, True, True, True, True, True, False, True, True, True, False, True, False, True, True, True, True, True, False, True, True, True, False, True, True, True, True, True, False, True, True, True, True, True, True, True, False, True, True, True]

Now everything is correct, Yay! we have found the prime factors for all the numbers less than 100.

The reason why we have found the prime factors of odd numbers first and later on even numbers will be better understood with the following example:

Lets go back in time, our prime_factors list had the following values:

    [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19],....]
First finding the prime factors for all the even composite numbers will result in the prime_factors as follows:

    [[0], [1], [2], [3], [2, 2], [5], [2, 3], [7], [2, 2, 2], [9], [2, 5], [11], [2, 2, 3], [13], [2, 7], [15], [2, 2, 2, 2], [17], [2, 9], [19],...]
That will be a blunder! So we found the prime factors for all the odd composite numbers first and then even composite numbers.

With the help of the prime factors for each number we can find the value of $\phi(n)$. But our requirement is $n/\phi(n)$ $$\phi(n) = n\prod_{i = 1}^{k}\left(1 - \frac{1}{p^k}\right)$$ $$\implies \frac{n}{\phi(n)} = \frac{1}{\prod_{i = 1}^{k}\left(1 - \frac{1}{p^k}\right)}$$

To make it simple for us, we will write a function to do this work. Remember that in the above formula, we can only use a given prime number once. If we had to find the value for 8, we will only use 2 once.

In [5]:
# function to calculate the value of
# n/phi(n)
def totient_function(prime_factors):
    multiplication = 1
    for factor in set(prime_factors):
        multiplication *= (1.0 - 1.0/factor)
    return 1.0/multiplication

print totient_function(prime_factors[6]) # lets find the value for 6
3.0

And turns out that our function is also working correctly.

Time to see the code for the problem

Program

In [6]:
# http://radiusofcircle.blogspot.com

# import time
import time

# start time
start = time.time()

# use xrange function if available
try:
    zrange = xrange
except:
    zrange = range

# function to calculate the value of
# n/phi(n)
def totient_function(prime_factors):
    multiplication = 1
    for factor in set(prime_factors):
        multiplication *= (1.0 - 1.0/factor)
    return 1.0/multiplication

# function to generate prime factors of numbers
# upto given number n
def prime_factors_generator(n):
    prime_factors = [[i] for i in zrange(n+1)]
    composite = [False]*(n+1)

    for i in zrange(3, int(n**0.5)+1, 2):
        if not composite[i]:
            for j in zrange(i, n/i+1, 2):
                mul = i*j
                prime_factors[mul] = [i] + prime_factors[j]
                composite[mul] = True

    for j in zrange(2, n/2+1):
        mul = 2*j
        prime_factors[mul] = [2] + prime_factors[j]
        composite[mul] = True
    return prime_factors


# generate prime factors upto 1000000
p_f = prime_factors_generator(1000000)

# remove 0, 1 from the list and find the
# value of n/phi(n)
p_f_nums = map(totient_function, p_f[2:])


# find the index of the maximum of p_h_nums
# add 2 to compensate the removal of 0, 1
print p_f_nums.index(max(p_f_nums))+2

# time at the end of the program
end = time.time()

# total time taken to execute the program
print end - start

The code is very simple and its just an extention of the example which I have explained in detail.

You can download the program from Github Gist pep69.py

Summary

Eventhough this problem was less difficult when compared to few other problems, it took a lot of time for me to write a good algorithm to solve this problem. The main problem was a question on whether to use prime numbers or not. After thinking for some time, I mixed up the concept of generating the prime numbers and finding the prime factors and have written a solution. There is a lot to improve in this code. I think we can even write a different algorithm. But I am satisfied with this solution and have sticked to this solution.

If you have any doubt or didn't understand anything then comment in the comment box below and I will be glad to help you. You can also Conact me for any suggestions or for any other help.

Popular posts from this blog

Problem 60 Project Euler Solution with python

Prime pair sets The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. This problem is j u st a brute force problem. If you have come here because you don't know the limit upto which you will h ave to gener ate the prime numbers t hen go ahe ad and t r y with 10,000 . When I first start ed solving the problem I chose 1 million(beca use most of the problem s on project E uler have this limit ), but it took very long for the computer to fin d the solution. After searching on the internet then I found many people choosing 10, 000 so I have changed my in put f rom 1 million to 10000 and the output was f ast. He...

Add/Embed SVG to Blogger website

In this post I will tell you my method(trick) of adding SVG images in a blogger website or blog. Before starting , the first thin g I am assu m ing is that you are aware of SVG if you are here. If not please see S calable V ec tor G raphics Recently when I tried to embed a SVG image for a post on pygal, I tried uploading the SVG file and blogger Image uploader came up with an error, because of which I had to find some other way.  SVG File upload Error in Blogger  I started sea rc hing Google " Embed SVG in Blogger " . I found blogorrhea , w h ich gave some i nformatio n on add ing SVG directly as a markup , which worked , but I faced another problem using this . Also th is guy has used lot of Javascript which was confusin g for me, being new to using SVG.   So I first t houg ht of learning on h ow to embed SVG in HTML and t his on e worked out. Actually we can embed SVG in HTML i n following ways: Using Object tag Using Iframe tag Using embed...

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documenta...