See also: The Introduction | Part 1: Raku Part 2: Arithmetic and Geometric Sequences | Part 3: Not True, or Not | Part 4: Primes and Fibonacci | Part 5: Divisors and Factors
1
s in the binary representation of the number.
We can start at 0:
(^Inf).map(*.base(2).comb.sum) # 072
say (^Inf).map(*.base(2).comb.sum)[^30];
(0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 2 3 3 4 3 4)
0
s as well:
(^Inf).map(*.base(2).comb.grep( * eq '0').elems) # 073
> say (^Inf).map(*.base(2).comb.grep( * eq '0').elems)[^30];
(1 0 1 0 2 1 1 0 3 2 2 1 2 1 1 0 4 3 3 2 3 2 2 1 3 2 2 1 2 1)
0
to 1
and vice versa.
E.g. the number 47 has 2 changes.
> say 47.base(2): # -> 101111 -> '1' (change 1) '0' (change 2) '1111'
We can use a Regexp to split the string on character changes. The number of changes is 1 less than the number of matches:
(^Inf).map( { ($_.base(2) ~~ m:g/((.)$0*)/).elems -1 }) # 074
> say (^Inf).map( { ($_.base(2) ~~ m:g/((.)$0*)/).elems -1 })[^30]
(0 0 1 0 1 2 1 0 1 2 3 2 1 2 1 0 1 2 3 2 3 4 3 2 1 2 3 2 1 2)
An Odious Number
is a positive integer that has an odd number of 1
s in its binary
representation:
(1..Inf) # 075
.map(*.base(2).comb.sum)
.map({ state $index = 0; $index++; $^a %% 2 ?? Any !! "$index" })
.grep( *.defined )
> say (1..Inf)
.map(*.base(2).comb.sum)
.map({ state $index = 0; $index++; $^a %% 2 ?? Any !! "$index" })
.grep( *.defined )[^25];
(1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35 37 38 41 42 44 47 49)
An Evil Number is a
non-negative integer that has an even number of 1
s in its binary
representation. This is the oppostive of the Odious Numbers, with zero added on.
(0..Inf) # 076
.map(*.base(2).comb.sum)
.map({ state $index = -1; $index++; $^a %% 2 ?? "$index" !! Any })
.grep( *.defined )
> say (0..Inf)
.map(*.base(2).comb.sum)
.map({ state $index = -1; $index++; $^a %% 2 ?? "$index" !! Any })
.grep( *.defined )[^25];
(0 3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48)
The Thue–Morse Sequence
is the binary sequence beginning with 0
(index 0). After that the number is
1
if the number of ones in the binary expansion of the index is odd, and 0
if not.
(0..Inf).map({ *.base(2).comb.sum %% 2 }) # 077
> say (0..Inf).map( +(*.base(2).comb.sum !%% 2) )[^30];
(0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0)
A Palindromic Prime is a Prime Number that is also a Palindrome (in the decimal numbering system):
(1..Inf).grep( *.is-prime).grep({ $_ eq $_.flip }) # 078
> say (1..Inf).grep( *.is-prime).grep({ $_ eq $_.flip })[^20];
(2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929)
A Palindromic Binary Sequence is the numbers (in the decimal numbering system), where the binary representation is a palindrome:
(1..Inf).grep( { my $b = $_.base(2); $b eq $b.flip }) # 079
> say (1..Inf).grep( { my $b = $_.base(2); $b eq $b.flip })[^20];
(1 3 5 7 9 15 17 21 27 31 33 45 51 63 65 73 85 93 99 107)
The Palindromic Binary Primes is the prime numbers, where the binary representation is a palindrome:
(1..Inf).grep( *.is-prime).grep( { my $b = $_.base(2); $b eq $b.flip }) # 080
> say (1..Inf)
.grep( *.is-prime).grep( { my $b = $_.base(2); $b eq $b.flip })[^15];
(3 5 7 17 31 73 107 127 257 313 443 1193 1453 1571 1619)