Skip to main content

Posts

Showing posts from 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.

URL shortener with python and flask - chapter 1 - Installations

In this chapter we will make some important installations. We will use these modules and softwares in the upcoming chapters. I am assuming that you are beginner and so we will start with python installation.

URL shortener with python and flask- Chapter 0 - Introduction

In this tutorial series, we will use flask to create a URL shortener service. At the end of this tutorial series, I am expecting that you will be in a position to extend this knowledge for use in some other application.

Pairwise Summation algorithm implementation with python

Pairwise summation algorithm is a summation algorithm like Kahan Algorithm but does perform better when compared to the Kahan Algorithm . This is because of the Divide and Conquer approach followed by this algorithm. According to Wikipedia, this is the official sum mation algorithm used in N umpy .

Binary GCD algorithm implementation with python

Binary GCD algorithm also known as Stein's algorithm is an improved version of Euclidean Algorithm.It takes two integer inputs and finds the Greatest Common Divisor(GCD) of the two numbers. The algorithm uses recursive techniques to find the GCD of the two numbers.

Heap's Algorithm implementation with python

Heap's Algorithm is used to generate all the permutations of a given object. It was first proposed by B.R. Heap in the year 1963. In this post, we will write a program usi ng Heap 's Algorithm which will gener at e permutations for a given object. Remember that the o bject shou ld be itera ble. Otherwise, you will get an error.

Problem 61 Project Euler Solution with python

Cyclical figurate numbers Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae: Triangle P 3, n = n ( n +1)/2 1, 3, 6, 10, 15, ... Square P 4, n = n 2 1, 4, 9, 16, 25, ... Pentagonal P 5, n = n (3 n −1)/2 1, 5, 12, 22, 35, ... Hexagonal P 6, n = n (2 n −1) 1, 6, 15, 28, 45, ... Heptagonal P 7, n = n (5 n −3)/2 1, 7, 18, 34, 55, ... Octagonal P 8, n = n (3 n −2) 1, 8, 21, 40, 65, ... The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties. The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first). Each polygonal type: triangle (P 3,127 =8128), square (P 4,91 =8281), and pentagonal (P 5,44 =2882), is represented by a different number in the set. This is the only set of 4-digit numbers with this property. Find...

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