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.
defget_primes_till_N(N):"""CreatesanarrayofprimestillN."""ifN<2:return[]# Initialize a list to track prime numbersis_prime=[True]*(N+1)is_prime[0],is_prime[1]=False,False#0and1arenotprimes# Apply the Sieve of Eratosthenes algorithmforstartinrange(2,int(N**0.5)+1):ifis_prime[start]:formultipleinrange(start*start,N+1,start):is_prime[multiple]=False# Collect all prime numbers into a listprimes=[numfornum,primeinenumerate(is_prime)ifprime]returnprimes# Create array of primes till 10001:primes=get_primes_till_N(10001)defwaiter(number,q):answers=list()A=number[:]foriinrange(q):# Finish iteration if A is emptyifnotA:break# Store values in pile BB=[numfornuminAifnum%primes[i]==0][::-1]# Remove values from pile AA=[numfornuminAifnum%primes[i]!=0][::-1]# Add values of pile B to answers:answers+=B[::-1]returnanswers+A[::-1]
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