Skip to content

Instantly share code, notes, and snippets.

@cure-honey
Last active August 7, 2017 14:06
Show Gist options
  • Save cure-honey/decf5be6d2d3a9bce9a0fa08e611ac09 to your computer and use it in GitHub Desktop.
Save cure-honey/decf5be6d2d3a9bce9a0fa08e611ac09 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# 夏休みFortran祭り\n## Jupyter-fortran-kernel\nhttps://github.com/ZedThree/jupyter-fortran-kernel\n\n修正点\ngit clone git@github.com:ZedThree/jupyter-fortran-kernel.git <- git clone https://github.com/ZedThree/jupyter-fortran-kernel.git\npip install -e --user jupyter-fortran-kernel <- pip install -e --user jupyter-fortran-kernel\n\njupyter-kernelspec install fortran_spec/ <- sudo jupyter-kernelspec install fortran_spec/\n\n時々2回 shift+ENTER しないと出力しません。謎\n\n## GitHub Gist の設定\nhttps://gist.github.com/genkuroki/81de23edcae631a995e19a2ecf946a4f\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## エラトステネスのふるいによる素数求め 1000以下"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "program eratos\n integer, parameter :: n = 10**3\n integer :: i, itab(n)\n integer, allocatable :: ip(:)\n itab = 0\n forall (i = 2:n) itab(i) = i\n do i = 2, int(sqrt(real(n)))\n if (itab(i) /= 0) itab(i**2::i) = 0\n end do\n ip = pack(itab, itab /= 0)\n print '(*(i5))', ip\n print *, 'number of primes', size(ip)\nend program eratos",
"execution_count": 11,
"outputs": [
{
"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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997\n number of primes 168\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## エラトステネスのふるいによる素数求め \n10^8 以下の素数の個数を求める。\n\nただし偶数を表から抜く。表を3から始める。[3,5,7,9,...10^8-1]\n"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "program eratos3\n integer(8), parameter :: ip = 8\n logical(1), allocatable :: itab(:) \n integer(8) :: i, j, np\n np = 10**ip / 2 - 1\n allocate(itab(np))\n forall(i = 1:np) itab(i) = .true.\n do i = 1, int( (sqrt(real(2*(np + 1))) ))\n if (itab(i)) forall(j = 2*i*(i+1):np:2*i+1) itab(j)= .false.\n end do\n print *, count(itab) + 1\nend program eratos3",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": " 5761455\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "fortran_spec",
"display_name": "Fortran",
"language": "Fortran"
},
"language_info": {
"name": "fortran",
"mimetype": "text/plain",
"file_extension": "f90"
},
"gist": {
"id": "decf5be6d2d3a9bce9a0fa08e611ac09",
"data": {
"description": "Sieve of Eratosthenes",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/decf5be6d2d3a9bce9a0fa08e611ac09"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment