Generate the first 18 terms of Fibonacci sequence as result table. The Fibonacci sequence is a series of numbers in which each number (after the first two) is the sum of the two preceding ones. The sequence typically starts with 0 and 1. Mathematically, the Fibonacci sequence is defined by the recurrence relation: 𝐹(𝑛)=𝐹(𝑛−1)+𝐹(𝑛−2)F(n)=F(n−1)+F(n−2) with the initial conditions: 𝐹(0)=0 and F(1)=1
📌 Challenge Details and Links
Challenge Number: 72
Challenge Difficulty: ⭐⭐⭐
📥Download Sample File
📥Link to the solutions on LinkedIn
Solving the challenge of Fibonacci Sequence! with Power Query
Power Query solution 1 for Fibonacci Sequence!, proposed by Zoran Milokanović:
let
Source = 18,
S = List.Accumulate(
{1 .. Source},
{},
(s, c) => s & {{List.Sum(List.LastN(s, 2))}, {c - 1}}{Number.From(c < 3)}
)
in
S
Power Query solution 2 for Fibonacci Sequence!, proposed by Zoran Milokanović:
let
Source = 18,
S = List.Generate(
() => {1, 0, 1},
each _{0} <= Source,
each {_{0} + 1, _{2}, _{1} + _{2}},
each _{1}
)
in
S
Power Query solution 3 for Fibonacci Sequence!, proposed by Ramiro Ayala Chávez:
let
a = List.Generate(()=>[x=0,y=1,c=1], each [c]<=18, each [y=[x]+[y], x=[y], c=[c]+1], each [x]),
Sol = Table.FromColumns({a},{"Fibonacci sequence"})
in
Sol
Power Query solution 4 for Fibonacci Sequence!, proposed by Aditya Kumar Darak 🇮🇳:
let
Generate = List.Generate(
() => [a = 0, b = {}, c = 0],
each [a] < 18,
each [a = [a] + 1, b = {[b]{1}? ?? [a], List.Sum([b]) ?? a}, c = b{1}],
each [c]
),
Return = List.Combine(Generate)
in
Return
Power Query solution 5 for Fibonacci Sequence!, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
N = 18,
FS = List.Generate(
() => [x = 1, y = 0, z = 1],
each [z] <= N,
each [x = [y], y = [x] + [y], z = [z] + 1],
each [y]
),
Sol = Table.FromColumns({FS}, {"Fibonacci sequence"})
in
Sol
Power Query solution 6 for Fibonacci Sequence!, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
n = 18,
a = Number.Power,
Sol = List.Transform({0 .. n - 1}, each Number.Round(a(1 + a(5, .5), _) / (a(2, _) * a(5, .5))))
in
Sol
Power Query solution 7 for Fibonacci Sequence!, proposed by Alexis Olson:
let
phi = (1 + Number.Sqrt(5)) / 2,
F = (n) => (Number.Power(phi, n) - Number.Power(- phi, - n)) / (2 * phi - 1),
Result = List.Transform({0 .. 18}, F)
in
Result
Power Query solution 8 for Fibonacci Sequence!, proposed by Abdallah Ally:
let
n = 18,
Numbers = List.Accumulate(
{0 .. n - 1},
{},
(x, y) => if y < 2 then x & {y} else x & {List.Sum(List.LastN(x, 2))}
)
in
Numbers
Power Query solution 9 for Fibonacci Sequence!, proposed by Abdallah Ally:
let
n = 18,
f = (x) => if x < 2 then x else @f(x - 2) + @f(x - 1),
Numbers = List.Transform({0 .. n - 1}, f)
in
Numbers
Power Query solution 10 for Fibonacci Sequence!, proposed by 🇮🇷 Navid Esmaeilzadeh اسماعیل زاده:
let
Query2 = List.Generate(
() => [x = 1, n = 1, y = 0],
each [n] <= 18,
each [x = [y], y = [x] + [y], n = [n] + 1],
each [y]
),
#"Converted to Table" = Table.FromList(
Query2,
Splitter.SplitByNothing(),
null,
null,
ExtraValues.Error
),
#"Renamed Columns" = Table.RenameColumns(#"Converted to Table", {{"Column1", "Answer"}})
in
#"Renamed Columns"
Power Query solution 11 for Fibonacci Sequence!, proposed by Meganathan Elumalai:
let
Cnt = 18,
Source = {1 .. Cnt - 2},
Result = List.Accumulate(Source, {0, 1}, (s, c) => s & {List.Sum(List.LastN(s, 2))})
in
Result
Power Query solution 12 for Fibonacci Sequence!, proposed by Venkata Rajesh:
let
n = 18,
list = List.Accumulate({2 .. n-1}, {1,0}, (s, c) => {s{0} + s{1}} & s),
Output = Table.FromColumns({List.Sort(list)},{"Fibonacci sequence"})
in
Output
Solving the challenge of Fibonacci Sequence! with Excel
Excel solution 1 for Fibonacci Sequence!, proposed by Bo Rydobon 🇹🇭:
=TEXTSPLIT(
SCAN(
"0-1",
SEQUENCE(
1151
),
LAMBDA(
a,
v,
LET(
b,
TEXTSPLIT(
a,
"-"
),
INDEX(
b,
2
)&"-"&Plus(
b
)
)
)
),
"-"
)
My formula broke at 1151
Where Plus is from
https://1drv.ms/x/s!Ak8Fla2fCeo6g-Fpe7d7v85Pm0EimA?e=5LHLvL
The 1150
https://zeptomath.com/calculators/fibonaccinumbers.php?number=1150
plus
=LAMBDA(
num,
[subtrack],
LET(
n,
TOROW(
num,
3
),
l,
CEILING(
MAX(
LEN(
n
)
),
10
), s,
-SORT(
-SEQUENCE(
l/10,
,
,
10
)
),
r,
REPT(
0,
10
), m,
VSTACK(
BYROW(
MID(
RIGHT(
REPT(
0,
l
)&n,
l
),
s,
10
)*IF(
subtrack,
{1,
-1},
1
),
SUM
),
0
), o,
TEXT(
MOD(
SCAN(
0,
m,
LAMBDA(
a,
v,
INT(
a/10^10
)+v
)
),
10^10
),
r
), c,
CONCAT(
TEXT(
SORTBY(
o,
VSTACK(
s,
0
)
),
r
)
), RIGHT(
c,
LEN(
TEXT(
c,
0
)
)
)
)
)
Excel solution 2 for Fibonacci Sequence!, proposed by Bo Rydobon 🇹🇭:
=LET(n,
SEQUENCE(
18
)-1,
f,
5^0.5,
((1+f)^n-(1-f)^n)/2^n/f)
Excel solution 3 for Fibonacci Sequence!, proposed by محمد حلمي:
=REDUCE(
{0;1},
SEQUENCE(
16
),
LAMBDA(
a,
v,
VSTACK(
a,
SUM(
TAKE(
a,
-2
)
)
)
)
)
Excel solution 4 for Fibonacci Sequence!, proposed by محمد حلمي:
=LET( r,
LAMBDA(
r,
a,
IF(
ROWS(
a
)=18,
a,
r(
r,
VSTACK(
a,
SUM(
TAKE(
a,
-2
)
)
)
)
)
), r(
r,
{0;1}
)
)
Excel solution 5 for Fibonacci Sequence!, proposed by Aditya Kumar Darak 🇮🇳:
=REDUCE(
{0; 1},
SEQUENCE(
16
),
LAMBDA(
a,
b,
VSTACK(
a,
SUM(
TAKE(
a,
-2
)
)
)
)
)
Excel solution 6 for Fibonacci Sequence!, proposed by Oscar Mendez Roca Farell:
=REDUCE(
{0;1},
ROW(
1:16
),
LAMBDA(
i,
x,
VSTACK(
i,
SUM(
LARGE(
i,
{1,
2}
)
)
)
)
)
Excel solution 7 for Fibonacci Sequence!, proposed by Julian Poeltl:
=REDUCE(
VSTACK(
0,
1
),
SEQUENCE(
16
),
LAMBDA(
A,
B,
VSTACK(
A,
INDEX(
A,
B
)+INDEX(
A,
B+1
)
)
)
)
=REDUCE(
VSTACK(
0,
1
),
SEQUENCE(
16
),
LAMBDA(
A,
B,
VSTACK(
A,
SUM(
TAKE(
A,
-2
)
)
)
)
)
Excel solution 8 for Fibonacci Sequence!, proposed by Abdallah Ally:
=LET(
n,
18,
f,
LAMBDA(
f,
a,
s,
IF(
COUNT(
a
)=n,
a,
f(
f,
IFS(
s=1,
a,
s=2,
VSTACK(
a,
1
),
1,
VSTACK(
a,
SUM(
TAKE(
a,
-2
)
)
)
),
s+1
)
)
),
f(
f,
0,
1
)
)
Excel solution 9 for Fibonacci Sequence!, proposed by Abdallah Ally:
=REDUCE(
0,
SEQUENCE(
18
),
LAMBDA(
x,
y,
IFS(
y=1,
x,
y=2,
VSTACK(
x,
1
),
1,
VSTACK(
x,
SUM(
TAKE(
x,
-2
)
)
)
)
)
)
Excel solution 10 for Fibonacci Sequence!, proposed by Sunny Baggu:
=MAP(
SEQUENCE(18, , 0),
LAMBDA(x,
((1 + SQRT(5)) ^ x - ((1 - SQRT(5)) ^ x)) /
(2 ^ x * SQRT(5))
)
)
Excel solution 11 for Fibonacci Sequence!, proposed by Sunny Baggu:
=MAP(
SEQUENCE(
18,
,
0
), LAMBDA(x, LET(
a,
SQRT(
5
), ((1 + a) ^ x - ((1 - a) ^ x)) / (2 ^ x * a)
)
)
)
Excel solution 12 for Fibonacci Sequence!, proposed by Sunny Baggu:
=TAKE( REDUCE( {0; 1}, SEQUENCE(
18
), LAMBDA(
a,
v,
VSTACK(
a,
SUM(
TAKE(
a,
-2
)
)
)
) ), 18)
Excel solution 13 for Fibonacci Sequence!, proposed by Bilal Mahmoud kh.:
=TOCOL(
REDUCE(
{0,
1},
SEQUENCE(
18
),
LAMBDA(
x,
y,
HSTACK(
x,
LARGE(
x,
1
)+LARGE(
x,
2
)
)
)
)
)
Excel solution 14 for Fibonacci Sequence!, proposed by Hussein SATOUR:
=REDUCE(
,
SEQUENCE(
18
),
LAMBDA(
x,
y,
VSTACK(
x,
SUM(
TAKE(
x,
-2
)
)
)
)
)
Excel solution 15 for Fibonacci Sequence!, proposed by Mey Tithveasna:
=LET(n,
SEQUENCE(
18,
,
0
),
((1+SQRT(
5
))^n-(1-SQRT(
5
))^n)/(2^n*SQRT(
5
)))
Excel solution 16 for Fibonacci Sequence!, proposed by Nicolas Micot:
=FRACTIONNER.TEXTE(
REDUCE(
"0;1";
SEQUENCE(
16
);
LAMBDA(
n_values;
seq_table;
n_values & ";" & SOMME(
PRENDRE(
FRACTIONNER.TEXTE(
n_values;
;
";"
)+0;
-2;
)
)
)
);
;
";"
)
Excel solution 17 for Fibonacci Sequence!, proposed by Rick Rothstein:
=REDUCE(
{0;1},
SEQUENCE(
16
),
LAMBDA(
a,
v,
VSTACK(
a,
SUM(
TAKE(
a,
-2
)
)
)
)
)
Solving the challenge of Fibonacci Sequence! with Python
Python solution 1 for Fibonacci Sequence!, proposed by Konrad Gryczan, PhD:
import pandas as pd
path = "CH-072 Fibonacci sequence .xlsx"
test = pd.read_excel(path, usecols="B").values.tolist()
test = [item for sublist in test for item in sublist]
def fibonacci_memo(n, memo={0: 0, 1: 1}):
if n not in memo:
memo[n] = fibonacci_memo(n - 1, memo) + fibonacci_memo(n - 2, memo)
return memo[n]
def generate_fibonacci_sequence(N):
return [fibonacci_memo(i) for i in range(N)]
result = generate_fibonacci_sequence(18)
print(result == test) # True
Python solution 2 for Fibonacci Sequence!, proposed by Abdallah Ally:
import pandas as pd
# Function to generate a list of the first n Fibonacci numbers
def fibonacci_sequence(n):
def fibonacci(n, memo={}):
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo)
return memo[n]
return [fibonacci(i) for i in range(n)]
# Perform data wrangling
df = pd.DataFrame(fibonacci_sequence(18), columns=['Fibonacci Sequence'])
# Display the final dataset
df
Python solution 3 for Fibonacci Sequence!, proposed by Saidi Namtanga:
def generate_fibonacci(n):
fib_sequence = [0, 1] # Starting the sequence with the first two numbers
while len(fib_sequence) < n:
fib_sequence.append(fib_sequence[-1] + fib_sequence[-2]) # Append the sum of the last two numbers
return fib_sequence
# Number of Fibonacci numbers to generate
n = 18
# Generate the Fibonacci sequence
fib_sequence = generate_fibonacci(n)
# Print the Fibonacci sequence
print(f"The first {n} Fibonacci numbers are:")
print(fib_sequence)
Solving the challenge of Fibonacci Sequence! with R
R solution 1 for Fibonacci Sequence!, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
path = "files/CH-072 Fibonacci sequence .xlsx"
test = read_excel(path, range = "B1:B19") %>% pull()
fibonacci <- function(n) {
if (n == 1) {
return(c(0))
} else if (n == 2) {
return(c(0, 1))
} else {
fib_sequence <- accumulate(3:n, .init = c(0, 1), ~ c(.x, .x[length(.x)] + .x[length(.x) - 1]))
return(unlist(fib_sequence[n-1]))
}
}
identical(fibonacci(18), test)
# [1] TRUE
------------------------------------------------------------
fibonacci1 <- function(n) {
fib_sequence <- numeric(n)
fib_sequence[1] <- 0
if (n > 1) {
fib_sequence[2] <- 1
}
if (n > 2) {
map(3:n, ~ {
fib_sequence[.x] <<- fib_sequence[.x - 1] + fib_sequence[.x - 2]
})
}
return(fib_sequence)
}
identical(fibonacci1(18), test)
# [1] TRUE
