Home » Find Pythagorean Dates

Find Pythagorean Dates

Pythagorean dates are those dates where DD^2 + MM^2 = YY^2 For example: 16-Dec-2020 where 16^2 + 12 ^2 = 20^2 List all Pythagorean dates (in YYYYMMDD format) from current century.

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

Solving the challenge of Find Pythagorean Dates with Power Query

Power Query solution 1 for Find Pythagorean Dates, proposed by Bo Rydobon 🇹🇭:
let
  Source = Table.ExpandListColumn(
    Table.AddColumn(Table.FromValue({1 .. 31}), "M", each {1 .. 12}), 
    "M"
  ), 
  AddY = Table.AddColumn(
    Source, 
    "Year", 
    each 
      let
        y = Number.Sqrt(Number.Power([Value], 2) + Number.Power([M], 2))
      in
        if Number.RoundDown(y) = y then 2000 + y else 0
  ), 
  Filtered = List.Sort(
    Table.TransformRows(
      Table.SelectRows(AddY, each ([Year] <> 0)), 
      each Text.From([Year] * 10000 + [M] * 100 + [Value])
    )
  )
in
  Filtered
Power Query solution 2 for Find Pythagorean Dates, proposed by Zoran Milokanović:
let
 Source = DateTime.LocalNow(),
 AddCenturyDates = let t = Number.IntegerDivide(Date.Year(Source), 100) * 100 in {Number.From(hashtag#date((t+1), 1, 1))..Number.From(hashtag#date((t+100), 12, 31))},
 ConvertToDates = List.Transform(AddCenturyDates, each Date.From(_)),
 FilterPythagoreanDates = List.Select(ConvertToDates, each let d = Date.Day(_), m = Date.Month(_), y = Number.Mod(Date.Year(_), 100) in Number.Power(d, 2) + Number.Power(m, 2) = Number.Power(y, 2)),
 FormatPythagoreanDates = List.Transform(FilterPythagoreanDates, each let d = Date.From(_) in Text.From(Date.Year(d) * 10000 + Date.Month(d) * 100 + Date.Day(d))),
 ConvertedToTable = Table.FromList(FormatPythagoreanDates, Splitter.SplitByNothing(), {"List of Dates"}, null, ExtraValues.Error)
in
 ConvertedToTable


                    
                  
          
Power Query solution 3 for Find Pythagorean Dates, proposed by Aditya Kumar Darak 🇮🇳:
let
  List = List.TransformMany({1 .. 12}, (x) => {1 .. 31}, (x, y) => {x} & {y}), 
  DM = Table.FromRows(List, {"M", "D"}), 
  Y = Table.AddColumn(DM, "Y", each Number.Sqrt(Number.Power([M], 2) + Number.Power([D], 2))), 
  Filter = Table.SelectRows(Y, each Number.IntegerDivide([Y], 1) = [Y]), 
  Transform = Table.TransformColumns(
    Filter, 
    {"Y", each Number.ToText(_, "2000")}, 
    each Number.ToText(_, "00")
  ), 
  Return = Table.CombineColumns(
    Transform, 
    {"Y", "M", "D"}, 
    Combiner.CombineTextByDelimiter("", QuoteStyle.None), 
    "Dates"
  )
in
  Return
Power Query solution 4 for Find Pythagorean Dates, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
 Source = Table.FromColumns({List.Transform({Number.From(hashtag#date(2000,01,01))..Number.From(hashtag#date(2099,12,31))}, each Date.ToText(Date.From(_), "YYYYMMDD"))}, {"List of Dates"}),
 Split = Table.AddColumn(Source, "Custom", each 
 let
 a = List.Skip(Splitter.SplitTextByRepeatedLengths(2)([List of Dates])),
 b = List.Transform({0..2}, each Number.Power(Number.From(a{_}),2)),
 c = b{1}+b{2}=b{0}
 in c),
 Sol = Table.SelectRows(Split, each ([Custom] = true))[[List of Dates]]
in
 Sol


                    
                  
          
Power Query solution 5 for Find Pythagorean Dates, proposed by Luan Rodrigues:
let
 Fonte = List.Buffer(List.Dates(hashtag#date(2001,01,01 ),36500,hashtag#duration(1, 0, 0, 0))),
 date = Table.FromList(Fonte, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
 tab = Table.AddColumn(date, "Personalizar", each 
 [
 a = {[Column1]}, 
 b = List.Transform(a,each Number.Power(Number.From(Date.ToText(_,"dd")),2)),
 c = List.Transform(a,each Number.Power(Number.From(Date.ToText(_,"MM")),2)),
 d = List.Transform(a,each Number.Power(Number.From(Text.End(Date.ToText(_,"yyyy"),2)),2)),
 e = List.Zip({List.Transform(List.Zip({b,c}), List.Sum),d}),
 f = Text.Combine(List.Transform( List.Select(List.Transform(List.Transform(e,List.Distinct),List.Count), each _ = 1),each Text.From(_)),"")
 ][f]),
 res = Table.SelectRows(tab, each [Personalizar] = "1")[[Column1]],
 tipo = Table.TransformColumnTypes(res,{{"Column1", type date}},"en-US")
in
 tipo


                    
                  
          
Power Query solution 6 for Find Pythagorean Dates, proposed by Brian Julius:
let
  Days = {1 .. 31}, 
  DayTable = Table.RenameColumns(Table.FromColumns({Days}), {"Column1", "Day"}), 
  Months = {1 .. 12}, 
  MonthTable = Table.RenameColumns(Table.FromColumns({Months}), {"Column1", "Month"}), 
  CrossJoin = Table.AddColumn(MonthTable, "CrossJoin", each DayTable), 
  Expand = Table.ExpandTableColumn(CrossJoin, "CrossJoin", {"Day"}, {"Day"}), 
  AddCalcYear = Table.AddColumn(
    Expand, 
    "CalcYear", 
    each Number.Power(Number.Power([Day], 2) + Number.Power([Month], 2), 0.5)
  ), 
  Filter = Table.SelectRows(AddCalcYear, each Number.Mod([CalcYear], 1) = 0), 
  Add2000 = Table.TransformColumns(Filter, {{"CalcYear", each _ + 2000, type number}}), 
  ReType = Table.TransformColumnTypes(
    Add2000, 
    {{"CalcYear", type text}, {"Day", type text}, {"Month", type text}}
  ), 
  Merge = Table.SelectColumns(
    Table.AddColumn(
      ReType, 
      "List of Dates", 
      each [CalcYear] & Text.PadStart([Month], 2, "0") & Text.PadStart([Day], 2, "0")
    ), 
    "List of Dates"
  )
in
  Merge
Power Query solution 7 for Find Pythagorean Dates, proposed by Jaroslaw Kujawa:
let
 Source = Excel.CurrentWorkbook(){[Name=Table29]}[Content],
 hashtag#Changed Type = Table.TransformColumnTypes(Source,{{List of Dates, type text}})
 in
Table.AddColumn(hashtag#Changed Type, Custom, each [yy=Number.FromText( Text.Start([List of Dates],4))-2000, mm=Number.FromText(Text.Middle([List of Dates],4,2)), dd=Number.FromText( Text.End([List of Dates],2)), check=if yy*yy-mm*mm-dd*dd=0 then Pythagorean date else false][check])


                    
                  
          
Power Query solution 8 for Find Pythagorean Dates, proposed by Jan Willem Van Holst:
let
 Source = List.Dates(hashtag#date(2000,1,1), Duration.Days(hashtag#date(2099,12,31)-hashtag#date(2000,1,1)) , hashtag#duration(1, 0, 0, 0)),
 select = List.Select(Source, each Number.Power(Date.Year(_)-2000,2) = Number.Power(Date.Month(_),2) + Number.Power(Date.Day(_),2)),
 format = List.Transform(select, each Text.End(Text.From(_),4) & Text.Middle(Text.From(_),3,2) & Text.Start(Text.From(_),2) )
in
 format


                    
                  
          

Solving the challenge of Find Pythagorean Dates with Excel

Excel solution 1 for Find Pythagorean Dates, proposed by Bo Rydobon 🇹🇭:
=LET(d,SEQUENCE(,31),m,SEQUENCE(12),y,(d^2+m^2)^0.5,SORT(TOCOL(IFS(INT(y)=y,2*10^7+y*10^4+m*100+d),3)&""))
Excel solution 2 for Find Pythagorean Dates, proposed by Bo Rydobon 🇹🇭:
=LET(d,SEQUENCE(366),y,(DAY(d)^2+MONTH(d)^2)^0.5,SORT(FILTER(2000+y&TEXT(d,"mmdd"),INT(y)=y)))
Excel solution 3 for Find Pythagorean Dates, proposed by Rick Rothstein:
=LET(d,SEQUENCE(36525,,"2000-01-01"),TEXT(FILTER(d,DAY(d)^2+MONTH(d)^2=(YEAR(d)-2000)^2),"yyyymmdd"))

If instead of calculating January 1, 2000 (the start date), we assume we know its serial number, then we can shorten the above formula by 7 characters...

=LET(d,SEQUENCE(36525,,36892),TEXT(FILTER(d,DAY(d)^2+MONTH(d)^2=(YEAR(d)-2000)^2),"yyyymmdd"))

And if we recognize that the starting year is unimportant for the bulk of the calculation (only needing to be rectified at the end), we can save an additional 6 characters...

=LET(d,SEQUENCE(36525),20&TEXT(FILTER(d,DAY(d)^2+MONTH(d)^2=(YEAR(d)-1900)^2),"yymmdd"))
Excel solution 4 for Find Pythagorean Dates, proposed by Rick Rothstein:
=LET(d,SEQUENCE(36525,,),20&TEXT(FILTER(d,DAY(d)^2+MONTH(d)^2=(YEAR(d)-1900)^2),"yymmdd"))
Excel solution 5 for Find Pythagorean Dates, proposed by John V.:
=LET(d,SEQUENCE(,31),m,ROW(1:12),a,(d^2+m^2)^0.5,SORT(TOCOL((2000+a&TEXT(m,"00")&TEXT(d,"00"))/(a=INT(a)),2)))
Excel solution 6 for Find Pythagorean Dates, proposed by Kris Jaganah:
=LET(a,SEQUENCE(36525,,DATE(2000,1,1),),TEXT(FILTER(a,(DAY(a)^2+MONTH(a)^2)=((YEAR(a)-2000)^2)),"YYYYMMDD"))
Excel solution 7 for Find Pythagorean Dates, proposed by Julian Poeltl:
=LET(S,SEQUENCE("12/31/2099"-"01/01/2000",,"01/012000"),TEXT(FILTER(S,DAY(S)^2+MONTH(S)^2=RIGHT(YEAR(S),2)^2),"YYYYMMDD"))
Excel solution 8 for Find Pythagorean Dates, proposed by Alejandro Campos:
=LET(
 s, SEQUENCE(36525, , DATE((YEAR(NOW()) - (--RIGHT(YEAR(NOW()), 2))), 1, 1), 1),
 a, YEAR(s),
 m, MONTH(s),
 d, DAY(s),
 p, m ^ 2 + d ^ 2,
 da, (--RIGHT(a, 2)) ^ 2,
 TEXT(FILTER(s, p = da), "aaaammdd")
)
Excel solution 9 for Find Pythagorean Dates, proposed by Aditya Kumar Darak 🇮🇳:
=LET(
 _dt, MAKEARRAY(
 12,
 31,
 LAMBDA(r, c, CONCAT(0, SQRT(r ^ 2 + c ^ 2), TEXT(HSTACK(r, c), "00")))
 ),
 _fdt, TOCOL(TEXT(_dt, "2000-00-00"), , 1),
 _r, FILTER(SUBSTITUTE(_fdt, "-", ""), ISNUMBER(DATEVALUE(_fdt))),
 _r
)
Excel solution 10 for Find Pythagorean Dates, proposed by Timothée BLIOT:
=LET(A,SEQUENCE(DATE(2099,12,31)-DATE(2000,1,1)+1,,DATE(2000,1,1)),TEXT(FILTER(A,--MAP(A,LAMBDA(x,(((DAY(x)*1)^2)+((MONTH(x)*1)^2))=((TEXT(x,"YY")*1)^2)))),"YYYYMMDD"))
Excel solution 11 for Find Pythagorean Dates, proposed by Hussein SATOUR:
=LET(d, SEQUENCE(DATE(2099,12,31)-DATE(1999,12,31),, DATE(2000,1,1)), FILTER(TEXT(d, "yyyymmdd"), TEXT(d,"dd")^2 + TEXT(d,"mm")^2 = TEXT(d,"yy")^2))
Excel solution 12 for Find Pythagorean Dates, proposed by Md. Zohurul Islam:
=LET(d,
    DATE(
        2000,
        1,
        1
    ),
    
dt,
    SEQUENCE(
        36525,
        ,
        d
    ),
    
a,
    DAY(
        dt
    )^2,
    
b,
    MONTH(
        dt
    )^2,
    
c,
    ABS(
        RIGHT(
            YEAR(
        dt
    ),
            2
        )
    )^2,
    
e,
    FILTER(TEXT(
        dt,
        "yyyymmdd"
    ),
    (a+b)=c),
    
e)
Excel solution 13 for Find Pythagorean Dates, proposed by Charles Roldan:
=LET(
Start, DATE(ROUNDDOWN(YEAR(TODAY()), -2), 1, 1), 
Dates, TEXT(SEQUENCE(EDATE(Start, 12*100)-Start, , Start), "yyyymmdd"), 
FILTER(Dates, MMULT(MID(Dates, {3, 5, 7}, 2)^2, {1;-1;-1})=0)
)
Excel solution 14 for Find Pythagorean Dates, proposed by Peter Bartholomew:
= LET(
 s, DATE(2000,1,1),
 e, EOMONTH(s-1, 12*100),
 n, SEQUENCE(e-s+1,,s),
 d, DAY(n),
 m, MONTH(n),
 y, MOD(YEAR(n),100),
 FILTER(n, d^2 + m^2 = y^2)
 )
Number format "yyyymmdd".
My question, though, is why should a formula be short?
I would argue that the aim should be to make the formula as readable as possible.  In fact, I argued for the use of an alternating pattern of separators, to be allowed specifically for LET, to increase readability and insert newlines automatically.
= LET(
 s := DATE(2000,1,1);
 e := EOMONTH(s-1, 12*100);
 n := SEQUENCE(e-s+1,,s);
 d := DAY(n);
 m := MONTH(n);
 y := MOD(YEAR(n),100);
 FILTER(n, d^2 + m^2 = y^2)
 )
Excel solution 15 for Find Pythagorean Dates, proposed by Guillermo Arroyo:
=LET(d,SEQUENCE(31),m,SEQUENCE(,12),y,SQRT((d^2)+(m^2)),SORT(FILTER(TOCOL("20"&TEXT(y,"00")&TEXT(m,"00")&TEXT(d,"00")),TOCOL(y=INT(y)))))

=LET(d,ROW(1:31),m,COLUMN(A:L),y,2000+SQRT((d^2)+(m^2)),TEXT(SORT(TOCOL(DATEVALUE(m&"-"&d&"-"&y),3)),"yyyymmdd"))
Excel solution 16 for Find Pythagorean Dates, proposed by Anup Kumar:
=LET(
allDatesofCentury, SEQUENCE(36524,,36892,1),
isPythagorean, SCAN(FALSE,allDatesofCentury,LAMBDA(s,k,DAY(k)^2+MONTH(k)^2=(--RIGHT(YEAR(k),2))^2)),
TEXT(FILTER(allDatesofCentury,isPythagorean),"YYYYMMDD")
)
Excel solution 17 for Find Pythagorean Dates, proposed by Mohamed Helmy:
=LET(
Y,SEQUENCE(,32)^2,
D,SEQUENCE(31),
M,SEQUENCE(,12),
V,TOCOL(M^2+D^2) &"-"&
TOCOL(RIGHT(0&M,2)&RIGHT(0&D,2)),
SORT(TOCOL(IF(TEXTSPLIT(V,"-")+0 =Y,20&
RIGHT(0&Y^0.5,2)&TEXTAFTER(V,"-"),NA()),2)))
Excel solution 18 for Find Pythagorean Dates, proposed by Gabriel Raigosa:
=LET(ini,DATE(2001,1,1),fin,DATE(2100,12,31),Dat,SEQUENCE(fin-ini+1,,ini),FILTER(Dat,DAY(Dat)^2+MONTH(Dat)^2=RIGHT(YEAR(Dat),2)^2))

ES:
=LET(ini,FECHA(2001,1,1),fin,FECHA(2100,12,31),Dat,SECUENCIA(fin-ini+1,,ini),FILTRAR(Dat,DIA(Dat)^2+MES(Dat)^2=DERECHA(AÑO(Dat),2)^2)) 

cell format: yyyymmdd

The 21st (twenty-first) century is the current century in the Anno Domini era or Common Era, under the Gregorian calendar. It began on 1 January 2001 (MMI) and will end on 31 December 2100 (MMC)
Excel solution 19 for Find Pythagorean Dates, proposed by roberto mensa:
=LET(
d,MOD(SEQUENCE(12*31,,0),31)+1,
m,INT(SEQUENCE(12*31,,0)/31)+1,
y,2000+SQRT(m^2+d^2),
SORT(FILTER(y&TEXT(m*100+d,"0000"),ISNUMBER(--(y&"/"&m&"/"&d)))))
Excel solution 20 for Find Pythagorean Dates, proposed by Enrico Giorgi:
=LET(x,SEQUENCE(DATEVALUE("31/12/2100")-DATEVALUE("31/12/2000"),1,DATEVALUE("01/01/2001"),1),d,DAY(x),m,MONTH(x),y,1*RIGHT(YEAR(x),2),output,IF(d^2+m^2=y^2,YEAR(x)&TEXT(x,"mm")&TEXT(x,"dd"),""),FILTER(output,output<>""))
ITALIAN VERSION
=LET(x;SEQUENZA(DATA.VALORE("31/12/2100")-DATA.VALORE("31/12/2000");1;DATA.VALORE("01/01/2001");1);d;GIORNO(x);m;MESE(x);y;1*DESTRA(ANNO(x);2);output;SE(d^2+m^2=y^2;ANNO(x)&TESTO(x;"mm")&TESTO(x;"gg");"");FILTRO(output;output<>""))

Solving the challenge of Find Pythagorean Dates with Python in Excel

Python in Excel solution 1 for Find Pythagorean Dates, proposed by Alejandro Campos:
current_year = pd.Timestamp.now().year
start_year = int(str(current_year)[-2:])
start_date = pd.Timestamp(year=current_year - start_year, month=1, day=1)
date_range = pd.date_range(start=start_date, periods=36525)
years = date_range.year
months = date_range.month
days = date_range.day
p = months**2 + days**2
da = (years % 100)**2
filtered_dates = date_range[p == da]
filtered_dates_str = filtered_dates.strftime('%Y%m%d')
result = filtered_dates_str.tolist()
result
                    
                  

&&

Leave a Reply