Find the first 500 numbers starting with 10 where number multiplied by its reversed digits contain only those digits which are there in the number. Ex. 5137 => 5137 * 7315 = 37577155 => it contains only 5, 1, 3 & 7.
📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 571
Challenge Difficulty: ⭐️⭐️⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn
Solving the challenge of Digit-Constrained Product Numbers with Power Query
Power Query solution 1 for Digit-Constrained Product Numbers, proposed by Kris Jaganah:
let
A = Table.FromColumns(
{
List.RemoveNulls(
List.Transform(
{10 .. 1000156},
each
let
a = Text.From(_),
b = Text.Reverse(a),
c = Number.From(b) * _,
d = Text.Combine(List.Sort(List.Distinct(Text.ToList(Text.From(c))))),
e = Text.Combine(List.Sort(List.Distinct(Text.ToList(a)))),
f = if d = e then _ else null
in
f
)
)
},
{"Answer Expected"}
)
in
A
Power Query solution 2 for Digit-Constrained Product Numbers, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
Source = List.Transform(
List.Select(
List.Skip(
List.Generate(
() => [x = 9, y = 0],
each [y] <= 500,
each [
x = [x] + 1,
z = x * Number.From(Text.Reverse(Text.From(x))),
k = List.Sort(List.Distinct(Text.ToList(Text.From(x))))
= List.Sort(List.Distinct(Text.ToList(Text.From(z)))),
y = if k then [y] + 1 else [y]
],
each Record.ToList([[x], [k]])
)
),
each _{1} = true
),
each _{0}
)
in
Source
Power Query solution 3 for Digit-Constrained Product Numbers, proposed by Abdallah Ally:
let
Generator = (n) =>
[
f = (x, y) => List.ContainsAll(Text.ToList(x), Text.ToList(y)),
a = Text.From(n),
b = Text.From(n * Number.From(Text.Reverse(a))),
c = f(a, b) and f(b, a)
][c],
Result = List.RemoveNulls(
List.Generate(
() => [num = 9, cond = Generator(num), counter = 0],
each [counter] <= 500,
each [num = [num] + 1, cond = Generator(num), counter = [counter] + Byte.From(cond)],
each if [cond] then [num] else null
)
)
in
Result
Power Query solution 4 for Digit-Constrained Product Numbers, proposed by Abdallah Ally:
let
Generator = (n) =>
[
f = each List.Distinct(List.Sort(Text.ToList(_))),
a = Text.From(n),
b = Text.From(n * Number.From(Text.Reverse(a))),
c = f(a) = f(b)
][c],
Result = List.RemoveNulls(
List.Generate(
() => [num = 9, cond = Generator(num), counter = 0],
each [counter] <= 500,
each [num = [num] + 1, cond = Generator(num), counter = [counter] + Byte.From(cond)],
each if [cond] then [num] else null
)
)
in
Result
Power Query solution 5 for Digit-Constrained Product Numbers, proposed by Tyler N.:
let
a = {2 .. 457000},
b = List.Transform(
a,
each
let
c = Text.From(_),
d = {Text.ToList(Text.From(_ * Number.From(Text.Reverse(c)))), Text.ToList(c)},
e = Int8.From(List.Transform({0, 1}, each List.RemoveItems(d{_}, d{1 - _})) = {{}, {}})
in
e * _
)
in
List.RemoveItems(b, {0})
Solving the challenge of Digit-Constrained Product Numbers with Excel
Excel solution 1 for Digit-Constrained Product Numbers, proposed by Bo Rydobon 🇹🇭:
=TOCOL(
MAP(
SEQUENCE(
2^20,
,
10
),
LAMBDA(
n,
LET(
m,
MID(
n,
-SORT(
-SEQUENCE(
LEN(
n
)
)
),
1
),
o,
n*CONCAT(
m
),
IFS(
AND(
SORT(
UNIQUE(
m
)
)=SORT(
UNIQUE(
MID(
o,
SEQUENCE(
LEN(
o
)
),
1
)
)
)
),
n
)
)
)
),
3
)
Excel solution 2 for Digit-Constrained Product Numbers, proposed by John V.:
=LET(s,SEQUENCE,n,9+s(10^6+150),f,LAMBDA(x,UNIQUE(SORT(MID(x,s(15),1)))),TOCOL(n/MAP(n,n*BYROW(MID(n,8-s(,7),1),CONCAT),LAMBDA(a,b,AND(f(a)=f(b)))),2))
Excel solution 3 for Digit-Constrained Product Numbers, proposed by Kris Jaganah:
=TOCOL(MAP(SEQUENCE(
10.00025^6,
,
10
),
LAMBDA(x,
LET(a,
LEN(
x
),
b,
SEQUENCE,
c,
MID(
x,
b(
a,
,
a,
-1
),
1
),
d,
CONCAT(
c
)*x,
e,
MID(
d,
b(
LEN(
d
)
),
1
),
x/(MIN(--IFNA((SORT(
UNIQUE(
c
)
)=SORT(
UNIQUE(
e
)
)),
0)))))),
3)
Excel solution 4 for Digit-Constrained Product Numbers, proposed by Julian Poeltl:
=TAKE(LET(L,
LAMBDA(A,
CONCAT((LEN(
A
)-LEN(
SUBSTITUTE(
A,
SEQUENCE(
10,
,
0
),
""
)
))>0)),
S,
SEQUENCE(
1001000,
,
10
),
R,
MAP(
S,
LAMBDA(
A,
CONCAT(
MID(
A,
SEQUENCE(
LEN(
A
),
,
LEN(
A
),
-1
),
1
)
)
)
),
FILTER(
S,
MAP(
S,
R*S,
LAMBDA(
A,
B,
L(
A
)=L(
B
)
)
)
)),
500)
Excel solution 5 for Digit-Constrained Product Numbers, proposed by Alejandro Campos:
=LET(
tInicial, NOW(),
array, LET(
sq, SEQUENCE(1000150, , 10),
rev, MAP(sq, LAMBDA(x, --CONCAT(MID(x, LEN(x) + 1 - SEQUENCE(, LEN(x)), 1)))),
prd, BYROW(HSTACK(sq, rev), PRODUCT),
VerFal, MAP(
sq,
prd,
LAMBDA(x, y,
LET(
num, CONCAT(SORT(UNIQUE(MID(x, SEQUENCE(LEN(x)), 1)))),
rvrs, CONCAT(SORT(UNIQUE(MID(y, SEQUENCE(LEN(y)), 1)))),
AND(num = rvrs)))),
FILTER(sq, VerFal)),
VSTACK(TEXT(NOW() - tInicial, "[s],000s"), array))
Excel solution 6 for Digit-Constrained Product Numbers, proposed by Timothée BLIOT:
=LET(S,SEQUENCE(10^6+200,,10),R,MAP(S,LAMBDA(x,CONCAT(MID(x,LEN(x)+1-SEQUENCE(LEN(x)),1))))*S,F,LAMBDA(n,CONCAT(SORT(UNIQUE( MID(n,SEQUENCE(LEN(n)),1))))),FILTER(S,MAP(S,R,LAMBDA(x,y, F(x)=F(y)))))
Excel solution 7 for Digit-Constrained Product Numbers, proposed by Md. Zohurul Islam:
=LET(
P,
MAP(
SEQUENCE(
10 ^ 6,
,
10,
1
),
LAMBDA(
x,
LET(
z,
LEN(
x
),
a,
ABS(
MID(
x,
SEQUENCE(
z
),
1
)
),
rev,
CONCAT(
ABS(
MID(
x,
z - SEQUENCE(
z
) + 1,
1
)
)
),
prd,
x * rev,
b,
ABS(
MID(
prd,
SEQUENCE(
LEN(
prd
)
),
1
)
),
_c1,
IFERROR(
XMATCH(
a,
b
),
0
),
_c2,
IFERROR(
XMATCH(
b,
a
),
0
),
d,
IF(
AND(
_c1 <> 0,
_c2 <> 0
),
x,
0
),
d
)
)
),
Q,
FILTER(
P,
P > 0
),
Q
)
Excel solution 8 for Digit-Constrained Product Numbers, proposed by Hamidi Hamid:
=LET(sq,
SEQUENCE(
1001^2,
,
10,
1
),
t,
sq,
p,
BYROW(
MID(
sq,
SEQUENCE(
,
15
),
1
),
LAMBDA(
a,
CONCAT(
UNIQUE(
SORT(
a,
,
1,
-1
),
1
)
)
)
),
q,
BYROW(
SORTBY(
MID(
sq,
SEQUENCE(
,
15
),
1
),
SEQUENCE(
,
15,
15,
-1
)
),
LAMBDA(
a,
CONCAT(
UNIQUE(
SORT(
a,
,
1,
-1
),
1
)
)
)
),
s,
BYROW(
BYROW(
SORTBY(
MID(
sq,
SEQUENCE(
,
15
),
1
),
SEQUENCE(
,
15,
15,
-1
)
),
CONCAT
)*sq,
LAMBDA(
a,
CONCAT(
SORT(
UNIQUE(
MID(
a,
SEQUENCE(
,
15
),
1
),
1
),
1,
1,
1
)
)
)
),
r,
IFERROR(FILTER(t,
(p*1=s*1)*(s=q)),
""),
IF(
SEQUENCE(
520
)>500,
"",
r
))
Excel solution 9 for Digit-Constrained Product Numbers, proposed by Edwin Tisnado:
=TOCOL(SEQUENCE(10^6+150,,10)/MAP(SEQUENCE(10^6+150,,10),LAMBDA(x,LET(l,LEN,s,SEQUENCE,i,x*CONCAT(MID(x,s(l(x),,l(x),-1),1)),j,SORT(UNIQUE(MID(i,s(l(i)),1))),AND(j=SORT(UNIQUE(MID(x,s(l(x)),1))))))),2)
Excel solution 10 for Digit-Constrained Product Numbers, proposed by RIJESH T.:
=LET(list,SEQUENCE(1000156,,10),
rev,MAP(list,LAMBDA(a,CONCAT(MID(a,SORT(SEQUENCE(LEN(a)),,-1),1)))),
mut,rev*list,
FILTER(list,MAP(list,LAMBDA(b,CONCAT(UNIQUE(SORT(MID(b,SEQUENCE(LEN(b)),1))))))=
MAP(mut,LAMBDA(b,CONCAT(UNIQUE(SORT(MID(b,SEQUENCE(LEN(b)),1))))))))
Solving the challenge of Digit-Constrained Product Numbers with Python
Python solution 1 for Digit-Constrained Product Numbers, proposed by Konrad Gryczan, PhD:
import pandas as pd
path = "571 Product of Number and its revese.xlsx"
input = pd.read_excel(path)
result = [n for n in range(10, 1200000) if set(str(n)) == set(str(int(str(n)[::-1]) * n))][:500]
print(input["Answer Expected"].equals(pd.Series(result))) # True
Python solution 2 for Digit-Constrained Product Numbers, proposed by Anshu Bantra:
import itertools as itt
def rev_num(num: int):
return int(str(num)[::-1])
def set_num(num: int):
return set([*str(num)])
def some_set():
for num in range(10, 1004**20):
if set_num(num*rev_num(num)) == set_num(num):
yield num
numbers = some_set()
lst = itt.islice(numbers, 500)
lst = [*lst]
lst
Solving the challenge of Digit-Constrained Product Numbers with Python in Excel
Python in Excel solution 1 for Digit-Constrained Product Numbers, proposed by Alejandro Campos:
import time
def is_valid_number(num):
num_str = str(num)
reversed_num = int(num_str[::-1])
product = num * reversed_num
product_str = str(product)
if all(digit in num_str for digit in product_str) and all(digit in product_str for digit in num_str):
return True
return False
start_time = time.time()
valid_numbers = [num for num in range(10, 1000160) if is_valid_number(num)][:500]
end_time = time.time()
total_time = end_time - start_time
valid_numbers
&
Python in Excel solution 2 for Digit-Constrained Product Numbers, proposed by Abdallah Ally:
from collections import Counter
from itertools import count, islice
# Create a function to identify required numbers
def meets_criteria(num):
rev = int(str(num)[::-1]) * num
k1, k2 = Counter(str(num)), Counter(str(rev))
return k1.keys() == k2.keys()
# Perform data manipulation
numbers = list(islice(filter(meets_criteria, count(10)), 500))
numbers[490:]
Solving the challenge of Digit-Constrained Product Numbers with R
R solution 1 for Digit-Constrained Product Numbers, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
path = "Excel/571 Product of Number and its revese.xlsx"
test = read_excel(path) %>% unlist() %>% as.integer()
check_number = function(num) {
get_digits = function(n) {
digits = integer()
while (n > 0) {
digits = c(digits, n %% 10)
n = n %/% 10
}
return(sort(unique(digits)))
}
dig_num = get_digits(num)
rev_num = as.numeric(paste(rev(as.integer(strsplit(as.character(num), "")[[1]])), collapse = ""))
prod_num = num * rev_num
dig_prod = get_digits(prod_num)
return(identical(dig_num, dig_prod))
}
find_numbers = function(limit) {
results = integer(limit)
count = 0
num = 10
while (count < limit) {
if (check_number(num)) {
count = count + 1
results[count] = num
}
num = num + 1
}
return(results)
}
result = find_numbers(500)
all.equal(result, test)
# [1] TRUE
&&
