Pandovan Sequence – In number theory, the Pandovan sequence is the sequence of integers P(n) defined[1] by the initial values P ( 0 ) = P ( 1 ) = P ( 2 ) = 1 and the recurrence relation P ( n ) = P ( n − 2 ) + P ( n − 3 ) Find the nth term given in column A.
📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 485
Challenge Difficulty: ⭐️⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn
Solving the challenge of Compute Pandovan Sequence Terms with Power Query
Power Query solution 1 for Compute Pandovan Sequence Terms, proposed by Aditya Kumar Darak 🇮🇳:
let
Source = Excel.CurrentWorkbook(){[Name = "data"]}[Content],
Max = List.Max(Source[n]),
Generate = List.Generate(
() => [a = - 1, b = {0, 0, 0}],
each [a] <= Max,
each [a = [a] + 1, b = {[b]{1}, [b]{2}, c}, c = List.Max({[b]{0} + [b]{1}, 1})],
each [n = [a], Answer = [c]]
),
Table = Table.FromRecords(List.Skip(Generate)),
Return = Table.Join(Source, "n", Table, "n")
in
Return
Power Query solution 2 for Compute Pandovan Sequence Terms, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
Sol = Table.AddColumn(
Source,
"Custom",
(x) =>
List.Last(
let
LG = List.Generate(
() => [x = 0, y = {1}],
each [x] <= x[n],
each [x = [x] + 1, y = if [x] < 2 then [y] & {1} else [y] & {[y]{x - 2} + [y]{x - 3}}],
each List.Last([y])
)
in
LG
)
)
in
Sol
Power Query solution 3 for Compute Pandovan Sequence Terms, proposed by Alexis Olson:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
Max = List.Max(Source[n]),
All = List.Accumulate({3 .. Max}, {1, 1, 1}, (L, n) => L & {L{n - 2} + L{n - 3}}),
ToTable = Table.FromColumns({{0 .. Max}, All}, {"n", "Answer Expected"}),
Result = Table.SelectRows(ToTable, each List.Contains(Source[n], [n]))
in
Result
Power Query solution 4 for Compute Pandovan Sequence Terms, proposed by Abdallah Ally:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
f = (num) =>
List.Last(
List.Accumulate(
{0 .. num},
{},
(x, y) => if y < 3 then x & {1} else x & {List.Sum(List.FirstN(List.LastN(x, 3), 2))}
)
),
a = Table.AddColumn(Source, "My Answer", each f([n])),
Result = Table.AddColumn(a, "Check", each [Answer Expectecd] = [My Answer])
in
Result
Solving the challenge of Compute Pandovan Sequence Terms with Excel
Excel solution 1 for Compute Pandovan Sequence Terms, proposed by Bo Rydobon 🇹🇭:
=INDEX(REDUCE(1,SEQUENCE(84),LAMBDA(a,v,VSTACK(a,SUM(TAKE(TAKE(a,-3),2))))),A2:A10)
Excel solution 2 for Compute Pandovan Sequence Terms, proposed by Rick Rothstein:
=CHOOSEROWS(DROP(REDUCE({1;1;1},SEQUENCE(84),LAMBDA(a,v,VSTACK(a,SUM(TAKE(TAKE(a,-3),2))))),1),A2:A10)
Excel solution 3 for Compute Pandovan Sequence Terms, proposed by John V.:
=INDEX(
REDUCE(
,
SEQUENCE(
A10
),
LAMBDA(
a,
v,
VSTACK(
a,
SUM(
TAKE(
TAKE(
a,
-3
),
2
)
)
)
)
),
A2:A10
)
Excel solution 4 for Compute Pandovan Sequence Terms, proposed by محمد حلمي:
=INDEX(
REDUCE(
{1;1},
SEQUENCE(
A10
),
LAMBDA(
a,
v,
VSTACK(
a,
SUM(
TAKE(
TAKE(
a,
-3
),
2
)
)
)
)
),
A2:A10
)
Excel solution 5 for Compute Pandovan Sequence Terms, proposed by محمد حلمي:
=INDEX(
REDUCE(
{1;1;1},
SEQUENCE(
MAX(
A2:A10
)
),
LAMBDA(
a,
v,
VSTACK(
a,
SUM(
CHOOSEROWS(
a,
-2,
-3
)
)
)
)
),
A2:A10+1
)
Excel solution 6 for Compute Pandovan Sequence Terms, proposed by Julian Poeltl:
=INDEX(REDUCE(VSTACK(1,1,1),SEQUENCE(MAX(A2:A10-2)),LAMBDA(A,B,VSTACK(A,INDEX(A,B)+INDEX(A,B+1)))),A2:A10+1)
Excel solution 7 for Compute Pandovan Sequence Terms, proposed by Timothée BLIOT:
=INDEX(
REDUCE(
{1;1;2},
SEQUENCE(
99
),
LAMBDA(
w,
v,
VSTACK(
w,
SUM(
DROP(
TAKE(
w,
-3
),
-1
)
)
)
)
),
A2:A10
)
Excel solution 8 for Compute Pandovan Sequence Terms, proposed by Sunny Baggu:
=LET(
_a, SEQUENCE(MAX(A2:A10) + 1, , 0),
INDEX(
REDUCE(
{1; 1; 1},
_a,
LAMBDA(a, v, VSTACK(a, SUM(CHOOSEROWS(a, -2, -3))))
),
XMATCH(A2:A10, _a)
)
)
Excel solution 9 for Compute Pandovan Sequence Terms, proposed by Abdallah Ally:
=MAP(A2:A10,LAMBDA(x,TAKE(REDUCE(1,SEQUENCE(x),LAMBDA(x ,y,VSTACK(x,IF(y<3,1,SUM(CHOOSEROWS(x,-2,-3)))))),-1)))
Excel solution 10 for Compute Pandovan Sequence Terms, proposed by Bilal Mahmoud kh.:
=MAP(A2:A10,LAMBDA(e,LET(r,IFERROR(REDUCE({1;1;1},SEQUENCE(IF(e>3,e-3,0)),LAMBDA(x,y,VSTACK(x,LARGE(x,2)+LARGE(x,3)))),1),b,IFERROR(LARGE(r,2)+LARGE(r,3),1),b)))
Excel solution 11 for Compute Pandovan Sequence Terms, proposed by Ziad A.:
=INDEX(REDUCE({1;1;1},SEQUENCE(A1),LAMBDA(a,_,{a;SUM(CHOOSEROWS(a,-2,-3))})),A1+1)
Recursive, (up to n=41)
=LET(P,LAMBDA(P,n,IF(n<3,1,P(P,n-2)+P(P,n-3))),P(P,A1))
Excel solution 12 for Compute Pandovan Sequence Terms, proposed by Edwin Tisnado:
=MAP(
A2:A10,
LAMBDA(
x,
IF(
x<3,
1,
TAKE(
REDUCE(
{1;1;1},
SEQUENCE(
x-2
),
LAMBDA(
u,
v,
VSTACK(
u,
INDEX(
u,
v
)+INDEX(
u,
v+1
)
)
)
),
-1
)
)
)
)
Excel solution 13 for Compute Pandovan Sequence Terms, proposed by Diarmuid Early:
=LET(p,
((9 - SQRT(
69
))^(1/3) + (9 + SQRT(
69
))^(1/3))/(2^(1/3)*3^(2/3)),
ROUND(p^5/(2*p+3)*p^A2:A10,
0))
Or if you want to make it shorter,
you can hard-code the numbers:
=ROUND(
0.722124418303123*1.32471795724475^A2:A10,
0
)
Excel solution 14 for Compute Pandovan Sequence Terms, proposed by Burhan Cesur:
=CHOOSEROWS(
DROP(
REDUCE(
SEQUENCE(
3
)^0,
SEQUENCE(
MAX(
A2:A10
)
),
LAMBDA(
s,
v,
VSTACK(
s,
SUM(
INDEX(
s,
SEQUENCE(
2,
,
COUNT(
s
)-2
)
)
)
)
)
),
1
),
A2:A10
)
Excel solution 15 for Compute Pandovan Sequence Terms, proposed by Bevon Clarke:
=LET(
n,
20,
seq,
SEQUENCE(
n,
1,
0,
1
),
p,
LAMBDA(
a,
c,
IF(
c<3,
1,
INDEX(
a,
c-2
)+INDEX(
a,
c-3
)
)
),
result,
SCAN(
0,
seq,
LAMBDA(
acc,
idx,
VSTACK(
acc,
p(
acc,
idx
)
)
)
),
DROP(
result,
1
)
)
Solving the challenge of Compute Pandovan Sequence Terms with Python
Python solution 1 for Compute Pandovan Sequence Terms, proposed by Konrad Gryczan, PhD:
With memoization to speed it up.
from functools import lru_cache
import pandas as pd
path = "485 Pandovan Sequence.xlsx"
input = pd.read_excel(path, usecols="A")
test = pd.read_excel(path, usecols="B")
@lru_cache(maxsize=None)
def padovan(n):
if n <= 2:
return 1
else:
return padovan(n - 2) + padovan(n - 3)
input["Pandovan"] = input["n"].apply(padovan)
print(input["Pandovan"].equals(test["Answer Expectecd"])) # True
Solving the challenge of Compute Pandovan Sequence Terms with Python in Excel
Python in Excel solution 1 for Compute Pandovan Sequence Terms, proposed by Abdallah Ally:
import pandas as pd
# Function to generate an nth pandovan number
def pandovan_number(n, memo={}):
if n in memo:
return memo[n]
if n < 3:
return 1
memo[n] = pandovan_number(n - 2, memo) + pandovan_number(n - 3, memo)
return memo[n]
file_path = 'Excel_Challenge_485 - Pandovan Sequence.xlsx'
df = pd.read_excel(file_path)
# Perform data wrangling
df['My Answer'] = df['n'].map(pandovan_number)
df['Check'] = df['Answer Expectecd'] == df['My Answer']
df
Solving the challenge of Compute Pandovan Sequence Terms with R
R solution 1 for Compute Pandovan Sequence Terms, proposed by Konrad Gryczan, PhD:
With memoization to speed it up.
library(purrr)
library(memoise)
library(readxl)
library(tidyverse)
path = "Excel/485 Pandovan Sequence.xlsx"
input = read_excel(path, range = "A1:A10")
test = read_excel(path, range = "B1:B10")
padovan <- function(n) {
if (n <= 2) {
return(1)
} else {
return(padovan_memo(n - 2) + padovan_memo(n - 3))
}
}
padovan_memo = memoise(padovan)
result = input %>%
mutate(`Answer Expectecd` = map_dbl(n, padovan_memo))
identical(result$`Answer Expectecd`, test$`Answer Expectecd`)
# [1] TRUE
R solution 2 for Compute Pandovan Sequence Terms, proposed by Anil Kumar Goyal:
pandovan <- function(n){
reduce(seq(n - 2), .init = c(1, 1, 1), .f = ~ {
c(.x, sum(head(.x, 2))) %>% tail(3)
}) %>%
tail(1)
}
map_dbl(c(2, 5, 9, 17, 38, 44, 67, 78, 84), pandovan)
&&&
