diff options
| author | dasJake <no_mail@github.com> | 2021-12-10 15:00:17 +0100 |
|---|---|---|
| committer | dasJake <no_mail@github.com> | 2021-12-10 15:00:17 +0100 |
| commit | 5601ab5c5b3cde0c5bfe35d0beee22625e6cf0d6 (patch) | |
| tree | 59f9ad98206605bdcc73ed1237e77f17e87db177 | |
| parent | d9e173549d904ceb2da49185c33f071496a779c1 (diff) | |
| download | perlweeklychallenge-club-5601ab5c5b3cde0c5bfe35d0beee22625e6cf0d6.tar.gz perlweeklychallenge-club-5601ab5c5b3cde0c5bfe35d0beee22625e6cf0d6.tar.bz2 perlweeklychallenge-club-5601ab5c5b3cde0c5bfe35d0beee22625e6cf0d6.zip | |
142 add solution ch-1.pl
| -rwxr-xr-x | challenge-142/jake/perl/ch-1.pl | 48 |
1 files changed, 48 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..8c2544489a --- /dev/null +++ b/challenge-142/jake/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/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 @res = count_divisors( 1, $num ); +my @rres = filter_last_digit ( $last_dig, \@res ); +say scalar ( @rres ); + +# collect numbers with specific last digit +sub filter_last_digit { + my ( $last_dig, $res ) = @_; + + my @result = grep { substr( $_, -1 ) == $last_dig } @$res ; + 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, $#divisors, 1 ); + return @divisors; +}
\ No newline at end of file |
