aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Rogerson <steve.git@yewtc.demon.co.uk>2019-04-12 15:43:21 +0100
committerSteve Rogerson <steve.git@yewtc.demon.co.uk>2019-04-12 15:43:21 +0100
commit8acda7a92b5bc1288eafe4ef1a027c7a765b5b99 (patch)
treef09cd108d5d708fa22378ec8a5cfc4b7123e1a05
parent387c67e9aee8e4404ea00513e32dc4e57706e9f0 (diff)
downloadperlweeklychallenge-club-8acda7a92b5bc1288eafe4ef1a027c7a765b5b99.tar.gz
perlweeklychallenge-club-8acda7a92b5bc1288eafe4ef1a027c7a765b5b99.tar.bz2
perlweeklychallenge-club-8acda7a92b5bc1288eafe4ef1a027c7a765b5b99.zip
Add my solution to week3 problem 1 perl5
-rwxr-xr-xchallenge-003/steve-rogerson/ch-1.pl29
1 files changed, 29 insertions, 0 deletions
diff --git a/challenge-003/steve-rogerson/ch-1.pl b/challenge-003/steve-rogerson/ch-1.pl
new file mode 100755
index 0000000000..e1378eaec3
--- /dev/null
+++ b/challenge-003/steve-rogerson/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::Util 'min';
+use Const::Fast;
+use 5.010;
+
+
+sub hamming {
+ my ($arg) = @_;
+ $arg ||= 0; # pass 1 to restart.
+ state %s;
+ if (! %s or $arg ==1 ) {
+ %s = (1=>1); # 1 is the first hamming number.
+ }
+ my $next = min (keys %s);
+ delete $s{$next};
+ for (2,3,5) {
+ $s{$next * $_} = 1;
+ }
+ return $next;
+}
+
+my $i =0;
+++$i, print hamming(), " " until $i > 20;
+print "...\n";
+
+++$i, hamming() until $i == 1690;
+print ++$i, "-th: ", hamming(), "\n";