{
"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",
"