Centenary Sequences with Raku
Part 6

Binary and Palindrome

by Arne Sommer

Centenary Sequences with Raku - Part 6: Binary and Palindrome

[100.6] Published 4. November 2020.

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

Binary Derived Sequences

These sequences are formed of normal base-10 numbers, but the rule relies on the binary representation.

072. Number of 1s
The number of 1s 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)

073. Number of 0s
We can count the number of 0s 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)

074. Number of changes 0 ⇌ 1
The number of times the digits in the binary representation of the number changes from 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)

075. Odious Numbers

An Odious Number is a positive integer that has an odd number of 1s 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)

076. Evil Numbers

An Evil Number is a non-negative integer that has an even number of 1s 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)

077. Thue–Morse sequence

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)

Palindromic Numbers

A Palindrome is a text or a number that is the same if we reverse it. E.g. «madam» and «11911».

078. Palindromic Primes

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)

079. Palindromic Binary Sequence

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)

080. Palindromic Binary Primes

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)

The Next Part

Part 7: Every Tom, Dick, and Harry.