Kevin Ryde > Math-NumSeq-63 > Math::NumSeq::SternDiatomic

Math-NumSeq-63.tar.gz

Dependencies

Annotate this POD

Website

# CPAN RT

 Open 1
View/Report Bugs
Module Version: 63   Source   Latest Release: Math-NumSeq-67

# NAME

Math::NumSeq::SternDiatomic -- Stern's diatomic sequence

# SYNOPSIS

``` use Math::NumSeq::SternDiatomic;
my \$seq = Math::NumSeq::SternDiatomic->new;
my (\$i, \$value) = \$seq->next;```

# DESCRIPTION

This is Moritz Stern's diatomic sequence

```    0, 1, 1, 2, 1, 3, 2, 3, ...
starting i=0```

It's constructed by successive levels with a recurrence

```    D(0)     = 0
D(1)     = 1
D(2*i)   = D(i)
D(2*i+1) = D(i) + D(i+1)```

So the sequence is extended by copying the previous level to the even indices of the next, and at the odd indices the sum of adjacent terms,

```   0,                    i=0
1,                    i=1
1,      2,            i=2 to 3
1,  3,  2,  3,        i=4 to 7
1,4,3,5,2,5,3,4,      i=8 to 15```

For example the i=4 row is a copy of the preceding values 1,2 with sums 1+2 and 2+1 interleaved. The value at the end of each row is the sum of the last of the previous row and the first of the current row (which is always 1).

## Odd and Even

The sequence makes a repeating pattern even,odd,odd,

```    0, 1, 1, 2, 1, 3, 2, 3
E  O  O  E  O  O  E ...```

This can be seen from the copying in the recurrence above. For example the i=8 to 15 row copying to i=16 to 31,

```    O . E . O . O . E . O . O . E .      spread
O   O   E   O   O   E   O   O      sum adjacent```

Adding adjacent terms odd+even and even+odd are both odd and odd+odd gives even, so the pattern EOO in the original row spread and added gives EOO again in the next row.

# FUNCTIONS

See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.

`\$seq = Math::NumSeq::SternDiatomic->new ()`

Create and return a new sequence object.

## Random Access

`\$value = \$seq->ith(\$i)`

Return the `\$i`'th value of the sequence.

`\$bool = \$seq->pred(\$value)`

Return true if `\$value` occurs in the sequence, which means simply integer `\$value>=0`.

Math::NumSeq

http://user42.tuxfamily.org/math-numseq/index.html