Instantly share code, notes, and snippets.

# komasaru/prime_numbers.f95

Created April 20, 2017 05:04
Fortran code to compute prime numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 !**************************************************** ! 素数一覧 ! : 入力値以下の素数を全て出力する ! ! date name version ! 2017.04.20 mk-mode.com 1.00 新規作成 ! ! Copyright(C) 2017 mk-mode.com All Rights Reserved. !**************************************************** ! program prime_numbers implicit none integer i, j, n logical is_prime print *, "自然数を入力してください N:" read *, n do i = 2, n is_prime = .true. do j = 2, int(sqrt(dble(i))) if (mod(i,j) == 0) then is_prime = .false. ! 割り切れるので素数ではない exit end if end do if (is_prime) print *, i ! もし素数ならば出力 end do end program

### EdThePro101 commented Sep 16, 2020

This is awesome! Thank you!

### taymazalizadeh commented Feb 22, 2022 • edited

What change do we have to make in the code if we want to distinguish prime numbers from the numbers we enter?

program prime number
implicit none
integer::n,s,i
s=0
do i=1,(n/2)+1
if (mod(n,i)==0)then
s=s+i
end if
end do
if (s==n)then
print*,"yes"
else
print*,"no"
end if
end program prime number

### komasaru commented Feb 23, 2022

I'm sorry.
I'm not sure what you want to do with this code.
(Is this the code to determine if it is a prime number?)

yes

### komasaru commented Feb 27, 2022

If you ask me what you should do,
I'm very sorry, but I can only say that you should refer to this 'prime_numbers.f95' code.