{ "metadata": { "language": "fsharp", "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Amicable numbers\n", "####Problem 21\n", "Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).\n", "If d(a) = b and d(b) = a, where a \u2260 b, then a and b are an amicable pair and each of a and b are called amicable numbers.\n", "\n", "For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.\n", "\n", "Evaluate the sum of all the amicable numbers under 10000." ] }, { "cell_type": "code", "collapsed": false, "input": [ "let d n =\n", " ({ 2.. int(sqrt(float n))}\n", " |> Seq.filter (fun x-> n%x=0)\n", " |> Seq.map (fun x -> if x*x = n then x else n/x + x)\n", " |> Seq.sum\n", " ) + 1\n", "\n", "let isAmicable a = d a |> fun b -> a = d b && a<>b\n", " \n", "{1 .. 10000}\n", " |> Seq.filter isAmicable\n", " |> Seq.sum" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "val it : int = 31626" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Names scores\n", "####Problem 22\n", "Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.\n", "\n", "For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 \u00d7 53 = 49714.\n", "\n", "What is the total of all the name scores in the file?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "open System.IO\n", "open System.Text.RegularExpressions\n", "\n", "let file = __SOURCE_DIRECTORY__ + \"/txt/p022.txt\"\n", "\n", "let score (s:string) =\n", " s.ToCharArray()\n", " |> Array.sumBy (fun c -> int c - 64)\n", "\n", "let text = File.ReadAllText(file)\n", "\n", "[for w in Regex.Matches(text, \"[A-Z]+\") -> w.Value]\n", "|> List.sort\n", "|> List.mapi (fun i name -> (score name)*(i+1))\n", "|> List.sum" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "val it : int = 871198282" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Non-abundant sums\n", "####Problem 23\n", "A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be $1 + 2 + 4 + 7 + 14 = 28$, which means that 28 is a perfect number.\n", "\n", "A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.\n", "\n", "As 12 is the smallest abundant number, $1 + 2 + 3 + 4 + 6 = 16$, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.\n", "\n", "Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers." ] }, { "cell_type": "code", "collapsed": false, "input": [ "open System.Collections.Generic\n", "\n", "let divisorSum n = {1 .. n/2} |> Seq.filter (fun k -> n%k=0) |> Seq.sum\n", "\n", "let abundants = \n", " [|1 .. 28123|] \n", " |> Array.filter (fun n -> n < divisorSum n)\n", "\n", "let ns = ResizeArray [1L..28123L]\n", "\n", "for i = 0 to abundants.Length-1 do\n", " for j = 0 to i do\n", " ns.Remove(int64(abundants.[i]+abundants.[j])) |> ignore\n", "\n", "ns |> Seq.sum" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "val it : int64 = 4179871L" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Lexicographic permutations\n", "####Problem 24\n", "A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:\n", "\n", "012 021 102 120 201 210\n", "\n", "What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "let factorial n = {1..n} |> Seq.reduce ( * )\n", "let numbers = ResizeArray [0..9]\n", "let rec loop remainder acc = \n", " match numbers.Count - 1 with\n", " |0 -> acc + string (numbers.[0])\n", " |k -> let next = numbers |> Seq.nth (remainder / factorial k)\n", " numbers.Remove(next) |> ignore\n", " loop (remainder%(factorial k)) (acc + string next)\n", "loop 999999 \"\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "val it : string = \"2783915460\"" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###1000-digit Fibonacci number\n", "####Problem 25\n", "The Fibonacci sequence is defined by the recurrence relation:\n", "\n", "$F_n = F_{n\u22121} + F_{n\u22122}$, where $F_1 = 1$ and $F_2 = 1$.\n", "Hence the first 12 terms will be:\n", "\n", "$F_1 = 1$ \n", "$F_2 = 1$ \n", "$F_3 = 2$ \n", "$F_4 = 3$ \n", "$F_5 = 5$ \n", "$F_6 = 8$ \n", "$F_7 = 13$ \n", "$F_8 = 21$ \n", "$F_9 = 34$ \n", "$F_{10} = 55$ \n", "$F_{11} = 89$ \n", "$F_{12} = 144$ \n", "The 12th term, $F_{12}$, is the first term to contain three digits. \n", "\n", "What is the first term in the Fibonacci sequence to contain 1000 digits?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "let fibs = Seq.unfold(fun (a,b) -> Some(a,(b,a+b))) (1I,1I)\n", "fibs |> Seq.findIndex (fun n -> n >= pown 10I 999)\n", " |> (+) 1" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "val it : int = 4782" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Reciprocal cycles\n", "####Problem 26\n", "A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: \n", "\n", "1/2\t= \t0.5 \n", "1/3\t= \t0.(3) \n", "1/4\t= \t0.25 \n", "1/5\t= \t0.2 \n", "1/6\t= \t0.1(6) \n", "1/7\t= \t0.(142857) \n", "1/8\t= \t0.125 \n", "1/9\t= \t0.(1) \n", "1/10\t= \t0.1 \n", "Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. \n", "\n", "Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part." ] }, { "cell_type": "code", "collapsed": false, "input": [ "let rec cycle denom =\n", " match denom with\n", " |2|5 -> 0\n", " |n when n%2=0 -> cycle (n/2)\n", " |n when n%5=0 -> cycle (n/5)\n", " |n ->\n", " Seq.initInfinite id\n", " |> Seq.skip 1\n", " |> Seq.map (fun x -> pown 10I x - 1I)\n", " |> Seq.findIndex(fun x -> x % (bigint n) = 0I)\n", " |> (+) 1\n", "\n", "[1..1000] |> List.maxBy cycle" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "val it : int = 983" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Quadratic primes\n", "####Problem 27\n", "Euler discovered the remarkable quadratic formula:\n", "\n", "$n^2 + n + 41$ \n", "\n", "It turns out that the formula will produce $40$ primes for the consecutive values $n = 0$ to $39$. However, when $n = 40$, $40^2 + 40 + 41 = 40(40 + 1) + 41$ is divisible by $41$, and certainly when $n = 41$, $41^2 + 41 + 41$ is clearly divisible by $41$.\n", "\n", "The incredible formula $n^2 \u2212 79n + 1601$ was discovered, which produces $80$ primes for the consecutive values $n = 0$ to $79$. The product of the coefficients, $\u221279$ and $1601$, is $\u2212126479$.\n", "\n", "Considering quadratics of the form:\n", "\n", "$n^2 + an + b$, where $|a| < 1000$ and $|b| < 1000$\n", "\n", "where $|n|$ is the modulus/absolute value of $n$\n", "e.g. $|11| = 11$ and $|\u22124| = 4$\n", "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$." ] }, { "cell_type": "code", "collapsed": false, "input": [ "let isPrime = function\n", " |n when n < 0 -> false\n", " |n -> {2 .. int(sqrt(float n))} |> Seq.exists (fun x -> n%x=0) |> not\n", "\n", "let primesGenerated (a,b) =\n", " Seq.initInfinite id\n", " |> Seq.map(fun n -> n*n + a*n + b)\n", " |> Seq.takeWhile isPrime\n", " |> Seq.length\n", " \n", "seq{for a in -999..999 do for b in -999..999 -> (a,b)}\n", "|> Seq.maxBy primesGenerated\n", "|> fun (a,b) -> a*b" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "val it : int = -59231" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Number spiral diagonals\n", "####Problem 28\n", "Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:\n", "\n", "21 22 23 24 25 \n", "20 07 08 09 10 \n", "19 06 01 02 11 \n", "18 05 04 03 12 \n", "17 16 15 14 13 \n", "\n", "It can be verified that the sum of the numbers on the diagonals is 101. \n", "\n", "What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "let n = 1001\n", "\n", "let rec collect depth start acc = \n", " if depth > n / 2 then acc\n", " else collect (depth + 1) (start + 8 * depth) (acc + 4 * start + 20 * depth)\n", "collect 1 1 1" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "val it : int = 669171001" ] } ], "prompt_number": 34 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Distinct powers\n", "####Problem 29\n", "Consider all integer combinations of $a^b$ for $2 \u2264 a \u2264 5$ and $2 \u2264 b \u2264 5$:\n", "\n", "$2^2=4$, $2^3=8$, $2^4=16$, $2^5=32$ \n", "$3^2=9$, $3^3=27$, $3^4=81$, $3^5=243$ \n", "$4^2=16$, $4^3=64$, $4^4=256$, $4^5=1024$ \n", "$5^2=25$, $5^3=125$, $5^4=625$, $5^5=3125$ \n", "\n", "If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:\n", "\n", "$4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125$\n", "\n", "How many distinct terms are in the sequence generated by $a^b$ for $2 \u2264 a \u2264 100$ and $2 \u2264 b \u2264 100$?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "set [ for a in 2I..100I do for b in 2..100 -> pown a b ]\n", "|> Set.count" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 37, "text": [ "val it : int = 9183" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Digit fifth powers\n", "####Problem 30\n", "Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:\n", "\n", "$1634 = 1^4 + 6^4 + 3^4 + 4^4$ \n", "$8208 = 8^4 + 2^4 + 0^4 + 8^4$ \n", "$9474 = 9^4 + 4^4 + 7^4 + 4^4$ \n", "As $1 = 1^4$ is not a sum it is not included. \n", "\n", "The sum of these numbers is $1634 + 8208 + 9474 = 19316$. \n", "\n", "Find the sum of all the numbers that can be written as the sum of fifth powers of their digits." ] }, { "cell_type": "code", "collapsed": false, "input": [ "let isSum n =\n", " (n |> string\n", " |> List.ofSeq\n", " |> Seq.map (fun x -> pown (int <| string x) 5)\n", " |> Seq.sum\n", " ) = n \n", " \n", "let maxN =\n", " ((Seq.unfold (fun x -> Some(x, (x+1))) 1\n", " |> Seq.find (fun x -> (pown 10 x) - 1 > x * (pown 9 5))\n", " ) - 1) * (pown 9 5)\n", " \n", "{2 .. maxN}\n", " |> Seq.filter isSum\n", " |> Seq.sum" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "val it : int = 443839" ] } ], "prompt_number": 38 } ], "metadata": {} } ] }