永发信息网

1、python编程2/1+3/2+5/3+8/5+13/8+……前50项和

答案:3  悬赏:40  手机版
解决时间 2021-01-29 18:14
1、python编程2/1+3/2+5/3+8/5+13/8+……前50项和
最佳答案
a=1.0
b=2.0
sum=0.0
c=0.0
for i in range(0,50):
sum= sum+ b/a
c= a+b
a= b
b= c
print sum

我主要是写JAVA,也是刚开始学习python
全部回答

Well, 'cause I noticed that some of the number can't be divided exactly, 
so I thought keeping the result as  a fraction would be more accurate. 
But after done writing, I found it's not that valuable to use fraction, 
the number would get huge because there's not manycommon factors between the nominator and denominator.


My code is here, just for reference~

#2/1 + 3/2 + 5/3 + 8/5 + ... ...
#Some number can't be divided exactly. Use fraction to keep the result.
#
#get all factors of a number
def getFactors(num):
    facList = []
    while num % 2 == 0:
        facList.append(2)
        num /= 2
    limit = int(num**(0.5)+1)
    for i in range(3,limit+2, 2):
        if num % i == 0:
            facList.append(i)
            num /= i
    facList.append(num)
    return facList
    
# n is nominator, dList is the factor list of denominator.
def factor(n, d, dList):
    for i in dList:
        if n % i == 0:
            n /= i
            d /= i
            dList.remove(i)
    return n,d,dList
if __name__ == '__main__':
    d = 2  #denominator
    n = 3  #nominator
    
    #beginning number is 2/1
    dRes = 1   
    nRes = 2
    
    #recording denominator's factors.
    dFactorList = []
    
    for i in range(49):
        #print nRes , dRes, n,d,
        nRes = nRes*d+n*dRes
        dRes = dRes*d
        
        #record factor of d
        dFactorList.extend(getFactors(d))
        
        #Use common factor to shrink the recorded number (nRes/dRes)
        nRes, dRes, dFactorList = factor(nRes, dRes, dFactorList)
        
        t = d
        d = n
        n = t+n
    print nRes, '/', dRes , nRes*1.0/dRes
x=1.0;y=2.0;ret=0
for i in range(50):
    ret+=y/x
    t=y
    y+=x
    x=t
print ret
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
傍晌的意思是什么啊?知道的请说下!
要四十八个脚趾的全文,急急急急急急急急急急
一个番茄饭怎么做
饲料用油有标准吗?如题 谢谢了
宇龙数控仿真3.7在win7上成功安装并打开了软
两个人在一起了,彼此最在乎的到底是什么?是
卡西欧自拍神器tr500的内存是多少? 是机器自
形容大树被吹倒的成语
切磋琢磨的意思是什么啊?知道的请说下!
酸菜鱼黑鱼怎么去腥味
股权转让中介费应列入哪个会计科目
疯狂猜成语一个人在风雨中走 前面放着一把梳
德汇广通科贸有限公司地址好找么,我有些事要
急救:我现在需要诗经采薇的诗意:行道迟迟,
硬笔书法中悬针竖和垂露竖,对应相应的汉字写
推荐资讯
我想春秋穿,陪牛仔裤,哪个好
2019年王朝霞各地期末试卷精选四年级语文上册
恩吉拉国际早教中心(滁州全椒县)地址在什么地
佳康汗疗养生馆地址好找么,我有些事要过去
深圳银行汽车贷款,哪家银行可以办理,在哪里
荧晔的意思是什么啊?知道的请说下!
徐汇区田林二中全国第几?
长安星卡短兜小货车换个节气门多少钱
悲伤沼泽AOE鱼人
我去年3月29 在银行存款两年期,现在利率调了
中国电信(东方大学城)地址在哪,我要去那里办
池镜的意思是什么啊?知道的请说下!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?