aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordasJake <no_mail@github.com>2021-12-01 00:49:02 +0100
committerdasJake <no_mail@github.com>2021-12-01 00:49:02 +0100
commitd9e173549d904ceb2da49185c33f071496a779c1 (patch)
tree0c009278a66eab7f6b756ac64d493a4e9002f437
parent931699cba0d6373b7f7a9e645ff1a6ef59ad6e5d (diff)
downloadperlweeklychallenge-club-d9e173549d904ceb2da49185c33f071496a779c1.tar.gz
perlweeklychallenge-club-d9e173549d904ceb2da49185c33f071496a779c1.tar.bz2
perlweeklychallenge-club-d9e173549d904ceb2da49185c33f071496a779c1.zip
141 add brute force ch-1.pl; method is heavy
-rwxr-xr-xchallenge-141/jake/perl/ch-1.pl62
1 files changed, 45 insertions, 17 deletions
diff --git a/challenge-141/jake/perl/ch-1.pl b/challenge-141/jake/perl/ch-1.pl
index 13e4c85102..e03638a082 100755
--- a/challenge-141/jake/perl/ch-1.pl
+++ b/challenge-141/jake/perl/ch-1.pl
@@ -4,25 +4,53 @@ use warnings;
use strict;
use feature 'say';
-my $number_attributes = {
- number => '1',
- divisor_counter => '0',
- subtractor => '0',
+# number attributes
+# num specifies the lowest number to start analysis
+# div_required specifies for how many divisors we aiming at
+# output specifies how many numbers will be displayed
+my $num_atr = {
+ num => '1',
+ div_required => '8',
+ output_required => '10'
};
-count_divisors( $number_attributes );
-# modulo number n through n, n-1, and so on
-# for each modulo 0 increase counter
-# when counter hits 8 add number to number array, proceed to next number and increase number counter
-# if number counter hits 10 output number array or if array size becomes 10
+
+# output
+my $res = collect_numbers();
+say join (' ', @$res);
+
+
+
+# aggregate required amount of numbers according to output_required
+sub collect_numbers {
+ my @result = ();
+
+ while ( $#result+1 < $num_atr->{output_required} ) {
+ push @result, count_divisors( $num_atr, 0, 0 );
+ $num_atr->{num}++;
+ }
+ return \@result;
+}
+
sub count_divisors {
- say ( $number_attributes->{divisor_counter}, $number_attributes->{subtractor}, $number_attributes->{number} );
- while ( $number_attributes->{divisor_counter} < 9 ) {
- say ( $number_attributes->{divisor_counter}, $number_attributes->{subtractor}, $number_attributes->{number} );
- $number_attributes->{divisor_counter}++ if $number_attributes->{number} % ( $number_attributes->{number} - $number_attributes->{subtractor} ) == 0;
- $number_attributes->{subtractor}++;
- print "div_c: $number_attributes->{divisor_counter}, subtr: $number_attributes->{subtractor}\n";
- $number_attributes->{number}++;
- die if $number_attributes->{number} == 10;
+ my ( $spec_num, $div_cntr, $subtractor) = @_;
+
+# 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 ( $subtractor != $num_atr->{num} ) {
+ $div_cntr++ if $num_atr->{num} % ( $num_atr->{num} - $subtractor ) == 0;
+ $subtractor++;
+ }
+
+# only return to sub collect_numbers if we hit the required amount of divisors
+ if ( $div_cntr == $num_atr->{div_required} ) {
+ return $num_atr->{num};
+ }
+
+# when ever the current num did not meet the required divisors we need to start over counting them for the next num
+# in this case we must not return num to sub collect_numbers
+ else {
+ $num_atr->{num}++;
+ return count_divisors( $num_atr, 0, 0 );
}
} \ No newline at end of file