diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-12-05 16:56:38 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-12-05 16:56:38 +0000 |
| commit | 9936ebffa929fe5dcf0e816a8f1a347037eaa2de (patch) | |
| tree | 33d76a73c0fa0617f13799ee0bd150a0fd4a3b9c /challenge-141 | |
| parent | e4b1faea54e0c95cf12f5803192b9c030e5b007e (diff) | |
| download | perlweeklychallenge-club-9936ebffa929fe5dcf0e816a8f1a347037eaa2de.tar.gz perlweeklychallenge-club-9936ebffa929fe5dcf0e816a8f1a347037eaa2de.tar.bz2 perlweeklychallenge-club-9936ebffa929fe5dcf0e816a8f1a347037eaa2de.zip | |
- Added solutions by Kaushik Tunuguntla.
Diffstat (limited to 'challenge-141')
| -rw-r--r-- | challenge-141/kaushik-tunuguntla/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-141/kaushik-tunuguntla/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-141/kaushik-tunuguntla/perl/ch-2.pl | 29 |
3 files changed, 62 insertions, 0 deletions
diff --git a/challenge-141/kaushik-tunuguntla/blog.txt b/challenge-141/kaushik-tunuguntla/blog.txt new file mode 100644 index 0000000000..dd807e435e --- /dev/null +++ b/challenge-141/kaushik-tunuguntla/blog.txt @@ -0,0 +1 @@ +https://notmondayagain.blogspot.com/2021/12/divisibility-sieves-and-masks-pwc-141.html diff --git a/challenge-141/kaushik-tunuguntla/perl/ch-1.pl b/challenge-141/kaushik-tunuguntla/perl/ch-1.pl new file mode 100644 index 0000000000..0c4116591f --- /dev/null +++ b/challenge-141/kaushik-tunuguntla/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +number_divisors_task(10, 8); + +sub number_divisors_task { + my ($number_count, $divisor_count) = @_; + + my @desired_numbers; + my $i = $divisor_count; + while (@desired_numbers < $number_count) { + if (number_of_divisors($i) == $divisor_count) { + push @desired_numbers, $i; + } + $i++; + } + print "Lowest 10 positive integers having exactly 8 divisors\n"; + print join("\n", @desired_numbers); +} + +sub number_of_divisors { + my ($num) = @_; + my $count = 0; + foreach my $divisor (1 .. $num) { + if ($num % $divisor == 0) { + $count++; + } + } + return $count; +} diff --git a/challenge-141/kaushik-tunuguntla/perl/ch-2.pl b/challenge-141/kaushik-tunuguntla/perl/ch-2.pl new file mode 100644 index 0000000000..ab54cd3025 --- /dev/null +++ b/challenge-141/kaushik-tunuguntla/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my $m = 1234; +my $n = 2; +likenumbers_divisible($m, $n); + +sub likenumbers_divisible { + my ($m, $n) = @_; + + my $length = length($m); + my @likenumbers; + my $divisible_count = 0; + # Get all binary masks of length $length excluding all zeroes and all ones. + foreach my $i (1 .. (2 ** $length)-2) { + my $mask = sprintf("%.${length}b", $i); + my $ministring = ''; + #perform the masking operation to generate 23 from 1234 if mask is 0110. + foreach my $i (0 .. $length-1) { + $ministring .= substr($m, $i, 1) if substr($mask, $i, 1); + } + if ($ministring % $n == 0) { + $divisible_count++; + } + } + print "divisible_count: [$divisible_count]\n"; +} |
