summaryrefslogtreecommitdiff
path: root/014/ch1.pl
diff options
context:
space:
mode:
authorGuillermo Ramos2019-06-29 21:15:34 +0200
committerGuillermo Ramos2019-06-29 21:18:32 +0200
commitc47078d3ec4bb3111d8c91e9d972fe9cfe32efa9 (patch)
tree5d181c68fb8453aee7fbbb950d76dc805c0af11c /014/ch1.pl
parent2d8184306c563fddc0dbdd8692d5bb438530020a (diff)
downloadperlweekly-c47078d3ec4bb3111d8c91e9d972fe9cfe32efa9.tar.gz
[013#4]
Diffstat (limited to '014/ch1.pl')
-rwxr-xr-x014/ch1.pl27
1 files changed, 27 insertions, 0 deletions
diff --git a/014/ch1.pl b/014/ch1.pl
new file mode 100755
index 0000000..60db7e2
--- /dev/null
+++ b/014/ch1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+#
+# Write a script to generate Van Eck’s sequence starts with 0
+# (https://en.wikipedia.org/wiki/Van_Eck%27s_sequence)
+################################################################################
+
+use strict;
+use warnings;
+
+my $iterations = shift or die "Usage: $0 <iterations>\n";
+
+# Computed sequence
+my @vaneck = (0);
+
+# Map numbers to the last time they appear in sequence
+my %lastpos = (0 => 0);
+
+# For every n, compute (push) n+1
+foreach my $n (0 .. $iterations-2) {
+ my $curn = $vaneck[$n];
+ my $m = $lastpos{$curn};
+ my $nextn = defined $m ? $n-$m : 0;
+ push @vaneck, $nextn;
+ $lastpos{$curn} = $n;
+}
+
+print "@vaneck\n";