Skip to main content

Posts

Showing posts from May, 2016

Problem 43 Project Euler Solution with python

Sub-string divisibility The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. Let d 1 be the 1 st digit, d 2 be the 2 nd digit, and so on. In this way, we note the following: d 2 d 3 d 4 =406 is divisible by 2 d 3 d 4 d 5 =063 is divisible by 3 d 4 d 5 d 6 =635 is divisible by 5 d 5 d 6 d 7 =357 is divisible by 7 d 6 d 7 d 8 =572 is divisible by 11 d 7 d 8 d 9 =728 is divisible by 13 d 8 d 9 d 10 =289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. One might write a simple solution using direct if else statements for this problem. Using if else statements, the execution time may be a few seconds. But this is not a very good approach. I too had written a program with if else statement which will take each and every permutation of the 0-9 Pandigital and check for the conditions given in the qu

Problem 42 Project Euler Solution with Python

Coded triangle numbers The n th term of the sequence of triangle numbers is given by, t n = ½ n ( n +1); so the first ten triangle numbers are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t 10 . If the word value is a triangle number then we shall call the word a triangle word. Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words? This question is pretty simple. You will only have to know that a triangle number n will satisfy the condition of (8n+1) being a perfect square for numbers less than (2 53 -1 = 9007199254740991. I got to know this from Matlab forum: Best way to determine if given number is triangular . All the other operations are related to coding stuf

Problem 41 project Euler Solution with python

Pandigital prime We shall say that an n -digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n -digit pandigital prime that exists? An another question on Pandigital. I have written two solutions, one solution demanded homework and the other required programming logic . Both had their own pros and cons. But I will finalize the solution which demanded the programming skills. Anyways you can see both the programs below. Better performing code considers the divisibility of 3. See the Answer written by engineer on Stack Overflow, you will understand the algorithm . (Se cond Program I wrote ) # http://radiusofcircle.blogspot.com # importing the permutations method from itertools import permutations # importing time module import time # time at the start of program execution start = time . time() def is_prime (n): """Function to ch

Problem 40 Project Euler Solution with python

Champernowne's constant An irrational decimal fraction is created by concatenating the positive integers: 0.12345678910 1 112131415161718192021... It can be seen that the 12 th digit of the fractional part is 1. If d n represents the n th digit of the fractional part, find the value of the following expression. d 1 × d 10 × d 100 × d 1000 × d 10000 × d 100000 × d 1000000 This problem requires a small homework to set the upper limit for the for loop. This is general mathematics. We have 9, 1 digit numbers 90, 2 digit numbers 900, 3 digit numbers 9000, 4 digit numbers 90000, 5 digit numbers 900000, 6 digit numbers Which will total to 5888889 digits. But we only need 1000000, so th at 9 x 1 digit 90 x 2 digit 900 x 3 digit 9000 x 4 digit 90000 x 5 digit n x 6 digit = 9*1 + 90*2 + 900*3+ 9000*4 + 90000*5 + n*6 = 1000000 So the value of n is approxima tely 85186 that means that we will have to do iterations until 185186. The same thing has been u

Problem 39 Project Euler Solution with python

Integer right triangles If p is the perimeter of a right angle triangle with integral length sides, { a , b , c }, there are exactly three solutions for p = 120.  {20,48,52}, {24,45,51}, {30,40,50} For which value of p ≤ 1000, is the number of solutions maximised? Again an easy problem, it just took me a few minutes to write the first draft of the code which worked in less than a second. I still modified it and made it to work under 0.1 second. Algorithm is very simple, we will use Pythagorean Theorem to generate the side lengths of the right angle triangle. If you don't know about Pythagorean Theorem, then the best places to learn about it are: Wikipedia - Pythagorean Theorem Pythagoras Theorem - Mathisfun Pythagorean Theorem - University of Georgia Pythagorean Theorem - Worlfram Math Anyways I will give you the formula that Pythagorean Theorem states: c 2 = a 2 +b 2 where c - Length of Hypotenuse a,b length of two other sides Pythagorean Triangle

Problem 38 Project Euler Solution with Python

Pandigital multiples Take the number 192 and multiply it by each of 1, 2, and 3: 192 × 1 = 192 192 × 2 = 384 192 × 3 = 576 By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and ( 1,2,3 ) The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5 ). What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with ( 1,2, ... , n ) where n > 1? Its been a long time since my last post, but due to some other work I was not able to post a solution. Sorry for that! This problem(38) is very easy once you have understood the question correctly. I will first explain you the question and then I will explain you the program. The first condition from n>1 is that you will for sure have to do iterations for a minimum of 2 times.