prime number and fibonacci series in python

Solutions on MaxInterview for prime number and fibonacci series in python by the best coders in the world

showing results for - "prime number and fibonacci series in python"
Valentina
08 Apr 2018
1import math
2
3num = int(input())
4
5
6def isPrime(num):
7    if num == 2 or num == 3 or num == 5 or num == 7:
8        prime = True
9    elif num % 2 == 0 or num % 3 == 0 or num % 5 == 0 or num % 7 == 0:
10        prime = False
11    elif math.ceil(math.sqrt(num)) - math.floor(math.sqrt(num)) == 0:
12        prime = False
13    else:
14        prime = True
15    return prime
16
17
18prime = []
19for i in range(2, num * 2):
20    if isPrime(i):
21        prime.append(i)
22
23fibo = [0, 1]
24for i in range(2, (num // 2) + 1):
25    fibo.append(fibo[-1] + fibo[-2])
26fibo.remove(0)
27
28
29prime = prime[:num//2]
30
31
32def merge_lists(list1, list2):
33    list3 = []
34    while True:
35        try:
36            list3.append(list1.pop(0))
37            list3.append(list2.pop(0))
38        except IndexError:
39            break
40    return list3
41
42print(merge_lists(fibo, prime))
similar questions