作者FosterIX (Jack The Ripper)
看板NTUCHE-99-HW
標題Re: [計程] 諶玉真作業5-3
時間Wed Nov 1 08:17:27 2006
※ 引述《wen7774 (文...)》之銘言:
: 我..我...我.....寫不出來啦~~~~~~
: real:: e, a
: integer::n
: e = 1.
: n = 1
^^^^^^^^應該是說n不用作SET
: DO i = 1,n,1
: a = 1. / n
: if (a< 0.0005) exit
: e = e + a
: n = n + 1
: ENDDO
: 為什麼我跑出來的值都是1
: 我不知道怎麼改啦><"
: 救我!!
當n=1時 電腦就會帶入1
而且妳要做出階乘
program hw5_3
!use "do-exit"
integer :: i,k,m
real :: e,p,y
write(*,*) "This program is to calculate the base of the natural logarithms."
write(*,*) "The base of the natural logarithms may be represent by the series"
write(*,*) "e=1+1/1!+1/2!+1/3!+...+1/n!+..."
write(*,*) "The program will continue to calculate until the value of 1/n! becomes less than 0.0005"
p=1
i=0
e=1.
do k=1,m
m=m+1
^^^^^為了跑次數所多加的 免得會卡在SET
i=i+1
p=p*i
^^^^^階乘先做好
y=(1./p)
e=e+y
if (y<0.0005) exit
write(*,*) i,"!=", p ,"1/(",i,"!) =",y
enddo
write(*,*) "The final value of e is",e,"."
end program hw5_3
我的方法是先做好階乘再做累加....
--
因為沒有真正的活著,所以也沒有純然的死亡
【I have been dead already.】
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.230.169.151
推 ssnick:jack你的p值沒有回到1...不會出現跟我之前一樣的問題嗎? 11/01 09:59
推 FosterIX:我有SET p=1 呀 XD 如果沒有好像會很奇怪 11/01 13:46
推 wen7774:感恩~~~~~~~~~ 11/02 04:00