aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-141/jake/perl/ch-1.pl44
1 files changed, 26 insertions, 18 deletions
diff --git a/challenge-141/jake/perl/ch-1.pl b/challenge-141/jake/perl/ch-1.pl
index ce8c9da777..33fb88012c 100755
--- a/challenge-141/jake/perl/ch-1.pl
+++ b/challenge-141/jake/perl/ch-1.pl
@@ -4,14 +4,22 @@ use warnings;
use strict;
use feature 'say';
-# number attributes
+###
+# Write a script to find lowest 10 positive integers having exactly 8 divisors
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-141/#TASK1
+###
+
+### number attributes
# num specifies the lowest number to start analysis
-# div_required specifies for how many divisors we're aiming at
+# div_required specifies how many divisors we're aiming at
# output specifies how many numbers will be displayed
-# div_required and output_required can be adjusted as desired
+# all of these values can be adjusted as desired
+# corner cases that won't comupte correctly: div_required = 0 and div_required = 1
+# also odd div_required may run easily into deep recursion because seemingly they tend to be larger numbers
my $num_atr = {
num => '1',
- div_required => '6',
+ div_required => '8',
output_required => '10'
};
@@ -33,11 +41,19 @@ sub collect_numbers {
# starting at 1 we aggregate all divisors in an array
# we also aggregate all corresponding reciprocal values in another array
# if these arrays are symmetric to each other at the position of half our's div_required, we know they have the required amount of divisors
-# p.e. num = 24; div_required = 8; the divisor arrays are cross-symmetric at div_required / 2
+# example for even div_required = 8; num = 24; the divisor arrays are cross-symmetric at div_required / 2
+# the axis is between $divisors[3] and $reciprocal[4]
# <==\\==>
# 24 12 8 6\\4 3 2 1
# 1 2 3 4\\6 8 12 24
# <==\\==>
+#
+# example for odd div_required = 9; num = 36; the divisor arrays are cross-symmetric at div_required / 2
+# the axis is on $divisors[4] and $reciprocal[4]
+# <==\ \==>
+# 36 18 12 09 \06\ 04 03 02 01
+# 01 02 03 04 \06\ 09 12 18 36
+# <==\ \==>
sub count_divisors {
my ( $spec_num, $divisor) = @_;
@@ -56,25 +72,17 @@ sub count_divisors {
push @reciprocal, ( $num_atr->{num} / $divisor );
}
- # if div_required is even
+ # if div_required is even we look for the axis between array elements
if ( $num_atr->{div_required} % 2 == 0 ) {
return $num_atr->{num} if $divisors[$num_atr->{div_required} / 2] && $divisors[$num_atr->{div_required} / 2] == $reciprocal[$num_atr->{div_required} / 2 - 1];
}
- # if div_required is odd; CAPUTTED: ref massacre
- #if ( $num_atr->{div_required} % 2 == 1 ) {
- # return $num_atr->{num} if $divisors[int($num_atr->{div_required} / 2) + 1]
- # && $divisors[int($num_atr->{div_required} / 2) + 1] == $reciprocal[int($num_atr->{div_required} / 2) + 1];
- #}
+ # if div_required is odd we look for the axis on an array element
if ( $num_atr->{div_required} % 2 == 1 ) {
- return $num_atr->{num} if $divisors[3]
- && $divisors[3] == $reciprocal[3];
+ return $num_atr->{num} if $divisors[int($num_atr->{div_required} / 2)]
+ && $divisors[int($num_atr->{div_required} / 2)] == $reciprocal[int($num_atr->{div_required} / 2)];
}
- #say ("line $num_atr->{div_required}");
- #say[int($num_atr->{div_required} / 2) + 1];
- #say $divisors[int($num_atr->{div_required} / 2) + 1];
- #say $reciprocal[int($num_atr->{div_required} / 2) + 1];
- #die if $num_atr->{num}==15;
+
$divisor++;
}