Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created October 3, 2016 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/8273db21d27682ab2882e753d277480d to your computer and use it in GitHub Desktop.
Save kurozumi/8273db21d27682ab2882e753d277480d to your computer and use it in GitHub Desktop.
【Python】エラトステネスの篩を使ってPythonで素数を探す
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def prime(n):\n",
" seq = list(range(2, n))\n",
" while len(seq) > 0:\n",
" prime = seq.pop(0)\n",
" yield prime\n",
" # 素数で割り切れない数字のリストを作成\n",
" seq = [i for i in seq if not i % prime == 0]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n"
]
}
],
"source": [
"print(list(prime(100)))"
]
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment