Multiplication factors fo a number

lets say i have a number 12 …the two numbers that multiply to form 12 is 62 or 26 ,43,121 …so how do i get these numbers usign dynamo for any number input


Python:

from math import sqrt
def factors(n):
	step = 2 if n%2 else 1
	return set(reduce(list.__add__,([i, n//i] for i in range(1, int(sqrt(n))+1, step) if n % i == 0)))

outlist = []
for i in IN[0]:
	outlist.append(factors(i))

OUT = outlist

Credit goes to this stackoverflow question, I just took their python script and put it into Dynamo.

3 Likes


This was a rather fun challenge.

3 Likes

thanks, i want to limit my input by using only numbers between 8 to 15…i cand do that by modifiying the second code block?? of by filtering the results??

** the final output numbers should only be beween 8 and 15

used a range node instead of the second code block …


You can filter after the results using an if node or codeblock with this code:

x<=15 && x>=8?true:false;

If you use @PauLtus’s method, you would need to flatten first. If the number has a square root, I think his method might have duplicates, which could be good or bad depending on what you want.

Can also work with an input as the limits, something like this:

x<=max && x>=min?true:false;

1 Like

If you wanted to use my method and get both the numbers of the multiplication this becomes rather complicated, it’s also dependent on whether you want both the numbers to be inbetween 8 or 15 or one of them, I added a boolean to choose between those options:



I did use the Clockwork package for the List.AnyTrue/False nodes.

1 Like

it works now but i get a null answer…i have to increase the input value until i get a pair of numbers. is there a way to automatically increase the numbers by certain increment if it returns a null output until i get an answer

I guess it doesn’t really work then :stuck_out_tongue: .

Checking the result and then rerunning the code or not based on that is pretty difficult in my experience. You can just run a whole bunch of numbers throught and then filter out the nulls (for which the Clockwork package as well), that’s probably what I’d do, and that’s probably not the most efficient.

Maybe @kennyb6 has a better answer, he seems to understand Python quite well

1 Like

Do you mean increasing the output range (8-15) until you get an answer? I can probably figure that out with a while loop and a counter but I am actually about to leave the office (6pm here) so it will have to wait until tomorrow afternoon.

Edit: after reading your comment again, you mean increasing the input number (the number you are finding factors of) until it has one or two factors within the range? That should still be possible, though I am curious on the efficiency. I’ll try tomorrow.

i meant increasing the orignal number (100) by increments of 10 untill a get answers.

Yeah that is possible. How many factors are you looking for within the range? A pair of factors both within the range or just a single factor?

i want just a single pair…the smallest numbers so for 100 it would be 10 x 10

so i i give 520 instead of 100 and these are no factors between 8 and 15 then it will increase 525+10 untill i get a pair

Can I ask what you’re planning to use this for in the end? There might be an easier method than the method you’re asking for.

So not the smallest pair, just the smallest pair within the range you want? And if none exists, increase the input number? I think its possible. But I am heading out now.

First i would input an area of a rectangle lets say 100, now i want to create four (numbers may vary) smaller rectangle inside the bigger rectangle…so i assign random percentages for the four rectangle based on 100( so rectangle r1 = 1000.35,r2=1000.20,r3=0.4,r4=0.15) so lets take r1=1000.35=35 now my input is 35 (this is where this graph starts) now i want to create a rectangle with area of 35 but using only numbers between 8 and 15 …if there is no number between 8 and 15 that makes 35 then r1 becomes r1=1000.40 (adding 0.5 with r1) now it repeats untill i get a peari of numbers between 8 and 15 that makes the area 100*0.40=40

r1=100x0,35 …etc

image
I made a node out of my previous code; would this be the result you’re after?

1 Like