From 4775f9d2416fc907d40c3cf14908dff3fe479631 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 12 Apr 2019 15:49:32 +0100 Subject: - Renamed solution script name. --- challenge-003/daniel-mantovani/perl5/ch-1.pl | 38 ++++++++++++++++++++++++++++ challenge-003/daniel-mantovani/perl5/ch1-pl | 38 ---------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) create mode 100644 challenge-003/daniel-mantovani/perl5/ch-1.pl delete mode 100644 challenge-003/daniel-mantovani/perl5/ch1-pl diff --git a/challenge-003/daniel-mantovani/perl5/ch-1.pl b/challenge-003/daniel-mantovani/perl5/ch-1.pl new file mode 100644 index 0000000000..fa23ce0075 --- /dev/null +++ b/challenge-003/daniel-mantovani/perl5/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +use strict; +use warnings; +use v5.10; + +my $q = $ARGV[0]; + +die + "Usage: perl $0 \n\nwhere is the (integer) quantity of hamming numbers you ask" + unless $q && $q =~ /^\d+$/; + +my @primes = (2, 3, 5); + +# for each prime, we will define one vector (array), whith just one element [1] +# note that [1] will be first hamming code inside each array! + +my %vec; +$vec{$_} = [1] for @primes; + +for my $i (1 .. $q) { + +# just look for the min value from the head of v2, v3 or v5 +# (without using List::Util, as the challenge encourages no to use external modules) + my $h; + map { $h = $vec{$_}[0] unless $h && $h < $vec{$_}[0] } @primes; + + # and we just got the next hamming number! + say "$i\t", $h; + + for my $f (@primes) { # now for every prime factor + # remove this value on each vector where it is present + shift @{$vec{$f}} if $vec{$f}[0] == $h; + + # and finally calculate and insert new values to each vector + # note that new pushed value is higer than any previous value in each vector + push @{$vec{$f}}, $h * $f; + } +} diff --git a/challenge-003/daniel-mantovani/perl5/ch1-pl b/challenge-003/daniel-mantovani/perl5/ch1-pl deleted file mode 100644 index fa23ce0075..0000000000 --- a/challenge-003/daniel-mantovani/perl5/ch1-pl +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/perl -use strict; -use warnings; -use v5.10; - -my $q = $ARGV[0]; - -die - "Usage: perl $0 \n\nwhere is the (integer) quantity of hamming numbers you ask" - unless $q && $q =~ /^\d+$/; - -my @primes = (2, 3, 5); - -# for each prime, we will define one vector (array), whith just one element [1] -# note that [1] will be first hamming code inside each array! - -my %vec; -$vec{$_} = [1] for @primes; - -for my $i (1 .. $q) { - -# just look for the min value from the head of v2, v3 or v5 -# (without using List::Util, as the challenge encourages no to use external modules) - my $h; - map { $h = $vec{$_}[0] unless $h && $h < $vec{$_}[0] } @primes; - - # and we just got the next hamming number! - say "$i\t", $h; - - for my $f (@primes) { # now for every prime factor - # remove this value on each vector where it is present - shift @{$vec{$f}} if $vec{$f}[0] == $h; - - # and finally calculate and insert new values to each vector - # note that new pushed value is higer than any previous value in each vector - push @{$vec{$f}}, $h * $f; - } -} -- cgit