fortran66のブログ

fortran について書きます。Amazonのアソシエイトとして収入を得ています。

【メモ帳】素数ゼータ関数

prime zeta function

とりあえず、エラトステネスのふるいで 109 までの素数を求めて、その逆数の冪和を二宮の組み手和(別名 Kahan's summation)で求めます。素数表はデータファイルとして取っておいて使うことにします。

 \displaystyle
\zeta_p(s) = \sum_{\rm{prime\,\, numbers}} {1\over p^s}

Sieve of Eratosthenes

実行結果

 number of primes =    50847534

プログラム

pack 使って filtering するとテンポラリ配列が大きくなるので heap array オプションをつける必要があります。

use Pre-trained Machine Learning Model というオプションが気になりますw 初代 FORTRAN の FREQUENCY 文を連想させます。

    program Sieve_of_Eratosthenes
        implicit none
        integer, parameter :: n = 10**9
        integer, allocatable :: iprimes(:)
        
eratos: block        
            import, only : n, iprimes
            integer, allocatable :: itable(:)
            integer :: j, k
            allocate(itable(n))
            forall(integer::i = 1:n) itable(i) = i
            itable(1) = 0
            do j = 1, int(n**0.5)
                if (itable(j) /= 0) itable(j**2::j) = 0
            end do
        
            iprimes = pack(itable, itable /= 0)
        end block eratos
        
        print *, 'number of primes =', size(iprimes)
        
  wr_primes: block
            import, only : iprimes
            integer :: iw
            open(newunit = iw, file = 'primes.dat', form = 'unformatted')
            write(iw) size(iprimes)
            write(iw) iprimes
            close(iw)
        end block wr_primes
    end program Sieve_of_Eratosthenes

Prime zeta

実行結果

s = 1 は理論的には発散しますが、109 以下の素数和では 3 程度にとどまります。ネット上の表の値と比較すると s = 2 は 10^-10 程度、その他は 15 桁程度の精度があるようです。組み手和の結果と単純和の結果を並べています。

           1   3.29275571879897        3.29275571880031
           2  0.452247419994942       0.452247419817970
           3  0.174762639299444       0.174762639299271
           4  7.699313976424685E-002  7.699313976424364E-002
           5  3.575501748392425E-002  3.575501748392401E-002
           6  1.707008685063651E-002  1.707008685063649E-002
           7  8.283832856133592E-003  8.283832856133586E-003
           8  4.061405366517831E-003  4.061405366517829E-003
           9  2.004467574962451E-003  2.004467574962450E-003
          10  9.936035744369802E-004  9.936035744369800E-004

en.wikipedia.org

プログラム

intel fortran ifx で計算しましたが、release mode で最適化すると組み手和のところが最適化されて崩れて期待通りの働きをせず精度が単純和よりも落ちたので最適化なしで計算しました。

    program prime_zeta
        implicit none
        integer, parameter :: kd = kind(1.0d0) !real64
        integer, allocatable :: iprimes(:)
        
 rd_primes: block
            integer :: ir, np
            open(newunit = ir, file = '..\202501\primes.dat', form = 'unformatted')
            read(ir), np
            allocate(iprimes(np))
            read(ir) iprimes
            close(ir)
        end block rd_primes
        
calc_prime_zeta: block
            integer :: i
            do i = 1, 10
                print *, i, sum_zeta(iprimes, i), sum((1.0/real(iprimes, kd))**i)
            end do
        end block calc_prime_zeta

    contains
        pure function sum_zeta(iarr, n) result(s)
            import, only : kd     
            integer, intent(in) :: iarr(:), n
            real(kd) :: x, y, r, s, t
            integer :: i
            s = 0.0_kd
            r = 0.0_kd
            do i = 1, size(iarr) ! Kumite-wa, Kahan's summation
                x = iarr(i)
                !
                y = 1.0_kd / x**n - r
                t = s + y
                r = (t - s) - y
                s = t
            end do    
        end function sum_zeta
    end program prime_zeta

追記: close 文を加えてみました。Fortran は JOB CARD の方で ファイルの指定をしたりするので OPEN 文がコンフリクトを起こさないようにわざと書かなかったり、複数回 OPEN 文によるファイル属性変更があったりするので(つまり必ずしも OPEN/CLOSE が対になって出てこない)、明示的な OPEN/CLOSE に消極的な派閥に属しているのですが、最近他言語からのマナー講師がうるさいのでうわべだけすり寄ることにしました。