diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-06-28 23:49:52 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-06-28 23:49:52 +0100 |
| commit | 3db37a343f741bced2ea826149881c59aa1bf8a9 (patch) | |
| tree | d1b2030a51b39e64047cd43ac178c8c78d2c7e97 /challenge-066 | |
| parent | d05736d272d9e28442f1ea13f2b8a584772e6633 (diff) | |
| download | perlweeklychallenge-club-3db37a343f741bced2ea826149881c59aa1bf8a9.tar.gz perlweeklychallenge-club-3db37a343f741bced2ea826149881c59aa1bf8a9.tar.bz2 perlweeklychallenge-club-3db37a343f741bced2ea826149881c59aa1bf8a9.zip | |
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-066')
| -rw-r--r-- | challenge-066/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-066/laurent-rosenfeld/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-066/laurent-rosenfeld/raku/ch-1.p6 | 13 | ||||
| -rw-r--r-- | challenge-066/laurent-rosenfeld/raku/ch-2.p6 | 38 |
4 files changed, 68 insertions, 0 deletions
diff --git a/challenge-066/laurent-rosenfeld/blog.txt b/challenge-066/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..32e54685f4 --- /dev/null +++ b/challenge-066/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2020/06/perl-weekly-challenge-66-divide-integers-and-power-integers.html diff --git a/challenge-066/laurent-rosenfeld/perl/ch-1.pl b/challenge-066/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..30954b1693 --- /dev/null +++ b/challenge-066/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,16 @@ +use strict; +use warnings; +use feature qw /say/; + +die "Two integers needed!" unless @ARGV == 2; +my ($m, $n) = @ARGV; +die "Second argument cannot be 0" if $n == 0; +my $neg = ($m <0 && $n >0 or $m > 0 && $n < 0) ? 1 : 0; +$_ = abs $_ for $m, $n; +my $quotient = 0; +while ($m > $n) { + $m -= $n; + $quotient++; +} +$quotient = -$quotient if $neg; +say $quotient; diff --git a/challenge-066/laurent-rosenfeld/raku/ch-1.p6 b/challenge-066/laurent-rosenfeld/raku/ch-1.p6 new file mode 100644 index 0000000000..e154b12c27 --- /dev/null +++ b/challenge-066/laurent-rosenfeld/raku/ch-1.p6 @@ -0,0 +1,13 @@ +use v6; + +sub MAIN (Int $m is copy, $n is copy where $n != 0) { + my $neg = ($m <0 && $n >0 or $m > 0 && $n < 0) ?? True !! False; + $_ = .abs for $m, $n; + my $quotient = 0; + while $m > $n { + $m -= $n; + $quotient++; + } + $quotient = -$quotient if $neg; + say $quotient; +} diff --git a/challenge-066/laurent-rosenfeld/raku/ch-2.p6 b/challenge-066/laurent-rosenfeld/raku/ch-2.p6 new file mode 100644 index 0000000000..0eac78b9a2 --- /dev/null +++ b/challenge-066/laurent-rosenfeld/raku/ch-2.p6 @@ -0,0 +1,38 @@ +use v6; + +my @primes = grep { .is-prime }, 1 .. *; + +sub prime-factors (UInt $num-in is copy) { + my @factors; + my $num = $num-in; + for @primes -> $div { + while ($num %% $div) { + push @factors, $div; + $num div= $div; + } + return @factors if $num == 1; + } + push @factors, $num unless $num == $num-in; + return @factors; +} + +sub find-powers (Int $n) { + my @prime-factors = prime-factors $n; + my $max = sqrt $n; + return 0 unless @prime-factors; + my @factors = @prime-factors.combinations.map({[*] $_}).grep({$_ <= $max and $_ > 1}).unique; + my @powers; + for @factors -> $div { + for 2..* -> $exp { + last if $div ^ $exp > $n; + push @powers, "$div ^ $exp" if $div ** $exp == $n; + } + } + return @powers; +} + +sub MAIN (Int $n is copy where $n > 1) { + my @pow = find-powers $n; + say 0 if @pow.elems == 0; + .say for @pow; +} |
