rhys.codesPutting random things here, mostly my Project Euler solutions.
http://rhys.codes/
Wed, 24 Apr 2019 12:39:50 +0000Wed, 24 Apr 2019 12:39:50 +0000Jekyll v3.8.5Problem 31<p>Solution to Problem 31, written in Python.</p>
<p>In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:</p>
<p>1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:</p>
<p>1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?</p>
<script src="https://gist.github.com/12hys/acbd11889d6cf9dfb5c4.js"></script>
Fri, 25 Sep 2015 22:01:09 +0000
http://rhys.codes/2015/09/25/problem-31.html
http://rhys.codes/2015/09/25/problem-31.htmlGetting Functional<p>This is my first Haskell program, I plan to solve more Project Euler problems using Haskell as I get better at it.</p>
<p>Problem 1 - Project Euler</p>
<script src="https://gist.github.com/12hys/506c2c8bdcf8228c0b5aed6fd67785f4.js"></script>
Tue, 24 Feb 2015 03:21:16 +0000
http://rhys.codes/2015/02/24/getting-functional.html
http://rhys.codes/2015/02/24/getting-functional.htmlProblem 55<p>Solution to Problem 55. Written in Python.</p>
<p>Problem: If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.</p>
<p>Not all numbers produce palindromes so quickly. For example,</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337
</code></pre></div></div>
<p>That is, 349 took three iterations to arrive at a palindrome.</p>
<p>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 palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).</p>
<p>Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.</p>
<p>How many Lychrel numbers are there below ten-thousand?</p>
<p>NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.</p>
<script src="https://gist.github.com/12hys/5a3bb7c7ebb15cff76ec.js"></script>
Tue, 20 Jan 2015 14:51:26 +0000
http://rhys.codes/2015/01/20/problem-55.html
http://rhys.codes/2015/01/20/problem-55.htmlProblem 79<p>Solution to Problem 79. Solved using pencil/paper.</p>
<p>Problem: A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may ask for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.</p>
<p>The text file, <a href="https://projecteuler.net/project/resources/p079_keylog.txt">keylog.txt</a>, contains fifty successful login attempts.</p>
<p>Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.</p>
<p>The solution to this problem is actually quite simple. Think of all the numbers in keylog.txt as rules that need to be satisifed.</p>
<p>We start with writing down the first and second rule next to each other. You should have <code class="highlighter-rouge">319680</code> written down. We can see the third rule, <code class="highlighter-rouge">180</code>, already has the digits <code class="highlighter-rouge">1</code>, <code class="highlighter-rouge">8</code>, and <code class="highlighter-rouge">0</code> in order in the number <code class="highlighter-rouge">319680</code>. The fourth rule, <code class="highlighter-rouge">690</code>, is where things start to change. In order to satisfy this rule, we need to swap the <code class="highlighter-rouge">6</code> and <code class="highlighter-rouge">9</code> to arrive at <code class="highlighter-rouge">316980</code>.</p>
<p>The next rule, <code class="highlighter-rouge">129</code>, introduces a new digit that we haven’t seen before, but it hasn’t changed the way we solve the problem. <code class="highlighter-rouge">1</code> and <code class="highlighter-rouge">9</code> appear to be satisfied, but in order to solve for this rule, we need to put the <code class="highlighter-rouge">2</code> somewhere. Putting it right after the <code class="highlighter-rouge">1</code> is fine; you should now have <code class="highlighter-rouge">3126980</code> written down. Keep doing the same thing all the way down the list, and you’ll arrive at your answer.</p>
Tue, 20 Jan 2015 13:45:58 +0000
http://rhys.codes/2015/01/20/problem-79.html
http://rhys.codes/2015/01/20/problem-79.htmlProblem 41<p>Solutions to Problem 41. Written in Python.</p>
<p>Problem: 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.</p>
<p>What is the largest n-digit pandigital prime that exists?</p>
<p>Method 1: This is my slowest (and original) solution without using any tricks or analysis of the problem. I also used a fast prime number generator from <a href="http://stackoverflow.com/a/2068548">this StackOverflow answer</a>. I first generate all primes up to 987654321 (the largest pandigital integer) and then parallelize the pandigital check and print the maximum of the returned values.</p>
<p>Method 2: Method 2 is much faster with runtimes of around 0.6s on my i7 machine (running on PyPy). We only need to permute on the pandigital numbers. Python’s itertools package has a sweet permutations function that does this for us. For a 9 digit number, the permuations are <code class="highlighter-rouge">9! = 362,880</code>, for 8 digits <code class="highlighter-rouge">8! = 40,320</code>, and so on.</p>
<p>Method 3: As it turns out, we only need to check the permutations of a 7 digit pandigital number. The <a href="http://en.wikipedia.org/wiki/Divisibility_rule#Divisibility_by_3_or_">Divisibility Rule</a> states that if the sum of a digits’ numbers are divisible by 3 or 9, then the original number is divisible by 3 or 9. This means that all numbers that are 1 to n pandigital are not prime, where n = 9, 8, 6, 5, 3, and 2. The runtime for this solution hovers around 0.15s.</p>
<script src="https://gist.github.com/12hys/bfd5eb8afdf2d73372ed.js"></script>
Mon, 19 Jan 2015 17:08:36 +0000
http://rhys.codes/2015/01/19/problem-41.html
http://rhys.codes/2015/01/19/problem-41.htmlProblem 39<p>Solution to Problem 39. Written in Python.</p>
<p>Problem: 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.</p>
<p><code class="highlighter-rouge">{20,48,52}, {24,45,51}, {30,40,50}</code></p>
<p>For which value of p ≤ 1000, is the number of solutions maximised?</p>
<script src="https://gist.github.com/12hys/075ba492779fbbe5ccac.js"></script>
Sat, 17 Jan 2015 18:28:10 +0000
http://rhys.codes/2015/01/17/problem-39.html
http://rhys.codes/2015/01/17/problem-39.htmlProblem 179<p>Solution to Problem 179. Written in Python.</p>
<p>Problem: Find the number of integers 1 < n < 10<sup>7</sup>, for which n and n + 1 have the same number of positive divisors. For example, 14 has the positive divisors 1, 2, 7, 14 while 15 has 1, 3, 5, 15.</p>
<script src="https://gist.github.com/12hys/ee2d95a0abadc08b6b92.js"></script>
Fri, 16 Jan 2015 20:22:50 +0000
http://rhys.codes/2015/01/16/problem-179.html
http://rhys.codes/2015/01/16/problem-179.htmlProblem 32<p>Solution to Problem 32. Written in Python.</p>
<p>Problem: 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.</p>
<p>The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.</p>
<p>Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.</p>
<p>HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.</p>
<script src="https://gist.github.com/12hys/e71cc7c3e18de31f4a4b.js"></script>
Fri, 16 Jan 2015 19:18:42 +0000
http://rhys.codes/2015/01/16/problem-32.html
http://rhys.codes/2015/01/16/problem-32.htmlProblem 27<p>Solution to Problem 27. Written in Python.</p>
<p>Problem: Euler discovered the remarkable quadratic formula: n<sup>2</sup> + n + 41</p>
<p>It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41<sup>2</sup> + 41 + 41 is clearly divisible by 41.</p>
<p>The incredible formula n<sup>2</sup> − 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, −79 and 1601, is −126479.</p>
<p>Considering quadratics of the form:</p>
<table>
<tbody>
<tr>
<td>n<sup>2</sup> + an + b, where</td>
<td>a</td>
<td>< 1000 and</td>
<td>b</td>
<td>< 1000</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>where</td>
<td>n</td>
<td>is the modulus/absolute value of n</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>e.g.</td>
<td>11</td>
<td>= 11 and</td>
<td>−4</td>
<td>= 4</td>
</tr>
</tbody>
</table>
<p>Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.</p>
<script src="https://gist.github.com/12hys/ec581840382b5cf75046.js"></script>
Fri, 16 Jan 2015 18:00:14 +0000
http://rhys.codes/2015/01/16/problem-27.html
http://rhys.codes/2015/01/16/problem-27.htmlProblem 52<p>Solution to Problem 52. Written in Python.</p>
<p>Problem: It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.</p>
<p>Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.</p>
<script src="https://gist.github.com/12hys/0fa1a07e91c0089551e8.js"></script>
Thu, 15 Jan 2015 16:03:38 +0000
http://rhys.codes/2015/01/15/problem-52.html
http://rhys.codes/2015/01/15/problem-52.html