summaryrefslogtreecommitdiff
path: root/014
diff options
context:
space:
mode:
Diffstat (limited to '014')
-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";