I have a feeling a need to use a variant of iterate however I’m
struggling to get the exact syntax. I have
gcd:{[x;y] $[0~y;:x;:gcd[y;x mod y]]; };
lcm:{[x;y] x*y%gcd[x;y]};
x: 1+ til 10
And I would like to fold lcm over x. Step by step
lcm [10;9] = 90
lcm [90;8] = 360
etc
To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1084)
> gcd:{[x;y] $[0~y;:x;:gcd[y;x mod y]]; };
> lcm:{[x;y] x*y%gcd[x;y]};
> x: 1+ til 10
>
> And I would like to fold lcm over x. Step by step
> lcm [10;9] = 90
> lcm [90;8] = 360
> etc
gcd:{$[y;gcd[y;x mod y];x]} / simplify
lcm:{x*y div gcd[x;y]} / fix 'type
x:1+til 10
over’s tricky sometimes
here’s how to work out the solution:
q)lcm[10;9]
90
q)lcm[lcm[10;9];8]
360
q)lcm[;8]lcm[10;9]
360
q)lcm[;8]lcm[;9]10
360
q)lcm over 10 9 8
360
q)lcm over reverse x
2520
(note that “reverse” isn’t actually useful in this particular case, =
since lcm is commutative, but that’s how to get that particular order)=