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 'primeDigitSums' function below.## The function is expected to return an INTEGER.# The function accepts INTEGER n as parameter.#_arr=[]_sum3=set()_sum4=set()_sum5=set()_primes=[]def_is_prime_trial_division(n):returnint(n)in_sum3orint(n)in_sum4orint(n)in_sum5def_build_prime_trial_division():foriinrange(0,10**3-1):if_primes[sum([int(x)forxinlist(str(i))])]:_sum3.add(i)foriin_sum3:forjinrange(10):if_primes[i+j]:_sum4.add(int(str(i)+str(j)))foriin_sum4:forjinrange(10):if_primes[i+j]:_sum5.add(int(str(i)+str(j)))def_build_sieve_of_eratosthenes(limit):global_primes_primes=[True]*(limit+1)p=2whilep*p<=limit:if_primes[p]:foriinrange(p*p,limit+1,p):_primes[i]=Falsep+=1def_prime_sum(x):x=str(x)foriinrange(len(x)-5):ifnot_is_prime_trial_division(x[i:i+5]):returnFalseforiinrange(len(x)-4):ifnot_is_prime_trial_division(x[i:i+4]):returnFalseforiinrange(len(x)-3):ifnot_is_prime_trial_division(x[i:i+3]):returnFalseprint(x)returnTruedefprimeDigitSums(n):_build_sieve_of_eratosthenes(10**5)_build_prime_trial_division()count=0foriinrange(10**(n),10**(n+1)-1):if_prime_sum(i):count+=1returncount# 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=primeDigitSums(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
Prime Digit Sums
You are viewing a single comment's thread. Return to all comments →
What is wrong with the solution: