Home » Triangle of Odd Numbers

Triangle of Odd Numbers

Generate the triangle of odd numbers as given as per number of rows given in cell B2. If B2 = 2 1 3  5 if B2 = 4 1 3  5 7  9  11 13  15 17  19

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

Solving the challenge of Triangle of Odd Numbers with Power Query

Power Query solution 1 for Triangle of Odd Numbers, proposed by Omid Motamedisedeh:
let
  m = Excel.CurrentWorkbook(){[Name = "size"]}[Content][Column1]{0}, 
  Custom1 = Table.FromRows(
    List.Transform(
      {0 .. (m - 1)}, 
      each List.Repeat({" "}, m - _ - 1)
        & List.Transform(
          {(_ * (_ + 1) + 1) .. ((_ + 1) * (_ + 2) - 1)}, 
          (ox) => if Number.IsEven(ox) then " " else ox
        )
        & List.Repeat({" "}, m - _ - 1)
    )
  )
in
  Custom1
Power Query solution 2 for Triangle of Odd Numbers, proposed by Bo Rydobon 🇹🇭:
let
  S = 5, 
  Ans = Table.FromRows(
    List.Transform(
      {1 .. S}, 
      (r) =>
        List.Transform(
          {1 .. S * 2 - 1}, 
          (c) => if Number.Abs(c - S) < r and Number.IsOdd(c - r + S) then r * r + c - S else null
        )
    )
  )
in
  Ans
Power Query solution 3 for Triangle of Odd Numbers, proposed by Zoran Milokanović:
let
  Source = Excel.CurrentWorkbook(){[Name = "Input"]}[Content], 
  B2 = Number.From(Table.DemoteHeaders(Source){0}[Column2]), 
  Solution = Table.FromRows(
    List.Transform(
      {1 .. B2}, 
      each 
        let
          e = List.Repeat({null}, B2 - _), 
          n = List.Accumulate(
            List.LastN(
              List.FirstN(
                List.Transform({1 .. List.Sum({1 .. B2})}, each _ * 2 - 1), 
                List.Sum({1 .. _})
              ), 
              _
            ), 
            {}, 
            (s, c) => s & (if s = {} then s else {null}) & {c}
          )
        in
          e & n & e
    )
  )
in
  Solution

Solving the challenge of Triangle of Odd Numbers with Excel

Excel solution 1 for Triangle of Odd Numbers, proposed by Bo Rydobon 🇹🇭:
=LET(s,
    B1,
    r,
    SEQUENCE(
        s
    ),
    c,
    SEQUENCE(
        ,
        s*2-1
    )-s,
    IF((ABS(
        c
    )
Excel solution 2 for Triangle of Odd Numbers, proposed by Bo Rydobon 🇹🇭:
=LET(
    x,
    B1,
    r,
    DROP(
        DROP(
            REDUCE(
                0,
                SEQUENCE(
                    x
                ),
                LAMBDA(
                    a,
                    v,
                    IFNA(
                        VSTACK(
                            a,
                            HSTACK(
                                SEQUENCE(
                                    ,
                                    x-v+1
                                )*0,
                                SEQUENCE(
                                    ,
                                    v*2,
                                    MAX(
                                        a
                                    )+1
                                )
                            )
                        ),
                        
                    )
                )
            ),
            1,
            1
        ),
        ,
        -1
    ),
    
    IF(
        MOD(
            r,
            2
        ),
        r,
        ""
    )
)
Excel solution 3 for Triangle of Odd Numbers, proposed by Rick Rothstein:
=LET(s,
    SEQUENCE(
        B1
    ),
    TEXTSPLIT(TEXTJOIN("/",
    ,
    MAP(s,
    s*s-s+1,
    LAMBDA(x,
    y,
    LEFT(
        ",",
        MOD(
            B1+x,
            2
        )
    )&REPT(",,",
    (B1-x)/2)&TEXTJOIN(
        ",,",
        ,
        SEQUENCE(
            x,
            ,
            y,
            2
        )
    )))),
    ",",
    "/",
    ,
    ,
    ""))
Excel solution 4 for Triangle of Odd Numbers, proposed by Rick Rothstein:
=TEXTSPLIT(TEXTJOIN("/",
    ,
    MAP(SEQUENCE(
        B1
    ),
    LAMBDA(x,
    IF(
        MOD(
            B1+x,
            2
        ),
        ",",
        ""
    )&REPT(", ,",
    (B1-x)/2)&TEXTJOIN(", ,",
    ,
    DROP(SEQUENCE(,
    x*(x+1)/2,
    ,
    2),
    ,
    x*(x-1)/2))))),
    ",",
    "/",
    ,
    ,
    "")
Excel solution 5 for Triangle of Odd Numbers, proposed by John V.:
=MAKEARRAY(B1,
    2*B1-1,
    LAMBDA(r,
    c,
    REPT(c+r^2-B1,
    (ABS(
        B1-c
    )
Excel solution 6 for Triangle of Odd Numbers, proposed by John V.:
=LET(n,
    2*B1-1,
    b,
    SEQUENCE(
        B1,
        n
    ),
    r,
    LAMBDA(x,
    1+INT((x-1)/n)),
    c,
    LAMBDA(
        x,
        1+MOD(
            x-1,
            n
        )
    ),
    z,
    LAMBDA(
        x,
        AND(
            ABS(
                B1-c(
                    x
                )
            )
Excel solution 7 for Triangle of Odd Numbers, proposed by محمد حلمي:
=LET(
    v,
    SEQUENCE(
        B1
    ),
    s,
    TOROW(
        v
    ),
    DROP(
        REDUCE(
            0,
            v,
            LAMBDA(
                a,
                d,
                IFNA(
                    VSTACK(
                        a,
                        HSTACK(
                            EXPAND(
                                " ",
                                ,
                                B1-d+1
                            ),
                             TEXTSPLIT(
                                 TEXTJOIN(
                                     "--",
                                     ,
                                     INDEX(
                                         IF(
                                             v+1>s,
                                             SCAN(
                                                 0,
                                                 v*2-2,
                                                 LAMBDA(
                                                     a,
                                                     d,
                                                     a+d
                                                 )
                                             )+s*2-1,
                                             ""
                                         ),
                                         d,
                                         
                                     )
                                 ),
                                 "-"
                             )
                        )
                    ),
                    ""
                )
            )
        ),
        1,
        1
    )
)
Excel solution 8 for Triangle of Odd Numbers, proposed by Kris Jaganah:
=LET(
    a,
    B1,
    b,
    IFNA(
        REDUCE(
            TOROW(
                SORT(
                    EXPAND(
                        1,
                        a,
                        ,
                        0
                    )
                )
            ),
            SEQUENCE(
                a-1,
                ,
                2
            ),
            LAMBDA(
                x,
                y,
                VSTACK(
                    x,
                    HSTACK(
                        TOROW(
                            SORT(
                                EXPAND(
                                    SEQUENCE(
                                        y-1,
                                        ,
                                        y^2-1,
                                        -1
                                    ),
                                    a-1,
                                    ,
                                    0
                                )
                            )
                        ),
                        SEQUENCE(
                            ,
                            y,
                            y^2
                        )
                    )
                )
            )
        ),
        0
    ),
    IF(
        MOD(
            b,
            2
        ),
        b,
        ""
    )
)
Excel solution 9 for Triangle of Odd Numbers, proposed by Timothée BLIOT:
=LET(R,
    LAMBDA(
        me,
        n,
        a,
        IF(
            n=0,
            a,
            me(
                me,
                n-1,
                a+n
            )
        )
    ),
    H,
    R(
        R,
        B1,
        0
    ),
    A,
    SEQUENCE(
        H,
        ,
        ,
        2
    ),
    B,
    IFERROR(
        TOCOL(
            HSTACK(
                A,
                ""
            )
        ),
        ""
    ),
    IFERROR(DROP(DROP(REDUCE("",
    SEQUENCE(
        B1,
        ,
        2,
        2
    ),
    LAMBDA(a,
    v,
    VSTACK(a,
    HSTACK(TEXTSPLIT(REPT(":",
    (B1-((v-2)/2))),
    ":"),
    TOROW(
        TAKE(
            DROP(
                B,
                v-2
            ),
            v
        )
    ))))),
    1,
    -1),
    ,
    2),
    ""))
Excel solution 10 for Triangle of Odd Numbers, proposed by Bhavya Gupta:
=LET(n,
    B1,
    r,
    SEQUENCE(
        n
    ),
    c,
    SEQUENCE(
        ,
        n
    ),
    str,
    SCAN(
        1,
        SEQUENCE(
            n,
            ,
            0,
            2
        ),
        LAMBDA(
            x,
            y,
            x+y
        )
    ),
    IFNA(INDEX(2*(IF(
        r>=c,
        c,
        ""
    )-1)+str,
    SEQUENCE(
        n
    ),
    MAKEARRAY(n,
    n*2-1,
    LAMBDA(x,
    y,
    LET(cr,
    y-(n-x+1),
    IF(ISEVEN(
        cr
    )*(cr>=0)*(cr
Excel solution 11 for Triangle of Odd Numbers, proposed by Charles Roldan:
=LAMBDA(n,
     MAKEARRAY(n,
     2 * n - 1,
     LAMBDA(x,
    y,
    
 REPT(x ^ 2 + y - n,
     MOD(
         x + y - n,
          2
     ) * (x > ABS(
         y - n
     )))))
 )(B1)
Excel solution 12 for Triangle of Odd Numbers, proposed by JvdV -:
=TEXTAFTER(
    TEXTBEFORE(
        MAP(
            SEQUENCE(
                B1
            ),
            LAMBDA(
                a,
                LET(
                    c,
                    REPT(
                        "-",
                        B1-a+1
                    ),
                    c&TEXTJOIN(
                        "--",
                        ,
                        SEQUENCE(
                            a,
                            ,
                            a*a+1-a,
                            2
                        )
                    )&c
                )
            )
        ),
        "-",
        SEQUENCE(
            ,
            B1*2-1,
            2
        )
    ),
    "-",
    -1
)
Excel solution 13 for Triangle of Odd Numbers, proposed by Pieter de Bruijn:
=DROP(REDUCE(-1,
    SEQUENCE(
        B1
    ),
    LAMBDA(x,
    y,
    VSTACK(IFERROR(
        x,
        ""
    ),
    TEXTSPLIT(CONCAT(REPT(
        ",",
        B1-y
    ),
    SEQUENCE(,
    y,
    y+(y-1)^2,
    2)&",,"),
    ",")))),
    1,
    -2)
Excel solution 14 for Triangle of Odd Numbers, proposed by Guillermo Arroyo:
=DROP(IFNA(REDUCE(0,
    SEQUENCE(
        B1
    ),
    LAMBDA(i,
    j,
    VSTACK(i,
    HSTACK(TEXTSPLIT(
        REPT(
            " ",
            B1-j+1
        ),
        " "
    ),
    TEXTSPLIT(TEXTJOIN(",,",
    ,
    SEQUENCE(,
    j,
    j*(j-1)+1,
    2)),
    ","))))),
    ""),
    1,
    2)
Excel solution 15 for Triangle of Odd Numbers, proposed by Stevenson Yu:
=SEQUENCE(
    B1
)

Next,
     for every row in,
     say,
     column D,
     I have this equation:

=LET(A,
     REPT(
         "|",
         $B$1-A3
     ),
    
B,
     SEQUENCE(
         ,
         A3,
         ,
         2
     )+(SUM(
         A$2:A2
     ))*2,
    
C,
     A&TEXTJOIN(
         "||",
         ,
         B
     )&A,
    
IFERROR(
    TEXTSPLIT(
        C,
        "|"
    ),
    ""
))

&&&

Leave a Reply