Skip to content

Instantly share code, notes, and snippets.

@mathiasose
Last active June 4, 2018 23:31
Show Gist options
  • Save mathiasose/da0e5f630cfe273d7642c77cd1c66397 to your computer and use it in GitHub Desktop.
Save mathiasose/da0e5f630cfe273d7642c77cd1c66397 to your computer and use it in GitHub Desktop.
euler5
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def divisible(x):\n",
" for i in range(1, 21):\n",
" if x % i != 0:\n",
" return False\n",
" return True\n",
"\n",
"def task(step):\n",
" i = 0\n",
" while True:\n",
" i += 1\n",
" n = step * i\n",
" if divisible(n):\n",
" return n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2min 9s, sys: 55.2 ms, total: 2min 9s\n",
"Wall time: 2min 9s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"assert task(step=1) == 232792560"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 7.07 s, sys: 4 ms, total: 7.07 s\n",
"Wall time: 7.07 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"assert task(step=20) == 232792560"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 0 ns, sys: 30 µs, total: 30 µs\n",
"Wall time: 34.8 µs\n"
]
}
],
"source": [
"%%time\n",
"\n",
"assert task(step=2*3*5*7*11*13*17*19) == 232792560"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 51 µs, sys: 0 ns, total: 51 µs\n",
"Wall time: 54.1 µs\n"
]
}
],
"source": [
"%%time\n",
"\n",
"# one-liner just for fun\n",
"\n",
"value = next(x for x in (n*2*3*5*7*11*13*17*19 for n in range(1, 100)) if all(x % d == 0 for d in range(2, 19)))\n",
"\n",
"assert value == 232792560"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment