Last active
          August 29, 2015 14:00 
        
      - 
      
- 
        Save mndrake/11407162 to your computer and use it in GitHub Desktop. 
    Example IFSharp notebook that intellisense does not work.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "metadata": { | |
| "language": "fsharp", | |
| "name": "" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Multiples of 3 and 5\n", | |
| "####Problem 1 \n", | |
| "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": [ | |
| "[1..999] |> List.filter(fun x -> x%3=0||x%5=0) |> List.sum" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 2, | |
| "text": [ | |
| "val it : int * float = (233168, 0.0016092)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 2 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Even Fibonacci numbers\n", | |
| "####Problem 2 \n", | |
| "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": [ | |
| "let fibs = (1,2) |> Seq.unfold (fun (a,b) -> Some(a,(b,b+a)))\n", | |
| "fibs |> Seq.takeWhile (fun n -> n < 4000000)\n", | |
| " |> Seq.filter (fun n -> n%2=0)\n", | |
| " |> Seq.sum" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 3, | |
| "text": [ | |
| "val it : int * float = (4613732, 0.0100563)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 3 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Largest prime factor\n", | |
| "####Problem 3\n", | |
| "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": [ | |
| "let rec factor n = \n", | |
| " {2L .. int64(sqrt(float n))} \n", | |
| " |> Seq.tryFind (fun k -> n%k=0L)\n", | |
| " |> function\n", | |
| " |Some k -> k :: factor (n/k)\n", | |
| " |None -> [n]\n", | |
| "\n", | |
| "factor 600851475143L |> List.max" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 2, | |
| "text": [ | |
| "val it : int64 = 6857L" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 2 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Largest palindrome product\n", | |
| "####Problem 4\n", | |
| "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": [ | |
| "let isPalindrome n = \n", | |
| " let s = (string n).ToCharArray() in Array.rev s = s\n", | |
| "\n", | |
| "// assuming the range of answers is 900 to 999\n", | |
| "\n", | |
| "seq [ for a in 900 .. 999 do\n", | |
| " for b in 900 .. a do\n", | |
| " if isPalindrome(a*b) then\n", | |
| " yield a*b ]\n", | |
| "|> Seq.max" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 1, | |
| "text": [ | |
| "val it : int = 906609" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 1 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Smallest multiple\n", | |
| "####Problem 5\n", | |
| "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": [ | |
| "/// greatest common divisor\n", | |
| "let rec gcd a b = if b = 0L then a else gcd b (a%b)\n", | |
| "/// least common multiple\n", | |
| "let lcm a b = a * b / gcd a b\n", | |
| "\n", | |
| "[1L .. 20L] |> List.reduce lcm" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 4, | |
| "text": [ | |
| "val it : int64 = 232792560L" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 4 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Sum square difference\n", | |
| "####Problem 6\n", | |
| "The sum of the squares of the first ten natural numbers is,\n", | |
| "\n", | |
| "$1^2 + 2^2 + ... + 10^2 = 385$ \n", | |
| "The square of the sum of the first ten natural numbers is,\n", | |
| "\n", | |
| "$(1 + 2 + ... + 10)^2 = 55^2 = 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": [ | |
| "let sqr x = x*x\n", | |
| "([1..100] |> List.sum |> sqr) - ([1..100] |> List.sumBy sqr)" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 55, | |
| "text": [ | |
| "val it : int = 25164150" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 55 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###10001st prime\n", | |
| "####Problem 7\n", | |
| "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 10 001st prime number?" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "let isPrime n =\n", | |
| " let isqrt x = int(sqrt(float x))\n", | |
| " {2 .. isqrt n} |> Seq.exists (fun i -> n%i=0) |> not\n", | |
| "\n", | |
| "// assuming that the 10001st prime is less than 1M\n", | |
| "{2 .. 1000000}\n", | |
| "|> Seq.filter isPrime\n", | |
| "|> Seq.nth 10000 // F# indices start at 0" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 18, | |
| "text": [ | |
| "val it : int = 104743" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 18 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Largest product in a series\n", | |
| "####Problem 8\n", | |
| "Find the greatest product of five consecutive digits in the 1000-digit number." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "open System.IO\n", | |
| "\n", | |
| "let file = __SOURCE_DIRECTORY__ + \"/txt/p008.txt\"\n", | |
| "\n", | |
| "File.ReadAllLines(file)\n", | |
| "|> Array.reduce (+)\n", | |
| "|> fun n -> n.ToCharArray()\n", | |
| "|> Seq.windowed 5\n", | |
| "|> Seq.map (fun s -> s |> Array.map(string >> int)\n", | |
| " |> Array.reduce ( * ))\n", | |
| "|> Seq.max" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 12, | |
| "text": [ | |
| "val it : int = 40824" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 12 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Special Pythagorean triplet\n", | |
| "####Problem 9\n", | |
| "A Pythagorean triplet is a set of three natural numbers, $a < b < c$, for which,\n", | |
| "\n", | |
| "$a^2 + b^2 = c^2$ \n", | |
| "For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$.\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": [ | |
| "let isTriplet a b c = a*a+b*b=c*c\n", | |
| "seq [ for a = 1 to 1000 do\n", | |
| " for b = 1 to a do\n", | |
| " if isTriplet a b (1000-a-b) then\n", | |
| " yield a*b*(1000-a-b) ]\n", | |
| "|> Seq.head" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 115, | |
| "text": [ | |
| "val it : int = 31875000" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 115 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "slide" | |
| } | |
| }, | |
| "source": [ | |
| "###Summation of primes\n", | |
| "###Problem 10\n", | |
| "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": [ | |
| "// prime sieve that generates primes under a given number\n", | |
| "let getPrimes n = \n", | |
| " let p = Array.create (n+1) true\n", | |
| " for i = 2 to int(sqrt(float n)) do\n", | |
| " if p.[i] then\n", | |
| " for k = 2 to n/i do\n", | |
| " p.[k*i] <- false\n", | |
| " [ for i = 2 to n do\n", | |
| " if p.[i] then yield i ]\n", | |
| "\n", | |
| "getPrimes 2000000\n", | |
| "|> List.sumBy int64" | |
| ], | |
| "language": "python", | |
| "metadata": { | |
| "slideshow": { | |
| "slide_type": "fragment" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "metadata": {}, | |
| "output_type": "pyout", | |
| "prompt_number": 1, | |
| "text": [ | |
| "val it : int64 = 142913828922L" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 1 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "// intellisense does not work here\n", | |
| "open System" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [] | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment