# Is there an Iterative solution to a variable length problem?

Posted 10 years ago
4758 Views
|
2 Replies
|
5 Total Likes
|
 I am looking for an efficient way to evaluate a set of variable length variables, if I can explain. Lets say I have 2 lists b = {2,3,7,11,...i}; c = {3,2,2,4,...n}. both lists will always be the same length, but will vary in length and have different numbers. I now wish to calculate in this example, x = 2^(0 to 3) x 3^(0 to 2) x 7^(0 to 2) x 11^(0 to 4) x ... i^(0 to n). So in the case of this example if we just had the 4 numbers as shown we would generate 180 individual values for c. At the moment I have separate 'modules' dependent on the length of the list. I am wondering if Mathematica can handle this without the need to re-direct.   this is part of my program that deals with the length of the lists.If[la==1,list=Reap[Do[x=b[[1]]^p b[[2]]^q;Sow[x],{p,0,c[[1]]},{q,0,c[[2]]}]];list=Union[Select[Flatten[list[[2]]],#<=2m&]];list=list+4m;Print[Length[list]];Print[Column[f[list]]];Abort[]];If[la==2,list=Reap[Do[x=b[[1]]^p b[[2]]^q b[[3]]^r;Sow[x],{p,0,c[[1]]},{q,0,c[[2]]},{r,0,c[[3]]}]];list=Union[Select[Flatten[list[[2]]],#<=2m&]];list=list+4m;Print[Length[list]];Print[Column[f[list]]];Abort[]];If[la==3,list=Reap[Do[x=b[[1]]^p b[[2]]^q b[[3]]^r b[[4]]^s;Sow[x],{p,0,c[[1]]},{q,0,c[[2]]},{r,0,c[[3]]},{s,0,c[[4]]}]];list=Union[Select[Flatten[list[[2]]],#<=2m&]];list=list+4m;Print[Length[list]];Print[Column[f[list]]];Abort[]]Thanking you in advancePaul.
2 Replies
Sort By:
Posted 10 years ago
 What an elegant solution Sander, thank you very much, it is also very quick.Paul.
Posted 10 years ago
 Very easy using Array:b = {2, 3, 7, 11}; (*base*)lengths = {3, 2, 2, 4} (*dimensions*)cbegin = ConstantArray[0, Length[lengths]];Array[Times @@ (b^{##}) &, lengths+1, cbegin]giving back:Out[] = {{{{1,11,121,1331,14641},{7,77,847,9317,102487},{49,539,5929,65219,717409}},{{3,33,363,3993,43923},{21,231,2541,27951,307461},{147,1617,17787,195657,2152227}},{{9,99,1089,11979,131769},{63,693,7623,83853,922383},{441,4851,53361,586971,6456681}}},{{{2,22,242,2662,29282},{14,154,1694,18634,204974},{98,1078,11858,130438,1434818}},{{6,66,726,7986,87846},{42,462,5082,55902,614922},{294,3234,35574,391314,4304454}},{{18,198,2178,23958,263538},{126,1386,15246,167706,1844766},{882,9702,106722,1173942,12913362}}},{{{4,44,484,5324,58564},{28,308,3388,37268,409948},{196,2156,23716,260876,2869636}},{{12,132,1452,15972,175692},{84,924,10164,111804,1229844},{588,6468,71148,782628,8608908}},{{36,396,4356,47916,527076},{252,2772,30492,335412,3689532},{1764,19404,213444,2347884,25826724}}},{{{8,88,968,10648,117128},{56,616,6776,74536,819896},{392,4312,47432,521752,5739272}},{{24,264,2904,31944,351384},{168,1848,20328,223608,2459688},{1176,12936,142296,1565256,17217816}},{{72,792,8712,95832,1054152},{504,5544,60984,670824,7379064},{3528,38808,426888,4695768,51653448}}}}output has dimensions 4*3*3*5.