{ "metadata": { "name": "", "signature": "sha256:8bf64f63993eee0047fe7434a25083c26784d486198f3c8828cad4e52caa044b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\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.\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 0\n", "threeTotal = 0\n", "fiveTotal = 0\n", "threeMultiple = 0\n", "fiveMultiple = 0\n", "total = 0\n", "\n", "while (threeTotal < 10):\n", " threeMultiple += 3\n", " threeTotal += threeMultiple\n", " \n", " \n", "while (fiveTotal < 10):\n", " fiveMultiple += 5\n", " fiveTotal += fiveMultiple\n", " \n", "fiveTotal + threeTotal\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "33" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "total = 0\n", "threeTotal = 0\n", "threeMultiple = 0\n", "fiveTotal = 0\n", "fiveMultiple = 0\n", "\n", "for i in range(10):\n", " print i\n", " if i%3 == 0:\n", " print \"three\", i\n", " total += i\n", " if i%5 == 0:\n", " print \"five\", i\n", " total += i\n", " if i%3 == 0 and i%5 == 0:\n", " print \"both\", i\n", " total -= i\n", " \n", "print total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "three 0\n", "five 0\n", "both 0\n", "1\n", "2\n", "3\n", "three 3\n", "4\n", "5\n", "five 5\n", "6\n", "three 6\n", "7\n", "8\n", "9\n", "three 9\n", "23\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "total = 0\n", "\n", "for i in range(10):\n", " if i%3 == 0 or i%5 == 0:\n", " print i\n", " total += i\n", " \n", "print total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "3\n", "5\n", "6\n", "9\n", "23\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "sum([ i for i in range(1000) if i%3 == 0 or i%5 == 0 ])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "233168" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }