【问题标题】:While loop and Fibonacci in Python (without importing or using "fib")Python中的while循环和斐波那契(不导入或使用“fib”)
【发布时间】:2014-01-12 11:32:42
【问题描述】:

我正在尝试使用变量来查找所有斐波那契,并且如果可以被所选变量整除,则能够将它们相加。我也想尝试在不使用任何 fib() 或 memoization 的情况下编写它。

这是我的代码:

endNum = int(raw_input("Enter the end number here "))
divisable_by = int(raw_input("Sum all numbers in the sequence that divide by: "))   

# below is the same as "a, b = 0, 1"
a = 0
b = 1
""" gave single letter variables to those above to use in formula below
(longer strings do not seem to work)"""
c = endNum
d = divisable_by

# while b is less than or equal to the ending number, it will loop.
while b <= c:
    print b
# below is the same as "a, b = b, a+b"
    a_old = a
    a = b
    b = a_old + b
# the below helps with summing the numbers that are divisable by number chosen.
    total = 0
    for i in range(a, c):
        if i%d == 0:
            total += i
#prints text and number calculated above.
print "Sum of all natural numbers that divide by the number you chose" 
print "and are less than the other number you chose is: ", total

我能够运行代码,但我得到了错误的答案。例如,如果我运行代码将所有可被 3 整除的斐波那契数加到 21,我得到答案“0”。答案应该是“24 = (3 + 21)”。

任何关于简单修改的​​帮助都会很棒!

【问题讨论】:

    标签: python while-loop fibonacci


    【解决方案1】:

    生成器使用的内存少于综合列表。

    def fibNum(n):
        a,b = 1,1
        while b < n:
            yield b
            b,a = a+b,b
    n = int(raw_input("Enter the end number here "))
    d = int(raw_input("Sum all numbers in the sequence that divide by: ")) 
    total = sum(i for i in fibNum(n) if i%d == 0)
    print ("Sum of all natural numbers that divide by the number you chose") 
    print ("and are less than the other number you chose is: %d" %total)
    

    【讨论】:

      【解决方案2】:

      您需要先将斐波那契数添加到列表中,以便稍后在进行除法检查时使用它们。其次,您需要将总数从循环中取出,因为正如kgiannakakis所说,它总是会被0替换。所以它应该是这样的:

      endNum = int(raw_input("Enter the end number here "))
      divisable_by = int(raw_input("Sum all numbers in the sequence that divide by: "))   
      
      # below is the same as "a, b = 0, 1"
      a = 0
      b = 1
      """ gave single letter variables to those above to use in formula below
      (longer strings do not seem to work)"""
      c = endNum
      d = divisable_by
      
      fib_list = []
      # while b is less than or equal to the ending number, it will loop.
      while b <= c:
          print b
          fib_list.append(b)
      # below is the same as "a, b = b, a+b"
          a_old = a
          a = b
          b = a_old + b
      # the below helps with summing the numbers that are divisable by number chosen.
      total = 0
      for i in fib_list:
        if i%d == 0:
          total += i
      #prints text and number calculated above.
      print "Sum of all natural numbers that divide by the number you chose" 
      print "and are less than the other number you chose is: ", total
      

      此代码还会将您提供的数字视为最后一个数字,例如,如果它是 21 和 3,那么总数将为 24(21 和 3 可被 3 整除),如您所愿。但是,这不是您的印刷品所说的“并且小于您选择的其他数字”...因此您应该考虑将其更改为“小于或等于”

      【讨论】:

      • 这很好用。很高兴看到一个简单的添加让它工作!
      【解决方案3】:

      您在每个循环中将总计重置为 0。将此移出循环。也不要在每个循环中测试范围。这也会给你错误的结果。只测试找到的新号码。

      【讨论】:

        猜你喜欢
        • 2016-04-28
        • 1970-01-01
        • 1970-01-01
        • 2022-12-06
        • 1970-01-01
        • 2013-10-19
        • 2015-04-30
        • 2016-01-02
        • 1970-01-01
        相关资源
        最近更新 更多