ソース・プログラム
program PEuler3 implicit none integer, parameter :: ki = selected_int_kind(12) integer(ki), parameter :: nx = 600851475143 !13195 integer, allocatable :: iptab(:) iptab = ieratos( int(sqrt(real(nx))) ) ! prime numbers up to possible max factor !print '(a, *(i8))', 'factors ', pack(iptab, mask = mod(nx, iptab) == 0) print '(a, i8 )', 'max factor =', maxval(iptab, mask = mod(nx, iptab) == 0) stop contains function ieratos(n) ! thieve of Eratostenes integer, intent(in) :: n integer, allocatable :: ieratos(:) logical :: tab(n) integer :: i tab = .true. tab(1) = .false. do i = 2, int( sqrt(real(n)) ) if ( tab(i) ) tab(i * i:n:i) = .false. end do ieratos = pack( [ (i, i = 1, n) ], tab ) return end function ieratos end program PEuler3
実行結果
max factor = 6857
続行するには何かキーを押してください . . .