We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
n,q=list(map(int,input().split()))number=list(map(int,input().rstrip().split()))length=len(number)defget_primes(n,q):# Primes using Sieve of Eratosthenesprimes=list()sieve=[True]*(n+1)forpinrange(2,n+1):ifsieve[p]:primes.append(p)foriinrange(p,n+1,p):sieve[i]=Falseiflen(primes)>=q:#weonlyneedthefirstqprimenumbersbreakreturnprimesprimes=get_primes(max(number),q)A=[]B=[]for_inrange(q):p=primes.pop(0)#thefirstprimenumberinthelistforeleminnumber:ifelem%p==0:#checkdivisibilitywithprimenumberB.append(elem)else:A.append(elem)number=list(reversed(A))#reversetheremainderelementsfornextiterationA=[]iflen(B)==length:print(*B,sep="\n")else:print(*(B+list(reversed(number))),sep="\n")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Waiter
You are viewing a single comment's thread. Return to all comments →
Python 3. Sieve of Eratosthenes