Prime factorization algorithm in gen?
Hi there! I was wondering if anybody knows of an efficient way to calculate prime factors for a given number in gen? Any resources would be helpful. Thank you!
This is one way of approaching it. It could be optimized by providing a list of prime numbers. Gen can't produce lists therefor in this example values are written into a buffer.
thanks so much. would you be able to explain a little bit of what is going on this patch? i see that it is reading from a buffer, what does i=0 mean exactly here? and i can't seem to find a good resource on how "while" works
Typically the function you're after would produce an array of values. Since gen can only output float values however, and only one each time it is triggered, a buffer is used to store the result into. The gen code outputs the count value of found primes instead and the variable (i) in the code represents this count. The while loop continues dividing the input (v) by a value (p) that increases by 1, until (p) is greater than the input. When a division by (p) results in a whole number it is stored in the buffer.
If the value 20 is entered starting values are: v=20, p=2, i=0.
First while loop (v>p?) -> v/p=10: v becomes 10, p remains 2, store p on index 0, i becomes 1;
Second while loop -> v/p=5: v=5, p=2, store on index 1, i=2;
Third while loop -> v/p = 2.5, division not whole number: v 5, p=3, i=2
Fourth while loop -> v/p = 1.667: v=5, p=4, i=2
Fifth while loop -> v/p = 1.25: v=5, p=5, i=2;
Sixth while loop -> v/p = 1: v=1, p=5, store on index2, i=3;
v is not any longer greater than p, while loop stops, and (i) is send to out1. This value is used to read values from the buffer object.
More info about while loop: https://en.wikipedia.org/wiki/While_loop
Study this part of the reference: https://docs.cycling74.com/max7/vignettes/gen_genexpr
thanks again! you've been a tremendous help