From 5601ab5c5b3cde0c5bfe35d0beee22625e6cf0d6 Mon Sep 17 00:00:00 2001 From: dasJake Date: Fri, 10 Dec 2021 15:00:17 +0100 Subject: 142 add solution ch-1.pl --- challenge-142/jake/perl/ch-1.pl | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 challenge-142/jake/perl/ch-1.pl 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 = ; +chomp $num; +my $last_dig = ; +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 -- cgit