Hi, Nartoo.
Thanks for the background on the product except 0. That's a fun problem!
Unless I'm missing something, here's a quite simple CP model for this:
"""
import cp. % Base 3..7: 1min33.5s
% import sat. % Base 3..7: 9min47.9s
main => go.
go ?=>
nolog,
member(Base,3..7),
println(base=Base),
member(Len,2..10), % number of digits
member(K,2..5),
power_k(Base,K,Len, A,X) ,
println([base=Base,len=Len,k=K,a=A,x_in_dec=X,x_in_base=X.to_radix_string(Base)]),
fail,
nl.
go => true.
power_k(Base,K,Len, A,X) =>
A = new_list(Len), % The list of digits
A :: 0..Base-1,
% No leading 0
A[1] #!= 0,
% convert to a number X (in base Base)
to_num(A,Base,X),
% Each digit ** K
% Here is the product except 0 logic
X #= prod([cond(T #= 0, 1,T) : I in 1..Len, T #= A[I]**K]),
solve($[ff,split],A ++ [X]).
% converts a number Num to/from a list of integer List given a base Base
to_num(List, Base, Num) =>
Len = length(List),
Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]).
"""
The output is
"""
base = 3
[base = 3,len = 3,k = 4,a = [1,2,1],x_in_dec = 16,x_in_base = 121]
[base = 3,len = 4,k = 5,a = [1,0,1,2],x_in_dec = 32,x_in_base = 1012]
[base = 3,len = 7,k = 5,a = [1,1,0,1,2,2,1],x_in_dec = 1024,x_in_base = 1101221]
[base = 3,len = 8,k = 4,a = [1,2,1,2,1,2,0,1],x_in_dec = 4096,x_in_base = 12121201]
base = 4
[base = 4,len = 2,k = 3,a = [2,0],x_in_dec = 8,x_in_base = 20]
[base = 4,len = 3,k = 5,a = [2,0,0],x_in_dec = 32,x_in_base = 200]
[base = 4,len = 4,k = 3,a = [3,1,2,0],x_in_dec = 216,x_in_base = 3120]
base = 5
[base = 5,len = 3,k = 4,a = [3,1,1],x_in_dec = 81,x_in_base = 311]
[base = 5,len = 3,k = 5,a = [1,1,2],x_in_dec = 32,x_in_base = 112]
[base = 5,len = 4,k = 2,a = [1,0,3,4],x_in_dec = 144,x_in_base = 1034]
[base = 5,len = 9,k = 3,a = [2,1,1,3,0,2,4,2,1],x_in_dec = 884736,x_in_base = 211302421]
base = 6
[base = 6,len = 2,k = 2,a = [1,3],x_in_dec = 9,x_in_base = 13]
[base = 6,len = 2,k = 3,a = [1,2],x_in_dec = 8,x_in_base = 12]
[base = 6,len = 4,k = 2,a = [1,5,0,4],x_in_dec = 400,x_in_base = 1504]
[base = 6,len = 4,k = 3,a = [2,2,1,2],x_in_dec = 512,x_in_base = 2212]
[base = 6,len = 4,k = 4,a = [1,1,0,4],x_in_dec = 256,x_in_base = 1104]
[base = 6,len = 6,k = 3,a = [3,2,5,0,0,0],x_in_dec = 27000,x_in_base = 325000]
[base = 6,len = 6,k = 3,a = [4,1,1,4,1,2],x_in_dec = 32768,x_in_base = 411412]
base = 7
[base = 7,len = 2,k = 2,a = [2,2],x_in_dec = 16,x_in_base = 22]
[base = 7,len = 6,k = 2,a = [5,2,3,2,5,1],x_in_dec = 90000,x_in_base = 523251]
"""
On my machine that took about 1min33s. On this specific test, the SAT solver is much slower (it took over 9 minutes), but it might be faster than the CP solver for larger instances.
I also did some other tests and found some more examples:
[base = 3,len = 12,k = 6,a = [1,1,1,0,2,2,1,2,1,0,0,1],x_in_dec = 262144,x_in_base = 111022121001]
[base = 3,len = 13,k = 5,a = [1,2,2,2,0,2,1,1,0,1,0,1,1],x_in_dec = 1048576,x_in_base = 1222021101011]
[base = 3,len = 14,k = 3,a = [1,0,2,2,1,1,1,2,2,0,2,0,2,2],x_in_dec = 2097152,x_in_base = 10221112202022]
[base = 3,len = 16,k = 5,a = [2,1,0,0,0,1,0,2,0,2,0,0,0,2,0,2],x_in_dec = 33554432,x_in_base = 2100010202000202]
[base = 5,len = 18,k = 2,a = [2,1,2,3,2,3,4,2,1,4,4,1,3,2,4,2,3,1],x_in_dec = 1761205026816,x_in_base = 212323421441324231]
Note that this CP model cannot handle numbers larger than 2**56 (base 10) since that's the limit of the constraint solvers.
But!
This is a change to play a little with the new bv module, introduced in the new version 3.9 which supports much larger domain variables.
There are some caveats, however:
Unfortunately, right now I don't know how to do the product_except_0 constraint for bv variables, so it will only give solutions without any 0s which is - of course - quite restricted.
It's also slower than the cp solver.
But it can give solutions with very large values, so it might worth to explore it.
"""
import sat.
import bv.
import bv_utils. %
https://hakank.org/picat/bv_utils.pi.
main => go.
go ?=>
nolog,
member(Base,3..10),
println(base=Base),
member(Len,2..20), % number of digits
member(K,2..15),
power_k_bv(Base,K,Len, A,X),
println([base=Base,len=Len,k=K,a=A.bti,x_in_dec=X.bti,x_in_base=X.bti.to_radix_string(Base)]),
fail,
nl.
go => true.
power_k_bv(Base,K,Len, A,X) =>
% A = make_bv_array(Len, 0, Base-1),
A = make_bv_array(Len, 1, Base-1), % Skipping 0s
% No leading 0
bv_gt(A[1],0),
% convert to a number X (in base Base)
to_num_bv(A,Base,X),
% Each digit ** K
bv_eq(bv_prod([bv_pow(A[I],K) : I in 1..Len]),X), % Note: no 0 correction
% product except 0 using cond/3 does not work
% bv_eq(bv_prod([cond(T.bv_eq(0),1,T) : I in 1..Len, bv_pow(A[I],K).bv_eq(T) ]),X),
solve(A ++ X).
"""
For the same setup as the cp model above, it gives these solutions (no numbers with 0s). This took about 2min47.3s (vs 1.33s using plain cp model above).
"""
base = 3
[base = 3,len = 3,k = 4,a = [1,2,1],x_in_dec = 16,x_in_base = 121]
base = 4
base = 5
[base = 5,len = 3,k = 4,a = [3,1,1],x_in_dec = 81,x_in_base = 311]
[base = 5,len = 3,k = 5,a = [1,1,2],x_in_dec = 32,x_in_base = 112]
base = 6
[base = 6,len = 2,k = 2,a = [1,3],x_in_dec = 9,x_in_base = 13]
[base = 6,len = 2,k = 3,a = [1,2],x_in_dec = 8,x_in_base = 12]
[base = 6,len = 4,k = 3,a = [2,2,1,2],x_in_dec = 512,x_in_base = 2212]
[base = 6,len = 6,k = 3,a = [4,1,1,4,1,2],x_in_dec = 32768,x_in_base = 411412]
base = 7
[base = 7,len = 2,k = 2,a = [2,2],x_in_dec = 16,x_in_base = 22]
[base = 7,len = 6,k = 2,a = [5,2,3,2,5,1],x_in_dec = 90000,x_in_base = 523251]
"""
Finding this takes about 25s:
[base = 5,len = 18,k = 2,a = [2,1,2,3,2,3,4,2,1,4,4,1,3,2,4,2,3,1],x_in_dec = 1761205026816,x_in_base = 212323421441324231]
/Hakan