Euclidean rhythm: Difference between revisions

Added Algol 68
(Added Sidef)
(Added Algol 68)
Line 17:
* 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.
 
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # Euclidean Rhythm #
 
# returns the Euclidean rhythm string generated by m and n #
# 0 < m < n must be true #
PROC e rhythm = ( INT m, n )STRING:
BEGIN
[ 1 : n ]STRING r;
# the rhythm string is n elements, initially the first m are #
# set to "1" and the remainder set to "0" #
# the first part is defined by a start and a end and the #
# second part by b start and b end #
FOR i TO m DO r[ i ] := "1" OD;
FOR i FROM m + 1 TO n DO r[ i ] := "0" OD;
INT a start := 1, a end := m;
INT b start := m + 1, b end := n;
# the b elements are appended to the a elements and removed #
# until either a or b or both have less than two elements left #
WHILE a end > a start # at least two elements in a #
AND b end > b start # at least two elements in b #
DO
INT a pos := a start, b pos := b start;
WHILE a pos <= a end AND b pos <= b end DO
r[ a pos ] +:= r[ b pos ];
a pos +:= 1;
b pos +:= 1
OD;
IF b pos < b end THEN
# 2 or more unused elements left in b #
b start := b pos
ELSE
# nothing left in b - use the remains of a #
b start := a pos;
b end := a end;
a end := a pos - 1
FI
OD;
STRING result := "";
FOR i FROM a start TO a end DO result +:= r[ i ] OD;
FOR i FROM b start TO b end DO result +:= r[ i ] OD;
result
END # e rhythm # ;
 
print( ( e rhythm( 5, 13 ), newline ) ); # task test case #
print( ( e rhythm( 3, 8 ), newline ) ) # JaveScriipt test case #
 
END
</syntaxhighlight>
{{out}}
<pre>
1001010010100
10010010
</pre>
 
=={{header|C#}}==
Line 86 ⟶ 141:
 
</pre>
 
 
 
=={{header|Dart}}==
3,048

edits