Introduction

You can find the definition of van Eck sequence in https://en.m.wikipedia.org/wiki/Van_Eck%27s_sequence. This sequence is not used for strictly research and 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.

Explanation with Example

Van Eck sequence can be explained with an example as given below.

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

0

So you have not seen 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 we have 0, which we have seen before 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 we 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 we have 0 and we 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

Now at fifth position we have 2 which we have not seen before, so next number is 0.

Therefore the van Eck sequence is

0 0 1 0 2 0

And the sequence goes on…

Implementation using Java

We have seen what is van Eck sequence and how it works with detail explanation in earlier sections.

Now we will see how we can implement van Eck sequence using Java programming language.

The source code is given below:

package com.roytuts.vanecksequence;
import java.util.HashMap;
import java.util.Map;
public class VanEckSequence {
	public static void main(String[] args) {
		System.out.println("Van Eck Sequence upto 100: " + vanEckSequence(100));
		System.out.println();
		System.out.println("Van Eck Sequence upto 1000: " + vanEckSequence(1000));
	}
	public static String vanEckSequence(int range) {
		int distance = 0;
		int nextNumber = 0;
		String sequence = "";
		Map<Integer, Integer> numberMap = new HashMap<>();
		for (int i = 0; i < range; i++) {
			if (numberMap.containsKey(nextNumber)) {
				distance = i - numberMap.get(nextNumber);
			} else {
				distance = 0;
			}
			numberMap.put(nextNumber, i);
			sequence += nextNumber + ", ";
			nextNumber = distance;
		}
		return sequence;
	}
}

Generating the Van Eck Sequence

Run the above Java class, you will see below output that prints up to 100 positions and up to 1000 positions respectively.

Van Eck Sequence upto 100: 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,
Van Eck Sequence upto 1000: 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, 23, 0, 3, 9, 21, 0, 4, 42, 56, 25, 0, 5, 21, 8, 18, 52, 0, 6, 18, 4, 13, 0, 5, 11, 62, 0, 4, 7, 40, 0, 4, 4, 1, 36, 0, 5, 13, 16, 0, 4, 8, 27, 0, 4, 4, 1, 13, 10, 0, 6, 32, 92, 0, 4, 9, 51, 0, 4, 4, 1, 14, 131, 0, 6, 14, 4, 7, 39, 0, 6, 6, 1, 12, 0, 5, 39, 8, 36, 44, 0, 6, 10, 34, 0, 4, 19, 97, 0, 4, 4, 1, 19, 6, 12, 21, 82, 0, 9, 43, 0, 3, 98, 0, 3, 3, 1, 15, 152, 0, 6, 17, 170, 0, 4, 24, 0, 3, 12, 24, 4, 6, 11, 98, 21, 29, 0, 10, 45, 0, 3, 13, 84, 0, 4, 14, 70, 0, 4, 4, 1, 34, 58, 0, 6, 23, 144, 0, 4, 9, 51, 94, 0, 5, 78, 0, 3, 26, 0, 3, 3, 1, 21, 38, 0, 6, 21, 4, 19, 76, 0, 6, 6, 1, 12, 56, 166, 0, 7, 111, 0, 3, 21, 16, 145, 0, 5, 33, 206, 0, 4, 23, 46, 194, 0, 5, 9, 47, 0, 4, 9, 4, 2, 223, 0, 6, 33, 19, 39, 132, 0, 6, 6, 1, 40, 185, 0, 6, 5, 23, 28, 0, 5, 4, 22, 0, 4, 3, 46, 36, 151, 0, 6, 15, 126, 0, 4, 10, 110, 0, 4, 4, 1, 29, 118, 0, 6, 14, 112, 0, 4, 9, 51, 102, 0, 5, 33, 50, 0, 4, 9, 9, 1, 20, 307, 0, 7, 88, 0, 3, 42, 262, 0, 4, 14, 27, 233, 0, 5, 23, 60, 0, 4, 9, 22, 60, 5, 8, 210, 0, 8, 3, 22, 8, 3, 3, 1, 34, 156, 0, 10, 63, 0, 3, 8, 11, 183, 0, 5, 22, 17, 199, 0, 5, 5, 1, 19, 109, 0, 6, 73, 0, 3, 19, 7, 58, 183, 20, 64, 0, 8, 26, 174, 0, 4, 52, 319, 0, 4, 4, 1, 25, 331, 0, 6, 25, 4, 7, 23, 69, 0, 7, 4, 6, 9, 71, 0, 6, 4, 6, 2, 158, 0, 6, 4, 6, 2, 6, 2, 2, 1, 30, 0, 10, 73, 54, 0, 4, 13, 247, 0, 4, 4, 1, 13, 6, 18, 367, 0, 8, 59, 0, 3, 70, 257, 0, 4, 14, 123, 0, 4, 4, 1, 19, 80, 0, 6, 21, 225, 0, 4, 9, 57, 0, 4, 4, 1, 14, 20, 91, 0, 7, 70, 29, 180, 0, 5, 112, 179, 0, 4, 15, 198, 0, 4, 4, 1, 20, 19, 35, 0, 7, 20, 5, 17, 134, 0, 6, 41, 0, 3, 58, 126, 218, 0, 5, 12, 282, 0, 4, 24, 341, 0, 4, 4, 1, 29, 43, 368, 0, 7, 29, 5, 17, 29, 3, 25, 131, 415, 0, 10, 107, 0, 3, 8, 95, 0, 4, 23, 140, 0, 4, 4, 1, 28, 274, 0, 6, 50, 241, 0, 4, 9, 91, 84, 372, 0, 6, 10, 28, 15, 79, 0, 6, 6, 1, 22, 207, 0, 6, 5, 48, 0, 4, 22, 8, 41, 78, 373, 0, 7, 60, 246, 0, 4, 11, 230, 0, 4, 4, 1, 25, 65, 0, 6, 25, 4, 7, 17, 75, 0, 7, 4, 6, 9, 52, 220, 0, 7, 7, 1, 20, 119, 0, 6, 11, 30, 195, 0, 5, 49, 0, 3, 89, 0, 3, 3, 1, 17, 30, 13, 196, 0, 8, 58, 133, 0, 4, 35, 149, 0, 4, 4, 1, 16, 411, 0, 6, 33, 342, 0, 4, 9, 48, 82, 508, 0, 6, 10, 100, 0, 4, 10, 4, 2, 245, 0, 6, 10, 6, 2, 6, 2, 2, 1, 31, 650, 0, 11, 63, 328, 0, 4, 19, 195, 67, 0, 5, 68, 0, 3, 64, 313, 0, 4, 12, 189, 0, 4, 4, 1, 26, 320, 0, 6, 33, 57, 244, 0, 5, 22, 136, 0, 4, 14, 247, 286, 0, 5, 9, 67, 35, 83, 0, 6, 20, 114, 0, 4, 15, 169, 0, 4, 4, 1, 34, 388, 0, 6, 14, 25, 145, 507, 0, 6, 6, 1, 12, 52, 143, 0, 7, 142, 0, 3, 64, 64, 1, 11, 80, 305, 0, 8, 129, 0, 3, 11, 8, 5, 50, 221, 0, 7, 21, 316, 0, 4, 43, 260, 0, 4, 4, 1, 25, 43, 7, 13, 156, 439, 0, 10, 122, 0, 3, 28, 235, 0, 4, 16, 154, 0, 4, 4, 1, 21, 31, 130, 0, 7, 23, 271, 0, 4, 10, 23, 5, 47, 569, 0, 7, 11, 54, 396, 0, 5, 9, 106, 0, 4, 16, 31, 25, 48, 179, 355, 0, 8, 69, 438, 0, 4, 12, 94, 641, 0, 5, 21, 42, 527, 0, 5, 5, 1, 49, 233, 528, 0, 7, 38, 645, 0, 4, 21, 16, 34, 129, 102, 563, 0, 8, 33, 165, 0, 4, 12, 33, 5, 25, 46, 600, 0, 8, 12, 8, 2, 211, 0, 6, 142, 136, 178, 0, 5, 16, 30, 265, 0, 5, 5, 1, 47, 84, 347, 0, 7, 47, 5, 8, 24, 398, 0, 7, 7, 1, 14, 175, 0, 6, 30, 24, 11, 100, 262, 601, 0, 8, 18, 490, 0, 4, 56, 705, 0, 4, 4, 1, 22, 226, 0, 6, 22, 4, 7, 30, 25, 67, 225, 488, 0, 10, 136, 

Source Code

Thanks for reading.

Tags:

Leave a Reply

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