Euclidean rhythm: Difference between revisions

javascript
imported>CosmiaNebula
(creating page)
 
imported>CosmiaNebula
(javascript)
Line 16:
** 10010 10010 100 |
* At this point, we can continue the algorithm until only one form remains, but notice that one of the groups has only one element [100], so the algorithm can actually terminate right here and output the concatenation 1001010010100.
 
== Javascript ==
Copied from [https://mrmr.io/mj/euclid here] and [https://github.com/mnvr/gm1k?tab=readme-ov-file here], under MIT license.<syntaxhighlight lang="javascript">
const E = (k, n) => {
let s = Array(n).fill(0)
.map((_, i) => (i < k ? [1] : [0]))
 
let d = n - k
n = Math.max(k, d)
k = Math.min(k, d)
let z = d
 
while (z > 0 || k > 1) {
for (let i = 0; i < k; i++)
s[i].push(...s[s.length - 1 - i])
s.splice(-k)
z = z - k
d = n - k
n = Math.max(k, d)
k = Math.min(k, d)
}
 
return s.flat()
}
 
</syntaxhighlight>Output:<syntaxhighlight lang="text">
> E(3,8)
[1, 0, 0, 1, 0, 0, 1, 0]
 
</syntaxhighlight>
Anonymous user