消息
×
loading..
首页 > 算法 > 正文

阅读排行

Fortran求大数阶乘
2014-03-10 15:42:10   来源:Fcode研讨团队   评论:2 点击:

本代码演示了大数求阶乘的实现算法

本代码演示了大数求阶乘的实现算法
01program www_fcode_cn
02    implicit none
03    integer(2) nLbound, nUbound
04    real t1,t2
05    call cpu_time(t1)
06    nLbound = 1
07    nUbound = 32767
08    call factorial( nLbound, nUbound )
09    call cpu_time(t2)
10    print*, 'CPU_Time:',t2-t1
11    pause
12 end program www_fcode_cn
13 
14 subroutine factorial( nLbound, nUbound )
15 Use, Intrinsic :: ISO_FORTRAN_ENV, only : OUTPUT_UNIT
16 implicit none
17 integer(2),intent(in) :: nLbound, nUbound
18 integer(8),parameter  :: pmax = 10**13, len = 10300
19 integer(8) ires(len), nL, nU
20 integer(8) i, j, m, n
21 character ch*24
22 
23 ires=0; ires(1)=1
24 nL = 1; nU = 1
25 do i = nLbound, nUbound
26     ires(nL:nU) = ires(nL:nU) * i
27     n = 0
28   do j = nL, nU
29     if( ires(j)/=0 ) then
30       m = ires(j) / pmax
31       ires(j+1) = ires(j+1) + m
32       ires(j) = ires(j) - pmax*m
33       n = 1
34     else if( n==0 ) then
35       nL = j
36     end if
37   end do
38   if( ires(nU+1) > 0 ) nU = nU + 1
39 end do
40 
41 ! 输出结果:n!
42 open(output_unit,file='1.txt') !输出到文件
43 
44 write(ch,*) ires(nU)
45 ch = adjustl(ch)
46 m = len_trim(ch)  !首元素中整数值的位数
47 n = (nU-1)*13+m-1 !指数
48 
49 !科学计数法
50 write(OUTPUT_UNIT,'(a)',advance='no') ch(1:1)//'.'//ch(2:m)//'e+'
51 write(ch,*) n
52 ch = adjustl(ch)
53 write(OUTPUT_UNIT,'(a)') trim(ch)
54 !全部数字
55 if( nU > 1 ) then
56   write(OUTPUT_UNIT,'(i<m>)',advance='no') ires(nU)
57   write(OUTPUT_UNIT,'(<nu-1>i13.13)') ires(nU-1:1:-1)
58 else
59   write(OUTPUT_UNIT,'(i<m>)') ires(nU)
60 end if
61 close(OUTPUT_UNIT)
62 end subroutine factorial

相关热词搜索:大数阶乘

上一篇:第一页
下一篇:宋叶志书 - 选主元消去法之我见

分享到:           收藏