131 lines
3.3 KiB
Plaintext
131 lines
3.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Euler Problem\n",
|
|
"\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?\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"My easiest guess is to multiply all prime numbers till the number."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def get_primes_smaller(number):\n",
|
|
" primes = []\n",
|
|
" prospects = [n for n in range(2, number)]\n",
|
|
" while prospects:\n",
|
|
" p = prospects[0]\n",
|
|
" prospects = [x for x in prospects if x % p != 0]\n",
|
|
" primes.append(p)\n",
|
|
" return primes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"210\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from operator import mul\n",
|
|
"from functools import reduce\n",
|
|
"\n",
|
|
"def get_number_which_is_divisible_by_all_numbers_from_one_to(n):\n",
|
|
" ps= get_primes_smaller(n + 1)\n",
|
|
" return reduce(mul, ps, 1)\n",
|
|
"\n",
|
|
"print(get_number_which_is_divisible_by_all_numbers_from_one_to(10))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"That obviously didn't work. The reason is that the same prime can occur multiple times in the factorization of a divisor. For example $2^{3} = 8$. We can always brute force of course. We do a smart brute force and only check multiples from the product of primes because this factor must be part of the solution."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"232792560\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def is_divisible_by_numbers_smaller_or_equal(number, maximum_number):\n",
|
|
" for n in range(2, maximum_number + 1):\n",
|
|
" if number % n != 0:\n",
|
|
" return False\n",
|
|
" return True\n",
|
|
"\n",
|
|
"def get_number_which_is_divisible_by_all_numbers_from_one_to(n):\n",
|
|
" ps = get_primes_smaller(n + 1)\n",
|
|
" factor = reduce(mul, ps, 1)\n",
|
|
" multiples_of_factor = factor\n",
|
|
" while True:\n",
|
|
" if is_divisible_by_numbers_smaller_or_equal(multiples_of_factor, n):\n",
|
|
" return multiples_of_factor\n",
|
|
" multiples_of_factor += factor\n",
|
|
"\n",
|
|
"assert(get_number_which_is_divisible_by_all_numbers_from_one_to(10) == 2520)\n",
|
|
"print(get_number_which_is_divisible_by_all_numbers_from_one_to(20))"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.5.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|