Skip to content

Instantly share code, notes, and snippets.

@Zircoz
Created December 17, 2020 19:04
Show Gist options
  • Save Zircoz/1816ca2f17221ebcdb469ff356f80f57 to your computer and use it in GitHub Desktop.
Save Zircoz/1816ca2f17221ebcdb469ff356f80f57 to your computer and use it in GitHub Desktop.
Alternate Series of Fibonacci & Prime Numbers
'''
Series of Fibonacci and Prime numbers arranged alternatively
Fibo: 1,1,2,3,5...
Prime: 2,3,5,7,5...
Merged Series: 1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17.........
'''
import math
def next_Fib(last, secLast):
return last+secLast
def next_Prime(last):
last=last+1
while(True):
if isPrime(last):
return last
else:
last+=1
def isPrime(n):
if(n <= 3):
return True
if(n % 2 == 0 or n % 3 == 0):
return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
IN = int(input("Enter the number of elements you want: "))
#IN = 14
series = [1,2,1]
for i in range(3,IN):
if i%2==0:
series.append(next_Fib(series[i-2], series[i-4]))
else:
series.append(next_Prime(series[i-2]))
print(*series)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment