Recursion in Excel and Power Query - 4
- V E Meganathan
- Oct 17
- 1 min read
Task 1:
Given m, where m = n³ + (n-1)³ + (n-2)³ + ... + 1³, determine the value of n if it exists; otherwise, return -1.
Solution:
Excel:
=LET(l,LAMBDA(ME,m,start,n,IF(start=m,n-1,IF(start>m,-1,ME(ME,m,start+n^3,n+1)))),l(l,25502500,0,1))
Power Query:
let
m = 3025,
fx = (m as number, start as number, n as number ) as number => if start = m then n - 1 else if start > m then -1 else @fx(m,start + Number.Power(n,3), n + 1),
Result = Table.FromValue(fx(m,0,1))
in
Result
An alternative non-recursive approach:
=LET(m,30969225,p,INT(SQRT(SQRT(m)*2)),q,p*(p+1)/2,IF(q^2=m,p,-1))
Task 2:
Find the Decrypted words corresponding to Encrypted words . The encryption (Baconian Cipher) was done using following logic - (This problem is case insensitive)
A is 0, B is 1, C is 2.....Z is 25. Binary representations of these numbers in 5 digits is 00000, 00001, 00010....11001.
Replace 0 with a and 1 with b in these binary reps. Hence, these will become aaaaa, aaaab, aaaba.....bbaab.
Now, to encrypt any word, you can replace the letters in the string with these text representations.
Hence, Excel would become aabaababbbaaabaaabaaababb.

Solution:
Excel:
=LET(l,LAMBDA(ME,s,IF(s="","",CHAR(BIN2DEC(CONCAT(N(MID(LEFT(s,5),SEQUENCE(5),1)="b")))+65)&ME(ME,MID(s,6,50)))),MAP(A2:A7,LAMBDA(x,PROPER(l(l,x)))))
Power Query:
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
fx = (s as text) as text =>
[
A = Text.Start(s,5),
B = List.Transform(Text.ToList(A), each if _ = "b" then "1" else "0"),
C = Character.FromNumber(List.Accumulate(B,0,(s,c) => s * 2 + Number.From(c)) + 65) & @fx(Text.Middle(s,5)),
D = if s = "" then "" else C
][D],
Result = Table.AddColumn(Source,"Result", each Text.Proper(fx([Encrypted Text])))
in
Result




Comments