diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-03-15 22:38:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-15 22:38:04 +0000 |
| commit | 18381c2458af8979c93ec69ff147e5aee3fd378e (patch) | |
| tree | badd21be71acd893372ff87893785fdb8729b6d4 | |
| parent | dd6e2ccfe1868422848c867cef3292a265a89899 (diff) | |
| parent | e5ae576266ff183196f59006c920ab6bc3ba4182 (diff) | |
| download | perlweeklychallenge-club-18381c2458af8979c93ec69ff147e5aee3fd378e.tar.gz perlweeklychallenge-club-18381c2458af8979c93ec69ff147e5aee3fd378e.tar.bz2 perlweeklychallenge-club-18381c2458af8979c93ec69ff147e5aee3fd378e.zip | |
Merge pull request #5780 from jacoby/master
DAJ Challenge 156
| -rw-r--r-- | challenge-156/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-156/dave-jacoby/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-156/dave-jacoby/perl/ch-2.pl | 41 |
3 files changed, 74 insertions, 0 deletions
diff --git a/challenge-156/dave-jacoby/blog.txt b/challenge-156/dave-jacoby/blog.txt new file mode 100644 index 0000000000..fa094f46c2 --- /dev/null +++ b/challenge-156/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2022/03/14/pernicious-and-weird-are-the-numbers-we-two-can-share-weekly-challenge-156.html diff --git a/challenge-156/dave-jacoby/perl/ch-1.pl b/challenge-156/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..b62a8dd082 --- /dev/null +++ b/challenge-156/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use List::Util qw{ product sum0 uniq }; + +my @pernicious; +my $i = 0; +while ( scalar @pernicious < 10 ) { + $i++; + if ( is_prime( count_ones( to_binary($i) ) ) ) { + push @pernicious, $i; + } +} +say join ', ', @pernicious; + +sub count_ones( $n ) { + return sum0 split //, $n; +} + +sub to_binary( $n ) { + return sprintf '%b', $n; +} + +sub is_prime ($n) { + return 0 if $n == 0 || $n == 1; + for ( 2 .. sqrt $n ) { return 0 unless $n % $_ } + return 1; +} diff --git a/challenge-156/dave-jacoby/perl/ch-2.pl b/challenge-156/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..f95d49b464 --- /dev/null +++ b/challenge-156/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use Carp; +use Getopt::Long; +use List::Util qw{ sum0 }; +use Scalar::Util qw{ looks_like_number }; + +my $n = 12; +GetOptions( 'number=i' => \$n ); +croak "Not Greater than 0" unless $n > 0; + +my $w = is_weird($n); +say <<"END"; + Input: \$n = $n + Output: $w +END + +sub is_weird ( $n ) { + my $m = $n; + my @factors = grep { $n % $_ == 0 } 1 .. $n - 1; + my $sum = sum0 @factors; + my $w = subset_sum( $n, \@factors ); + return ( $sum > $n && !$w ) ? 1 : 0; +} + +sub subset_sum ( $n, $factors, $i = 0, @values ) { + if ( !defined $factors->[$i] ) { + my $sum = sum0 @values; + return $n == $sum ? 1 : 0; + } + my @o; + return 1 if subset_sum( $n, $factors, $i + 1, @values, $factors->[$i] ); + return 1 if subset_sum( $n, $factors, $i + 1, @values, 0 ); + return 0; +} + |
