Finding all possible multiplier of a number

dynamo
#1

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<x)
{
while(dd<x)
{
y = (d+1)*(dd+1);
if (y == x)
{
z[d] = (d+1) + " x " + (dd+1);
}
dd=dd+1;
}
d=d+1;
}
}
out = z;
0 Likes

#2

@stillgotme Easily done in Python :grinning:

image

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

#3

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.

0 Likes

#4

@stillgotme you can have a look at https://ironpython-test.readthedocs.io/en/latest/library/itertools.html &
https://stackoverflow.com/

0 Likes

#5

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 :wink: 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;
};
2 Likes

#6

hey there @mellouze, tried your code block and i have this error.
image
would you mind enlighten me in this issue? thanks

0 Likes

#7

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

1 Like

#8

@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;
};
0 Likes

#9

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

5 Likes