Mercurial > coderloop
view src/collatz.f @ 0:307a81e46071 tip
initial committ
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 01:17:49 -0700 |
parents | |
children |
line wrap: on
line source
1 program collatz2 implicit none3 character (len = 100) :: file_name4 integer, parameter :: big = selected_int_kind(11)5 integer :: n6 call getarg( 1, file_name )7 OPEN(unit = 7, file = file_name)8 read (7,*) n9 write (unit = *, fmt = "(I0)") search_collatz(n)11 contains13 function collatz_next (n) result (r)14 implicit none15 integer (kind = big) :: r,n16 if (0 == mod(n,2)) then17 r = n/218 else19 r = 1 + 3*n20 end if21 end function collatz_next23 function collatz_length (n) result (r)24 implicit none25 integer (kind = big) :: t26 integer :: n,r27 r = 128 t = n29 do30 if (1 == t) then31 exit32 end if33 r = r + 134 t = collatz_next(t)35 end do36 end function collatz_length38 function search_collatz (n) result (r)39 implicit none40 integer n,r,i,max,max_val,temp_val41 integer (kind = big) :: t42 t = n43 do i = 1,n-144 temp_val = collatz_length(i)45 if (temp_val > max_val) then46 max = i47 max_val = temp_val48 end if49 end do50 r = max51 end function search_collatz52 end program collatz