【问题标题】:smallest value greater than N in fibonacci series in pythonpython中斐波那契数列中大于N的最小值
【发布时间】:2021-05-30 03:14:35
【问题描述】:

我希望从其他练习网站解决这个问题。我是 python 新手。 将斐波那契数列视为 1,2,3,5,8,13,21,34,55,89... 我想打印值不超过 N(例如 N=10)的 (1) 斐波那契数列,求偶数项的总和。

(2)和大于N的最小值。

我已经写了以下代码:

N = 10
def fibonacci(n):
    if n in fibonacci_cashe: 
        return fibonacci_cashe[n]
    if n==1:
        value = 1
    elif n == 2:
        value = 1
    elif n>2:
        value = fibonacci(n-2)+fibonacci(n-1)

    fibonacci_cashe[n] = value
    return value 
for i in range(N):
    test = fibonacci(N)
    #I don't know what I should do here

例如,对于 N=10:

系列是1,2,3,5,8,13,21,34,55,89, 144, 144+89, ... 甚至小于(N=10)的是:2, 8, 34,它们的总和是 44。 大于(N=10)的最小的是:13

你能帮我解决这个问题吗?

非常感谢

【问题讨论】:

  • 1) 可以添加预期值吗? 2)你能格式化问题的数量吗?您似乎在问 3 个问题:打印值不超过 N 的 Fib 系列、偶数值之和以及大于 N 的最小值。我理解正确吗?
  • value = fibonacci(n-1)+fibonacci(n-1) 应该是 value = fibonacci(n-2)+fibonacci(n-1)。实际上,您只是将前一项加倍,而不是添加前两项。此外,“缓存”拼写为“缓存”,而不是“现金”。
  • @ianquah 感谢您的评论。我刚刚编辑了问题。
  • @Tomkarzes 感谢您的评论。我刚刚编辑了它。

标签: python


【解决方案1】:
def fibonacci(n):
   if n == 1 or n == 2:
        return 1
   return fibonacci(n - 1) + fibonacci(n - 2)


N = 10
result = []
total = 0
smallest = 1
for i in range(1, N + 1):
   term = fibonacci(i)
   if term < N:
        result.append(fibonacci(i))
        # also sum the even valued term
        if term % 2 == 0:
            total += term
    else:
        # term exceeded the limit N
        # so the term for which we exceeded the limit
        # is actually the smallest term greater than N
        # or you can say it is the first term which is greater than N
        smallest = term
        break
print(*result)
print('sum of even terms :', total)
print('smallest value greater than N:', smallest)

带记忆功能

def fibonacci(n):
if n == 1 or n == 2:
    fibonacci_cashe[n] = 1
if fibonacci_cashe[n] != 0:
    return fibonacci_cashe[n]

fibonacci_cashe[n] = fibonacci(n - 1) + fibonacci(n - 2)
return fibonacci_cashe[n]


N = 10
fibonacci_cashe = [0]*N # for memorization
result = []
total = 0
smallest = 1
for i in range(1, N + 1):
    term = fibonacci(i)
    if term < N:
        result.append(fibonacci(i))
        # also sum the even valued term
        if term % 2 == 0:
            total += term
    else:
        # term exceeded the limit N
        # so the term for which we exceeded the limit
        # is actually the smallest term greater than N
        # or you can say it is the first term which is greater than N
        smallest = term
        break
print(*result)
print('sum of even terms :', total)
print('smallest value greater than N:', smallest)

输出

1 1 2 3 5 8
sum of even terms : 10
smallest value greater than N: 13

【讨论】:

  • 你为什么要删除备忘录?对于较大的 n,这将运行得非常缓慢。
  • @TomKarzes 感谢您指出这一点。我认为这会给答案增加额外的复杂性。不过,我现在添加了一个带记忆的替代解决方案。
  • “memoization”中没有“r”。词根是“备忘录”,而不是“记忆”。你可以阅读它here
  • 哇,我不知道。英语不是我的第一语言。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 2013-08-03
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多