输出Fibonacci序列中的素数

时间:2015-04-11 22:27:55

标签: python initialization boolean primes fibonacci

有人可以查看我的代码。我在循环中提取素数时遇到问题。如果用户输入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=" ")

6 个答案:

答案 0 :(得分:4)

while循环没有缩进。

如果是,它只计算第N个Fibonacci数。

在for循环中,看起来你设置了checkPrime = True,如果这个数字是某些号不可分割的话,情况总是如此。

答案 1 :(得分: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=" ")          

答案 2 :(得分:3)

以下代码使用递归来使用递归输出Fibonacci序列:

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

答案 3 :(得分:3)

此技术使用&#34; 元组&#34;数据类型:

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

答案 4 :(得分:3)

使用可以执行此操作:

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]

答案 5 :(得分:0)

作为Bjorn Munch noticed,您的主要检查并没有做到它应该做的事情。你能注意到自己吗?使用当前可能很难的代码。但是,如果您将单独的问题分解为单独的功能,则可以对它们进行单独测试和验证:

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)