# Finding all possible multiplier of a number

Hi all, i am trying a find all possible multiplier of a particular number. For example, a number 4 has 1x4 & 2x2, and a number 12 has 1x12, 2x6 & 3x4, and so on. So i basically came up with a code block for this but it only gives me the first multiplier. Any idea what is wrong with the codeblock?

``````x:int;
z = {};
y = {};
[Imperative]
{
d=0;
dd=0;
while (d&lt;x)
{
while(dd&lt;x)
{
y = (d+1)*(dd+1);
if (y == x)
{
z[d] = (d+1) + &quot; x &quot; + (dd+1);
}
dd=dd+1;
}
d=d+1;
}
}
out = z;``````

@stillgotme Easily done in Python

``````from itertools import combinations_with_replacement

n = IN[0]
product = 1
outlist=([x for x in combinations_with_replacement(range(1,n), 2) if reduce(lambda x, y: x*y, x)  == n])
outlist.append([1,n])
OUT=[]
for lst in outlist:
OUT.append(" x ".join(map(str,lst)))

OUT=sorted(OUT)``````
1 Like

wet… thanks @salvatoredragotta! Possible to enlighten me where can i get queries on these kind of libraries and what is the use of the library.

Hi,

Easy answer : the initialisation of the variable “dd” was two lines too high. You should have put it right after the beggining of the first loop. Try executing your code by hand to see where is the problem I made three versions : the first is the code as you intended, the second is the same code but without the null values, the third one is the same code, but avoiding some unecessary loops.

``````x;
z=[];
[Imperative]{
d=0;
i = 0;
while(d<=Math.Round(Math.Sqrt(x))){
dd=Math.Round(Math.Sqrt(x),0);
while(dd<=x){
if((d+1)*(dd+1)==x){
z[i] = [d+1,dd+1];
i = i+1;
}
dd = dd+1;
}
d=d+1;
}
return = z;
};``````
3 Likes

hey there @mellouze, tried your code block and i have this error.

would you mind enlighten me in this issue? thanks

I’m using Dynamo 2.0.1. I guess just switch `z=[];` by `z={};`

1 Like

@mellouze Yes figured that out.
below is code for dynamo 1.xx

Thank you all for the help

``````x;
z={};
[Imperative]{
d=0;
i=0;
while(d<=Math.Round(Math.Sqrt(x))){
dd=Math.Round(Math.Sqrt(x),0);
while(dd<=x){
if((d+1)*(dd+1)==x){
z[i]={d+1,dd+1};
i=i+1;
}
dd=dd+1;
}
d=d+1;
}
return = z;
};``````

Can also do this with nodes
multiples.dyn (18.7 KB)

5 Likes