Let’s code golf. Code golfing means writing the code in shortest number of characters disregarding all other parameters like performance, readability, maintainability etc. I request you not to compete with others to write the shortest code. You need to compete with yourself only not with anybody else. You should, of course, post multiple answers to improve upon previous answers. Problem – For first 20 Fibonacci numbers, find the number for which sum of individual digits is the largest. Answer – First 20 Fibonacci numbers = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181. Answer is 987 for which sum of individual digits is 24.
📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 580
Challenge Difficulty: ⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn
Solving the challenge of Digit Sum in Fibonacci Sequence with Power Query
Power Query solution 1 for Digit Sum in Fibonacci Sequence, proposed by Kris Jaganah:
let
A = List.Zip(
List.Transform(
{0 .. 19},
each
let
a = Number.Round(Number.Power(((1 + Number.Sqrt(5)) / 2), _) / Number.Sqrt(5), 0),
b = List.Sum(List.Transform(Text.ToList(Text.From(a)), Number.From))
in
{a, b}
)
),
B = A{0}{List.PositionOf(A{1}, List.Max(A{1}))}
in
B
Power Query solution 2 for Digit Sum in Fibonacci Sequence, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
N = 20,
Fibo = List.Accumulate({1 .. N - 2}, {0, 1}, (s, l) => s & {List.Sum(List.LastN(s, 2))}),
Sum = List.Transform(
Fibo,
each
let
a = Text.ToList(Text.From(_)),
b = List.Sum(List.Transform(a, Number.From))
in
b
),
Sol = Fibo{List.PositionOf(Sum, List.Max(Sum))}
in
Sol
Power Query solution 3 for Digit Sum in Fibonacci Sequence, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
N = 20,
Fibo = List.Generate(
() => [x = 1, y = 0, z = 1],
each [z] <= N,
each [x = [y], y = [x] + [y], z = [z] + 1],
each [y]
),
Sum = List.Transform(
Fibo,
each
let
a = _,
b = Text.ToList(Text.From(a)),
c = List.Sum(List.Transform(b, Number.From))
in
c
),
Sol = Fibo{List.PositionOf(Sum, List.Max(Sum))}
in
Sol
Power Query solution 4 for Digit Sum in Fibonacci Sequence, proposed by Abdallah Ally:
let
Result = List.Sort(
List.Generate(
() => [nums = {0, 1}, count = 0],
each [count] < 20,
each [nums = [nums] & {List.Sum(List.LastN([nums], 2))}, count = [count] + 1],
each List.LastN([nums], 2){0}
),
each - List.Sum(List.Transform(Text.ToList(Text.From(_)), Number.From))
){0}
in
Result
Power Query solution 5 for Digit Sum in Fibonacci Sequence, proposed by Ramiro Ayala Chávez:
let
L = List.Transform,
a = List.Accumulate({0 .. 17}, {0, 1}, (s, c) => s & {s{c} + s{c + 1}}),
b = L(a, each {_} & {List.Sum(L(Text.ToList(Text.From(_)), Number.From))}),
c = Table.Max(Table.FromRows(b), "Column2")[Column1]
in
c
Power Query solution 6 for Digit Sum in Fibonacci Sequence, proposed by Alexandre Garcia:
let x=Text.From([x])in{x,Expression.Evaluate(Text.Combine(Text.ToList(x),"+"))}),{each _{1},1}){0}{0}
Power Query solution 7 for Digit Sum in Fibonacci Sequence, proposed by Mihai Radu O:
let
GR = 1.618034,
fi = (n) => Int64.From((Number.Power(GR, n) - Number.Power(1 - GR, n)) / Number.Sqrt(5)),
s = (nr) => List.Sum(List.Transform(Text.ToList(Text.From(nr)), Number.From)),
a = List.Transform({0 .. 19}, (x) => fi(x)),
b = List.Max(List.Transform(a, (x) => s(x))),
c = List.Select(a, (x) => s(x) = b)
in
c
Power Query solution 8 for Digit Sum in Fibonacci Sequence, proposed by Tyler N.:
let
a = (b) => if b < 2 then b else @a(b - 1) + @a(b - 2),
c = List.Transform(
{0 .. 20},
each List.Sum(List.Transform(Text.ToList(Text.From(a(_))), each Number.From(_)))
),
d = a(List.PositionOf(c, List.Max(c)))
in
d
Solving the challenge of Digit Sum in Fibonacci Sequence with Excel
Excel solution 1 for Digit Sum in Fibonacci Sequence, proposed by Bo Rydobon 🇹🇭:
=LET(f,5^0.5,s,ROW(1:19),b,((1+f)^s-(1-f)^s)/f/2^s,@SORTBY(b,BYROW(-(0&MID(b,{1,2,3,4},1)),SUM)))
Excel solution 2 for Digit Sum in Fibonacci Sequence, proposed by Rick Rothstein:
=LET(n,
ROW(
1:20
)-1,
p,
5^0.5,
f,
((1+p)^n-(1-p)^n)/2^n/p,
@SORTBY(f,
BYROW(-(0&MID(
f,
{1,
2,
3,
4},
1
)),
SUM)))
=LET(n,
ROW(
1:20
)-1,
p,
5^0.5,
f,
((1+p)^n-(1-p)^n)/2^n/p,
TAKE(
SORTBY(
f,
BYROW(
-MID(
f,
{1,
2,
3,
4},
1
),
SUM
)
),
-1
))
=LET(n,
ROW(
1:20
)-1,
p,
5^0.5,
f,
((1+p)^n-(1-p)^n)/2^n/p,
b,
BYROW(0+(0&MID(
f,
{1,
2,
3,
4},
1
)),
SUM),
FILTER(
f,
b=MAX(
b
)
))
=LET(n,
ROW(
1:20
)-1,
p,
5^0.5,
f,
((1+p)^n-(1-p)^n)/2^n/p,
b,
BYROW(0+(0&MID(
f,
{1,
2,
3,
4},
1
)),
SUM),
XLOOKUP(
MAX(
b
),
b,
f
))
Excel solution 3 for Digit Sum in Fibonacci Sequence, proposed by John V.:
=LET(s,ROW(1:19),n,5^0.5,f,((1+n)^s-(1-n)^s)/n/2^s,@SORTBY(f,BYROW(-(0&MID(f,{1,2,3,4},1)),SUM)))
Excel solution 4 for Digit Sum in Fibonacci Sequence, proposed by Kris Jaganah:
=LET(
a,
SCAN(
1,
SEQUENCE(
17
),
LAMBDA(
x,
y,
ROUND(
x*1.618,
0
)
)
),
b,
MMULT(
IFERROR(
--MID(
a,
{1,
2,
3,
4},
1
),
),
{1;1;1;1}
),
FILTER(
a,
MAX(
b
)=b
)
)
Excel solution 5 for Digit Sum in Fibonacci Sequence, proposed by Julian Poeltl:
=LET(
N,
REDUCE(
{0,
1},
SEQUENCE(
20
),
LAMBDA(
A,
B,
VSTACK(
A,
SUM(
TAKE(
A,
-2
)
)
)
)
),
D,
MAP(
N,
LAMBDA(
A,
SUM(
--MID(
A,
SEQUENCE(
LEN(
A
)
),
1
)
)
)
),
LOOKUP(
MAX(
D
),
D,
N
)
)
Excel solution 6 for Digit Sum in Fibonacci Sequence, proposed by Timothée BLIOT:
=LET(
A,
REDUCE(
{0;1},
ROW(
1:18
),
LAMBDA(
w,
v,
VSTACK(
w,
SUM(
TAKE(
w,
-2
)
)
)
)
),
B,
MAP(
A,
LAMBDA(
x,
SUM(
--MID(
x,
SEQUENCE(
LEN(
x
)
),
1
)
)
)
),
FILTER(
A,
B=MAX(
B
)
)
)
Excel solution 7 for Digit Sum in Fibonacci Sequence, proposed by Hussein SATOUR:
=LET(
S,
SEQUENCE,
a,
REDUCE(
{0;1},
S(
18
)^0,
LAMBDA(
x,
y,
VSTACK(
x,
TAKE(
x,
-1
)+DROP(
TAKE(
x,
-2
),
-1
)
)
)
),
b,
MAP(
a,
LAMBDA(
z,
SUM(
--MID(
z,
S(
LEN(
z
)
),
1
)
)
)
),
FILTER(
a,
b=MAX(
b
)
)
)
Excel solution 8 for Digit Sum in Fibonacci Sequence, proposed by Sunny Baggu:
=LET(
_a, REDUCE(
{0; 1},
SEQUENCE(18),
LAMBDA(a, v, VSTACK(a, SUM(TAKE(a, -2))))
),
_b, MAP(_a, LAMBDA(a, SUM(--MID(a, SEQUENCE(LEN(a)), 1)))),
FILTER(_a, _b = MAX(_b))
)
Excel solution 9 for Digit Sum in Fibonacci Sequence, proposed by Sunny Baggu:
=LET(
_a,
MAP(
SEQUENCE(
20,
,
0
),
LAMBDA(x,
((1 + SQRT(
5
)) ^ x - ((1 - SQRT(
5
)) ^ x)) / (2 ^ x * SQRT(
5
)))
),
_b,
MAP(
_a,
LAMBDA(
a,
SUM(
--MID(
a,
SEQUENCE(
LEN(
a
)
),
1
)
)
)
),
FILTER(
_a,
_b = MAX(
_b
)
)
)
Excel solution 10 for Digit Sum in Fibonacci Sequence, proposed by LEONARD OCHEA 🇷🇴:
=LET(n,
SEQUENCE(
20
)-1,
k,
5^0.5,
f,
((1+k)^n-(1-k)^n)/k/2^n,
t,
MAP(
f,
LAMBDA(
x,
SUM(
--REGEXEXTRACT(
x,
".",
1
)
)
)
),
XLOOKUP(
MAX(
t
),
t,
f
))
Excel solution 11 for Digit Sum in Fibonacci Sequence, proposed by Md. Zohurul Islam:
=MAX(
num
)),
result)
Excel solution 12 for Digit Sum in Fibonacci Sequence, proposed by ferhat CK:
=LET(a,LAMBDA(n, IF(n=1,1,REDUCE(1,SEQUENCE(n-1), LAMBDA(b,_,VSTACK(b,SUM(TAKE(b,-2)))))))(20),m,MAP(a,LAMBDA(x,SUM(--MID(x,SEQUENCE(LEN(x)),1)))),XLOOKUP(MAX(m),m,a))
Excel solution 13 for Digit Sum in Fibonacci Sequence, proposed by Jaroslaw Kujawa:
=LET(
all;
REDUCE(
SEQUENCE(
2;
;
0
);
SEQUENCE(
18
);
LAMBDA(
a;
x;
LET(
fib;
SUM(
TAKE(
a;
-2;
1
)
);
fib_sum;
SUM(
1*MID(
fib;
SEQUENCE(
LEN(
fib
)
);
1
)
);
ciag;
VSTACK(
a;
HSTACK(
IFNA(
fib;
""
);
IFNA(
fib_sum;
""
)
)
);
ciag
)
)
);
MAX(
FILTER(
all;
TAKE(
IFNA(
all;
0
);
;
-1
)=MAX(
TAKE(
IFNA(
all;
0
);
;
-1
)
)
)
)
)
Excel solution 14 for Digit Sum in Fibonacci Sequence, proposed by Bilal Mahmoud kh.:
=LET(a,REDUCE({0;1},SEQUENCE(18),LAMBDA(x,y,VSTACK(x,SUM(TAKE(x,-2))))),b,MAP(a,LAMBDA(n,SUM(--MID(n,SEQUENCE(LEN(n)),1)))),FILTER(a,MAX(b)=b))
Excel solution 15 for Digit Sum in Fibonacci Sequence, proposed by Bilal Mahmoud kh.:
=LET(a,REDUCE({0;1},SEQUENCE(18),LAMBDA(x,y,VSTACK(x,SUM(TAKE(x,-2))))),b,MAP(a,LAMBDA(n,SUM(--MID(n,SEQUENCE(LEN(n)),1)))),FILTER(a,MAX(b)=b))
Excel solution 16 for Digit Sum in Fibonacci Sequence, proposed by Nicolas Micot:
=LET(
a;
REDUCE(
{0;
1};
SEQUENCE(
18
);
LAMBDA(
b;
c;
ASSEMB.V(
b;
SOMME(
PRENDRE(
b;
-2
)
)
)
)
);
PRENDRE(
TRIERPAR(
a;
& MAP(
a;
LAMBDA(
d;
SOMME(
STXT(
d;
SEQUENCE(
NBCAR(
d
)
);
1
)+0
)
)
);
-1
);
1
)
)
Excel solution 17 for Digit Sum in Fibonacci Sequence, proposed by Diarmuid Early:
=LET(p,
(5^0.5+1)/2,
s,
SEQUENCE(
20
),
f,
(p^s-(-p)^-s)/5^0.5,
@SORTBY(
f,
BYROW(
-MID(
TEXT(
f,
"0000"
),
{1,
2,
3,
4},
1
),
SUM
)
))
Excel solution 18 for Digit Sum in Fibonacci Sequence, proposed by Cary Ballard, DML:
=LET(
s,
SEQUENCE,
f,
REDUCE(
{0;1},
s(
18
),
LAMBDA(
a,
v,
VSTACK(
a,
SUM(
TAKE(
a,
-2
)
)
)
)
),
x,
--MAKEARRAY(
20,
4,
LAMBDA(
r,
c,
INDEX(
MID(
INDEX(
f,
r
),
s(
,
4
),
1
),
c
)
)
),
XLOOKUP(
LARGE(
BYROW(
IFERROR(
x,
0
),
SUM
),
1
),
BYROW(
IFERROR(
x,
0
),
SUM
),
f
)
)
=LET(
s,
SEQUENCE,
f,
REDUCE(
{0;1},
s(
18
),
LAMBDA(
a,
v,
VSTACK(
a,
SUM(
TAKE(
a,
-2
)
)
)
)
),
x,
MAP(
f,
LAMBDA(
m,
SUM(
--MID(
m,
s(
LEN(
m
)
),
1
)
)
)
),
FILTER(
f,
x=MAX(
x
)
)
)
Excel solution 19 for Digit Sum in Fibonacci Sequence, proposed by Ranjith A:
=LET(
fib, FIBONACCI(ROW(1:20)),
sums, SUM(INT(MID(fib,ROW(INDIRECT("1:"&LEN(fib))),1))),
INDEX(fib, MATCH(MAX(sums), sums, 0))
)
Solving the challenge of Digit Sum in Fibonacci Sequence with Python
Python solution 1 for Digit Sum in Fibonacci Sequence, proposed by Konrad Gryczan, PhD:
fibs = [0, 1]
[fibs.append(fibs[-1] + fibs[-2]) for _ in range(18)]
print(max(fibs, key=lambda n: eval('*'.join(str(n)))))
# 987
Solving the challenge of Digit Sum in Fibonacci Sequence with Python in Excel
Python in Excel solution 1 for Digit Sum in Fibonacci Sequence, proposed by Alejandro Campos:
max([(sum(map(int,str(x))),x)for x in[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181]])[1]
Solving the challenge of Digit Sum in Fibonacci Sequence with R
R solution 1 for Digit Sum in Fibonacci Sequence, proposed by Konrad Gryczan, PhD:
Tried many times and here I have.
library(numbers)
library(tidyverse)
f = map(1:20, fibonacci)
f[[which.max(map_int(f, ~sum(as.integer(strsplit(as.character(.x), "")[[1]]))))]]
&&
