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=int(input())primes={2,3,5,7}truncatable_primes=[]defis_prime(n):ifninprimes:returnTrueelifn==1:returnFalseelifn%2==0:returnFalseelse:# Check for divisibility up to the square root of nforiinrange(3,int(n**0.5)+1,2):ifn%i==0:returnFalsereturnTruedefis_truncatable_prime(n):num_strL=num_strR=str(n)whilelen(num_strL)>0:curr=int(num_strL)ifis_prime(curr):primes.add(curr)num_strL=num_strL[1:]else:returnFalsewhilelen(num_strR)>0:curr=int(num_strR)ifis_prime(curr):primes.add(curr)num_strR=num_strR[:-1]else:returnFalsereturnTrueforiinrange(11,N):ifis_prime(i):primes.add(i)ifis_truncatable_prime(i):truncatable_primes.append(i)# print(truncatable_primes)print(sum(truncatable_primes))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #37: Truncatable primes
You are viewing a single comment's thread. Return to all comments →
Python