Skip to main content

Posts

Showing posts from June, 2016

Problem 59 Project Euler Solution with python

XOR decryption Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107. A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65. For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message. Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than

Problem 58 Project Euler Solution with python

Spiral primes Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18   5  4   3 12 29 40 19  6  1  2 11 28 41 20   7  8  9 10 27 42 21 22 23 24 25 26 43 44 45 46 47 48 49 It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%. If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%? You can call this problem a continuation to the Problem 28 . I simply have modified the problem 28 project euler Solution with python and have used to solve this problem.  Please have a look at the solution w

Problem 57 Project Euler Solution with python

Square root convergents It is possible to show that the square root of two can be expressed as an infinite continued fraction. √ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213... By expanding this for the first four iterations, we get: 1 + 1/2 = 3/2 = 1.5 1 + 1/(2 + 1/2) = 7/5 = 1.4 1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666... 1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379... The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator. In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator? Before we start with the solution, have a look at the following links: Mathematics Stack: Convergent of square root of 2 Convergent Wolfram You can also have a look at Wikipedia Continued fraction if you want. According to the links, if p/q is is the first conv

Problem 56 project Euler Solution with python

Powerful digit sum A googol (10 100 ) is a massive number: one followed by one-hundred zeros; 100 100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1. Considering natural numbers of the form, a b , where a, b < 100, what is the maximum digital sum? An another problem on sum of digits. Algorithm is very simple. It is as follows: 1) Write a function to find the sum of digits of the given number. The function is as follows: Let the number be n. Start a while loop. Create a variable to store the sum of numbers. Find the remainder of the number when divided by 10. This remainder will be the last digit . Add this number to the sum. Divide the number with 10 and go again for step 2, until the number becomes 0. 2) Create two for loops both in the range of 0 to 100. One for a and one for b . Now find if any of the previous values of sum of digits is lesser than the present one, th

Problem 55 Project Euler Solution with python

Lychrel numbers If we take 47, reverse and add, 47 + 74 = 121, which is palindromic . Not all numbers produce palindromes so quickly. For example, 349 + 943 = 1292, 1292 + 2921 = 4213 4213 + 3124 = 7337 That is, 349 took three iterations to arrive at a palindrome. Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palind

Problem 54 Project Euler Solution with python

Poker hands In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way: High Card : Highest value card. One Pair : Two cards of the same value. Two Pairs : Two different pairs. Three of a Kind : Three cards of the same value. Straight : All cards are consecutive values. Flush : All cards of the same suit. Full House : Three of a kind and a pair. Four of a Kind : Four cards of the same value. Straight Flush : All cards are consecutive values of same suit. Royal Flush : Ten, Jack, Queen, King, Ace, in same suit. The cards are valued in the order: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace. If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highes