diff options
| author | chirvasitua <stuart-little@users.noreply.github.com> | 2021-03-29 14:22:56 -0400 |
|---|---|---|
| committer | chirvasitua <stuart-little@users.noreply.github.com> | 2021-03-29 17:13:16 -0400 |
| commit | 8e15cb8bb60cf6ee48fd376566c4f5fa89a857a6 (patch) | |
| tree | e737070e059191d5afd059a7b8db4d33ba3b5476 | |
| parent | 0beed338bde9c2ee3c13c79878d4225103c346e8 (diff) | |
| download | perlweeklychallenge-club-8e15cb8bb60cf6ee48fd376566c4f5fa89a857a6.tar.gz perlweeklychallenge-club-8e15cb8bb60cf6ee48fd376566c4f5fa89a857a6.tar.bz2 perlweeklychallenge-club-8e15cb8bb60cf6ee48fd376566c4f5fa89a857a6.zip | |
1st commit on 106_perl
| -rwxr-xr-x | challenge-106/stuart-little/perl/ch-1.pl | 15 | ||||
| -rwxr-xr-x | challenge-106/stuart-little/perl/ch-2.pl | 44 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-106/stuart-little/perl/ch-1.pl b/challenge-106/stuart-little/perl/ch-1.pl new file mode 100755 index 0000000000..2411728b57 --- /dev/null +++ b/challenge-106/stuart-little/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl +use warnings; +use v5.12; + +# run <script> <space-separated numbers> + +use feature qw(signatures); +no warnings qw(experimental::signatures); + +use List::Util qw(max); + +my @sorted = sort {$a <=> $b} @ARGV; +my @diffs = map {$sorted[$_+1]-$sorted[$_]} (0..scalar @sorted-2); + +say ((scalar @diffs) ? (max @diffs) : (0)); diff --git a/challenge-106/stuart-little/perl/ch-2.pl b/challenge-106/stuart-little/perl/ch-2.pl new file mode 100755 index 0000000000..fb738b8aa3 --- /dev/null +++ b/challenge-106/stuart-little/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +use warnings; +use v5.12; + +# run <script> <numerator> <denominator> + +use feature qw(signatures); +no warnings qw(experimental::signatures); + +use bigint; +use List::Util qw(max); + +sub maxExp($p,$n) { + ($n % $p) ? (0) : (1+maxExp($p, int($n/$p))) +} + +sub ordExp($n,$p) { + my ($ord,$base)=(1,$n % $p); + while ($base-1 % $p) { + $ord++; + $base = ($base*$n) % $p; + } + return $ord; +} + +sub when10copr($num,$den) { + my $int = int($num/$den); + my $digs = ordExp(10,$den); + my $fract = int ((($num % $den) * (10**$digs-1))/$den); + return $int, ('0' x ($digs - length($fract))) . $fract; +} + +sub fractPart($num,$den) { + my $exp2 = maxExp(2,$den); + my $exp5 = maxExp(5,$den); + my $newNum = ($exp2 >= $exp5) ? (5**($exp2-$exp5) * $num) : (2**($exp5-$exp2) * $num); + my @copr = when10copr($newNum, int $den/(2**$exp2 * 5**$exp5)); + my $nonRep = ($copr[0] || max($exp2,$exp5)) ? ('0' x (max($exp2,$exp5) - length($copr[0])) . $copr[0]) : (""); + my $rep = ($copr[1] ne "0") ? (qq|($copr[1])|) : (""); + return $nonRep . $rep; +} + +my ($num,$den) = @ARGV; +say int($num/$den) . "." . fractPart($num % $den,$den); |
