diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-12-10 16:52:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-10 16:52:54 +0000 |
| commit | 86a3e9024efb280382a7abca879697b5d20d2091 (patch) | |
| tree | 5e37a71d438a892fb476e41e8b698a190018eeb3 | |
| parent | a0df6bfc4d83588e44f30dc7e376e4ca135fb750 (diff) | |
| parent | 5dd233be0cc765ce4d6ddf75ad80faf87d8955d3 (diff) | |
| download | perlweeklychallenge-club-86a3e9024efb280382a7abca879697b5d20d2091.tar.gz perlweeklychallenge-club-86a3e9024efb280382a7abca879697b5d20d2091.tar.bz2 perlweeklychallenge-club-86a3e9024efb280382a7abca879697b5d20d2091.zip | |
Merge pull request #5358 from dasJake/142
142
| -rwxr-xr-x | challenge-142/jake/perl/ch-1.pl | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-142/jake/perl/ch-1.pl b/challenge-142/jake/perl/ch-1.pl new file mode 100755 index 0000000000..c18257da47 --- /dev/null +++ b/challenge-142/jake/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +use warnings; +use strict; +use feature 'say'; + +### +# You are given positive integers, $m and $n. +# Write a script to find total count of divisors of $m having last digit $n. +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-142/#TASK1 +### + + +my $num = <STDIN>; +chomp $num; +my $last_dig = <STDIN>; +chomp $last_dig; + +# output +my @divisors = count_divisors( 1, $num ); +my @res = filter_last_digit ( $last_dig, \@divisors ); +say scalar ( @res ); + +# collect numbers with specific last digit +sub filter_last_digit { + my ( $last_dig, $divisors ) = @_; + + # courtesy ccntrq + my @result = grep { substr( $_, -1 ) == $last_dig } @$divisors ; + return @result; +} + +sub count_divisors { + my ( $divisor, $num ) = @_; + my @divisors; + +# divide num through all numbers <= num and count every time modulo is 0 +# each time modulo is 0 we know it's a divisor + while ( $divisor <= $num ) { + #$div_cntr++ if $num_atr->{num} % ( $num_atr->{num} - $divisor ) == 0; + push @divisors, $divisor if $num % $divisor == 0; + $divisor++; + } + + #according to the task the number itself is not considered a divisor, so we cut it off of the result + splice( @divisors, -1 ); + return @divisors; +}
\ No newline at end of file |
