blob: c2792a390e2a3e32d19a556a7ca54065420a1fb9 (
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
use v6;
################################################################################
=begin comment
Perl Weekly Challenge 014
=========================
Challenge #1
------------
Write a script to generate Van Eck's sequence starts with 0. For more
information, please check out wikipedia
[ https://en.wikipedia.org/wiki/Van_Eck%27s_sequence |page]. This challenge was
proposed by team member *Andrezgz*.
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2019 PerlMonk Athanasius #
#--------------------------------------#
my Int constant $LENGTH = 27;
sub MAIN(Int:D $length = $LENGTH)
{
say "\nThe first $length terms in Van Eck's sequence are:\n",
van-eck($length).join(', ');
}
sub van-eck(Int:D $length --> Array)
{
my @seq = (Nil, 0);
my %indices;
for 1 .. $length - 1 -> Int $n
{
my $old_term = @seq[$n];
push @seq, %indices{$old_term}:exists ?? $n - %indices{$old_term} !! 0;
%indices{$old_term} = $n;
}
shift @seq;
return @seq;
}
################################################################################
|