Skip to main content

Posts

Problem 37 Project Euler Solution with python

Truncatable primes The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. I would like to call this problem an extension for Problem 35 for which we have already written a solution. You can see the solution from here: Problem 35 Project Euler Solution with python Now with this problem, it just took me a few minutes to write the code.But the real problem before I started writing the code was the upper bound. I have searched on internet for finding the upper bound, but couldn't get any, so I have assumed the upper bound to be 1 million. I don't have any reason for that. It's just an assumption by me. O...

Problem 36 Project Euler Solution with python

Double-base palindromes The decimal number, 585 = 1001001001 2 (binary), is palindromic in both bases. Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. (Please note that the palindromic number, in either base, may not include leading zeros.) Before you continue reading the solution I would recommend you having a look at Palindrome Number Wikipedia , if you don't know what a palindrome number is. This problem is pretty easy if you have already solved problem 4. You can see my solution to understand the program for this problem. Problem 4 Project Euler Solution with python. After reading this question it just took me a few minutes to get the solution. Algorithm is simple, use python's inbuilt bin function and then get the binary version of the number. In the similar way use extended slicing to get the reverse of the number. If you don't want to use extended slicing but to use some other program for getti...

Problem 35 Project Euler Solution with python

Circular primes The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? Actually when I started writing code for this problem, I just thought of using itertools.permutations to get all the permutations of the number and then find the answer. But as I noticed " circular prime ", then I changed the code and completed writing the program. In the first attempt the program took 214 seconds for execution. But as I started optimizing the code the execution time drastically reduced. I took a lot of time optimizing the code, even I have optimized our previous Sieve of Erotosthenes to make this program faster. You can see the explanation for sieve of Erotosthenes program we have used from here: Problem 7 Project Euler Solution with python . We know that ev...

Problem 34 Project Euler Solution with Python

Digit factorials 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. The question might seem simple as it is just asking the factorials of the digit and to check if the digit factorials is same as the number. But there are some hurdles one will have to come across to solve this problem. The first problem will be to find the limit up to which the iterations are to be calculated. To find the upper bound you will have to take a look at Factorion Wikipedia . Now to split each and every digit in the number you will have to take a look at: Split an integer to digits to check ISBN - Stack Overflow First I have imported the factorial function from math module . As we knew that we will have to find the factorial of the digits form 0 -9, instead of calculating it every time in the iteration, I stored the pre-...

Problem 33 Project Euler Solution with Python

Digit cancelling fractions The fraction 49 / 98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49 / 98 = 4 / 8 , which is correct, is obtained by cancelling the 9s. We shall consider fractions like, 30 / 50 = 3 / 5 , to be trivial examples. There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator. If the product of these four fractions is given in its lowest common terms, find the value of the denominator. This problem is very simple if you were to solve it with the help of python. In python you will have to use a module to deal with fractions. It is called fractions module . It is so simple to learn and you can see the documentation: fractions - Rational Numbers. Please go and have a look at the official documentation and you will see its simplicity. If you will understand the fractions module then you ...

Problem 32 Project Euler Solution with python

Pandigital products 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, the 5-digit number, 15234, is 1 through 5 pandigital. The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital. Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum. Even though I have written the correct code I didn't get the correct answer. The answer lies in the question. Read on to understand why? One might consider that we will have to loop till 987654321 and then find all the pandigital numbers. Then find if any part of the number will form the product of the other two parts. While this might be a case, but looping 987654321 times is not really necessary. Even if you will lo...