Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function 1 - Find Fibonacci numbers given the number of terms
- def fibonacciTerms(nTerms):
- if nTerms <= 0 or nTerms != int(nTerms):
- print("Error while using function '" + fibonacciTerms.__name__ + "'")
- return -1000
- if nTerms == 1:
- return [1]
- elif nTerms == 2:
- return [1, 2]
- else:
- terms = [0 for i in range(nTerms)]
- terms[0] = 1
- terms[1] = 2
- for i in range(2, len(terms)):
- terms[i] = terms[i-1] + terms[i-2]
- terms2 = [str(terms[i]) for i in range(len(terms))]
- terms2 = ", ".join(terms2)
- print("First " + str(nTerms) + " Fibonacci terms are: " + terms2)
- return terms
- # Function 2 - Find the primes between 1 and a limit
- def findPrimes(limit):
- primes = [2, 3]
- for n in range(4, limit+1):
- isPrime = True
- for prime in primes:
- if n % prime == 0:
- isPrime = False
- break
- if isPrime:
- primes.append(n)
- return primes
- # FUNCTION 3 - Find the primes in the first "nTerms" Fibonacci terms
- def primesInFibonacci(nTerms):
- terms = fibonacciTerms(nTerms)
- limit = max(terms)
- primes = findPrimes(limit)
- print()
- for i in range(len(terms)):
- for prime in primes:
- if terms[i] == prime:
- print("* " + str(terms[i]) + " (index " + str(i+1) + ") is prime")
- # MAIN FUNCTION
- nTerms = 25
- primesInFibonacci(nTerms)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement