aboutsummaryrefslogtreecommitdiff
path: root/challenge-014/walt-mankowski/perl5/ch-1.pl
blob: 54525d2c84ff5cdf7a841af2bf2e9226ac203b83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env perl

# Perl Weekly Challenge 014-1
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-014/

# Write a script to generate Van Eck's sequence starting with 0. For
# more information, please check out wikipedia page at
# https://en.wikipedia.org/wiki/Van_Eck%27s_sequence

use strict;
use warnings;
use feature 'say';

my $N = shift @ARGV || 10;
my @vals;
my $next = 0;

for my $n (0..$N) {
    push @vals, $next;

    # have we seen it before?
    $next = 0;
    for (my $i = $n-1; $i >= 0; $i--) {
        if ($vals[$i] == $vals[$n]) {
            $next = $n - $i;
            last;
        }
    }
}

say join ", ", @vals;