-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.py
More file actions
54 lines (48 loc) · 1.62 KB
/
Copy pathFibonacci.py
File metadata and controls
54 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Prompts the user for a certain number of fibonacci numbers
# Outputs that number of fibonacci numbers and identifies the numbers that are prime
# Gives the user the runtime of the program.
import time
def fibonacciSequence(lengthOfSequence):
baseCase = [1,1]
recursive = []
if lengthOfSequence <= 0:
return None
elif lengthOfSequence == 1:
return [1]
elif lengthOfSequence == 2:
return [1, 1]
else:
recursive = [1,1]
for j in range(2, lengthOfSequence):
j = recursive[j-1] + recursive[j-2]
recursive.append(j)
return recursive
def isPrime(input):
if input <= 0:
return False
array1 = range(1, input + 1)
divisors = [i for i in array1 if input % i == 0]
if len(divisors) == 2 and divisors[0] == 1 and divisors[1] == input:
return True
else:
return False
def listPrimes(fibList):
newList = []
for i in fibList:
if isPrime(i):
newList.append(i)
return newList
length = -1
while length < 0 or type(length) is not int:
length = int(input("Enter your desired number of fibonacci numbers.\n"))
# Start timer
start = time.time()
arrayFib = fibonacciSequence(length)
listPrimeAndFib = listPrimes(arrayFib)
end = time.time()
print("The list of the first {} fibonacci number(s) is {}".format(length, arrayFib))
print("Of those fibonacci numbers the following are prime {}".format(listPrimeAndFib))
# End timer and print result
print("Your prgrams run time period was: {}".format(end - start))
#Displaying a goodbye message
print("Thank you for using the Fibonnaci numbers generator")