calc GCD (greatest common divisor) and LCM (least common multiple) inside gen~

yaniki's icon

Dear Friends

Can I calc - as mentioned in the topic's title - GCD (greatest common divisor) and LCM (least common multiple) of two values inside gen~?

thanks in advance for any indications

Graham Wakefield's icon

You'll need to use a codebox, as these require an iterative procedure, i.e. a for loop, and then implement one of the typical algorithms. Once you have GCD, LCM is easy:

Max Patch
Copy patch and select New From Clipboard in Max.

yaniki's icon

That's really elegant. Thank you so much, Graham.

AudioMatt's icon

hemiola.zip
application/zip 15.12 KB
Hemiola.zip

FWIW, 4 yrs later, I did this with these. Given how integral this is, I'm very curious why there aren't GCD and LCM objects in max.

hemiola.zip

Roman Thilenius's icon

GCD = % until rest is 0, then it is the last div?

AudioMatt's icon

R, That would be the implication of Graham's gen patch that I used.