Project Euler #37: Truncatable primes

Sort by

recency

|

22 Discussions

|

  • + 0 comments
    n            = int(input())
    trunc_primes = [23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397]
    my_primes    = [i for i in trunc_primes if i < n]
    print(sum(my_primes))
    
  • + 0 comments

    100 points

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    def is_prime(n):
        if n < 2:
            return False
        for i in range(2, int(n**0.5) + 1):
            if n % i == 0:
                return False
        return True
    
    def is_truncatable(n):
        s = str(n)
        for i in range(1, len(s)):
            if not is_prime(int(s[i:])) or not is_prime(int(s[:i])):
                return False
        return True
    
    limit = int(input())
    trunc_primes = []
    for p in range(10, limit):
        if is_prime(p) and is_truncatable(p):
            trunc_primes.append(p)
    print(sum(trunc_primes))
    
  • + 0 comments

    Python

    N = int(input())
    primes = {2, 3, 5, 7}
    truncatable_primes = []
    
    def is_prime(n):
        if n in primes:
            return True
        elif n == 1:
            return False
        elif n % 2 == 0:
            return False
        else:
            # Check for divisibility up to the square root of n
            for i in range(3, int(n**0.5) + 1, 2):
                if n % i == 0:
                    return False
            return True
    
    def is_truncatable_prime(n):
        num_strL = num_strR = str(n)
        
        while len(num_strL) > 0:
            curr = int(num_strL)
    				
            if is_prime(curr):
                primes.add(curr)
                num_strL = num_strL[1:]
            else:
                return False
        
        while len(num_strR) > 0:
            curr = int(num_strR)
    				
            if is_prime(curr):
                primes.add(curr)
                num_strR = num_strR[:-1]
            else:
                return False
                
        return True
    
    for i in range(11, N):
        if is_prime(i):
            primes.add(i)
            if is_truncatable_prime(i):
                truncatable_primes.append(i)
    
    # print(truncatable_primes)
    print(sum(truncatable_primes))
    
  • + 0 comments

    Easy logic. 100 points

    def is_prime(n):
        if n==2:
            return True
        if n%2==0 or n<2:
            return False
        if all([n%i for i in range(3,int(n**0.5)+1,2)]):
            return True
        return False
    n=int(input().strip())
    s=0
    for i in range(10,n+1):
        if is_prime(i):
            t=i
            while t>0:
                if not is_prime(t):
                    break
                t=t%(10**(len(str(t))-1))
            if t==0:
                t=i
                while t>0:
                    if not is_prime(t):
                        break
                    t=t//10
                if t==0:
                    s+=i
                    
    print(s)
    
  • + 0 comments

    code in c:

    int is_prime(int num){

      if (num == 1) return 0;
      for (int i= 2; i<=sqrt(num); i++) {
         if(num%i == 0) return 0;
      }
      return 1;
    

    }

    int main() {

    int n;
    scanf("%d",&n);
    unsigned long long int sum = 0;
    for (int i = 11 ; i<n; i++) {
         if(is_prime(i)){
             int digit = 1+log10(i),flag = 0;
             for (int j = 1; j<digit; j++) {
                  if(is_prime(i/pow(10,j)) && is_prime(i%(int) pow(10,digit-j))) continue;
                   else {
                       flag = 1;
                       break;
                   }
             }
             if(flag == 0)  
                 sum += i;
         }
    }
    printf("%lld\n",sum);
    return 0;
    

    }