-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thue-Morse Sequence
43 lines (32 loc) · 912 Bytes
/
Thue-Morse Sequence
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
https://www.codewars.com/kata/591aa1752afcb02fa300002a
Given a positive integer n, return first n dgits of Thue-Morse sequence, as a string (see examples).
Thue-Morse sequence is a binary sequence with 0 as the first element. The rest of the sequece is obtained by adding the Boolean (binary) complement of a group obtained so far.
For example:
0
01
0110
01101001
and so on...
alt
Ex.:
thueMorse(1); //'0'
thueMorse(2); //'01'
thueMorse(5); //'01101'
thueMorse(10): //'0110100110'
You don't need to test if n is valid - it will always be a positive integer.
n will be between 1 and 10000
*/
using System.Linq;
public class Kata
{
public static string ThueMorse(int n)
{
string thueMorse = "0";
while (thueMorse.Length < n)
{
thueMorse += string.Join("", thueMorse.ToCharArray().Select(ch => ch == '0'? '1':'0').ToArray());
}
return thueMorse.Substring(0, n);
}
}