gfortran-10 インストールからの約数和 σ
非標準リポジトリからインストールします。
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gfortran-10
sudo add-apt-repository --remove ppa:ubuntu-toolchain-r/test
$gfortran-10 --version
GNU Fortran (Ubuntu 10-20200416-0ubuntu1~18.04) 10.0.1 20200416 (experimental) [master revision 3c3f12e2a76:dcee354ce56:44b326839d864fc10c459916abcc97f35a9ac3de]
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
おまけ
$ gfortran-9 --version
]GNU Fortran (Ubuntu 9.3.0-11ubuntu0~18.04.1) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gfortran-8 --version
GNU Fortran (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gfortran --version
GNU Fortran (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
割り算しないで求める約数和 σn
約数を求めるには剰余(mod)計算が必要な気がしますが、一気にたくさん求めるなら単なるループと和で求まります。基本的にはエラトステネスの篩と同じ原理で、表にバッテンを付ける代わりに数字の計算をします。
10000までの整数の約数和を求めたうえで、そのなかから完全数を選びだします。
program sigma_perfect
implicit none
integer, parameter :: nmax = 10000
integer :: i, m(nmax)=0
do i = 1, nmax
m(i::i)=m(i::i)+i**1
if (m(i) == 2*i) print *, i, m(i)
end do
end program sigma_perfect
$ gfortran-10 sigma.f90
$ ./a.out
6 12
28 56
496 992
8128 16256
約数の 0 乗(すなわち1)の和 σ0 は、約数の個数なのでそれが2個のものは素数ということになります。これによって 100 までの素数を求めます。
program sigma
implicit none
integer, parameter :: nmax = 100
integer :: i, m(nmax)=0
do i = 1, nmax
m(i::i)=m(i::i)+i**0
if (m(i)==2) print *, i
end do
end program sigma
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