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.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'downToZero' function below.## The function is expected to return an INTEGER.# The function accepts INTEGER n as parameter.## Python program to print all Primes Smaller # than or equal to N using Sieve of EratosthenesdefSieveOfEratosthenes(num):prime=[Trueforiinrange(num+1)]arr=[]p=2while(p*p<=num):if(prime[p]==True):foriinrange(p*p,num+1,p):prime[i]=Falsep+=1forpinrange(2,num+1):ifprime[p]:arr.append(p)returnarrarr=SieveOfEratosthenes(1000000)arr=set(arr)# print(arr)defcheck(n):ifninarr:return0foriinrange(int(math.sqrt(n))+1,2,-1):ifn%i==0:returnn//ireturn0defdownToZero(n):dp=[-1foriinrange(n+1)]defsolve(val):ifval==1:return1ifdp[val]!=-1:returndp[val]vv=check(val)aa=float("inf")ifvv:aa=1+solve(vv)dp[val]=min(aa,1+solve(val-1))returndp[val]returnsolve(n)# Write your code hereif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')q=int(input().strip())forq_itrinrange(q):n=int(input().strip())result=downToZero(n)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Down to Zero II
You are viewing a single comment's thread. Return to all comments →