Home » Bi And Trimorphic Numbers

Bi And Trimorphic Numbers

Bi & Trimorphic Numbers – List numbers (n<100000) if n^3 and n^2 both end with n. Ex. 76 => 76^3 = 438976 and 5776 => Both end with 76

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

Solving the challenge of Bi And Trimorphic Numbers with Power Query

Power Query solution 1 for Bi And Trimorphic Numbers, proposed by Kris Jaganah:
= Table.FromColumns( {List.Select( {1..100000} , each [ a = Text.From( _), b= Text.Length(a), c = (x)=> Text.End( Text.From( Number.Power (_,x)),b) ,d = c(2) = c(3) and c(2) = a ] [d ] )},{"Expected Answer"})
Power Query solution 2 for Bi And Trimorphic Numbers, proposed by Abdallah Ally:
let
  IsBiTrimorphicNumber = (num) =>
    [
      a = Text.From(Number.Power(num, 2)), 
      b = Text.From(Number.Power(num, 3)), 
      c = Text.From(num), 
      d = Text.Length(c), 
      e = Text.End(a, d) = c and Text.End(b, d) = c
    ][e], 
  BiTrimorphicNumbers = List.RemoveNulls(
    List.Generate(
      () => [num = 1, cond = IsBiTrimorphicNumber(num)], 
      each [num] < 100000, 
      each [num = [num] + 1, cond = IsBiTrimorphicNumber(num)], 
      each if [cond] then [num] else null
    )
  ), 
  Result = Table.FromColumns({BiTrimorphicNumbers}, {"Expected Answer"})
in
  Result
Power Query solution 3 for Bi And Trimorphic Numbers, proposed by Abdallah Ally:
let
  IsBiTrimorphicNumber = (num) =>
    [
      a = Text.From(Number.Power(num, 2)), 
      b = Text.From(Number.Power(num, 3)), 
      c = Text.From(num), 
      d = Text.Length(c), 
      e = Text.End(a, d) = c and Text.End(b, d) = c
    ][e], 
  Result = Table.FromColumns(
    {List.Select({1 .. 99999}, each IsBiTrimorphicNumber(_))}, 
    {"Expected Answer"}
  )
in
  Result
Power Query solution 4 for Bi And Trimorphic Numbers, proposed by Ramiro Ayala Chávez:
let
  a = {1 .. 99999}, 
  LT = List.Transform, 
  b = LT(a, each {_} & {_ * _} & {_ * _ * _}), 
  c = LT(b, each LT(_, Text.From)), 
  d = List.Select(
    c, 
    each Text.End(_{1}, Text.Length(_{0})) = _{0} and Text.End(_{2}, Text.Length(_{0})) = _{0}
  ), 
  e = LT(d, each _{0}), 
  Sol = Table.FromColumns({e}, {"Expected Answer"})
in
  Sol
Power Query solution 5 for Bi And Trimorphic Numbers, proposed by Meganathan Elumalai:
let
  Source = List.Select(
    {1 .. 100000}, 
    each [
      fx = (n) =>
        (Text.End(Text.From(Number.Power(_, 2)), Text.Length(Text.From(_))) = Text.From(_)), 
      fin = fx(2) and fx(3)
    ][fin]
  )
in
  Source
Power Query solution 6 for Bi And Trimorphic Numbers, proposed by Peter Krkos:
let
  F = (x, y) =>
    List.Max({1, Number.Mod(Number.Power(x, y), Number.Power(10, Number.RoundUp(Number.Log10(x))))})
in
  List.Select({1 .. 99999}, each List.AllTrue(List.Transform({2, 3}, (v) => F(_, v) = _)))
Power Query solution 7 for Bi And Trimorphic Numbers, proposed by Alexandre Garcia:
Table.FromColumns(
  {
    List.Select(
      {1 .. 99999}, 
      (x) =>
        List.Count(
          List.Distinct(
            {x, Number.Power(x, 2), Number.Power(x, 3)}, 
            each Text.End(Text.From(_), Text.Length(Text.From(x)))
          )
        )
          = 1
    )
  }, 
  {"Answer"}
)
Power Query solution 8 for Bi And Trimorphic Numbers, proposed by Tyler N.:
let k=Text.From,a=List.Transform({2,3},(y)=>Text.End(k(Number.Power(x,y)),Text.Length(k(x))))={k(x),k(x)} in a)

Solving the challenge of Bi And Trimorphic Numbers with Excel

Excel solution 1 for Bi And Trimorphic Numbers, proposed by Bo Rydobon 🇹🇭:
=LET(
    s,
    SEQUENCE(
        100000
    ),
    FILTER(
        s,
        BYROW(
            MOD(
                s^{2,
                3},
                10^LEN(
                    s
                )
            )=s,
            AND
        )
    )
)
Excel solution 2 for Bi And Trimorphic Numbers, proposed by Rick Rothstein:
=LET(a,SEQUENCE(100000),FILTER(a,a=MOD(a^2,10^LEN(a))))
Excel solution 3 for Bi And Trimorphic Numbers, proposed by Rick Rothstein:
=0+RIGHT(
    n^2,
    LEN(
        n
    )
) since if that is satisfied,
     then multiplying by n (for the cube test) just repeats the calculation that we just did...

=LET(
    a,
    SEQUENCE(
        100000
    ),
    FILTER(
        a,
        a=0+RIGHT(
            a^2,
            LEN(
                a
            )
        )
    )
)

To demonstrate,
     let's use n=376...
n*n = 141376
so it meets the n-square test. But this can be written as...
n*n= 141000 + 376
So...
n*n*n = (141000 +376) * 376
or...
n*n*n = 141000 *3 76 + 376 * 376
Nothing in the first part of the calculation can affect the calculation in the second part for the right 3 characters (the 3 zeros insure that),
     so the second part of the calculation is just a repeat of the square test (when examining the last 3 digits)
Excel solution 4 for Bi And Trimorphic Numbers, proposed by John V.:
=LET(i,ROW(1:99999),FILTER(i,BYROW(--RIGHT(i^{2,3},LEN(i))=i,AND)))
Excel solution 5 for Bi And Trimorphic Numbers, proposed by Timothée BLIOT:
=LET(A,
    SEQUENCE(
        10^5
    ),
    FILTER(A,
    (A=--(RIGHT(
        A^2,
        LEN(
            A
        )
    )))*(A=--(RIGHT(
        A^3,
        LEN(
            A
        )
    )))))
Excel solution 6 for Bi And Trimorphic Numbers, proposed by Oscar Mendez Roca Farell:
=LET(n,SEQUENCE(10^5),TOCOL(n/BYROW(-RIGHT(n^{2,3},LEN(n))=-n,AND),2))
Excel solution 7 for Bi And Trimorphic Numbers, proposed by Duy Tùng:
[i for i in range(
    1,
     100000
) if str(
    i**2
).endswith(
    str(
        i
    )
) and str(
    i**3
).endswith(
    str(
        i
    )
)]
Excel solution 8 for Bi And Trimorphic Numbers, proposed by Sunny Baggu:
=LET(
    
     s,
     SEQUENCE(
         100000
     ),
    
     FILTER(
         
          s,
         
          MAP(
              
               s,
              
               LAMBDA(
                   n,
                    AND(
                        RIGHT(
                            n ^ {2,
                             3},
                             LEN(
                                 n
                             )
                        ) + 0 = n
                    )
               )
               
          )
          
     )
    
)
Excel solution 9 for Bi And Trimorphic Numbers, proposed by LEONARD OCHEA 🇷🇴:
=TOCOL(
    MAP(
        SEQUENCE(
            10^5
        ),
        LAMBDA(
            s,
            s/AND(
                s=--RIGHT(
                    s^{2;3},
                    LEN(
                        s
                    )
                )
            )
        )
    ),
    2
)
Excel solution 10 for Bi And Trimorphic Numbers, proposed by Anshu Bantra:
=LET(
seq_,SEQUENCE(100000),
sqr_,POWER(seq_,2),
cube_,POWER(seq_,3),
len_,LEN(seq_),
FILTER(seq_,(((--RIGHT(sqr_,len_))=seq_)*((--RIGHT(cube_,len_))=seq_)))
)
Excel solution 11 for Bi And Trimorphic Numbers, proposed by Anshu Bantra:
def is_bimorphic(n):
 return str(n**2).endswith(str(n))
def is_trimorphic(n):
 return str(n**3).endswith(str(n))
[n for n in range(1, 100001) if is_bimorphic(n) and is_trimorphic(n)]
                    
                  
Excel solution 12 for Bi And Trimorphic Numbers, proposed by Md. Zohurul Islam:
=LET(sq,SEQUENCE(99999),f,LAMBDA(x,y,MAP(x,LAMBDA(x,(--RIGHT(x^y,LEN(x)))=x))),d,FILTER(sq,f(sq,2)*f(sq,3)),d)
Excel solution 13 for Bi And Trimorphic Numbers, proposed by Md. Zohurul Islam:
=LET(sq,SEQUENCE(99999),
a,MAP(sq,LAMBDA(x,(--RIGHT(x^2,LEN(x)))=x)),
b,MAP(sq,LAMBDA(x,(--RIGHT(x^3,LEN(x)))=x)),
d,FILTER(sq,a*b),
d)
Excel solution 14 for Bi And Trimorphic Numbers, proposed by Pieter de B.:
=LET(s,SEQUENCE(99999),TOCOL(s/BYROW(--RIGHT(s^{2,3},LEN(s))=s,AND),2))
Excel solution 15 for Bi And Trimorphic Numbers, proposed by Hamidi Hamid:
=LET(x,SEQUENCE(100000),y,RIGHT(x^2,LEN(x)),z,RIGHT(x^3,LEN(x)),FILTER(x,((x-y)=(y-z))*(z-x)=y-x))
Excel solution 16 for Bi And Trimorphic Numbers, proposed by Eric Laforce:
=LET(
 s, SEQUENCE(10000),
 FILTER( s,
 RIGHT(s^2, LEN(s)) = TEXT(s,"0") * RIGHT(s^3, LEN(s)) = TEXT(s,"0"))
)
Excel solution 17 for Bi And Trimorphic Numbers, proposed by ferhat CK:
=TOCOL(MAP(SEQUENCE(100000),LAMBDA(x,LET(a,--RIGHT(x^{2,3},LEN(x)),IF((TAKE(a,,1)*TAKE(a,,-1))^0.5=x,x,1/0)))),2)
Excel solution 18 for Bi And Trimorphic Numbers, proposed by Jaroslaw Kujawa:
=DROP(REDUCE("";SEQUENCE(10^5);LAMBDA(a;x;let(l;LEN(x);IF((1*RIGHT(x^2;l)=x)*(RIGHT(x^2;l)=RIGHT(x^3;l));VSTACK(a;x);a))));1)
Excel solution 19 for Bi And Trimorphic Numbers, proposed by Meganathan Elumalai:
=LET(n,SEQUENCE(100000),FILTER(n,BYROW(--(RIGHT(n^{2,3},LEN(n)))=n,AND)))
Excel solution 20 for Bi And Trimorphic Numbers, proposed by Tolga Demirci, PMP, PMI-ACP, MOS-Expert:
=LET(a,
    SEQUENCE(
        100000
    ),
    LET(x,
    VALUE(
        RIGHT(
            a^3,
            LEN(
                a
            )
        )
    ),
    y,
    VALUE(
        RIGHT(
            a^2,
            LEN(
                a
            )
        )
    ),
    FILTER(a,
    ((x=y)*(x=a)*(y=a))>0)))
Excel solution 21 for Bi And Trimorphic Numbers, proposed by Mihai Radu O:
=LET(
    n,
    SEQUENCE(
        99999
    ),
    nSq,
    n^2,
    nC,
    n^3,
    TOCOL(
        n/BYROW(
            N(
                REGEXTEST(
                    HSTACK(
                        nSq,
                        nC
                    ),
                    n&"$"
                )
            ),
            PRODUCT
        ),
        3
    )
)
Excel solution 22 for Bi And Trimorphic Numbers, proposed by Erdit Qendro:
=LET(no,
    SEQUENCE(
        100000
    ),
    noXno,
    POWER(
        no,
        2
    ),
    
aMn,
    FILTER(no,
    MOD(noXno,
    10^(LEN(
        no
    )))=no),
    
aMn)
Excel solution 23 for Bi And Trimorphic Numbers, proposed by Hussain Ali Nasser:
=LET(s,SEQUENCE(100000),FILTER(s,BYROW(--(s=RIGHT(s^{2,3},LEN(s))+0),PRODUCT)))
Excel solution 24 for Bi And Trimorphic Numbers, proposed by abdelaziz kamal allam:
=LET(se,SEQUENCE(99999),qqq,se^3,qq,se^2,le,LEN(se),rqqq,RIGHT(qqq,le),rqq,RIGHT(qq,le),FILTER(se,MAP(rqq,rqqq,se,LAMBDA(a,b,c,IF(AND(--a=c,--b=c),0,"x")))=0))
Excel solution 25 for Bi And Trimorphic Numbers, proposed by Leonid Koyfman:
=LET(
    s,
    SEQUENCE(
        100000
    ),
    TOCOL(
        s/BYROW(
            --RIGHT(
                s^{2,
                3},
                LEN(
                    s
                )
            )=s,
            AND
        ),
        3
    )
)

Solving the challenge of Bi And Trimorphic Numbers with Python

Python solution 1 for Bi And Trimorphic Numbers, proposed by Bo Rydobon 🇹🇭:
Up to 8 digits
=TOCOL(REDUCE({1;5;6},SEQUENCE(7),LAMBDA(a,i,LET(x,10^i,s,SEQUENCE(10)*x+TOROW(a,3),HSTACK(a,TOCOL(IFS(MOD(s^2,x*10)=s,s),3))))),3,1)
a=[1,5,6]
for i in range(1,15):
 x=10**i
& a+=[d*x+n for d in range(1,10)for n in a if(d*x+n)**2%(x*10)==d*x+n]
a
                    
                  
Python solution 2 for Bi And Trimorphic Numbers, proposed by Konrad Gryczan, PhD:
import pandas as pd
path = "665 Bi & Trimorphic Numbers.xlsx"
test = pd.read_excel(path, usecols="A", nrows=10)
df = pd.DataFrame({'n': range(1, 100001)})
df = df[df['n'].apply(lambda x: str(x)[-1] in ['0', '1', '5', '6'])]
df['n2'], df['n3'] = df['n'] ** 2, df['n'] ** 3
df['nlen'] = df['n'].astype(str).str.len()
df['n2m2'] = df.apply(lambda row: str(row['n2'])[-row['nlen']:] == str(row['n']), axis=1)
df['n3m3'] = df.apply(lambda row: str(row['n3'])[-row['nlen']:] == str(row['n']), axis=1)
df = df[df['n2m2'] & df['n3m3']].reset_index(drop=True)
df = df[['n']]
print(df['n'].equals(test['Expected Answer'])) # True
                    
                  

Solving the challenge of Bi And Trimorphic Numbers with R

R solution 1 for Bi And Trimorphic Numbers, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
path = "Excel/665 Bi & Trimorphic Numbers.xlsx"
test = read_excel(path, range = "A1:A10")
df = data.frame(n = 1:100000) %>% 
 filter(n %% 10 %in% c(0, 1, 5, 6)) %>%
 mutate(n2 = n^2, n3 = n^3) %>%
 filter(str_ends(as.character(n2), as.character(n)), 
 str_ends(as.character(n3), as.character(n)))
all.equal(df$n, test$`Expected Answer`)
#> [1] TRUE
                    
                  

&&

Leave a Reply