List those numbers which can be expressed as the sum of cube of two numbers. Also list those two numbers whose sum of cube is equal to that number. Ex. 133 which can be expressed as 2^3+5^3. Hence, answer is 133, 2 and 5 in 3 different cells in a row.
📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 331
Challenge Difficulty: ⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn
Solving the challenge of List numbers expressible as sum of two cubes with Power Query
Power Query solution 1 for List numbers expressible as sum of two cubes, proposed by Zoran Milokanović:
let
Source = Excel.CurrentWorkbook(){[Name = "Input"]}[Content],
S = Table.FromRows(
Table.SelectRows(
Table.AddColumn(
Source,
"L",
each
let
n = [Number],
p = (x, y) => Number.Power(x, y),
l = Int64.From(p(n, 1 / 3))
in
{n}
& (
List.Select(
List.TransformMany({1 .. l}, (x) => {x .. l}, (x, y) => {x, y}),
(s) => p(s{0}, 3) + p(s{1}, 3) = n
){0}?
?? {}
)
),
each List.Count([L]) = 3
)[L]
)
in
S
Power Query solution 2 for List numbers expressible as sum of two cubes, proposed by Aditya Kumar Darak 🇮🇳:
let
Source = Excel.CurrentWorkbook(){[Name = "data"]}[Content],
Record = Table.AddColumn(
Source,
"R",
each [
CF = Int16.From(Number.Power([Number], 1 / 3)),
L = List.TransformMany(
{1 .. CF},
(x) => {x .. CF},
(x, y) =>
[
A = Number.Power(x, 3) + Number.Power(y, 3),
B = if A = [Number] then {[Number], x, y} else null
][B]
)
][L]
),
Combine = List.RemoveNulls(List.Combine(Record[R])),
Return = Table.FromRows(Combine, {"Number", "Number1", "Number2"})
in
Return
Power Query solution 3 for List numbers expressible as sum of two cubes, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
Sol = Table.SelectRows(
Table.Combine(
Table.AddColumn(
Source,
"A",
(x) =>
let
a = {1 .. Number.RoundUp(Number.Power(x[Number], 1 / 3))},
b = {x[Number]}
& List.Sort(
List.Combine(
List.Transform(
a,
each List.Select(
a,
(y) => (Number.Power(_, 3) + Number.Power(y, 3) = x[Number])
)
)
)
)
in
Table.FromRows({b})
)[A]
),
each [Column2] <> null
)
in
Sol
Power Query solution 4 for List numbers expressible as sum of two cubes, proposed by Luan Rodrigues:
let
Fonte = Tabela1,
tab = Table.AddColumn(
Fonte,
"Personalizar",
each [
a = List.Transform(
{1 .. Number.RoundDown(Number.Power([Number], 1 / 3))},
(x) => {x} & {Number.Power(x, 3)}
),
b = List.Select(
List.Transform(
a,
(x) => Number.Round(Number.Power([Number] - Number.Power(x{0}, 3), 1 / 3), 3)
),
(x) => Number.Round(x, 3) = Number.From(Int64.From(x))
),
c = if List.Count(b) = 0 then null else Table.FromRows({{[Number]} & List.Sort(b)})
][c]
)[Personalizar],
res = Table.Combine(List.Select(tab, each _ <> null))
in
res
Power Query solution 5 for List numbers expressible as sum of two cubes, proposed by Alexis Olson:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source, {{"Number", Int64.Type}}),
#"Added Custom" = Table.AddColumn(
#"Changed Type",
"Custom",
each [
r3 = (x) => Number.Round(Number.Power(x, 1 / 3)),
lst = List.Transform({1 .. r3([Number])}, (a) => {a, r3([Number] - Number.Power(a, 3))}),
filter = List.Select(
lst,
(p) => (p{0} <= p{1}) and (Number.Power(p{0}, 3) + Number.Power(p{1}, 3) = [Number])
),
result = [first = try filter{0}{0} otherwise null, second = try filter{0}{1} otherwise null]
][result]
),
#"Expanded Custom" = Table.ExpandRecordColumn(
#"Added Custom",
"Custom",
{"first", "second"},
{"first", "second"}
),
#"Filtered Rows" = Table.SelectRows(#"Expanded Custom", each ([second] <> null))
in
#"Filtered Rows"
Power Query solution 6 for List numbers expressible as sum of two cubes, proposed by Brian Julius:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
AddCubeList = Table.AddColumn(
Source,
"NumList",
each [
a = {1 .. Number.RoundDown(Number.Power([Number], 1 / 3))},
b = List.Transform(a, each Number.Power(_, 3))
][b]
),
Expand = Table.ExpandListColumn(AddCubeList, "NumList"),
FilterCubeRootDiff = Table.AddColumn(
Expand,
"CubeRootDiff",
each Number.Power(([Number] - [NumList]), Value.Divide(1, 3)),
type number
),
AddNoRemainder = Table.SelectRows(
Table.AddColumn(
FilterCubeRootDiff,
"NoRemainder",
each (Number.RoundDown([CubeRootDiff], 1) - [CubeRootDiff]) < 0
),
each [NoRemainder] = false
),
Group = Table.Group(AddNoRemainder, {"Number"}, {{"All", each [CubeRootDiff], type list}}),
Extract = Table.TransformColumns(
Group,
{"All", each Text.Combine(List.Transform(_, Text.From), " "), type text}
),
Split = Table.SplitColumn(
Extract,
"All",
Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv),
{"Number2", "Number1"}
),
Clean = Table.ReorderColumns(
Table.TransformColumnTypes(Split, {{"Number2", Int64.Type}, {"Number1", Int64.Type}}),
{"Number", "Number1", "Number2"}
)
in
Clean
Power Query solution 7 for List numbers expressible as sum of two cubes, proposed by Ramiro Ayala Chávez:
let
Origen = Excel.CurrentWorkbook(){[Name = "Tabla1"]}[Content],
Fx = (x) =>
let
a = x,
b = {1 .. Number.RoundDown(Number.Power(a, 1 / 3))},
c = List.Select(List.TransformMany(b, (x) => b, (x, y) => {x, y}), each _{0} < _{1}),
d = List.Transform(c, each List.Transform(_, each Number.Power(_, 3))),
e = List.Select(d, each List.Sum(_) = a),
f = List.Transform(e, each List.Transform(_, each Number.Round(Number.Power(_, 1 / 3)))),
g = Text.Combine(List.Transform(f{0}, Text.From), ",")
in
try g otherwise null,
h = Table.AddColumn(Origen, "N", each Fx([Number])),
i = Table.SelectRows(h, each [N] <> null),
Sol = Table.SplitColumn(i, "N", Splitter.SplitTextByDelimiter(","), {"N1", "N2"})
in
Sol
Power Query solution 8 for List numbers expressible as sum of two cubes, proposed by Rafael González B.:
let
Source = Excel.CurrentWorkbook(){0[Content]},
TP = Table.AddColumn(Source, "Tbl", each
let
N = [Number],
N3 = Number.Round(Number.Power(N, 1/3)),
LP = {1..N3},
L3 = List.Transform(LP, each Number.Power(_,3)),
T1 = Table.AddColumn(Table.FromColumns({L3}, {"N"}), "Index", each 1),
TJ = Table.NestedJoin(T1, "Index", T1, "Index", "Join",1)[[N],[Join]],
TE = Table.ExpandTableColumn(TJ, "Join", {"N"}, {"N1"}),
TS = Table.SelectRows(TE, each [N] < [N1]),
TA = Table.AddColumn(TS, "Sum", each [N] + [N1]),
TC = Table.SelectRows(TA, each [Sum] = N),
TT = Table.TransformColumns(TC, {{"N", each LP{List.PositionOf(L3, _)}},
{"N1", each LP{List.PositionOf(L3, _)}}}
)[[N],[N1]]
in
TT
),
PR = Table.SelectRows(TP, each not Table.IsEmpty([Tbl])),
Anw = Table.ExpandTableColumn(PR, "Tbl", {"N", "N1"}, {"N", "N1"})
in
Anw
🧙♂️🧙♂️🧙♂️
Power Query solution 9 for List numbers expressible as sum of two cubes, proposed by Reece Adams, CFA:
let
Source = List.Numbers(1, n),
#"Converted to Table" = Table.FromList(
Source,
Splitter.SplitByNothing(),
null,
null,
ExtraValues.Error
),
#"Renamed Columns" = Table.RenameColumns(#"Converted to Table", {{"Column1", "n"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "n2", each List.Numbers(1, n)),
#"Expanded n2" = Table.ExpandListColumn(#"Added Custom", "n2"),
#"Added Custom1" = Table.AddColumn(
#"Expanded n2",
"helper",
each
if [n] < [n2] then
Text.Combine({Text.From([n]), Text.From([n2])})
else
Text.Combine({Text.From([n2]), Text.From([n])})
),
#"Removed Duplicates" = Table.Distinct(#"Added Custom1", {"helper"}),
#"Removed Columns" = Table.RemoveColumns(#"Removed Duplicates", {"helper"}),
#"Added Custom2" = Table.AddColumn(
#"Removed Columns",
"output",
each Number.Power([n], 3) + Number.Power([n2], 3)
)
in
#"Added Custom2"
Solving the challenge of List numbers expressible as sum of two cubes with Excel
Excel solution 1 for List numbers expressible as sum of two cubes, proposed by Bo Rydobon 🇹🇭:
=DROP(REDUCE(0,A2:A10,LAMBDA(a,v,LET(p,1/3,b,ROUND((v-SEQUENCE(v^p)^3)^p,9),x,XMATCH(0,MOD(b,1)),IF(ISNA(x),a,VSTACK(a,HSTACK(v,x,INDEX(b,x))))))),1)
Excel solution 2 for List numbers expressible as sum of two cubes, proposed by John V.:
=--TEXTSPLIT(TEXTJOIN(" ",
,
MAP(A2:A10,
LAMBDA(x,
LET(s,
SEQUENCE(
x^0.4
),
t,
TOROW(
s
),
CONCAT(REPT(x&"|"&s&"|"&t,
(s<=t)*(x=s^3+t^3))))))),
"|",
" ")
Excel solution 3 for List numbers expressible as sum of two cubes, proposed by محمد حلمي:
=--TEXTSPLIT(
CONCAT(
TOCOL(
MAP(
A2:A10,
LAMBDA(
a,
LET(
v,
SEQUENCE(
a^0.34
),
i,
TOROW(
v
),
@TOCOL(
IFS(
v^3+i^3=a,
a&" "&v&" "&i
),
2
)
)
)
),
2
)&"/"
),
" ",
"/",
1
)
Excel solution 4 for List numbers expressible as sum of two cubes, proposed by Kris Jaganah:
=LET(a,
500,
b,
SEQUENCE(
a
),
c,
TOCOL(
INDEX(
b,
SEQUENCE(
a,
a,
,
1/a
)
),
,
1
),
d,
INDEX(
b,
SEQUENCE(
a*a,
,
,
1/a
)
),
e,
(c^3)+(d^3),
f,
REDUCE("",
A2:A10,
LAMBDA(x,
y,
VSTACK(x,
LET(p,
FILTER(
HSTACK(
e,
c,
d
),
e=y,
""
),
q,
TAKE(
p,
,
-1
),
FILTER(p,
(q=MAX(
q
)),
""))))),
FILTER(
f,
TAKE(
f,
,
1
)<>""
))
Excel solution 5 for List numbers expressible as sum of two cubes, proposed by Julian Poeltl:
=LET(
S,
SEQUENCE(
500
),
R,
REDUCE(
"",
A2:A10,
LAMBDA(
A,
B,
VSTACK(
A,
HSTACK(
B,
IFNA(
LET(
X,
XMATCH(
B,
TOCOL(
S^3+TOROW(
S
)^3
)
),
HSTACK(
ROUNDUP(
X/500,
0
),
MOD(
X,
500
)
)
),
""
)
)
)
)
),
FILTER(
R,
IFNA(
CHOOSECOLS(
R,
2
),
""
)<>""
)
)
Excel solution 6 for List numbers expressible as sum of two cubes, proposed by Timothée BLIOT:
=LET(A,
MAKEARRAY(500,
500,
LAMBDA(x,
y,
(x^3)+(y^3))),
B,
TRANSPOSE(
SORT(
WRAPCOLS(
DROP(
TOCOL(
REDUCE(
"",
A2:A10,
LAMBDA(
w,
v,
VSTACK(
w,
IF(
A=v,
SEQUENCE(
,
500
),
1/0
)
)
)
),
3
),
1
),
2
),
,
,
0
)
),
HSTACK(
INDEX(
B,
,
1
)^3+INDEX(
B,
,
2
)^3,
B
))
Excel solution 7 for List numbers expressible as sum of two cubes, proposed by Hussein SATOUR:
=TEXTSPLIT(CONCAT(IFERROR(MAP(A2:A10,
LAMBDA(x,
LET(b,
SEQUENCE(ROUNDUP(x^(1/3),
0)),
c,
x-b^3,
TEXTJOIN(",",
,
x,
FILTER(b,
c^(1/3)=INT(c^(1/3))))&"/"))),
"")),
",",
"/",
1)
Excel solution 8 for List numbers expressible as sum of two cubes, proposed by Sunny Baggu:
=LET(
_s, SEQUENCE(500),
_c, TOROW(_s),
_z, DROP(
REDUCE(
"",
A2:A10,
LAMBDA(a, v,
VSTACK(
a,
IFERROR(HSTACK(v, TOROW(SORT(TOCOL(IF(_s ^ 3 + _c ^ 3 = v, _c, 1 / x), 3)))), "")
)
)
),
1
),
FILTER(_z, ISNUMBER(TAKE(_z, , -1)))
)
Excel solution 9 for List numbers expressible as sum of two cubes, proposed by LEONARD OCHEA 🇷🇴:
=TEXTSPLIT(TEXTJOIN("*",
,
MAP(A2:A10,
LAMBDA(a,
LET(s,
SEQUENCE(a^(1/3)),
t,
TOROW(
&s
),
CONCAT(IF((s>t)*s^3+t^3=a,
a&"|"&t&"|"&s,
"")))))),
"|",
"*")
Excel solution 10 for List numbers expressible as sum of two cubes, proposed by Abdallah Ally:
=DROP(REDUCE("",
A2:A10,
LAMBDA(x,
y,
LET(s,
LET(a,
y,
b,
SEQUENCE(,
a^(1/3)-1,
2),
c,
b^3,
d,
IFERROR(
DROP(
REDUCE(
"",
b,
LAMBDA(
x,
y,
IF(
OR(
y^3+c=a
),
HSTACK(
x,
y
),
x
)
)
),
,
1
),
""
),
IF(
COUNT(
d
)=2,
HSTACK(
a,
d
),
""
)),
IF(
COUNT(
s
)=3,
VSTACK(
x,
s
),
x
)))),
1)
Excel solution 11 for List numbers expressible as sum of two cubes, proposed by 🇵🇪 Ned Navarrete C.:
=--TEXTSPLIT(TEXTJOIN("/",
,
MAP(A2:A10,
LAMBDA(r,
LET(d,
SEQUENCE(r^(1/3)),
m,
r=((d^3)+(TOROW(
d
)^3)),
IF(
SUM(
m*1
)>0,
TEXTJOIN(
"*",
,
r,
FILTER(
d,
BYROW(
m,
LAMBDA(
f,
SUM(
f*1
)
)
)
)
),
""
))))),
"*",
"/")
Excel solution 12 for List numbers expressible as sum of two cubes, proposed by Md. Zohurul Islam:
=LET(z,
A2:A10,
mx,
MAX(INT(z^(1/3))),
sq,
SEQUENCE(
mx^2
),
a,
MOD(
sq,
mx
)+1,
b,
INT(
sq/mx
)+1,
c,
IF(
a>b,
a^3+b^3,
0
),
d,
XLOOKUP(
c,
z,
z,
0
),
e,
HSTACK(
c,
b,
a
),
f,
SORT(
FILTER(
e,
d>0
),
1
),
f)
Excel solution 13 for List numbers expressible as sum of two cubes, proposed by Charles Roldan:
=LET(n,
A2:A10,
f,
LAMBDA(x,
[y],
(x - y ^ 3) ^ (1 / 3)),
a,
MAP(n,
LAMBDA(k,
LET(s,
SEQUENCE(
f(
k / 2
)
),
XLOOKUP(TRUE,
LAMBDA(
x,
x = INT(
x
)
)(f(
k,
s
)),
s,
0)))),
FILTER(
HSTACK(
n,
a,
f(
n,
a
)
),
a
))
Excel solution 14 for List numbers expressible as sum of two cubes, proposed by Pieter de Bruijn:
=LET(r,
REDUCE("",
A2:A10,
LAMBDA(xx,
yy,
VSTACK(xx,
IFERROR(HSTACK(yy,
BYCOL(LET(a,
SEQUENCE(yy^(1/3))^3,
i,
ROWS(
a
),
f,
--MID(
BASE(
SEQUENCE(
,
2^i
),
2,
i
),
SEQUENCE(
i
),
1
),
IF(FILTER(
f,
MMULT(
TOROW(
a
),
f
)=yy
),
a^(1/3),
"")),
LAMBDA(
x,
TEXTJOIN(
"+",
1,
x
)
))),
"")))),
IFNA(
FILTER(
r,
TAKE(
r,
,
1
)<>""
),
""
))
Excel solution 15 for List numbers expressible as sum of two cubes, proposed by Pieter de Bruijn:
=TEXTSPLIT(TEXTAFTER(TOCOL(MAP(A2:A10,
LAMBDA(a,
LET(s,
SEQUENCE(a^(1/3)),
c,
s^3,
TEXTJOIN(
"|",
,
"|"&a,
TOCOL(
IFS(
c+TOROW(
c
)=a,
s&"|"&TOROW(
s
)
),
2
)
)))),
2),
"|",
{1,
2,
3}),
"|")
Excel solution 16 for List numbers expressible as sum of two cubes, proposed by Giorgi Goderdzishvili:
=LET(
_nmb,
A2:A10,
_comb,
TOCOL(
SEQUENCE(
500
)&"-"&SEQUENCE(
,
500
)
),
_fr,
TEXTBEFORE(
_comb,
"-"
)*1,
_sc,
TEXTAFTER(
_comb,
"-"
)*1,
_nm,
_fr^3+_sc^3,
_stck,
HSTACK(
_nm,
_fr,
_sc
),
_flt,
FILTER(
_stck,
ISNUMBER(
XMATCH(
_nm,
_nmb,
0
)
)
),
CHOOSEROWS(
SORT(
_flt
),
SEQUENCE(
,
ROWS(
_flt
)/2,
1,
2
)
)
)
Excel solution 17 for List numbers expressible as sum of two cubes, proposed by Edwin Tisnado:
=--TEXTSPLIT(TEXTJOIN("/",
1,
TOCOL(MAP(A2:A10,
LAMBDA(t,
ARRAYTOTEXT(HSTACK(t,
TOROW(1/(1/(MAP(SEQUENCE(t^(1/3)),
LAMBDA(x,
SUM(MAP(SEQUENCE(t^(1/3)),
LAMBDA(y,
(x^3+y^3=t)*x))))))),
2))))),
2)),
", ",
"/")
Excel solution 18 for List numbers expressible as sum of two cubes, proposed by Abdelrahman Omer, MBA, PMP:
=--TEXTSPLIT(TEXTJOIN("#",
,
TOCOL(MAP(A2:A10,
LAMBDA(a,
LET(b,
SEQUENCE(ROUNDUP(a^(1/3),
1)),
c,
BYROW(b,
LAMBDA(x,
(--(MOD(ROUND((a-x^3)^(1/3),
10),
1)=0)))),
d,
FILTER(b,
(a-b)*c),
TEXTJOIN(
",",
,
a,
d
)))),
2)),
",",
"#")
Excel solution 19 for List numbers expressible as sum of two cubes, proposed by Gabriel Raigosa:
=LET(x,
A2:A10,
s,
INT(MAX(x^(1/3))),
t,
SEQUENCE(
s^2
),
b,
MOD(
t,
s
)+1,
a,
INT(
t/s
)+1,
n,
IF(
b>a,
a^3+b^3,
),
SORT(FILTER(HSTACK(
n,
a,
b
),
(XLOOKUP(
n,
x,
x,
0
))),
3))
➡️ES:
=LET(x,
A2:A10,
s,
ENTERO(MAX(x^(1/3))),
t,
SECUENCIA(
s^2
),
b,
RESIDUO(
t,
s
)+1,
a,
ENTERO(
t/s
)+1,
n,
SI(
b>a,
a^3+b^3,
),
ORDENAR(FILTRAR(APILARH(
n,
a,
b
),
(BUSCARX(
n,
x,
x,
0
))),
3))
OP_2:
▶️EN:
=LET(x,
A2:A10,
s,
INT(MAX(x^(1/3))),
t,
SEQUENCE(
s^2
),
b,
MOD(
t,
s
)+1,
a,
INT(
t/s
)+1,
n,
IF(
b>a,
a^3+b^3,
),
SORT(
FILTER(
HSTACK(
n,
a,
b
),
ISNUMBER(
XMATCH(
n,
x
)
)
),
3
))
▶️ES:
=LET(x,
A2:A10,
s,
ENTERO(MAX(x^(1/3))),
t,
SECUENCIA(
s^2
),
b,
RESIDUO(
t,
s
)+1,
a,
ENTERO(
t/s
)+1,
n,
SI(
b>a,
a^3+b^3,
),
ORDENAR(
FILTRAR(
APILARH(
n,
a,
b
),
ESNUMERO(
COINCIDIRX(
n,
x
)
)
),
3
))
Excel solution 20 for List numbers expressible as sum of two cubes, proposed by Jeff Blakley:
=--TEXTSPLIT(CONCAT(MAP(A2:A10,
LAMBDA(x,
LET(r,
SEQUENCE(x^(1/3)),
c,
TOROW(
r
),
v,
TOCOL(
r^3+c^3
),
XLOOKUP(
x,
v,
v&" "&TOCOL(
r&" "&c
)&";",
""
))))),
" ",
";",
1)
Excel solution 21 for List numbers expressible as sum of two cubes, proposed by Reece Adams, CFA:
=LET(
n;
100;
exponent;
3;
nCombos;
HSTACK(
INT(
SEQUENCE(
n*n;
;
0
)/n
)+1;
MOD(
SEQUENCE(
n*n;
;
0
);
n
)+1
);
DuplicateHelper;
BYROW(
nCombos;
LAMBDA(
x;
TEXTJOIN(
"|";
;
SORT(
x;
;
;
TRUE
)
)
)
);
UniqueRows;
UNIQUE(
DuplicateHelper;
FALSE;
FALSE
);
nCombosHelper;
CHOOSEROWS(
nCombos;
XMATCH(
UniqueRows;
DuplicateHelper
)
);
output;
HSTACK(
BYROW(
nCombosHelper;
LAMBDA(
x;
SUM(
x^exponent
)
)
);
nCombosHelper
);
output
)
Excel solution 22 for List numbers expressible as sum of two cubes, proposed by Reece Adams, CFA:
=LET(n;
100;
data;
HSTACK(TOCOL(((SEQUENCE(
n
)^3)+(SEQUENCE(
;
n
)^3)));
INT(
SEQUENCE(
n*n;
;
0
)/n
)+1;
MOD(
SEQUENCE(
n*n;
;
0
);
n
)+1);
DuplicateHelper;
BYROW(
DROP(
data;
0;
1
);
LAMBDA(
x;
TEXTJOIN(
"|";
;
SORT(
x;
;
;
TRUE
)
)
)
);
dataHelper;
HSTACK(
data;
DuplicateHelper
);
UniqueRows;
UNIQUE(
DuplicateHelper;
FALSE;
FALSE
);
DROP(
CHOOSEROWS(
dataHelper;
XMATCH(
UniqueRows;
INDEX(
dataHelper;
0;
4
);
0
)
);
;
-1
)
)
Solving the challenge of List numbers expressible as sum of two cubes with Python in Excel
Python in Excel solution 1 for List numbers expressible as sum of two cubes, proposed by John V.:
Hi everyone!
[(n, i, j) for n in xl("A2:A10")[0] for i, j in combinations(range(int(n ** 0.4)), 2) if i**3 + j**3 == n]
Blessings!
Solving the challenge of List numbers expressible as sum of two cubes with R
R solution 1 for List numbers expressible as sum of two cubes, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
range = "B2:D6",
col_names = c("Number", "Factor1", "Factor2"))
check_if_sum_of_cubes = function(number) {
x = floor(number^(1/3))
range = data.frame(Number = number, Factor1 = 1:x) %>%
mutate(diff = number - Factor1^3,
is_cube = round(diff^(1/3))^3 == diff) %>%
filter(is_cube) %>%
mutate(Factor2 = diff^(1/3)) %>%
slice(1) %>%
select(Number, Factor1, Factor2)
return(range)
}
result = map_dfr(input$Number, check_if_sum_of_cubes) %>% as_tibble()
Solving the challenge of List numbers expressible as sum of two cubes with Excel VBA
Excel VBA solution 1 for List numbers expressible as sum of two cubes, proposed by Nicolas Micot:
=FRACTIONNER.TEXTE(JOINDRE.TEXTE("|";VRAI;f_challenge331(A2:A10));";";"|")+0
VBA code used:
Function f_challenge331(nombres) As Variant
Dim nombreMax, premierNombre, secondNombre
Dim resultat() As String
nombres = nombres
If Not IsArray(nombres) Then
temp = nombres
ReDim nombres(1 To 1, 1 To 1)
nombres(1, 1) = temp
End If
ReDim resultat(1 To UBound(nombres, 1), 1 To 1)
For i = 1 To UBound(nombres)
nombreMax = Round(nombres(i, 1) ^ (1 / 3), 0)
For premierNombre = 1 To nombreMax
For secondNombre = premierNombre To nombreMax
If premierNombre ^ 3 + secondNombre ^ 3 = nombres(i, 1) Then
resultat(i, 1) = nombres(i, 1) & ";" & premierNombre & ";" & secondNombre
GoTo nombreSuivant
End If
Next secondNombre
Next premierNombre
nombreSuivant:
Next i
f_challenge331 = resultat
End Function
&&
