Home » Cyclops and Triangular Numbers

Cyclops and Triangular Numbers

A Cyclops number is a number that has a zero in the center (so, it needs to have odd number of digits and >=3 digits). The 0 should not appear anywhere else other than in center. Hence, 12035 is a Cyclops number but 12005 is not as there are more than one 0s. Nth Triangular number is calculated by N(N+1)/2. Hence, 1, 3, 6, 10, 15….are Triangular numbers. Find the list of first 100 Cyclops Triangular numbers i.e. which are both Cyclops as well as Triangular.

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

Solving the challenge of Cyclops and Triangular Numbers with Power Query

Power Query solution 1 for Cyclops and Triangular Numbers, proposed by Aditya Kumar Darak 🇮🇳:
let
  Generate = List.Generate(
    () => [a = 0, b = 0, e = false, f = 0], 
    each [f] < 101, 
    each [
      a = [a] + 1, 
      b = a * (a + 1) / 2, 
      c = Text.From(b), 
      d = Text.Length(c), 
      e = Number.IsOdd(d)
        and (Text.At(c, Number.RoundDown(d / 2)) = "0")
        and (Text.Length(Text.Select(c, "0")) = 1), 
      f = [f] + Number.From(e)
    ], 
    each [Expected Number = [b], TF = [e]]
  ), 
  Table = Table.FromRecords(Generate), 
  Return = Table.SelectRows(Table, each [TF])[[Expected Number]]
in
  Return
Power Query solution 2 for Cyclops and Triangular Numbers, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
  Source = List.Select(
    List.Transform({101 .. 10000000}, each {_, Text.From(_)}), 
    each Text.Length(Text.Select(_{1}, "0")) = 1
  ), 
  Impar = List.Select(Source, each Number.IsOdd(Text.Length(_{1}))), 
  Lista = List.Transform(
    List.Select(Impar, each Text.Range(_{1}, Number.RoundDown(Text.Length(_{1}) / 2), 1) = "0"), 
    each _{0}
  ), 
  Triang = List.Transform({1 .. 1000000}, each _ * (_ + 1) / 2), 
  Sol = List.FirstN(List.Intersect({Triang, Lista}), 100)
in
  Sol
Power Query solution 3 for Cyclops and Triangular Numbers, proposed by Alexis Olson:
let
  L = {1 .. 4000}, 
  T = List.Transform(L, (n) => n * (n + 1) / 2), 
  IsCyclops = (n as number) =>
    let
      digits   = Text.ToList(Number.ToText(n)), 
      len      = List.Count(digits), 
      mid      = if Number.IsOdd(len) then (len - 1) / 2 else null, 
      all_eyes = List.PositionOf(digits, "0", Occurrence.All)
    in
      all_eyes = {mid}, 
  Result = List.Select(T, IsCyclops)
in
  List.FirstN(Result, 100)
Power Query solution 4 for Cyclops and Triangular Numbers, proposed by Ramiro Ayala Chávez:
let
  T = List.Transform, 
  L = Text.Length, 
  A = Text.At, 
  S = List.Select, 
  a = List.Generate(() => [i = 1], each [i] <= 5000, each [i = [i] + 1], each [i] / 2 * ([i] + 1)), 
  b = S(T(a, Text.From), each Number.IsOdd(L(_)) and Text.Contains(_, "0")), 
  c = T(
    b, 
    each 
      if L(_) = 3 and A(_, 1) = "0" or L(_) = 5 and A(_, 2) = "0" or L(_) = 7 and A(_, 3) = "0" then
        _
      else
        null
  ), 
  d = T(
    c, 
    each 
      if L(_)
        = 5 and A(_, 1)
        = "0" or L(_)
        = 5 and A(_, 3)
        = "0" or L(_)
        = 7 and A(_, 1)
        = "0" or L(_)
        = 7 and A(_, 2)
        = "0" or L(_)
        = 7 and A(_, 4)
        = "0" or L(_)
        = 7 and A(_, 5)
        = "0"
      then
        null
      else
        _
  ), 
  e = List.RemoveNulls(S(d, each Text.End(_, 1) <> "0")), 
  f = T(List.FirstN(e, 100), Number.From), 
  Sol = Table.FromColumns({f}, {"Expect Answer"})
in
  Sol
Power Query solution 5 for Cyclops and Triangular Numbers, proposed by Eric Laforce:
let
  fxIsCyclop = (n as number) =>
    let
      digits = Text.ToList(Number.ToText(n)), 
      nD     = List.Count(digits), 
      zPos   = List.PositionOf(digits, "0", Occurrence.All)
    in
      List.Count(zPos) = 1 and Number.IsOdd(nD) and (zPos{0} = Number.RoundDown(nD / 2)), 
  List = List.Generate(
    () => [n = 1, r = {}], 
    each List.Count([r]) <= 100, 
    each 
      let
        _n = [n] + 1, 
        _t = _n * (_n + 1) / 2
      in
        [n = _n, r = [r] & (if fxIsCyclop(_t) then {_t} else {})]
  ), 
  Result = Table.FromColumns({List.Last(List)[r]}, {"Expect Answer"})
in
  Result
Power Query solution 6 for Cyclops and Triangular Numbers, proposed by Alexandra Popoff:
let
  Max_Result = 100, 
  Cacl_Opt = Table.FromList(
    List.Generate(
      () => [Output = 0, z_temp = 0, z_test = false, z_Len = 0, z_i = 1, z_count = 0], 
      each [z_count] <= 100, 
      each [
        z_temp = Number.ToText(([z_i] * ([z_i] + 1)) / 2, "0"), 
        z_Len = Text.Length(Text.From(z_temp)), 
        z_test = (Text.Length(z_temp) - Text.Length(Text.Replace(z_temp, "0", "")))
          = 1 and Text.End(Text.Start(z_temp, Number.RoundUp(z_Len / 2, 0)), 1)
          = "0" and Number.IsOdd(z_Len), 
        z_count = if z_test then [z_count] + 1 else [z_count], 
        Output = if z_test then Number.FromText(z_temp) else 0, 
        z_i = [z_i] + 1
      ], 
      each [Output]
    ), 
    Splitter.SplitByNothing(), 
    {"Result"}
  ), 
  Keep_Result = Table.SelectRows(Cacl_Opt, each ([Result] <> 0))
in
  Keep_Result

Solving the challenge of Cyclops and Triangular Numbers with Excel

Excel solution 1 for Cyclops and Triangular Numbers, proposed by Bo Rydobon 🇹🇭:
=LET(n,
    SEQUENCE(
        3100
    ),
    m,
    n*(n+1)/2,
    TAKE(TOCOL(m/(FIND(
        0,
        m
    )*2-1=LEN(
        m
    ))/ISNA(
        TEXTAFTER(
            m,
            0,
            2
        )
    ),
    3),
    100))
Excel solution 2 for Cyclops and Triangular Numbers, proposed by Rick Rothstein:
=LET(s,
    SEQUENCE(
        5000
    ),
    n,
    s*(s+1)/2,
    c,
    LEN(
        n
    ),
    TAKE(TOCOL(IF((c>4)*ISODD(
        c
    )*(c-LEN(
        SUBSTITUTE(
            n,
            0,
            
        )
    )=1)*(MID(
        n,
        1+c/2,
        1
    )="0"),
    n,
    1/0),
    3),
    100))
Excel solution 3 for Cyclops and Triangular Numbers, proposed by Rick Rothstein:
=LET(s,
    SEQUENCE(
        5000
    ),
    n,
    s*(s+1)/2,
    c,
    LEN(
        n
    ),
    x,
    n*(c>4)*ISODD(
        c
    )*(c-LEN(
        SUBSTITUTE(
            n,
            0,
            
        )
    )=1)*(-MID(
        n,
        
        1+c/2,
        1
    )=0),
    TAKE(
        FILTER(
            x,
            x
        ),
        100
    ))
Excel solution 4 for Cyclops and Triangular Numbers, proposed by John V.:
=TOCOL(LET(n,
    ROW(
        1:3023
    ),
    MAP((n^2+n)/2,
    LAMBDA(
        x,
        x/AND(
            LEN(
                TEXTSPLIT(
                    x,
                    0
                )
            )*{2,
            2}=LEN(
                x
            )-1
        )
    ))),
    2)
Excel solution 5 for Cyclops and Triangular Numbers, proposed by محمد حلمي:
=LET(n,
    SEQUENCE(
        3040
    ),
    j,
    n*(n+1)/2,
    
FILTER(j,
    MAP(j,
    LAMBDA(a,
    LET(i,
    LEN(
        a
    )/2,
    IFERROR(
        1-FIND(
            0,
            LEFT(
                a,
                i
            )
        )^0,
        1
    )*
IFERROR(
    1-FIND(
        0,
        RIGHT(
                a,
                i
            )
    )^0,
    1
)*(MID(
    a,
    i+0.5,
    1
)="0"))))))
Excel solution 6 for Cyclops and Triangular Numbers, proposed by Kris Jaganah:
=LET(a,
    SEQUENCE(
        999
    ),
    b,
    SORT(
        --TOCOL(
            a&0&TOROW(
                a
            )
        )
    ),
    c,
    LEN(
        b
    ),
    d,
    MOD((-1+(1+(8*b))^0.5)/2,
    1)=0,
    e,
    FIND(
        0,
        b
    )=(c/2)+0.5,
    f,
    (c-LEN(
        SUBSTITUTE(
            b,
            "0",
            ""
        )
    ))=1,
    g,
    MOD(
        c,
        2
    ),
    TAKE(
        FILTER(
            b,
            d*e*f*g
        ),
        100
    ))
Excel solution 7 for Cyclops and Triangular Numbers, proposed by Julian Poeltl:
=TAKE(LET(N,
    SEQUENCE(
        100000,
        ,
        14
    ),
    T,
    N*(N+1)/2,
    LT,
    LEN(
        T
    ),
    CZ,
    LT-LEN(
        SUBSTITUTE(
            T,
            "0",
            ""
        )
    ),
    MZ,
    RIGHT(
        LEFT(
            T,
            LT/2+1
        ),
        1
    )="0",
    FILTER(T,
    (CZ=1)*MZ*ISODD(
        LT
    ))),
    100)
Excel solution 8 for Cyclops and Triangular Numbers, proposed by Timothée BLIOT:
=LET(A,
    SEQUENCE(
        10^4
    ),
    B,
    A*(A+1)/2,
    C,
    LEN(
        B
    ),
    TAKE(FILTER(B,
    (MID(
        B,
         FLOOR(
             C/2,
             1
         )+1,
        1
    )="0")*(ISODD(
        C
    ))*(LEN(
        SUBSTITUTE(
            B,
            "0",
            ""
        )
    )=C-1)),
    100))
Excel solution 9 for Cyclops and Triangular Numbers, proposed by Hussein SATOUR:
=LET(a,
    SEQUENCE(
        457990,
        ,
        10
    ),
    b,
    FILTER(a,
    (ISEVEN(
        LEN(
            a
        )
    ))*(ISERR(
        FIND(
            0,
            a
        )
    ))),
    c,
    --REPLACE(
        b,
        LEN(
            b
        )/2+1,
        0,
        0
    ),
    
d,
    INT(
        SQRT(
            c*2
        )
    ),
    FILTER(c,
    c*2=d*(d+1)))
Excel solution 10 for Cyclops and Triangular Numbers, proposed by Sunny Baggu:
=LET(
 _s,
     SEQUENCE(
         3073
     ),
    
 _n,
     _s * (_s + 1) / 2,
    
 _cri,
     (ISODD(
         LEN(
             _n
         )
     )) * (LEN(
             _n
         ) >= 3) *
 (MID(
     _n,
      ROUNDUP(
          LEN(
             _n
         ) / 2,
           0
      ),
      1
 ) = "0") *
 (1 = MAP(
     _n,
      LAMBDA(
          a,
           SUM(
               N(
                   MID(
                       a,
                        SEQUENCE(
                            LEN(
                                a
                            )
                        ),
                        1
                   ) = "0"
               )
           )
      )
 )),
    
 TAKE(
     FILTER(
         _n,
          _cri
     ),
      100
 )
)
Excel solution 11 for Cyclops and Triangular Numbers, proposed by LEONARD OCHEA 🇷🇴:
=LET(s,
    SEQUENCE(
        3100
    ),
    TAKE(TOCOL(MAP(s*(s+1)/2,
    LAMBDA(a,
    LET( m,
    (LEN(
        a
    )+1)/2,
    a/AND(
        INT(
            m
        )=m,
        --MID(
            a,
            m+{-1,
            1}*SEQUENCE(
                m-1
            ),
            1
        ),
        --MID(
            a,
            m,
            1
        )=0
    )))),
    3),
    100))
Excel solution 12 for Cyclops and Triangular Numbers, proposed by Charles Roldan:
=LET(
 First,
     LAMBDA(n,
    f,
     
SCAN(0,
     SEQUENCE(
         n
     ),
     
LAMBDA(
    g,
     g(
         g
     )
)(LAMBDA(
    g,
     LAMBDA(
         n,
         [_],
          
         IF(
             f(
                 n + 1
             ),
              n + 1,
              g(
         g
     )(
                 n + 1
             )
         )
     )
)))),
     

 B,
     LAMBDA(
         f,
         g,
          LAMBDA(
              n,
               
              f(
                  g(
         n
     )
              )
          )
     ),
     

 Triangle,
     LAMBDA(
         n,
          
         n * (
                 n + 1
             ) / 2
     ),
    

 isCyclops,
     LAMBDA(n,
     
LET(f,
     LAMBDA(
         k,
          IFERROR(
              SEARCH(
                  "0",
                   n,
                   k + 1
              ),
               
          )
     ),
     
AND(f(
    HSTACK(
        0,
         f(
             0
         )
    )
) * 2 = {1,
    0} * (LEN(
         n
     ) + 1)))),
    

Triangle(
    First(
        100,
         B(
             isCyclops,
              Triangle
         )
    )
)
)
Excel solution 13 for Cyclops and Triangular Numbers, proposed by Charles Roldan:
=LET(
 First,
     LAMBDA(f,
     LAMBDA(n,
    
 SCAN(0,
     SEQUENCE(
         n
     ),
     LAMBDA(
         g,
          g(
              g
          )
     )(LAMBDA(
         g,
          LAMBDA(
              n,
               [_],
               IF(
                   f(
                       n + 1
                   ),
                    n + 1,
                    g(
              g
          )(
                       n + 1
                   )
               )
          )
     ))))),
    

 Triangle,
     LAMBDA(
         n,
          
          SERIESSUM(
              n,
               1,
               1,
               {1,
              1}
          ) / 2
     ),
    

 isCyclops,
     LAMBDA(
         n,
         
          AND(
              HSTACK(
                  2 * IFERROR(
                      SEARCH(
                          "0",
                           n
                      ),
                       
                  ),
                   LEN(
                       SUBSTITUTE(
                           n,
                            "0",
                            
                       )
                   )
              ) = LEN(
         n
     ) + {1,
               -1}
          )
     ),
    

 B,
     LAMBDA(
         f,
          g,
          LAMBDA(
              n,
               
               f(
                   g(
         n
     )
               )
          )
     ),
    

 B(
     Triangle,
      First(
          B(
              isCyclops,
               Triangle
          )
      )
 )
)(100)
Excel solution 14 for Cyclops and Triangular Numbers, proposed by Giorgi Goderdzishvili:
= 1
 while True:
 triangular_number = n * (n + 1) // 2
 if triangular_number == num:
 return True
 elif triangular_number > num:
 return False
 n += 1
def is_cyclops(
    num
):
 t = str(
    num
)
 mdl = len(
     t
 ) // 2 
 
 cyc_check = all(
     [t[mdl] == "0",
      t.count(
          '0'
      ) == 1]
 )
 
 if len(
     t
 ) % 2 == 0:
 return False
 elif cyc_check:
 return True
 else:
 return False
 
# Output
fin = []
counter = 1
N = 100
while len(
    fin
)
Excel solution 15 for Cyclops and Triangular Numbers, proposed by LUIS FLORENTINO COUTO CORTEGOSO:
=LET(a,
    ROW(
        1:10000
    ),
    t,
    a*(a+1)/2,
    l,
    LEN(
        t
    ),
    b,
    TOCOL(t/((ISODD(
        l
    ))*((l-LEN(
        SUBSTITUTE(
            t,
            "0",
            ""
        )
    ))=1)*(MID(
        t,
        INT(
            l/2
        )+1,
        1
    )="0")),
    3),
    TAKE(
        b,
        100
    ))

other formula:

=LET(s,
    ROW(
        1:9
    ),
    g,
    LAMBDA(y,
    LET(n,
    ((1+8*y)^0.5-1)/2,
    n=INT(
        n
    ))),
    f,
    LAMBDA(
        x,
        TOCOL(
            TOROW(
                s
            )&TOCOL(
                x&TOROW(
                s
            )
            )
        )
    ),
    a,
    f(
        0
    ),
    b,
    f(
        a
    ),
    c,
    f(
        b
    ),
    d,
    VSTACK(
        a,
        b,
        c
    ),
    TAKE(
        SORT(
            TOCOL(
                d/MAP(
                    d,
                    g
                ),
                3
            )
        ),
        100
    ))
Excel solution 16 for Cyclops and Triangular Numbers, proposed by Ernesto Vega Castillo:
=TAKE(LET(f,
    LET(sec,
    SEQUENCE(
        3500
    ),
    num,
    sec*(sec+1)/2,
    cic,
    LEN(
        num
    ),
    IFERROR(IF((cic>1)*ISODD(
        cic
    )*(cic-LEN(
        SUBSTITUTE(
            num,
            0,
            ""
        )
    )=1)*(MID(
        num,
        1+cic/2,
        1
    )="0"),
    num,
    1/0),
    "")),
    FILTER(
        f,
        f<>""
    )),
    100)
Excel solution 17 for Cyclops and Triangular Numbers, proposed by Tyler Cameron:
=LET(a,SEQUENCE(4000),b,a*(a+1)/2,c,MAP(b,LAMBDA(x,IF(AND(ISODD(LEN(x)),--MID(x,ROUNDUP(LEN(x)/2,0),1)=0,SUM(--(--MID(x,SEQUENCE(LEN(x)),1)=0))=1),x,""))),INDEX(FILTER(c,c<>""),SEQUENCE(100)))
Excel solution 18 for Cyclops and Triangular Numbers, proposed by Alexandra Popoff:
= LAMBDA(Max_Resul,
    [Max_Seq],
    
LET(
 z_Max_Seq,
     if(
         isomitted(
             Max_Seq
         ),
         10000,
         Max_Seq
     ),
    
 z_Input,
     SEQUENCE(
         z_Max_Seq,
          1,
          1,
          1
     ),
    
 z_Numb_Temp,
     TEXT(z_Input * (z_Input + 1) / 2,
     "0"),
    
 z_Numb,
     FILTER(z_Numb_Temp,
     (LEN(
         z_Numb_Temp
     ) > 2) * (ISODD(
         LEN(
         z_Numb_Temp
     )
     ))),
    
 z_Test,
     SCAN(
         0,
         z_Numb,
         LAMBDA(
             x_Count,
              z_i,
             
              IF(
                  x_Count >= Max_Resul,
                  
                   Max_Resul,
                  x_Count + N(
                      
                       LET(
                           
                            x_temp,
                            z_i,
                           
                            x_Len,
                            LEN(
                                INDEX(
                                    x_temp,
                                     1,
                                     1
                                )
                            ),
                           
                            x_Seq,
                            SEQUENCE(
                                x_Len,
                                 1,
                                 1,
                                 1
                            ),
                           
                            x_Arr,
                            BYROW(
                                SEQUENCE(
                                x_Len,
                                 1,
                                 1,
                                 1
                            ),
                                 LAMBDA(
                                     y_i,
                                      RIGHT(
                                          LEFT(
                                              x_temp,
                                               y_i
                                          ),
                                           1
                                      )
                                 )
                            ),
                           
                            AND(
                                INDEX(
                                    x_Arr,
                                     ROUNDUP(
                                         x_Len / 2,
                                          0
                                     ),
                                     1
                                ) = "0",
                                IFERROR(
                                    ROWS(
                                        FILTER(
                                            x_Arr,
                                             x_Arr = "0"
                                        )
                                    ),
                                     0
                                ) = 1
                            )
                            
                       )
                  )
              )
         )
     ),
    
 z_Out,
     VALUE(FILTER(z_Numb,
     (z_Test - VSTACK(
         0,
          DROP(
              z_Test,
               -1
          )
     )) = 1)),
    
 IF(
     ROWS(
         z_Out
     ) < Max_Resul,
     
      "z_Max Seq too low",
     
      z_Out
 )
))

Solving the challenge of Cyclops and Triangular Numbers with Python

Python solution 1 for Cyclops and Triangular Numbers, proposed by Cristobal Salcedo Beltran:
Code:
import pandas as pd
def is_cyclops(n):
 s = str(n)
 length = len(s)
 if length % 2 == 0 or length < 3:
 return False
 middle_index = length // 2
 return s[middle_index] == '0' and '0' not in s[:middle_index] + s[middle_index+1:]
num_range = pd.DataFrame({'id': range(1, 10000)}) 
num_range['Expected Number'] = num_range['id'] * (num_range['id'] + 1) // 2
cyclops_triangular_numbers = num_range[num_range['Expected Number'].apply(is_cyclops)].drop('id', axis=1)
print(cyclops_triangular_numbers.head(100))
                    
                  
Python solution 2 for Cyclops and Triangular Numbers, proposed by Cristobal Salcedo Beltran:
Code:
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, udf
from pyspark.sql.types import BooleanType, LongType
spark = SparkSession.builder.appName("CyclopsTriangularNumbers").getOrCreate()
def is_cyclops(n):
 s = str(n)
 length = len(s)
 if length % 2 == 0 or length < 3:
 return False
 middle_index = length // 2
 return s[middle_index] == '0' and '0' not in s[:middle_index] + s[middle_index+1:]
is_cyclops_udf = udf(is_cyclops, BooleanType())
num_range = spark.range(1, 10000) 
triangular_numbers = num_range.withColumn("Expected Number", (col("id") * (col("id") + 1) / 2).cast(LongType()))
cyclops_triangular_numbers = triangular_numbers.filter(is_cyclops_udf("Expected Number")).drop("id")
cyclops_triangular_numbers.show(100)
                    
                  

Solving the challenge of Cyclops and Triangular Numbers with Python in Excel

Python in Excel solution 1 for Cyclops and Triangular Numbers, proposed by Abdallah Ally:
(Influenced by Konrad Gryczan, PhD link he posted)
import pandas as pd
from math import sqrt
file_path = 'Excel_Challenge_415 - Triangular Cyclops Numbers.xlsx'
df = pd.read_excel(file_path)
# Create a function to generate the
def triangular_cyclops(n):
 numbers = []
 number = 100
 while len(numbers) != n:
 number += 1
 num_str = str(number)
 if len(num_str) % 2:
 single_zero = num_str.count('0') == 1
 middle_zero = num_str[int(len(num_str) / 2)] == '0'
 odd_length = (len(num_str) % 2) == 1
 triangular = int(sqrt(number * 8 + 1)) == sqrt(number * 8 + 1)
 else:
 continue
 if all([single_zero, middle_zero, odd_length, triangular]):
 numbers.append(number)
 return numbers
# Add results column
df['My Answer'] = pd.Series(triangular_cyclops(100))
print(f'n{df.head()}nn{df.tail()}')
                    
                  
Python in Excel solution 2 for Cyclops and Triangular Numbers, proposed by Abdallah Ally:
import pandas as pd
from math import sqrt
file_path = 'Excel_Challenge_415 - Triangular Cyclops Numbers.xlsx'
df = pd.read_excel(file_path)
# Create a function to generate the
def triangular_cyclops(n):
 numbers = []
 number = 100
 while len(numbers) != n:
 number += 1
 num_str = str(number)
 if len(num_str) % 2:
 single_zero = num_str.count('0') == 1
 middle_zero = num_str[int(len(num_str) / 2)] == '0'
 odd_length = (len(num_str) % 2) == 1
 triangular = False
 for i in range(1, int(sqrt(2 * number)) + 2):
 if i * (i + 1) == 2 * number:
 triangular = True
 break
 else:
 continue
 if all([single_zero, middle_zero, odd_length, triangular]):
 numbers.append(number)
 return numbers
df['My Answer'] = pd.Series(triangular_cyclops(100))
print(f'n{df.head()}n{df.tail()})
                    
                  
Python in Excel solution 3 for Cyclops and Triangular Numbers, proposed by JvdV -:
=PY() in ms365:
import regex as re
[n for n in [int(i*(i+1)/2) for i in range(3024)] if re.match(r'(.(?=[^0]*0[^0]*$)0?(?1)?[^0])$',str(n))]
                    
                  

Solving the challenge of Cyclops and Triangular Numbers with R

R solution 1 for Cyclops and Triangular Numbers, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
test = read_excel("Excel/415 Triangular Cyclops Numbers.xlsx", range = "A1:A101" )
range = 1:1e7
is_triangular <- function(x) {
 n <- (-1 + sqrt(1 + 8 * x)) / 2
 n == floor(n)
}
r = data.frame(n = range) %>%
 mutate(nchar = nchar(n)) %>%
 filter(nchar %% 2 == 1) %>%
 mutate(zeroes = str_count(n, "0"), 
 central = substr(n, nchar/2+1, nchar/2+1)) %>%
 filter(zeroes == 1, 
 central == "0") %>%
 mutate(triangular = is_triangular(n)) %>%
 filter(triangular == TRUE) %>%
 head(100) %>%
 mutate(n = as.numeric(n))
https://github.com/kgryczan/excelbi_puzzles/blob/main/Excel/415%20Challenge.R
                    
                  

&&

Leave a Reply