{ "metadata": { "language": "Julia", "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Project Euler" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Multiples of 3 and 5\n", "
\n", "Problem 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n", "\n", "Find the sum of all the multiples of 3 or 5 below 1000." ] }, { "cell_type": "code", "collapsed": false, "input": [ "function Mult3or5_below(num)\n", " sum_all = 0\n", " for i = 1:num\n", " if ((i%5 == 0) || (i%3 == 0))\n", " sum_all += i\n", " end\n", " end\n", " return sum_all\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 146, "text": [ "Mult3or5_below (generic function with 1 method)" ] } ], "prompt_number": 146 }, { "cell_type": "code", "collapsed": false, "input": [ "Mult3or5_below(999)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 148, "text": [ "233168" ] } ], "prompt_number": 148 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Even Fibonacci numbers\n", "
\n", "Problem 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:\n", "\n", "1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\n", "\n", "By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms." ] }, { "cell_type": "code", "collapsed": false, "input": [ "function SumFibEvens_below(num)\n", " fib_num_prev = 1\n", " fib_num = 1\n", " fib_sum = 0\n", " answer = 0\n", " \n", " while fib_sum < num\n", " fib_sum = fib_num_prev + fib_num\n", " fib_num_prev = fib_num\n", " fib_num = fib_sum\n", " if fib_sum%2 == 0\n", " answer += fib_sum\n", " end\n", " end\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 65, "text": [ "SumFibEvens_below (generic function with 1 method)" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "SumFibEvens_below(4000000)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 66, "text": [ "4613732" ] } ], "prompt_number": 66 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Largest prime factor\n", "
\n", "Problem 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The prime factors of 13195 are 5, 7, 13 and 29.\n", "\n", "What is the largest prime factor of the number 600851475143 ?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "function LargestPrimeFactor(num)\n", " factor_dict = factor(num)\n", " return maximum(factor_dict)[1]\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 84, "text": [ "LargestPrimeFactor (generic function with 1 method)" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "LargestPrimeFactor(600851475143)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 83, "text": [ "6857" ] } ], "prompt_number": 83 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Largest palindrome product\n", "
\n", "Problem 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 \u00d7 99.\n", "\n", "Find the largest palindrome made from the product of two 3-digit numbers." ] }, { "cell_type": "code", "collapsed": false, "input": [ "function IsPalindrome(num1, num2)\n", " product = string(num1 * num2) \n", " return product == reverse(product)\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "IsPalindrome (generic function with 1 method)" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "IsPalindrome(91,99)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 40, "text": [ "true" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "function MaxPalindrome_digits(num)\n", " min_num = 10^(num-1)\n", " max_num = int(repeat(\"9\",num))\n", " product = 0\n", " answer = 0\n", " \n", " for x = min_num:max_num\n", " for y = min_num:max_num\n", " product = x * y\n", " if IsPalindrome(x,y) && product > answer\n", " answer = product\n", " end\n", " end\n", " end\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 41, "text": [ "MaxPalindrome_digits (generic function with 1 method)" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "MaxPalindrome_digits(3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 42, "text": [ "906609" ] } ], "prompt_number": 42 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Smallest multiple\n", "
\n", "Problem 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.\n", "\n", "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "function SmallestMult(num)\n", " range_list = Int64[]\n", " for i = 1:num\n", " push!(range_list,i)\n", " end\n", " answer = lcm(range_list...)\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 101, "text": [ "SmallestMult (generic function with 1 method)" ] } ], "prompt_number": 101 }, { "cell_type": "code", "collapsed": false, "input": [ "SmallestMult(20)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 102, "text": [ "232792560" ] } ], "prompt_number": 102 }, { "cell_type": "code", "collapsed": false, "input": [ "lcm(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 176, "text": [ "232792560" ] } ], "prompt_number": 176 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Sum square difference\n", "
\n", "Problem 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The sum of the squares of the first ten natural numbers is,\n", "\n", "12 + 22 + ... + 102 = 385\n", "The square of the sum of the first ten natural numbers is,\n", "\n", "(1 + 2 + ... + 10)2 = 552 = 3025\n", "Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 \u2212 385 = 2640.\n", "\n", "Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum." ] }, { "cell_type": "code", "collapsed": false, "input": [ "function SumSqDif(num)\n", " sumsquares = 0\n", " runningsum = 0\n", " for i = 1:num\n", " sumsquares += i^2\n", " runningsum += i\n", " end\n", " squaresum = runningsum^2\n", " answer = squaresum - sumsquares\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 218, "text": [ "SumSqDif (generic function with 1 method)" ] } ], "prompt_number": 218 }, { "cell_type": "code", "collapsed": false, "input": [ "SumSqDif(100)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 219, "text": [ "25164150" ] } ], "prompt_number": 219 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "10001st prime\n", "
\n", "Problem 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.\n", "\n", "What is the 10001st prime number?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "function Prime(n)\n", " x = 2\n", " i = 0\n", " answer = 0\n", " while i < n\n", " if isprime(x)\n", " answer = x\n", " i = i + 1\n", " end\n", " x = x + 1\n", " end\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "Prime (generic function with 1 method)" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "Prime(10001)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "104743" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Largest product in a series\n", "
\n", "Problem 8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the greatest product of five consecutive digits in the 1000-digit number.\n", "\n", "" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = \"7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 249, "text": [ "\"7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450\"" ] } ], "prompt_number": 249 }, { "cell_type": "code", "collapsed": false, "input": [ "function GrtstProdofFive(input_string)\n", " answer = 0\n", " for i = 1:length(input_string)-4\n", " product = input_string[i] - '0' # subtract '0' to convert char into int\n", " for k = 1:4\n", " product = product * (input_string[i+k] - '0')\n", " end\n", " if product > answer\n", " answer = product\n", " end\n", " end\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 318, "text": [ "GrtstProdofFive (generic function with 1 method)" ] } ], "prompt_number": 318 }, { "cell_type": "code", "collapsed": false, "input": [ "GrtstProdofFive(x)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 320, "text": [ "40824" ] } ], "prompt_number": 320 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Special Pythagorean triplet\n", "
\n", "Problem 9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,\n", "\n", "\n", "For example, 32 + 42 = 9 + 16 = 25 = 52.\n", "\n", "There exists exactly one Pythagorean triplet for which a + b + c = 1000.\n", "Find the product abc." ] }, { "cell_type": "code", "collapsed": false, "input": [ "function PythagTriple()\n", " answer = 0\n", " for a = 1:1000\n", " for b = a:1000\n", " c = sqrt(a^2 + b^2)\n", " if (a+b+c) == 1000\n", " answer = a*b*c\n", " return int(answer)\n", " end\n", " end\n", " end\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 361, "text": [ "PythagTriple (generic function with 1 method)" ] } ], "prompt_number": 361 }, { "cell_type": "code", "collapsed": false, "input": [ "PythagTriple()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 362, "text": [ "31875000" ] } ], "prompt_number": 362 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Summation of primes\n", "
\n", "Problem 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.\n", "\n", "Find the sum of all the primes below two million." ] }, { "cell_type": "code", "collapsed": false, "input": [ "function Prime2(n)\n", " x = 1\n", " answer = 2\n", " while x < n\n", " if isprime(x)\n", " answer += x\n", " end\n", " x = x + 2\n", " end\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 381, "text": [ "Prime2 (generic function with 1 method)" ] } ], "prompt_number": 381 }, { "cell_type": "code", "collapsed": false, "input": [ "Prime2(2000000)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 384, "text": [ "142913828922" ] } ], "prompt_number": 384 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Largest product in a grid\n", "
\n", "Problem 11" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the 20\u00d720 grid below, four numbers along a diagonal line have been marked in red.\n", "\n", "\n", "\n", "The product of these numbers is 26 \u00d7 63 \u00d7 78 \u00d7 14 = 1788696.\n", "\n", "What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20\u00d720 grid?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "My2DArray = [[08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n", "49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n", "81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\n", "52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\n", "22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\n", "24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\n", "32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\n", "67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\n", "24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\n", "21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\n", "78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\n", "16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\n", "86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\n", "19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\n", "04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\n", "88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\n", "04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\n", "20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\n", "20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\n", "01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48]]\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 84, "text": [ "20x20 Array{Int64,2}:\n", " 8 2 22 97 38 15 0 40 0 \u2026 5 7 78 52 12 50 77 91 8\n", " 49 49 99 40 17 81 18 57 60 40 98 43 69 48 4 56 62 0\n", " 81 49 31 73 55 79 14 29 93 67 53 88 30 3 49 13 36 65\n", " 52 70 95 23 4 60 11 42 69 56 1 32 56 71 37 2 36 91\n", " 22 31 16 71 51 67 63 89 41 54 22 40 40 28 66 33 13 80\n", " 24 47 32 60 99 3 45 2 44 \u2026 53 78 36 84 20 35 17 12 50\n", " 32 98 81 28 64 23 67 10 26 67 59 54 70 66 18 38 64 70\n", " 67 26 20 68 2 62 12 20 95 39 63 8 40 91 66 49 94 21\n", " 24 55 58 5 66 73 99 26 97 78 96 83 14 88 34 89 63 72\n", " 21 36 23 9 75 0 76 44 20 14 0 61 33 97 34 31 33 95\n", " 78 17 53 28 22 75 31 67 15 \u2026 80 4 62 16 14 9 53 56 92\n", " 16 39 5 42 96 35 31 47 55 24 0 17 54 24 36 29 85 57\n", " 86 56 0 48 35 71 89 7 5 37 44 60 21 58 51 54 17 58\n", " 19 80 81 68 5 94 47 69 28 13 86 52 17 77 4 89 55 40\n", " 4 52 8 83 97 35 99 16 7 32 16 26 26 79 33 27 98 66\n", " 88 36 68 87 57 62 20 72 3 \u2026 67 46 55 12 32 63 93 53 69\n", " 4 42 16 73 38 25 39 11 24 18 8 46 29 32 40 62 76 36\n", " 20 69 36 41 72 30 23 88 34 69 82 67 59 85 74 4 36 16\n", " 20 73 35 29 78 31 90 1 74 71 48 86 81 16 23 57 5 54\n", " 1 70 54 71 83 51 54 69 16 48 61 43 52 1 89 19 67 48" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "function FourProd(array_2d)\n", " enum = size(array_2d)\n", " transpose_array = array_2d'\n", " rotate_array = rotl90(array_2d)\n", " answer = 0\n", " for x = 1:enum[1]-4\n", " for y = 1:enum[2]-4\n", " product1 = array_2d[x,y]\n", " product2 = transpose_array[x,y]\n", " product3 = array_2d[x,y]\n", " product4 = rotate_array[x,y]\n", " for z = 1:3\n", " h = x + z\n", " v = y + z\n", " product1 *= array_2d[h,y] # search for up/down product\n", " product2 *= transpose_array[h,y] # search for left/right product\n", " product3 *= array_2d[h,v] # search for diagonal \\ product \n", " product4 *= rotate_array[h,v] # search for diagonal / product\n", " end\n", " max_product = max(product1, product2, product3, product4)\n", " if max_product > answer\n", " answer = max_product\n", " end\n", " end\n", " end\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 82, "text": [ "FourProd (generic function with 1 method)" ] } ], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "FourProd(My2DArray)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 85, "text": [ "70600674" ] } ], "prompt_number": 85 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Highly divisible triangular number\n", "
\n", "Problem 12" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:\n", "\n", "\n", "\n", "Let us list the factors of the first seven triangle numbers:\n", "\n", "We can see that 28 is the first triangle number to have over five divisors.\n", "\n", "What is the value of the first triangle number to have over five hundred divisors?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "function NumDivisors(n)\n", " answer = 0\n", " x = floor(sqrt(n))\n", " for i=1:x\n", " if n%i == 0\n", " answer += 2\n", " end\n", " end\n", " if n/x == x\n", " answer -= 1\n", " end\n", " return answer\n", "end\n", "\n", "function TriangleDivisors(n)\n", " count_div = 0\n", " counter = 1\n", " answer = 0\n", " while count_div < n\n", " answer += counter\n", " count_div = NumDivisors(answer)\n", " counter += 1\n", " end\n", " return answer\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 179, "text": [ "TriangleDivisors (generic function with 1 method)" ] } ], "prompt_number": 179 }, { "cell_type": "code", "collapsed": false, "input": [ "TriangleDivisors(500)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 182, "text": [ "76576500" ] } ], "prompt_number": 182 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Large sum\n", "
\n", "Problem 13" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.\n", "