1. Find prime numbers between n1 and n2, then 2. Make all possible unique combinations of numbers from the prime numbers list you found in step 1. 3. From this new list, again find all prime numbers. 4. Find smallest (a) and largest (b) number from the 2nd generated list, also count of this list. 5. Consider smallest and largest number as the 1st and 2nd number to generate Fibonacci series respectively till the count (number of primes in the 2nd list). 6. Print the last number of a Fibonacci series as an output
Answers
Language used : Python Programming
I have attached the captures of the program and it's execution below, to show you the execution. Have a look.
Program:
x,y=input().split(" ")
l1=[]
l2=[]
def primeornot(i):
if i==1:
return False
elif i==2:
return True
else:
for j in range(2,i):
if i%j==0:
return False
return True
x=int(x)
y=int(y)
if x>=2 and y<=100 and abs(x-y)>=35:
for i in range(x,y+1):
if(primeornot(i)==True):
l1.append(i)
for i in range(len(l1)):
for j in range(len(l1)):
if i!=j:
l2.append(int(str(l1[i])+str(l1[j])))
l1.clear()
for i in l2:
if primeornot(i)==True:
if i not in l1:
l1.append(i)
l2.clear()
mini=min(l1)
maxi=max(l1)
nextterm=0
lengthoffib=len(l1)-2
l1.clear()
while(lengthoffib!=0):
nextterm=mini+maxi
mini=maxi
maxi=nextterm
lengthoffib=lengthoffib-1
print(nextterm)
Input 1 :
2 40
Output 1 :
13158006689
Input 2:
30 70
Output 2:
2027041
Procedure:
First we will be taking two input parameters x and y and will be forming a list of prime numbers between the two (x&y).
On creating the list, we will make combinations of the numbers in the list and make the whole as a list 2. On forming the second list, we will again form a list of primes from the list 2 and find min and max elements of those primes.
On finding the min and max elements, make a fibanocci series starting from min, max elements and find the nth term of the series, where n is the length of the new prime list formed from list 2.
The nth term of the series will be your output.
Explanation of a test case:
Let the numbers inputted be 2 40
First list, i.e, primes between the two is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
Using this list, we make combinations of numbers and form another list as in Combination of all the primes = [23, 25, 27, 211, 213, 217, 219, 223, 229, 231, 32, 35, 37, 311, 313, 319, 323, 329, 331, 337, 52, 53, 57, 511, 513, 517, 519, 523, 529, 531, 537, 72, 73, 75, 711, 713, 717, 719, 723, 729, 731, 737, 112, 113, 115, 117, 1113, 1117, 1119, 1123, 1129, 1131, 1137, 132, 133, 135, 137, 1311, 1317, 1319, 1323, 1329, 1331, 1337, 172, 173, 175, 177, 1711, 1713, 1719, 1723, 1729, 1731, 1737, 192, 193, 195, 197, 1911, 1913, 1917, 1923, 1929, 1931, 1937, 232, 233, 235, 237, 2311, 2313, 2317, 2319, 2329, 2331, 2337, 292, 293, 295, 297, 2911, 2913, 2917, 2919, 2923, 2931, 2937, 312, 315, 317, 3111, 3113, 3]
And the prime list from the list 2 is prime list=[193, 3137, 197, 2311, 3719, 73, 137, 331, 523, 1931, 719, 337, 211, 23, 1117, 223, 1123, 229, 37, 293, 2917, 1319, 1129, 233, 173, 3119, 113, 53, 373, 311, 313, 1913, 1723, 317]
Minimum element = 23
Maximum element = 3719
As the length of the prime list is 34 (34 elements are present in the prime list), we need to find the 34th element on stating the fibanocci sequence with 23 and 3719
As in, 23, 3719, 3742 (23+3719), 7461 (3742+3719), ......, 13158006689 (the 34th term of the series)
So, the output is 13158006689
Learn more :
1) Printing all the palindromes formed by a palindrome word.
brainly.in/question/19151384
2) Indentation is must in python. Know more about it at :
brainly.in/question/17731168
3) Write a Python function sumsquare(l) that takes a nonempty list of integers and returns a list [odd,even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.
brainly.in/question/15473120
1. Find prime numbers between n1 and n2, then 2. Make all possible unique combinations of numbers from the prime numbers list you found in step 1. 3. From this new list, again find all prime numbers. 4. Find smallest (a) and largest (b) number from the 2nd generated list, also count of this list. 5. Consider smallest and largest number as the 1st and 2nd number to generate Fibonacci series respectively till the count (number of primes in the 2nd list). 6. Print the last number of a Fibonacci series as an output
Explanation:
def check_prime(x):
if x&1 == 0: return 0
for i in range(3,int(x**0.5)+1,2):
if x%i==0: return 0
return 1
n1,n2 = 30,70
min = 9789
max = 23
count = 0
freq = {}
if n1&1 == 0:
l = [i for i in range(n1+1,n2+1,2) if check_prime(i)]
else:
l = [i for i in range(n1,n2+1,2) if check_prime(i)]
if n1 == 2: l = [2] + l
for x in l:
for y in l:
if x != y:
n = int(str(x)+str(y))
if n not in freq:
if check_prime(n):
freq[n] = 1
if n<min: min = n
elif n>max: max = n
count += 1
while count>1:
min,max = max,min+max
count -= 1
Speedup of 2 by checking even separately and than only check in the loop only odd numbers.
1b. We can improve this more by storing a list of the first m primes and check them up front.
We can keep found primes up to a certain value and every number smaller than the highest of your primes that is not in the set of the primes is no prime.
print(min)