diff options
| -rw-r--r-- | challenge-015/michael-hamlin/perl5/GetPrime.pm | 4 | ||||
| -rw-r--r-- | challenge-015/michael-hamlin/perl5/Vigenere.pm (renamed from challenge-015/michael-hamlin/perl5/Vignere.pm) | 1 | ||||
| l---------[-rw-r--r--] | challenge-015/michael-hamlin/perl5/ch-1.pl | 29 | ||||
| l---------[-rw-r--r--] | challenge-015/michael-hamlin/perl5/ch-2.pl | 24 | ||||
| -rw-r--r-- | challenge-015/michael-hamlin/perl5/task1.txt | 15 | ||||
| -rw-r--r-- | challenge-015/michael-hamlin/perl5/task2.txt | 2 | ||||
| -rw-r--r-- | challenge-016/michael-hamlin/perl5/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-016/michael-hamlin/perl5/task1.txt | 6 |
8 files changed, 62 insertions, 52 deletions
diff --git a/challenge-015/michael-hamlin/perl5/GetPrime.pm b/challenge-015/michael-hamlin/perl5/GetPrime.pm index 17559ce4ea..9812b88ed2 100644 --- a/challenge-015/michael-hamlin/perl5/GetPrime.pm +++ b/challenge-015/michael-hamlin/perl5/GetPrime.pm @@ -13,7 +13,7 @@ use integer; my @primes = (2, 3, 5); sub getprime ($) { - my $n = shift || die "must give an ordinal number to getprime"; + my $n = shift || die "must give an ordinal integer to getprime"; push @primes, _nextprime() while @primes < $n; return $primes[$n - 1]; } @@ -41,8 +41,10 @@ sub _hasfactor { 1; __END__ + #foreach (1..20) { # printf "%2u: %u\n", $_, getprime($_); #} + say 'the hundredth prime is ', getprime(100); diff --git a/challenge-015/michael-hamlin/perl5/Vignere.pm b/challenge-015/michael-hamlin/perl5/Vigenere.pm index 494c322f5a..bc54f2d23b 100644 --- a/challenge-015/michael-hamlin/perl5/Vignere.pm +++ b/challenge-015/michael-hamlin/perl5/Vigenere.pm @@ -62,5 +62,6 @@ sub vdecode ($@) { } return $msg; } + 1; diff --git a/challenge-015/michael-hamlin/perl5/ch-1.pl b/challenge-015/michael-hamlin/perl5/ch-1.pl index bbbf14099c..97193b3d16 100644..120000 --- a/challenge-015/michael-hamlin/perl5/ch-1.pl +++ b/challenge-015/michael-hamlin/perl5/ch-1.pl @@ -1,28 +1 @@ -#! /usr/bin/env perl - -use 5.18.0; -use GetPrime; -use integer; - -my $n = shift @ARGV || 10; - -my (@strong, @weak); - -for (my $i = 2; @strong < $n && @weak < $n; $i++) { - my $prime = getprime($i); - # avoiding division for speed - - my $doubled = 2 * $prime; # saves an op later - my $neighbor_sum = getprime($i-1) + getprime($i+1); - - if ($doubled > $neighbor_sum) { - push @strong, $prime; - } elsif ($doubled < $neighbor_sum) { - push @weak, $prime; - } else { - say "zomg prime #$i ($prime) is neither!" -} -} - -say "Strongs are: @strong"; -say "Weaks are: @weak"; +t1-prime-types.pl
\ No newline at end of file diff --git a/challenge-015/michael-hamlin/perl5/ch-2.pl b/challenge-015/michael-hamlin/perl5/ch-2.pl index 9d30e5e5a1..036430d10c 100644..120000 --- a/challenge-015/michael-hamlin/perl5/ch-2.pl +++ b/challenge-015/michael-hamlin/perl5/ch-2.pl @@ -1,23 +1 @@ -#! /usr/bin/env perl - -use 5.18.0; -use Vigenere; - -use Getopt::Long; - -my %opt; -GetOptions(\%opt, - 'key|k=s@', - 'msg|m=s', -); - -my $plaintext = $opt{msg} || 'attack at dawn'; -my @keys = $opt{key} ? @{ $opt{key} } : 'lemon'; - -my $cipher = vencode($plaintext, @keys); -my $decoded = vdecode($cipher, reverse @key -s); - -print "ciphertext: $cipher\n"; -print "decoded: $decoded\n"; - +t2-vig-cipher.pl
\ No newline at end of file diff --git a/challenge-015/michael-hamlin/perl5/task1.txt b/challenge-015/michael-hamlin/perl5/task1.txt new file mode 100644 index 0000000000..50cc038091 --- /dev/null +++ b/challenge-015/michael-hamlin/perl5/task1.txt @@ -0,0 +1,15 @@ + + + Write a script to generate first 10 strong and weak prime numbers. + + For example, the nth prime number is represented by p(n). + + p(1) = 2 + p(2) = 3 + p(3) = 5 + p(4) = 7 + p(5) = 11 + + Strong Prime number p(n) when p(n) > [ p(n-1) + p(n+1) ] / 2 + Weak Prime number p(n) when p(n) < [ p(n-1) + p(n+1) ] / 2 + diff --git a/challenge-015/michael-hamlin/perl5/task2.txt b/challenge-015/michael-hamlin/perl5/task2.txt new file mode 100644 index 0000000000..1c00f90e2b --- /dev/null +++ b/challenge-015/michael-hamlin/perl5/task2.txt @@ -0,0 +1,2 @@ +Write a script to implement Vigenère cipher. The script should be able encode and decode. Checkout wiki page for more information. +https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher diff --git a/challenge-016/michael-hamlin/perl5/ch-1.pl b/challenge-016/michael-hamlin/perl5/ch-1.pl new file mode 100644 index 0000000000..f172617f68 --- /dev/null +++ b/challenge-016/michael-hamlin/perl5/ch-1.pl @@ -0,0 +1,33 @@ +#! /usr/bin/env perl +# +use 5.18.0; +use List::Util qw<sum>; +use Getopt::Std; + +my %opt; +getopts('v', \%opt); + +# each piece is a percent. the sum must equal 100. +my @pieces; +my $biggest; + +# given the conditions, and the nature of the problem, +# we can surmise the function must have a single maximum. +# with this knowledge, we can skip calculating once we +# pass the largest value. + +my $remaining = 100; +for my $p (1..100) { + my $this = $remaining * $p/100; + printf "piece %3u: %.2f\n", $p, $this if $opt{v}; + push @pieces, $this; + $remaining -= $this; + # once we passed the peak, we can stop + last if $biggest && $this < $pieces[$biggest - 1]; + # otherwise we just found a new maximum: + $biggest = $p; +} + +printf "biggest was #%2u, at %.2f of the pie\n", $biggest, $pieces[$biggest - 1]; +# say 'checking our math, sum is ', List::Util::sum @pieces if $opt{v}; + diff --git a/challenge-016/michael-hamlin/perl5/task1.txt b/challenge-016/michael-hamlin/perl5/task1.txt new file mode 100644 index 0000000000..d2b904c7ed --- /dev/null +++ b/challenge-016/michael-hamlin/perl5/task1.txt @@ -0,0 +1,6 @@ + + Pythagoras Pie Puzzle, proposed by Jo Christian Oterhals. + + At a party a pie is to be shared by 100 guest. The first guest gets 1% of the pie, the second guest gets 2% of the remaining pie, the third gets 3% of the remaining pie, the fourth gets 4% and so on. + + Write a script that figures out which guest gets the largest piece of pie. |
