Van Eck Sequence Using Python

Introduction

In this example I am going to generate Van Eck sequence using Python programming language. Van Eck sequence is not used strictly for research purpose and it is actually application based professional activity.

Van Eck sequence can be illustrated as follows:

  • You start with a number 0 at the first position. Not necessarily you have to start with a 0, but you can use any integer number to start with.
  • If you have seen the number at current position before, the next number is the distance between current position and the position where you saw this number before.
  • If you haven’t seen this number before, print 0.

Related Posts:

Van Eck Sequence Example

So, you start with 0. You haven’t seen 0 before, so next number is also 0. But, now the next number is 1, because you saw 0 at position 1 and position 2. You haven’t seen 1 before, so next number is 0. You saw 0 at position 2 and position 4, so the next number is 2. And so on…

0, 0, 1, 0, 2,...

Now let’s examine how the sequence numbers are calculated.

Say, you start with a number 0. The van Eck sequence is:

0

So you have not seen the number 0 before. So the distance is (1-1)=0 at position one.

Therefore the next number at second position in the sequence is 0. The van Eck sequence is given below:

0 0

Now at second position, the number is 0, which you have seen earlier at position one. So distance is (2-1)=1.

Therefore the next number at third position in the sequence is 1. The van Eck sequence is as follows:

0 0 1

Now at third position, the number is 1, which you have not seen before, so according to the rule, the next number will be 0.

Therefore the van Eck sequence will be:

0 0 1 0

Again at fourth position, the number is 0 and you have last seen this number at position 2. So the distance is (4-2)=2.

Therefore the van Eck sequence is:

0 0 1 0 2

Van Eck Sequence using Python

Now let’s see how to implement van Eck sequence using Python programming language.

num_dict = {}
next_num = 0
sequence = ''

for i in range(100):
    if next_num in num_dict:
        distance = i - num_dict[next_num]
    else:
        distance = 0

    num_dict[next_num] = i
    sequence += "%d, " % next_num
    next_num = distance

print(sequence)

Testing Van Eck Sequence

Let’s run the above Python program to generate the Van Eck sequence. For the first 100 numbers, the sequence is generated as:

0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, 2, 9, 0, 4, 9, 3, 6, 14, 0, 6, 3, 5, 15, 0, 5, 3, 5, 2, 17, 0, 6, 11, 0, 3, 8, 0, 3, 3, 1, 42, 0, 5, 15, 20, 0, 4, 32, 0, 3, 11, 18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3, 6, 3, 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0, 5, 37, 0, 3, 8, 8, 1, 46, 0, 6,

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *