Mr_HO1A

Prime Fibonnaci

Jun 20th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. import time
  2.  
  3. # Sieve Prime Finding BoilerPlate [20]
  4. def sivePrimes(n):
  5.     prime = [True for i in range(n + 1)]
  6.     p = 2
  7.     while (p * p <= n):
  8.  
  9.         if (prime[p] == True):
  10.  
  11.             for i in range(p * 2, n + 1, p):
  12.                 prime[i] = False
  13.         p += 1
  14.     prime[0]= False
  15.     prime[1]= False
  16.     primes = []
  17.     for p in range(n + 1):
  18.         if prime[p]:
  19.             primes.append(p)
  20.     return primes
  21.  
  22. # Get Inputs
  23. a,b = list(map(int,input().split()))
  24. sTime = time.time()
  25. # Find Primes Between List
  26. primes = sivePrimes(b)
  27. for x in primes:
  28.     if x < a:
  29.         continue
  30.     else:
  31.         index = primes.index(x)
  32.         primes = primes[index:len(primes)]
  33.         break
  34.  
  35. # Creating Premutations
  36. from itertools import permutations
  37. PrimePermutaions = permutations(primes,2)
  38. PermutedPrimes = []
  39. for x in PrimePermutaions:
  40.     number = int(str(x[0]) + str(x[1]))
  41.     if not(number in PermutedPrimes):
  42.         PermutedPrimes.append(number)
  43.  
  44.  
  45. PermutedPrimes = set(PermutedPrimes)
  46. a,b = min(PermutedPrimes),max(PermutedPrimes)
  47. primes = set(sivePrimes(b))
  48.  
  49. # Find Primes in permuted primes
  50. commonPrimes = primes.intersection(PermutedPrimes)
  51. a,b = min(commonPrimes),max(commonPrimes)
  52.  
  53. # Create Fib Serues
  54. FibArray = [a,b]
  55.  
  56. def fibonacci(n):
  57.     if n<0:
  58.         print("Incorrect input")
  59.     elif n<=len(FibArray):
  60.         return FibArray[n-1]
  61.     else:
  62.         temp_fib = fibonacci(n-1)+fibonacci(n-2)
  63.         FibArray.append(temp_fib)
  64.         return temp_fib
  65. print(fibonacci(len(commonPrimes)))
  66. print("Time Taken : ",time.time() - sTime)
Add Comment
Please, Sign In to add comment