Home » List all Right Truncatable Primes

List all Right Truncatable Primes

List all Right Truncatable Primes. A Right Truncatable Prime is a prime number which contains no 0 and if the rightmost digit is successively removed, then all resulting numbers are primes. For example, 7193 – since 7193, 719, 71 and 7 all are primes.

📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 286
Challenge Difficulty: ⭐️⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn

Solving the challenge of List all Right Truncatable Primes with Power Query

Power Query solution 1 for List all Right Truncatable Primes, proposed by Zoran Milokanović:
let
  Source = Excel.CurrentWorkbook(){[Name = "Input"]}[Content][Numbers], 
  IsPrime = (n) =>
    not List.Accumulate(
      {2 .. Number.RoundDown(Number.Sqrt(n))}, 
      n = 1, 
      (s, d) => s or (Number.Mod(n, d) = 0)
    ), 
  S = List.Select(
    Source, 
    each 
      let
        t = Text.From(_)
      in
        List.Accumulate(
          {1 .. Text.Length(t)}, 
          true, 
          (s, d) => s and IsPrime(Number.From(Text.Start(t, d)))
        )
  )
in
  S
Power Query solution 2 for List all Right Truncatable Primes, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  Sol = Table.SelectRows(
    Source, 
    each 
      let
        a = Text.From([Numbers]), 
        b = Text.Length(a), 
        c = List.Transform(
          {1 .. b}, 
          each 
            let
              d = Number.From(Text.Range(a, 0, _)), 
              e = List.Transform(
                {2} & List.Select({3 .. Int64.From(Number.Sqrt(d))}, Number.IsOdd), 
                (x) => Number.Mod(d, x) <> 0
              )
            in
              List.AllTrue(e)
        )
      in
        List.AllTrue(c)
  )
in
  Sol
Power Query solution 3 for List all Right Truncatable Primes, proposed by Luan Rodrigues:
let
  Fonte = Tabela1, 
  fx = (n) =>
    List.Select({2 .. Number.RoundDown(Number.Sqrt(n))}, (x) => Number.Mod(n, x) = 0){0}? = null, 
  fil = Table.SelectRows(
    Fonte, 
    each [
      a = List.Select(
        {2 .. Number.RoundDown(Number.Sqrt([Numbers]))}, 
        (x) => Number.Mod([Numbers], x) = 0
      ){0}?
        = null, 
      b = Text.From([Numbers]), 
      c = List.Select(
        List.Transform({0 .. Text.Length(b)}, each Number.From(Text.Middle(b, 0, _))), 
        each _ <> null
      ), 
      d = List.AllTrue(List.Transform(c, (x) => fx(x))) = true, 
      e = a = true and d = true
    ][e]
  )
in
  fil
Power Query solution 4 for List all Right Truncatable Primes, proposed by Rafael González B.:
let
  Source = Excel.CurrentWorkbook(){0}[Content], 
  TC = Table.TransformColumnTypes(Source, {{"Numbers", type number}}), 
  Fx_Prime = (n as number) =>
    let
      a = n, 
      b = Text.From(a), 
      c = Text.ToList(b), 
      d = List.Count(c), 
      e = d - 1, 
      ee = 
        let
          ee1 = List.FirstN(c, e), 
          ee2 = Text.Combine(ee1), 
          ee3 = Number.From(ee2)
        in
          ee3, 
      f = {2 .. Number.RoundDown(Number.Sqrt(a))}, 
      g = Table.FromList(f, Splitter.SplitByNothing(), {"Divisors"}), 
      h = Table.AddColumn(g, "Num", each Number.Mod(a, [Divisors])), 
      i = Table.SelectRows(h, each [Num] = 0), 
      j = Table.IsEmpty(i), 
      k = if not j then "No" else if ee = null then "Yes" else @Fx_Prime(ee)
    in
      k, 
  Col = Table.AddColumn(TC, "Check", each Fx_Prime([Numbers])), 
  Result = Table.SelectRows(Col, each ([Check] = "Yes"))[[Numbers]]
in
  Result
Power Query solution 5 for List all Right Truncatable Primes, proposed by Luke Jarych:
let
 Source = Table1,
 ConvertToText = Table.TransformColumnTypes(Source, {{"Numbers", type text}}), // Convert "Numbers" back to number
 IsAllPrime = Table.AddColumn(ConvertToText, "AllDigitsPrime", each IsAllPrime([Numbers])),
 #"Filtered Rows" = Table.SelectRows(IsAllPrime, each ([AllDigitsPrime] = true)),
 #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"AllDigitsPrime"})
in
 #"Removed Columns"

IsPrime = let
 IsPrime = (num as number) as logical =>
 let
 maxDivisor = Number.RoundDown(Number.Sqrt(num)),
 divisible = List.Numbers(2, maxDivisor - 1),
 checkDivisible = List.Select(divisible, each Number.Mod(num, _) = 0)
 in
 List.IsEmpty(checkDivisible) and num >= 1
in IsPrime

IsAllPrime = 
let CheckRightTruncatablePrime = (numText as text) as logical =>
 let
 num = Number.From(numText),
 numLength = Text.Length(numText),
 truncatedPrime = Number.RoundDown(num / 10),
 isCurrentPrime = IsPrime(num),
 isNextPrime = if numLength > 1 then IsAllPrime(Text.Start(Text.From(truncatedPrime), numLength - 1)) else true
 in
 isCurrentPrime and isNextPrime
in
 CheckRightTruncatablePrime


                    
                  
          

Solving the challenge of List all Right Truncatable Primes with Excel

Excel solution 1 for List all Right Truncatable Primes, proposed by John V.:
=TOCOL(
    MAP(
        A2:A11,
        LAMBDA(
            n,
            n/AND(
                MAP(
                    INT(
                        n/10^SEQUENCE(
                            LEN(
                                n
                            ),
                            ,
                            0
                        )
                    ),
                    LAMBDA(
                        x,
                        AND(
                            MOD(
                                x,
                                1+SEQUENCE(
                                    x^0.5
                                )
                            )
                        )
                    )
                )
            )
        )
    ),
    2
)
Excel solution 2 for List all Right Truncatable Primes, proposed by محمد حلمي:
=TOCOL(
    MAP(
        A2:A11,
        LAMBDA(
            a,
            a/AND(
                MAP(
                    9-SEQUENCE(
                        8
                    ),
                    LAMBDA(
                        d,
                        AND(
                            MOD(
                                LEFT(
                                    a,
                                    d
                                )/SEQUENCE(
                                    LEFT(
                                    a,
                                    d
                                )^0.5,
                                    ,
                                    2
                                ),
                                1
                            )
                        )
                    )
                )
            )
        )
    ),
    2
)


=TOCOL(
    MAP(
        A2:A11,
        LAMBDA(
            a,
            a/REDUCE(
                1,
                9-SEQUENCE(
                        8
                    ),
                LAMBDA(
                    c,
                    d,
                    AND(
                        VSTACK(
                            c,
                            MOD(
                                LEFT(
                                    a,
                                    d
                                )/SEQUENCE(
                                    LEFT(
                                    a,
                                    d
                                )^0.5,
                                    ,
                                    2
                                ),
                                1
                            )
                        )
                    )
                )
            )
        )
    ),
    2
)
Excel solution 3 for List all Right Truncatable Primes, proposed by Kris Jaganah:
=TOCOL(MAP(A2:A11,
    LAMBDA(x,
    LET(a,
    --MID(
        x,
        1,
        SEQUENCE(
            LEN(
                x
            )
        )
    ),
    b,
    (a+1)/6,
    c,
    (a-1)/6,
    x/MIN(IFS(a=2,
    1,
    a=3,
    1,
    a=5,
    1,
    1,
    (INT(
        b
    )=b)+(INT(
        c
    )=c)))))),
    3)
Excel solution 4 for List all Right Truncatable Primes, proposed by Timothée BLIOT:
=FILTER(A2:A11,MAP(A2:A11,LAMBDA(z,LET(D,LAMBDA(n,IF(n<5,2,VSTACK(2,SEQUENCE(ROUNDDOWN(((n^0.5)-3),0)/2+1,,3,2)))),P,LAMBDA(x, SWITCH(x,1,0,2,1,LET(A,D(x),--(SUM(MAP(A, LAMBDA(a,--(MOD(x,a)=0)))) =0)))),A,MAP(SEQUENCE(LEN(z),,LEN(z),-1),LAMBDA(x,P(MID(z,1,x)*1))),IF(--RIGHT(z)>0,SUM(A)=ROWS(A))))))
Excel solution 5 for List all Right Truncatable Primes, proposed by Hussein SATOUR:
=FILTER(
    A2:A11,
     MAP(
         A2:A11,
          LAMBDA(
              x,
               MIN(
                   MAP(
                       --UNIQUE(
                           MID(
                               x,
                                1,
                                SEQUENCE(
                                    9
                                )
                           )
                       ),
                        LAMBDA(
                            y,
                             MIN(
                                 DROP(
                                     MOD(
                                         y,
                                          SEQUENCE(
                                              ROUNDUP(
                                                  SQRT(
                                                      y
                                                  ),
                                                   0
                                              )
                                          )
                                     ),
                                      1
                                 )
                             )
                        )
                   )
               )
          )
     )<>0
)
Excel solution 6 for List all Right Truncatable Primes, proposed by Sunny Baggu:
=TOCOL(
 A2:A11 * 1 /
 MAP(
 A2:A11,
 LAMBDA(num,
 AND(
 MAP(
 LEFT(num, SEQUENCE(LEN(num))) + 0,
 LAMBDA(x,
 IF(x <= 3, "TRUE", OR(BYCOL((x + {1, -1}) / 6, LAMBDA(a, a = INT(a)))))
 )
 )
 )
 )
 ),
 3
)
Excel solution 7 for List all Right Truncatable Primes, proposed by LEONARD OCHEA 🇷🇴:
=TOCOL(MAP(A2:A11,
    LAMBDA(n,
    LET(e,
    INT(
        n*10^-SEQUENCE(
            LEN(
                n
            ),
            ,
            0
        )
    ),
    n/AND(MAP(e,
    LAMBDA(a,
    LET(x,
    (a+1)/6,
    y,
    (a-1)/6,
    OR(
        a=3,
        a=2,
        x=INT(
            x
        ),
        y=INT(
            y
        )
    )))))))),
    3)
Excel solution 8 for List all Right Truncatable Primes, proposed by Charles Roldan:
=LET(
 B, LAMBDA(f, LAMBDA(g, LAMBDA(x, f(g(x))))),
 M, LAMBDA(f, LAMBDA(x, MAP(x, f))),
 F, LAMBDA(f, LAMBDA(x, FILTER(x, M(f)(x)))),

 All, B(F)(B(LAMBDA(x, AND(x)))),
 Primes, B(M(LAMBDA(n, IFERROR(AND(MOD(n, DROP(SEQUENCE(SQRT(n)), 1))), n > 1)))),
 Pieces, LAMBDA(n, --LEFT(n, SEQUENCE(LEN(n)))),

 All(Primes(Pieces))
)(A2:A11)
Excel solution 9 for List all Right Truncatable Primes, proposed by Julien Lacaze:
=LET(
    data,
    A2:A10,
     
    isPrime,
    LAMBDA(
        value,
        SUM(
            N(
                MOD(
                    value,
                    SEQUENCE(
                        SQRT(
                            value
                        )
                    )
                )=0
            )
        )=1
    ),
     
    isRightPrime,
    LAMBDA(
        n,
        f,
        IF(
            LEN(
                n
            )=1,
            isPrime(
                n
            ),
            f(
                QUOTIENT(
                    n,
                    10
                ),
                f
            )
        )
    ),
     
    FILTER(
        data,
        MAP(
            data,
            LAMBDA(
                d,
                isRightPrime(
                    d,
                    isRightPrime
                )
            )
        )
    )
)
Excel solution 10 for List all Right Truncatable Primes, proposed by Pieter de Bruijn:
=TOCOL(
    MAP(
        A2:A11,
        LAMBDA(
            a,
            a/AND(
                MAP(
                    LEN(
                        a
                    )-SEQUENCE(
                        LEN(
                        a
                    )-1
                    ),
                    LAMBDA(
                        d,
                        AND(
                            ISERROR(
                                FIND(
                                    0,
                                    a
                                )
                            ),
                            MOD(
                                LEFT(
                                    a,
                                    d
                                )/SEQUENCE(
                                    LEFT(
                                    a,
                                    d
                                )^0.5,
                                    ,
                                    2
                                ),
                                1
                            )
                        )
                    )
                )
            )
        )
    ),
    2
)

or since any number containing a 0 will have a number ending with -0  between the checks for primes,
     the multiple of 10 will be divisable (by at least 2 and 5 (and 10)),
     therefore we can skip the ISERROR(
                                FIND(
                                    0,
                                    a
                                )
                            ) part:

=TOCOL(
    MAP(
        A2:A11,
        LAMBDA(
            a,
            a/AND(
                MAP(
                    LEN(
                        a
                    )-SEQUENCE(
                        LEN(
                        a
                    )-1
                    ),
                    LAMBDA(
                        d,
                        AND(
                            MOD(
                                LEFT(
                                    a,
                                    d
                                )/SEQUENCE(
                                    LEFT(
                                    a,
                                    d
                                )^0.5,
                                    ,
                                    2
                                ),
                                1
                            )
                        )
                    )
                )
            )
        )
    ),
    2
)
Excel solution 11 for List all Right Truncatable Primes, proposed by Giorgi Goderdzishvili:
=LAMBDA(x,MAP(x,
LAMBDA(y, 
IF(OR(y=2,y=3,y=1),1,IF(SUM(--(MOD(y,SEQUENCE(INT(y^0.5)-3,,3,1))=0))>0,0,1)))))

Solution:
=TOCOL(MAP(A2:A11,LAMBDA(x,LET(
nm,x,
sq,SUM( IFERROR(isPrime(--MID(nm,1,SEQUENCE(,LEN(nm),LEN(nm),-1))),1)),
nm/(sq=LEN(nm))))),3)
Excel solution 12 for List all Right Truncatable Primes, proposed by Abdelrahman Omer, MBA, PMP:
=TOCOL(MAP(A2:A11,LAMBDA(a,LET(b,M&ID(a,1,SEQUENCE(LEN(a),,LEN(a),-1)),c,(MID(a,SEQUENCE(LEN(a)),1)+0<>0),d,IF(b=2,TRUE,IF(AND(MOD(b,ROW(INDIRECT("2:"&ROUNDUP(SQRT(b),0))))<>0),TRUE,FALSE)),e,SUM(c*d),FILTER(a,e>0)))),3)
Excel solution 13 for List all Right Truncatable Primes, proposed by Daniel Garzia:
=TOCOL(
    MAP(
        A2:A11,
        LAMBDA(
            x,
            x/AND(
                MAP(
                    0+LEFT(
                        x,
                        SEQUENCE(
                            LEN(
                                x
                            ),
                            ,
                            LEN(
                                x
                            ),
                            -1
                        )
                    ),
                    LAMBDA(
                        r,
                        AND(
                            ISERR(
                                FIND(
                                    0,
                                    r
                                )
                            ),
                            MOD(
                                r,
                                SEQUENCE(
                                    ROUNDUP(
                                        r^0.5,
                                        
                                    )-1,
                                    ,
                                    2
                                )
                            )
                        )
                    )
                )
            )
        )
    ),
    2
)
Excel solution 14 for List all Right Truncatable Primes, proposed by samir tobeil:
=1,
    
SUM(--(MOD(
    x,
    SEQUENCE(
        9
    )
)=0))=1),
    
--CONCAT(
    FIND(
        MID(
            x,
            ROW(
                1:20
            ),
            1
        ),
        "13579"
    )
),
    "a")))))
Excel solution 15 for List all Right Truncatable Primes, proposed by Jeff Blakley:
=LET(isPrime,
     LAMBDA(num,
     IFS(num<10,
     OR(
         num={2,
         3,
         5,
         7}
     ),
     OR(
         MOD(
             num,
              2
         )=0,
          MOD(
              num,
              3
          )=0
     ),
     FALSE,
     1,
     SUM(--MAP(SEQUENCE(ROUNDUP((INT(
         SQRT(
             num
         )
     )-4)/6,
     0),
    ,
    5,
    6),
     LAMBDA(
         i,
          OR(
              MOD(
                  num,
                   i
              )=0,
               MOD(
                   num,
                    i+2
               )=0
          )
     )))=0)),
    
 criteria,
     MAP(
         A2:A11,
          LAMBDA(
              x,
               SUM(
                   --MAP(
                       --LEFT(
                           x,
                            SEQUENCE(
                                LEN(
                                    x
                                )
                            )
                       ),
                        LAMBDA(
                            y,
                             NOT(
                                 isPrime(
                                     y
                                 )
                             )
                        )
                   )
               )=0
          )
     ),
    
 FILTER(
     A2:A11,
      criteria
 ))

Solving the challenge of List all Right Truncatable Primes with Python

Python solution 1 for List all Right Truncatable Primes, proposed by Alejandro Simón 🇵🇦 🇪🇸:
                    
                      
  
                  
    
      
        Show translation
      
      

Solving the challenge of List all Right Truncatable Primes with Python in Excel

Python in Excel solution 1 for List all Right Truncatable Primes, proposed by John V.:
Hi everyone!
One (Python) option could be:
Blessings!
                    
                  
Python in Excel solution 2 for List all Right Truncatable Primes, proposed by JvdV -:
With PY():
from sympy import *
list(filter(lambda n:all([isprime(int(str(n)[:x+1])) for x in range(len(str(n)))]), xl("A2:A11")[0]))

Solving the challenge of List all Right Truncatable Primes with R

R solution 1 for List all Right Truncatable Primes, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
input = read_excel("Right Truncatable Primes.xlsx") %>% select(1)
is_prime <- function(n) {
 if (n <= 1) {
 return(FALSE)
 }
 if (n <= 3) {
 return(TRUE)
 }
 if (n %% 2 == 0 || n %% 3 == 0) {
 return(FALSE)
 }
 i <- 5
 while (i * i <= n) {
 if (n %% i == 0 || n %% (i + 2) == 0) {
 return(FALSE)
 }
 i <- i + 6
 }
 return(TRUE)
}
 
is_right_truncatable_prime <- function(n) {
 while (n > 0) {
 if (!is_prime(n)) {
 return(FALSE)
 }
 n <- n %/% 10
 }
 return(TRUE)
}
result = input %>%
 mutate(is_rtp = map_lgl(Numbers, is_right_truncatable_prime)) %>%
 filter(is_rtp == TRUE) %>%
 select(Numbers)
                    
                  

&&

Leave a Reply