Last active
August 29, 2015 14:10
-
-
Save uggds/87edb74aaa3ac8711cf5 to your computer and use it in GitHub Desktop.
Collatz問題
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
module m_collatz | |
implicit none | |
contains | |
recursive function icollatz(n) result(result) | |
integer, intent(in) :: n | |
integer, allocatable :: result(:) | |
if (n == 1) then | |
result = [1] | |
else if (mod(n, 2) == 0) then ! even | |
result = [n, icollatz(n / 2)] | |
else ! odd | |
result = [n, icollatz(3 * n + 1)] | |
end if | |
return | |
end function icollatz | |
end module m_collatz | |
!=========================== | |
program main | |
use m_collatz | |
implicit none | |
integer :: i, target, maxStep | |
integer, allocatable :: m(:) | |
integer t1, t2, t_rate, t_max, diff | |
call system_clock(t1) ! 開始時を記録 | |
target = 1 | |
maxStep = 1 | |
!$omp parallel shared(maxStep), private(m) | |
!$omp do | |
do i = 1, 10**5 | |
m = icollatz(i) | |
if (maxStep < size(m)) then | |
maxStep = max(maxStep, size(m)) | |
target = i | |
end if | |
end do | |
!$omp end do | |
!$omp end parallel | |
print "(2i10, ' step')", target, maxStep | |
call system_clock(t2, t_rate, t_max) ! 終了時を記録 | |
if ( t2 < t1 ) then | |
diff = t_max - t1 + t2 | |
else | |
diff = t2 - t1 | |
endif | |
print "(a, f10.3)", "time it took was:", diff/dble(t_rate) | |
stop | |
end program main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
time it took was: 3.073