aboutsummaryrefslogtreecommitdiff
path: root/challenge-156
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2022-03-20 21:44:45 +0000
committerNiels van Dijke <perlboy@cpan.org>2022-03-20 21:44:45 +0000
commit224d13cbe1329507e3cae06aef3a42e66ac8dac4 (patch)
tree44b010ccfe39365a79bf292f1325e6f63cb48a6e /challenge-156
parentd107bb7bca1ee3dea1ed319ed17f4192ff0fa40e (diff)
downloadperlweeklychallenge-club-224d13cbe1329507e3cae06aef3a42e66ac8dac4.tar.gz
perlweeklychallenge-club-224d13cbe1329507e3cae06aef3a42e66ac8dac4.tar.bz2
perlweeklychallenge-club-224d13cbe1329507e3cae06aef3a42e66ac8dac4.zip
Task 1 & 2
Diffstat (limited to 'challenge-156')
-rwxr-xr-xchallenge-156/perlboy1967/perl/ch-1.pl39
-rwxr-xr-xchallenge-156/perlboy1967/perl/ch-2.pl62
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-156/perlboy1967/perl/ch-1.pl b/challenge-156/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..123be36251
--- /dev/null
+++ b/challenge-156/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 156
+ - https://perlweeklychallenge.org/blog/perl-weekly-challenge-156/#TASK1
+
+Author: Niels 'PerlBoy' van Dijke
+
+TASK #1 › Pernicious Numbers
+Submitted by: Mohammad S Anwar
+
+Write a script to permute first 10 Pernicious Numbers.
+
+ || A pernicious number is a positive integer which has prime number of ones
+ || in its binary representation.
+
+The first pernicious number is 3 since binary representation of 3 = (11) and
+1 + 1 = 2, which is a prime.
+
+Expected Output
+
+3, 5, 6, 7, 9, 10, 11, 12, 13, 14
+
+=cut
+
+use v5.16;
+
+use Math::Primality qw(is_prime);
+
+my @pN;
+
+my $i = 1;
+while (scalar @pN < 10) {
+ push(@pN,$i) if (is_prime(scalar(grep/1/,split(//,sprintf('%b',$i)))));
+ $i++;
+}
+
+say join(',',@pN);
diff --git a/challenge-156/perlboy1967/perl/ch-2.pl b/challenge-156/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..4562a08bb0
--- /dev/null
+++ b/challenge-156/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 156
+ - https://perlweeklychallenge.org/blog/perl-weekly-challenge-156/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+TASK #2 › Weird Number
+Submitted by: Mohammad S Anwar
+
+You are given number, $n > 0.
+
+Write a script to find out if the given number is a Weird Number.
+
+According to Wikipedia, it is defined as:
+
+ || The sum of the proper divisors (divisors including 1 but not itself)
+ || of the number is greater than the number, but no subset of those divisors
+ || sums to the number itself.
+
+=cut
+
+use v5.16;
+
+use List::Util qw(sum);
+use Algorithm::Combinatorics qw(combinations);
+
+sub isWeirdNumber($);
+sub divisors($);
+
+@ARGV = (12, 70, 252, 111, 836, 4030, 5830, 7192, 7912, 9272, 10430, 10570, 10792, 10990, 11410, 11690, 12110, 12530, 12670, 13370, 13510, 13790, 13930, 14770) unless (@ARGV);
+
+foreach my $n (@ARGV) {
+ say "n=$n => ",isWeirdNumber($n);
+}
+
+
+sub isWeirdNumber($) {
+ my ($n) = @_;
+
+ # Get proper divisors
+ my @d = (1, grep {$n % $_ == 0} 2 .. $n >> 1);
+
+ my $sum = sum(@d);
+ my $delta = $sum - $n;
+
+ return 0 if ($delta < 0 or
+ grep { $_ == $delta } @d);
+
+ my @r = grep { $_ <= $delta } @d;
+ foreach my $i (2 .. scalar(@r)) {
+ my $iter = combinations(\@r, $i);
+ while (my $ar = $iter->next) {
+ return 0 if (sum(@$ar) == $delta);
+ }
+ }
+
+ return 1;
+}
+