Skip to main content

Posts

Showing posts from November, 2016

Project Euler Problem 65 Solution with python

Convergents of e ¶ The square root of 2 can be written as an infinite continued fraction. $$ \begin{equation} \sqrt{2} = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2....} } } } \end{equation} $$ The infinite continued fraction can be written, $ \sqrt{2} = [1;(2)]$, (2) indicates that 2 repeats ad infinitum. In a similar way, $\sqrt{23} = [4;(1,3,1,8)]$. It turns out that the sequence of partial values of continued fractions for square roots provide the best rational approximations. Let us consider the convergents for $\sqrt{2}$. \begin{equation} 1 + \cfrac{1}{2} = \cfrac{3}{2}\end{equation} \begin{equation} 1 + \cfrac{1}{2 + \cfrac{1}{2}} = \cfrac{7}{5} \end{equation} \begin{equation} 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2} } } = \cfrac{17}{12} \end{equation} \begin{equation} \sqrt{2} = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2} } } } = \cfrac{41}{2...

Project Euler Problem 64 Solution with python

Odd period square roots ¶ All square roots are periodic when written as continued fractions and can be written in the form: $$ \begin{equation} \sqrt n = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{a_3 + \cfrac{1}{a_4....} } } } \end{equation} $$ For example, let us consider $\sqrt 23$: $$ \sqrt 23 = 4 + \sqrt 23 -4 = 4 + \cfrac{1}{\cfrac{1}{\sqrt 23 - 4}} = 4 + \cfrac{1}{1 + \cfrac{\sqrt 23 - 3}{7}} $$ If we continue we would get the following expansion: $$ \begin{equation} \sqrt 23 = 4 + \cfrac{1}{1 + \cfrac{1}{3 + \cfrac{1}{1 + \cfrac{1}{8...} } } } \end{equation} $$ The process can be summarised as follows: $$ a_0 = 4, \cfrac{1}{\sqrt 23 - 4} = \cfrac{\sqrt 23 + 4}{7} = 1 + \cfrac{\sqrt 23 - 3}{7} $$ $$ a_1 = 1, \cfrac{7}{\sqrt 23 - 3} = \cfrac{7(\sqrt 23 + 3)}{14} = 3 + \cfrac{\sqrt 23 - 3}{2} $$ $$ a_2 = 3, \cfrac{2}{\sqrt 23 - 3} = \cfrac{2(\sqrt 23 + 3)}{14} = 1 + \cfrac{\sqrt 23 - 4}{7} $$ $$ a_3 = 1, \cfrac{7}{\sqr...

URL Shortner with Python and Flask - Chapter 4 - Blueprints

Remember? In the previous part we created flask app which had all the code in a single file and we called it app.py . As our site was small, we were able to manage the code. But what if, you want to create a website as big as facebook? Yes, features like groups, friends, newsfeed, Pages etc. can be created using flask. But do you think, that it would be better if all the data were to be on the same file? If you are becoming bigger then you should consider separating code into different files so that if you want to edit one feature then you can do easily. The main point here is, organising the files and features becomes easy. But how do we do we organize and seperate features in flask? The best answer is Flask-Blueprints. There are many advantages of using Blueprints, and you can read them from here: Why Blueprints? . Starting from scratch ¶ Now that you are convinced, how do we use blueprints?. Lets see an example. The following is the __init__.py file which you will st...

URL shortener with python and flask - Chapter 3 - Let's play with flask

In this chapter, we will look at some of the important concepts of flask. I am assuming that you know what flask is all about and what you can do with flask. If not then visit Flask I am also assuming that you have a solid understanding of python programming language. If not just google python tutorial and you will see a lot of tutorials. But Python docs is the best way to learn python. In this chapter, we will be creating a lot of scripts or python files which we will run from the command prompt. To create a script you can open your favourite text editor and then type the python code, save it with a file extension .py . Now open the command prompt in the folder you have saved python and then type python your_file_name.py Then command prompt will run the script and gives the expected output.

Project Euler Problem 63 solution with python

Powerful digit counts ¶ The 5-digit number, $16807=7^5$, is also a fifth power. Similarly, the 9-digit number, $134217728=8^9$, is a ninth power. How many n-digit positive integers exist which are also an nth power?

Project Euler Problem 62 solution with python

Cubic permutations ¶ The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube. Find the smallest cube for which exactly five permutations of its digits are cube.

URL shortener with python and flask - Chapter 2 - Let's play with python

In this chapter we will be brushing up our concepts in python. If you are good in python then you can skip this chapter and continue with the next one. But I recommend you to just skim through the contents of this chapter. I am assuming that you already have python installed and the same is accessible from the command prompt. Now to get a hands on experience, open your IDLE or command prompt and start typing the code. Just remember that you will be typing line by line. As I am using Ipython notebook, we will see a little bit of variation from your output at some points. But the core concepts are the same.