Skip to main content

Problem 26 Project Euler Solution with python

Reciprocal cycles

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
1/20.5
1/30.(3)
1/40.25
1/50.2
1/60.1(6)
1/70.(142857)
1/80.125
1/90.(1)
1/100.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

First of all I am sorry for not posting these many days. Firstly I was not able to concentrate on posting the solution because I had some other important work. Secondly as I was not really paying attention I was not able to find the solution for this problem. I was completely lost in a wrong way to solve this problem. I was thinking that I should use python decimal module to solve this problem but my assumption was wrong and I had to come all the way back and start from scratch. I started yesterday and today I present you the solution.

This post will be somewhat big because I will neatly explain each and every concept behind the scenes so that you will understand it perfectly. If you are good at directly understanding the code without reading any algorithm, then you are welcome. The code is well commented to encourage people like you. If you are going to read through this whole post, firstly be patient as I will explain even elementary methods(Skip them if you already know). Secondly stay with me and you will leave this page with lots and lots of new concepts learned and with flying colors. 

Okay lets get started.

 Long Divison

This is not a new concept for you, but I would just like to give you a brush up on this topic. Let us understand this concept with a few examples. Take 10/3, 100/13,1000/101.
You should have observed a pattern in the above selected numbers.
  1. All the denominators are prime numbers.
  2. All the numerators are multiples of 10.
I will explain it to you why. 

To get a whole number as a quotient after division then the number should be greater than or equal to the number in the denominator. We are considering multiples of 10.

I have chosen prime numbers especially because they show this repeating decimal characteristics(Except 5 and 2).
Now see how I am doing the division, you should simply understand it directly seeing the image.
10 divided by 3
10 divided by 3
In the above image, as we all know that 3 times 3 is 9, after dividing it with 10 were are left with 1 as remainder. Now as you can see, if we multiply the remainder with 10 then we again arrive at the same number 10 with which we have started so now we can terminate our division and say that when 10 is divided by 3 then 3 is the repeating cycle. This is not completely true, but for your explanation I have made it simple for you. Read on to understand the perfect answer.
Now see the image 100/13:
100 divided by 13
100 divided by 13
Now in the first block we have successfully divided 100, multiplying 13-7 times. The remainder will be 9, as 9 is less than 10 we will have to multiply it by 10 to make it divisible by 13, so we will add a decimal to the number. This is also true in the case 10/3, but we have not seen this step because even though we will add the decimal, the recurring number is still 3 as there cannot be more than one decimal points. Coming back, we use 13x6 to divide 90 and the remainder is 12, which is again less than 13 so multiply it by 10 because we have already added the decimal. Next one will be 3, so multiply by 10 and we get 4 as remainder. Multiply 4 by 10 to get 40 which is more than 13 and divide it by 13 and you get 1 as the remainder. As 1 is less than 10 then we get 10 which when multiplied by 10 we get 100 and so we have again reached the starting point and the algorithm terminates.

But the repeating cycle is not just 769, it is '7690', because at the end of the algorithm we had 10, but to get 100 we need to multiply with 10 to get 100. Adding another decimal is not the solution, so simply add 0 to get 100. See 1000/101 to further understand the addition of zero after decimal.
1000 divided by 101
1000 divided by 101
Now see this example to get a clear cut idea of long division for reciprocal cycles. When we started with 1000, we used 101 times 9 to get 909 and the remainder was 91. As 91 was less than 101 we added a decimal and then got it multiplied by 10 which gave us 910. Divide it again by 909 to get 1. As we have a decimal we multiply it by 10 to get 10. Now we need another 100(10x10) to prove that we have come to the starting point and we can eliminate the algorithm. To get first 10 I will add a 0 to the quotient and so I can multiply it by 10. So now my remainder is 100 and thus the algorithm terminates.

Finally as discussed in the previous example, my recurring cycle is not just 990 but 9900, as we are multiplying it with 10 to get 1000(100x10).

Hope you have understood this. If you have understood this properly then you have understood almost the underlying algorithm of the program.

Prime Sieve(Sieve of Eratosthenes)

I have used the same code from Problem 7 Project Euler Solution with python. Of course there are many other places where you can learn about this concept. You can visit Wikipedia- Sieve of Eratosthenes, Geeks of Geeks - Sieve of Eratosthenes.

Greatest Common Divisor of two numbers

As the name suggests, if we consider two numbers the largest number that can divide both the numbers evenly is called as greatest common divisor of that two numbers or GCD in short. For example consider two numbers 6,12, the largest number which can divide both the numbers is 6.

The best way to find GCD of two numbers is to make use of Euclid Algorithm. To learn more you can visit: How to Find the Greatest Common Divisor of two numbers - Wikihow.

Least Common Multiple of two numbers

Least common multiple also called as LCM is the smallest number that is a multiple of both the numbers. For example consider 6 and 12, the smallest number which is a multiple of both of them is 12. If you want to know more about LCM then you can visit: How to find the Least Common multiple of two numbers - Wikihow. For our program I have used the code snippet from Problem 5 Project Euler Solution with python.

the last part....

Properties of Repeating decimals

Consider a number 'n', the maximum number of repeating decimals it can have is 'n-1'.

If a number say 'n' is co-prime to 10(not divisible by either 2 or 5 or both 2 and 5), and the number 'n' can be written as n = axb, then the length of repeating decimals is equal to the LCM of number of repeating decimals of a,b.

This can be written mathematically as:

number of repeating decimals of a = x(say)

number of repeating decimals of b = y(say)

number of repeating decimals of n(axb) = LCM(x,y)

This is where we will use the LCM which we have found out in the previous section.

If number say 'n' is not co-prime to 10(divisible by either 2 or 5 or both 2 and 5), and the number after prime factorization can be written as n = 2a5bpcqd , where p, q are prime numbers and a,b,c,d are the powers of the 2, 3, p, q respectively, then number of repeating decimals of n is equal to the number of repeating decimals of pcqd.

For example consider 28 which can be written as 22x71, then the number of repeating decimals of 28 is 6(repeating decimals of 7). to be for this kind of numbers, take the number and divide it until it not divisible by 2, divide it by 5 until it is not divisible by 5 and the length of the repeating numbers of the remaining number is the length of repeating number of the given number.

From the above paragraph(s), you can see that if we find the length of the repeating decimals of the prime numbers then we can use the same for finding the length of the upcoming composite numbers.

One final thing to remember is that the last two properties explained above are not valid for numbers 2 and 5😛.

Finally we have seen all the underlying concepts of the program. Let's see the program.

Program

Like the algorithm the program also looks big, but it is very easy.
I think you might have confused with the for loop used in Line 79 and Line 85. I will explain it to you.

We know that, when we call sieve(1000), we will get a list like [2,3,5,7,11,13......], but if you remember the long division algorithm which we framed will return None when the number will evenly divide the numerator(if there is no repeating decimal). So we can say that out of all the prime numbers  2,5 will return None. But we just want 0 which means that there is no repeating decimals. Adding an if else statement to check this is inefficient because all the prime numbers except 2 and 5 will return some value. So what I simply did was to start my loop from 7 or primes[3:] and manually added the value for 3.

In a similar way in Line 85 the numbers which we have left are 1,2,3,4,5. While 1, 2, 4, 5 will divide 1 evenly and we have already added the value for 3 in the previous for loop, so there was no need for extra looping, wasting the resources.

If you want you can download the source code from Github Gist pep26.py

Output

Summary

This problem took around 1 day for me to solve and post. But framing the algorithm took few days, because I was searching for Diamonds when I had to search for gold. When I realized that I had to find gold which I have already seen somewhere when I was searching for diamonds it became easy for me. No problem being lost in a wrong way had given me some benefits like, I came to know about the suffix trees, the python decimal module and some regular expressions. This may be helpful for me in the future.

As always 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.

Also please don't hesitate to comment if you have any suggestions, or have a better program, then please do share it with the community.

You can also contact me.

Thank you. Have a nice day😄.

Popular posts from this blog

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 documentation

Problem 11 Project Euler Solution with python

Largest product in a grid In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07

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