I was getting Wrong Answer in private test cases. Can anybody please help me?
import math
from itertools import permutations
primes = []
def getPrimes(limit):
for i in range(2, limit+1):
isPrime = True
for j in range(2, i//2 + 1):
if i%j == 0:
isPrime = False
break
if isPrime:
primes.append(i)
if __name__ == "__main__":
getPrimes(10000)
n1, n2 = [int(x) for x in input().split()]
filtered_primes = [x for x in primes if x >= n1 and x <= n2]
l = [int(str(x[0]) + str(x[1])) for x in list(set(permutations(filtered_primes, 2))) if int(str(x[0]) + str(x[1])) in primes]
l = list(set(l))
a = min(l)
b = max(l)
count = len(l)
for c in range(3, count+1):
s = a + b
a = b
b = s
print(b, end="")
I couldn’t do this too, I wrote this explicitly in python because for range 1 to 100, the 2nd list will have around 128 primes. As we know considering starting points (0,1), we can only store 94th fibonacci number in signed long long. so there is no way we can store 128th fibonacci number whose starting 2 number are greater than 2… However one of my friend got this accepted in Java using “Long” type.
There can be two things -
My solution is wrong.
The output generator didn’t take into account the overflow that might occur.So the test cases were faulty.
I would like to know if someone got it correct using Big-Integer or in python Only.
I don’t know actually why but initially I wrote the code in python but it gave me runtime error + presentation error but when I wrote the same code in C++, it got accepted.
Approach was simple just followed the instructions of question and nothing else.
See, i made this list just to make you understand about the concept. when did I say that this is prime list??
I just thought it would help you in making sense of what the question is asking
I understand what you’re trying to say…but in the code I’m making combinations by converting them to strings, concatenating them and putting the result in a new list…that’s like a shortcut to combine integers.
from itertools import permutations
a,b=map(int,input().split())
l=[]
for j in range(a,b+1):
p=0
for k in range(2,j):
if(j%k==0):
p=1
break
else:
pass
if(p==0):
l.append(str(j))
l=set(l)
lx=[]
lp=permutations(l, 2)
for e in lp:
z="".join(e)
lx.append(z)
l2=[]
lx=list(set(lx))
for e in lx:
e=int(e)
p=0
for k in range(2,e):
if(e%k==0):
p=1
break
else:
pass
if(p==0):
l2.append(e)
l1=sorted(l2)
a=l1[0]
b=l1[-1]
n=len(l1)
for i in range(3,n+1):
s=a+b
a=b
b=s
print(b)
A s i suspected, the integer overflow was not taken into account, using long long int was not sufficient, but still it got accepted because the test cases were made using that only. That’s why all python solutions kinda failed.
Don’t get mislead by his use of permutations function… His full sentence means finding the permutation of any two objects taking from that list. Meanwhile permutation of a and b, is “ab” and “ba” only, which you tried to explain. So there is nothing wrong in that part. It does something similar what you said in the example.