输出斐波那契数列中的素数

output prime numbers in Fibonacci sequence

有人可以检查我的代码吗?我无法在循环中提取质数。如果用户输入 5,输出应该是:2 3 5 13 89 如果用户输入 7,输出应该是 2 3 5 13 89 233 1597 等等,

 countPrime= int(0)
 endNum= int(0)
 a0= int(0)
 a1= int(1)
 checkPrime= False
 valueN= int(input("Enter the value of N: \n"))

 while (countPrime < valueN): 
      endNum= a0 + a1
      a0= a1
      a1= endNum

      for i in range(2, endNum+1):                   # prime check 
           if (not endNum % i ==0 and endNum > 2):       #
                checkPrime= True                         #

      if(checkPrime==True):
          countPrime+=1   
          print(endNum, end=" ")

while 循环没有缩进。

如果是,它将只计算第 N 个斐波那契数。

并且在 for 循环中,如果数字不能被 some 数字整除,看起来你设置了 checkPrime= True,情况总是如此。

作为 ,您的 prime 支票没有达到预期的效果。你自己能注意到吗?使用您当前的代码可能会很困难。但是,如果您将单独的关注点提取到单独的函数中,则可以单独测试和验证它们:

import unittest

def is_prime(number):
    for divisor in range(2, number+1):
        if (not number % divisor == 0 and number > 2):
            return True
    return False

class TestIsPrime(unittest.TestCase):
    def test_0_isnt_prime(self):
        self.assertFalse(is_prime(0))

    def test_1_isnt_prime(self):
        self.assertFalse(is_prime(1))

    def test_2_is_prime(self):
        self.assertTrue(is_prime(2))

    def test_3_is_prime(self):
        self.assertTrue(is_prime(3))

    def test_4_isnt_prime(self):
        self.assertFalse(is_prime(4))

    def test_23_is_prime(self):
        self.assertTrue(is_prime(23))

    def test_25_isnt_prime(self):
        self.assertFalse(is_prime(25))

if __name__ == '__main__':
    unittest.main()

运行 这会给你:

...FF.F
======================================================================
FAIL: test_25_isnt_prime (__main__.TestIsPrime)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "fibprime.py", line 29, in test_25_isnt_prime
    self.assertFalse(is_prime(25))
AssertionError: True is not false

======================================================================
FAIL: test_2_is_prime (__main__.TestIsPrime)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "fibprime.py", line 17, in test_2_is_prime
    self.assertTrue(is_prime(2))
AssertionError: False is not true

======================================================================
FAIL: test_4_isnt_prime (__main__.TestIsPrime)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "fibprime.py", line 23, in test_4_isnt_prime
    self.assertFalse(is_prime(4))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 7 tests in 0.000s

FAILED (failures=3)

我很久以前忘记上传这个代码了

countPrime= int(0)
checkPrime=True     
endNum= int(0)      
prevNum= int(0)         
nextNum= int(1)         
valueN= int(input("Enter the value of N: \n"))   
print("\nThe Prime numbers are: ")

def checkIfPrime(endNum):              
     sumFact=0                         
     for i in range(1, endNum+1):       
          if(endNum % i == 0):         
               sumFact+=1             
     if(sumFact==2):                   
          return True
     else:
          return False
while (countPrime < valueN):
     endNum= prevNum + nextNum        
     prevNum= nextNum                
     nextNum= endNum                  
     checkPrime= checkIfPrime(endNum)     
     if(checkPrime==True):                
          countPrime+=1                   
          checkPrime= False               
          print(endNum," ", end=" ")          

以下代码使用递归,使用递归输出斐波那契数列:

sequence=[0,1]
def fibonacci(num,prev, after):
    if(len(sequence) !=num):       # base case
        nextNum = prev +after           # change of state
        sequence.append(nextNum)
        fibonacci(num,after,nextNum)     # function calls itself
    return sequence

print(fibonacci(10,0,1))  # test with 10 numbers

此技术使用“元组”数据类型:

def fibonacci(n):
 a,b = 1,1
 for i in range(n-1):
 a,b = b,a+b
 return a
print fibonacci(5)      # test with 5 numbers

使用 你可以这样做:

class Fibo:
 def __init__(self, fn):
  self.fn = fn
  self.memo = {}
 def __call__(self, arg):
  if arg not in self.memo:
   self.memo[arg] = self.fn(arg)
   return self.memo[arg]