Is it a turtle prime?

28

2

As we all know, it's turtles all the way down. But is it primes all the way down too?

A number is considered a "turtle-prime" if it satisfies the following conditions:

1) It is prime.
2) It is possible to remove a single digit leaving a prime number.
3) Step 2 can be repeated until left with a single digit prime.

For example, 239 is a "turtle-prime", as it can be reduced to 23 then either 2 or 3, both of which are prime. It also can be reduced to 29 then 2. 151 is not a turtle prime, as it reduces to 15 (not prime), 51 (not prime), or 11. 11 is prime, but can only reduce to 1, which is not.

Given a positive integer, determine if it is a "turtle-prime". Your output can be in any form so long as it gives the same output for any truthy or falsey value.

Test cases:

input -> output
1     -> false
2     -> true
17    -> true
19    -> false
239   -> true
389   -> false

Scoring

This is , so the shortest answer in each language wins!

Lord Farquaad

Posted 2017-07-17T16:53:17.877

Reputation: 1 513

5Related – Magic Octopus Urn – 2017-07-17T16:55:29.257

@MagicOctopusUrn WOW – Keyu Gan – 2017-07-17T16:56:03.007

8Actually related – FryAmTheEggman – 2017-07-17T16:56:15.890

3Can we take input as a list of digits? – totallyhuman – 2017-07-17T17:02:18.603

@totallyhuman I don't see why not. Go for it! – Lord Farquaad – 2017-07-17T17:29:57.310

1Your conditions say that all single-digit primes are not turtle primes. Condition 2 fails: it is not possible to remove a digit and still leave a prime number, as removing the only digit leaves nothing. – hvd – 2017-07-18T20:04:48.220

Answers

6

Jelly, 16 bytes

DŒPḊṖLÐṀḌ߀¬Ȧ<ÆP

Try it online!

How it works

DŒPḊṖLÐṀḌ߀¬Ȧ<ÆP  Main link. Argument: n

D                 Decimal; convert n to base 10.
 ŒP               Powerset; get all sub-arrays of n's decimal digits.
   Ḋ              Dequeue; remove the first sub-array (empty array).
    Ṗ             Pop; remove the last sub-array (all of n's digits).
     LÐṀ          Maximal by length; keep those of the remaining subarrays that
                  have maximal length. This keep exactly those sub-arrays that have
                  one (and only one) digit removed. If n < 10, this yields an empty
                  array. Without Ḋ, it would yield [[]] instead.
        Ḍ         Undecimal; turn the generated digit arrays into integers.
         ߀       Recursively map the main link over the generated integers.
           ¬      Negate; map 1 to 0 and 0 to 1.
            Ȧ     Any and all; yield 0 if the array is empty (n < 10) or any of the
                  recursive calls returned 1 (mapped to 0). If all calls returned
                  0, this will yield 1.
              ÆP  Test n for primality, yielding 1 for primes, 0 otherwise.
             <    Test if the result to the left is less than the result to the
                  right. This is possible only if the left result is 0 (n < 10 or
                  removing a digit results in a turtle prime) and the right result
                  is 1 (n itself is prime).

Dennis

Posted 2017-07-17T16:53:17.877

Reputation: 196 637

More magic Jelly! Man, this stuff gets everywhere... – caird coinheringaahing – 2017-07-18T21:32:19.757

7

Haskell, 104 102 99 98 97 95 91 bytes

p x=product[2..x-1]^2`mod`x>0
f[]=1>0
f x|y<-zip[0..]x=or[f[snd b|b<-y,b/=a]|a<-y,p$read x]

Try it online!

Explanation

First we set up a primality test

p x=product[2..x-1]^2`mod`x>0

This uses Wilson's Theorem to determine the primality of an input.

We then declare a base case, which will assert that the empty string is truthy.

f[]=1>0

Now we define the actual function

f x|y<-zip[0..]x=or[f[snd b|b<-y,b/=a]|a<-y,p$read x]

We use a pattern guard to bind zip[0..]x to y, because we need to use it twice later. We then assert the answer is

or[f[snd b|b<-y,b/=a]|a<-y,p$read x]

[[snd b|b<-y,b/=a]|a<-y] is all of the numbers that are a digit removed from our input. So we are asserting that at least one of these numbers is truthy for f. In order to ensure that composite numbers are falsy we add in prime$read x. If the number is not prime the list will become empty and the any of a empty list is false.

Post Rock Garf Hunter

Posted 2017-07-17T16:53:17.877

Reputation: 55 382

1−2 bytes: any f[[or[f[ – Anders Kaseorg – 2017-07-17T21:37:28.287

1−4 bytes: [b|(i,b)<-y,i/=a]|(a,_)<-y[snd b|b<-y,b/=a]|a<-y – Anders Kaseorg – 2017-07-17T21:43:24.627

6

R, 124 122 120 113 95 93 106 105 bytes

 g=pryr::f(`if`(gmp::isprime(sum(x*10^((l<-sum(x|1)-1):0))),any(!l,sapply(0:l+1,function(z)g(x[-z]))),!1))

Which evaluates to the function:

function (x) 
if (gmp::isprime(sum(x * 10^((l <- sum(x | 1) - 1):0)))) any(!l, 
    sapply(0:l + 1, function(z) g(x[-z]))) else !1

Recursive solution. Takes input as a list of digits.

Has 2 logical statements:

  1. Is x prime when concatenated?

  2. Is any of the following TRUE:

    1. Is the length of x nonzero? This is our final terminating condition.

    2. Is f TRUE for any subset of x?

The first statement ensures we keep working with primes only. The second does the actual recursion.

Saved two bytes thanks to @Giuseppe.

I had to revert some of my golfs because of a bug, where I was testing with a previous function definition by accident.

R, 98 bytes, non-competing

Like I mentioned in the comments, I made a package. Since the challenge predates that, this is non-competing, but I wanted to showcase it a bit. It's not much so far, but we'll get there.

g=pryr::f(`if`(gmp::isprime(RG::C(x)),any(!(l<-sum(x|1)-1),sapply(0:l+1,function(z)g(x[-z]))),!1))

C() is the first function in the package, and takes care of concatenating digits into a numeric.

JAD

Posted 2017-07-17T16:53:17.877

Reputation: 2 898

Some of these operations (Looking at you sum(x*10^(((l<-sum(x|1))-1):0))) are sooo damned wordy. I am really considering creating a golf package for R. – JAD – 2017-07-17T18:34:09.210

this would have been my solution but I couldn't wrap my head around the sapply... Also I think you may want to do f=pryr::f(...) or else you need to use f in the sapply. – Giuseppe – 2017-07-17T19:07:02.317

package name: golfR: somehow longer than the original language name – Giuseppe – 2017-07-17T19:08:03.553

Oh and the exponents can be sum(x*10^((l<-sum(x|1)):1-1)) which is two bytes shorter. – Giuseppe – 2017-07-17T19:12:56.073

Woops, I changed the function name to g to avoid confusion with pryr::f, but forgot the change that in the body... – JAD – 2017-07-17T19:57:02.650

1@Giuseppe Single letter names for everything :D Why not call the package g or something? – JAD – 2017-07-17T19:58:57.523

@JarkoDubbeldam, R packages need to be at least two letters long.

– sebastian-c – 2017-07-18T15:54:51.430

I didn't know that. Two it is! Thinking of adding a feature that every function in the package automatically calls library so the package needs only be referenced once. – JAD – 2017-07-18T16:27:12.363

1

@Giuseppe Created the start for a package :p Take a look: https://github.com/JarkoDubbeldam/RG

– JAD – 2017-07-18T20:20:58.273

1@JarkoDubbeldam beautiful. I'm sure future challenges will make obvious what additional functions need to be added. String manipulation is big: something for el(strsplit(x,'')) would save a ton of bytes. – BLT – 2017-07-18T20:59:13.680

@BLT https://github.com/JarkoDubbeldam/RG/pull/2 on it :)

– JAD – 2017-07-20T11:02:38.770

5

Jelly, 19 bytes

DJḟЀ`ịDḌḟ0߀¬Ȧ¬aÆP

Try it online!

How it works

DJḟЀ`ịDḌḟ0߀¬Ȧ¬aÆP                input:239

D                    decimal         [2,3,9]
 J                   range@length    [1,2,3]
  ḟЀ`               filter out each [[2,3],[1,3],[1,2]]
      ịD             index&decimal   [[3,9],[2,9],[2,3]]
        Ḍ            undecimal       [39,29,23]
         ḟ0          filter out 0    [39,29,23]
           ߀        this@each       [1,1,1]
             ¬       logical not     [0,0,0]
              Ȧ      any and all     0
               ¬     logical not     1
                aÆP  and&is_prime    1

Recursion without base case ftw.

Leaky Nun

Posted 2017-07-17T16:53:17.877

Reputation: 45 011

3

Jelly, 27 26 bytes

DµœcL’$Ḍµ€FÆPÐf
×⁵WÇÐĿFṪ<8

A monadic link taking and returning integers (1 for turtle 0 otherwise).

Try it online!

How?

DµœcL’$Ḍµ€FÆPÐf  Link 1: primes by digit removal: list of numbers  e.g. [19790]
D                cast to decimal list (vectorises)                      [[1,9,7,9,0]]
 µ      µ€       monadic chain for €ach:
      $            last two links as a monad:
    L                length                                             5
     ’               decrement                                          4
  œc             combinations without replacement                       [[1,9,7,9],[1,9,7,0],[1,9,9,0],[1,7,9,0],[9,7,9,0]]
       Ḍ         cast from decimal list (vectorises)                    [1979,1970,1990,1790,9790]
          F      flatten (from a list of lists form the for €ach to a single list)
             Ðf  filter keep if:
           ÆP      is prime?

×⁵WÇÐĿFṪ<8  Main Link: number, n             e.g. 1979
 ⁵          literal 10
×           multiply                              19790
              (this is so the first number is tested as prime too)
  W         wrap in a list                        [19790]
    ÐĿ      loop, collecting results (including the input×10) while change still occurs:
   Ç          call the last (1) link as a monad   [[19790],[1979],[197,199,179],[19,17,97,19,19,17,19,79],[7,7,7,7],[]]
      F     flatten                               [19790,1979,197,199,179,19,17,97,19,19,17,19,79,7,7,7,7]
       Ṫ    tail                                  7
        <8  less than 8?                          1
              (if a single digit prime was reached this will be 1
               otherwise it will be 0
               e.g. an input of 4 yields 40 at the end which is not <8)

Jonathan Allan

Posted 2017-07-17T16:53:17.877

Reputation: 67 804

1It's interesting seeing two substantially different Jelly answers. Lets see who can cut theirs down smaller. – Lord Farquaad – 2017-07-17T18:00:18.947

2

Ruby, 72 57+8 = 80 65 bytes

Uses the -rprime flag. -15 bytes from histocrat!

f=->n{n==''||n.to_i.prime?&!n.scan(/./){f[$`+$']&&break}}

Try it online!

Value Ink

Posted 2017-07-17T16:53:17.877

Reputation: 10 608

You can replace &&!! with just &, it will cast the result to a boolean. Your recursive call can also get a little shorter using perlisms: !n.scan(/./){f[$\+$']&&break}}` – histocrat – 2017-07-17T20:52:58.290

@histocrat Ah yes, I forgot that I didn't actually need boolean short-circuiting for that last part due to the initial condition. Do you know why the n.scan trick works the way it does? – Value Ink – 2017-07-17T21:08:23.213

1Yeah, the two global variables there are set to the string to the left and right of the most recent match, so concatenating them gives you the string minus the one character. Since we need state at each point in the iteration, we can't do anything like .scan.find, but we can manually break out of the loop on success. If we break, scan returns nil, otherwise it returns the string which is always truthy. – histocrat – 2017-07-17T21:57:22.277

2

Java, 220 bytes

Try it online!

Golfed:

boolean t(String n){int l=n.length();if(f(x->{for(int i=2;i<x;)if(x%i++==0)return 1<0;return x>1;},new Integer(n)))if(l<2)return 1>0;else for(int i=0;i<l;)if(t(n.substring(0,i)+n.substring(++i,l)))return 1>0;return 1<0;}

Ungolfed:

  boolean t(String n) {
    int l = n.length();
    if (f(x -> {
      for (int i = 2; i < x;) {
        if (x % i++ == 0) {
          return 1 < 0;
        }
      }
      return x > 1;
    } , new Integer(n))) {
      if (l < 2) {
        return 1 > 0;
      }
      else {
        for (int i = 0; i < l;) {
          if (t(n.substring(0, i) + n.substring(++i, l))) {
            return 1 > 0;
          }
        }
      }
    }
    return 1 < 0;
  }

user18932

Posted 2017-07-17T16:53:17.877

Reputation:

Ignore my previous comment. But you can golf it to this: boolean t(String n){int l=n.length(),x=new Integer(n),i;for(i=2;i<x;x=x%i++<1?0:x);if(x>1)if(l<2)return 1>0;else for(i=0;i<l;)if(t(n.substring(0,i)+n.substring(++i,l)))return 1>0;return 1<0;} (191 bytes) – Kevin Cruijssen – 2017-07-18T06:54:23.733

You can save some bytes by returning 1 and 0 instead of true and false. – Nevay – 2017-07-18T14:19:55.187

@Nevay That would work in C++, but not in Java. Integers cannot be implicitly converted to booleans. – None – 2017-07-18T15:26:09.687

1Not sure but where does f come from? – Roman Gräf – 2017-07-18T17:09:07.933

The question states that any value may be used for true/false; the only place where you need a boolean result of the method is in the last if condition (where you can add >0 to convert the int to a boolean) which should save 22+14=8 bytes in Kevin Cruijssen's version. – Nevay – 2017-07-18T23:28:04.970

1

Python 2, 132 124 119 bytes

-8 Thanks to @WheatWizard

-5 Thanks to @LeakyNun

p=lambda i:i>1and all(i%v for v in range(2,i))
f=lambda n:n<'0'or any(f(n[:i]+n[i+1:])for i in range(len(n)))*p(int(n))

Try it online!

Can't think of anything to hone it down without some builtin prime checker. Takes the number as a string (I assumed this given the OP allowed a list of digits, but if not then +14 bytes for another lambda), and recursively computes each "turtled" number's turtleness.

Arnold Palmer

Posted 2017-07-17T16:53:17.877

Reputation: 443

2Shorter Primality test – Post Rock Garf Hunter – 2017-07-17T18:13:19.713

I think f=lambda n,i=0:n==''or p(int(n))and i<len(n)and(f(n[:i]+n[i+1:])or f(n,i+1)) saves a byte. Someone with better Python golfing skills could probably shorten that further. – Neil – 2017-07-17T18:36:30.263

@Neil, it does save a byte, but the wording "the same output for any truthy or falsey value" prevents me from taking it, as inputting 1 returns 0 instead of False like the other cases (due to the -8 primality check). If the OP allows different (although synonymous) outputs, then I would change it up. – Arnold Palmer – 2017-07-17T18:51:09.180

1

Sorry, my previous suggestion is invalid. 119 bytes

– Leaky Nun – 2017-07-17T20:06:04.723

1

05AB1E, 28 27 bytes

Iterative solution.

¸[D0èg2‹#εæ¨D€gZQÏDpÏ}˜]p1å

Try it online!

Explanation

¸                              # wrap input in a list
 [                             # start a loop
  D0èg2‹#                      # if the length of the first element is less than 2, break
         ε                     # apply to each element in the list
          æ                    # compute powerset
           ¨                   # remove last element (the full number)
            D€gZQÏ             # keep only the elements whose length is the max length
                  DpÏ          # keep only primes
                     }         # end apply
                      ˜        # flatten list
                       ]       # end loop
                        p1å    # is any element in the resulting list prime

Emigna

Posted 2017-07-17T16:53:17.877

Reputation: 50 798

1

C#, 355 bytes

namespace System{using B=Numerics.BigInteger;class A{static void Main(){Console.WriteLine(D(Console.ReadLine()));}static bool P(B x){if(x<2)return 1<0;B r=1;for(int i=1;i<=x-1;i++)r*=i;return(r+1)%x==0;}static bool D(string x){if(x.Length==0)return 1>0;bool b;if(b=P(B.Parse(x))){var n=1<0;for(int i=0;i<x.Length;i++)n|=D(x.Remove(i,1));b&=n;}return b;}}}

Try it online!

My first code golf, so I hope I did it alright. I couldn't think of a way to make it even smaller (other than using int instead of BigInteger, but I did it so it would work for all provided test cases). Anyway, here's the same properly formatted:

namespace System
{
    using B = Numerics.BigInteger;
    class A
    {
        static void Main()
        {
            Console.WriteLine(D(Console.ReadLine()));
        }

        static bool P(B x)
        {
            if (x < 2)
                return 1<0;
            B r = 1;
            for (int i = 1; i <= x - 1; i++)
                r *= i;
            return (r + 1) % x == 0;
        }

        static bool D(string x)
        {
            if (x.Length == 0)
                return 1>0;
            bool b;
            if (b = P(B.Parse(x)))
            {
                var n = 1<0;
                for (int i = 0; i < x.Length; i++)
                    n |= D(x.Remove(i, 1));
                b &= n;
            }
            return b;
        }
    }
}

Grzegorz Puławski

Posted 2017-07-17T16:53:17.877

Reputation: 781

0

Perl 6, 65 bytes

my&f={.is-prime&&(10>$_||?grep {f +[~] .list},m:ex/^(.*).(.*)$/)}

Try it online!

Sean

Posted 2017-07-17T16:53:17.877

Reputation: 4 136

0

PHP, 164 bytes

function t($n){for($i=1;++$i<$n;)if($n%$i<1)return 0;if($n<10)return $n>1;foreach($r=str_split($n)as$k=>$v){$q=$r;array_splice($q,$k,1);$z|=t(join($q));}return $z;}

Try it online!

Starts off by testing the number for primality, then loops through the digits as an array, popping each one out and joining the rest back together and feeding them recursively through the function again. Each link downwards does a logical OR with the lower paths, only returning true if there exists at least one path of all primes.

Xanderhall

Posted 2017-07-17T16:53:17.877

Reputation: 1 236

0

Javascript 167 bytes

n=>{a=[];for(i=1;i++<=n;)a.every(x=>i%x)?a.push(i):0;b=k=>(s=''+k,a.indexOf(k)>-1&&(k<10||[...s].some((x,i)=>(r=[...s],r.splice(i,1),b(~~(r.join('')))))));return b(n)}

Explanation

n=>{
    a=[];                             // create array to store primes in
    for(i=1;i++<=n;)                  // iterate from 2 to n
        a.every(x=>i%x)?a.push(i):0;  // if i % x is truthy for all x in a,
                                      // then i is prime
    b=k=>(                            // function to test is k is turtle prime
        s=''+k,                       // convert k to a string
        a.indexOf(k)>-1 && (          // if k is prime and
            k<10 ||                   // k is a single digit or
            [...s].some((x,i)=>(      // iterate over the digits of k
                                      // and check to see if, by removing each
                                      // any of the resulting numbers is turtle prime
                                      // ... is spread operator
                                      // [...s] converts string s to an array of characters 
                r=[...s],             // convert s to an array again,
                                      // importantly, this cannot be the same array
                                      // we created above, as we need to
                r.splice(i,1),        // splice out the ith element of the array
                b(~~(r.join('')))     // join the array to a string, convert to int,
                                      // and check if this number is turtle prime
                                      // ~ is bitwise negate, implicitly converts to int first before negating
                                      // ~~ negates the negation, getting us the int
            ))
        )
    );
    return b(n)
}

F=n=>{a=[];for(i=1;i++<=n;)a.every(x=>i%x)?a.push(i):0;b=k=>(s=''+k,a.indexOf(k)>-1&&(k<10||[...s].some((x,i)=>(r=[...s],r.splice(i,1),b(~~(r.join('')))))));return b(n)}

$('#input').on('keyup', () => $('#output').text(F(parseInt($('#input').val(), 10))))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="input" />
<span id="output"></span>

asgallant

Posted 2017-07-17T16:53:17.877

Reputation: 309