Problem of the Day
A new programming or logic puzzle every Mon-Fri

Truncatable Primes

The number 3797 is considered prime. But you can also take away a number from either side of the number and still be left with a prime number. 379, 37, 3 and 797, 97, and 7 are all prime. Starting with numbers greater than 9 can you find other truncatable primes?

Permalink: http://problemotd.com/problem/truncatable-primes/

Comments:

  • Anonymous - 9 years, 11 months ago

    23 37 53 73 313 317 373 797 3137 3797 739397

    reply permalink

  • David - 9 years ago

    Some Python to get truncatable primes. Too lazy today to make the script into a function. I threw the problem at memory instead of time. Each iteration is guaranteed to be truncatable by concatenating one digit primes to prime numbers and and keeping prime results.

    import math
    
    debug = 0
    
    primes = []
    primes.append([])
    primes.append(['2','3','5','7'])
    
    def isPrime(n):
        for m in range(2, int(n**0.5)+1):
            if not n%m:
                return False
        return True
    
    for depth in range(1,4):
        tempList = []
        for p in primes[1]:
            for q in primes[depth]:
                if(debug): print math.log(int(q),10)
                r = int(p)*10**int(math.log(int(q),10)+1)
                r += int(q)
                if(debug): print "r = ", r
                if(isPrime(r)):
                    # print p + q
                    tempList.append(p+q)
        # print tempList
        primes.append(tempList)
    
    for l in primes:
        for p in l:
            print p, " ",
    print ""
    

    reply permalink

  • David - 8 years, 11 months ago

    So I guess I have Left Truncatable primes, but technically not all of them. My class of truncatable primes does not seem to appear in the Wikipedia article.
    https://en.wikipedia.org/wiki/Truncatable_prime Also interesting is the property that this eventually runs out of additional lengths due to the lack of prime numbers comprised of only prime digits with length 5.

    reply permalink

Content curated by @MaxBurstein