diff options
| author | Simon Green <mail@simon.green> | 2022-06-13 23:23:30 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2022-06-13 23:23:30 +1000 |
| commit | 78d5dd9442299652bc56f892d2736256453c08cd (patch) | |
| tree | 781a2902a0c6c1e104111e42755b0f821af6320a | |
| parent | cd7f24db7bbf5f10450ba2fa1450ad42dbf17262 (diff) | |
| download | perlweeklychallenge-club-78d5dd9442299652bc56f892d2736256453c08cd.tar.gz perlweeklychallenge-club-78d5dd9442299652bc56f892d2736256453c08cd.tar.bz2 perlweeklychallenge-club-78d5dd9442299652bc56f892d2736256453c08cd.zip | |
sgreen solutions to challenge 169
| -rw-r--r-- | challenge-169/sgreen/README.md | 4 | ||||
| -rwxr-xr-x | challenge-169/sgreen/perl/ch-1.pl | 63 | ||||
| -rwxr-xr-x | challenge-169/sgreen/perl/ch-2.pl | 70 | ||||
| -rwxr-xr-x | challenge-169/sgreen/python/ch-1.py | 54 | ||||
| -rwxr-xr-x | challenge-169/sgreen/python/ch-2.py | 56 |
5 files changed, 245 insertions, 2 deletions
diff --git a/challenge-169/sgreen/README.md b/challenge-169/sgreen/README.md index 9b6e026bc2..9956720a38 100644 --- a/challenge-169/sgreen/README.md +++ b/challenge-169/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 158 +# The Weekly Challenge 169 -Blog: [Weekly Challenge 160](https://dev.to/simongreennet/weekly-challenge-160-3206)
\ No newline at end of file +Blog: [It's all about the numbers](https://dev.to/simongreennet/its-all-about-the-numbers-4fh5)
\ No newline at end of file diff --git a/challenge-169/sgreen/perl/ch-1.pl b/challenge-169/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..04ded95aa7 --- /dev/null +++ b/challenge-169/sgreen/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub is_prime ($number) { + # Return true or false if the number is a prime + if ( $number < 2 ) { + return; + } + + foreach my $i ( 2 .. int( sqrt($number) ) ) { + if ( $number % $i == 0 ) { + return; + } + } + + # It's a prime + return 1; +} + +sub numerical { $a <=> $b } + +sub get_bril_nums ($l) { + # Get all primes of length l + my @primes = (); + foreach my $i ( 10**( $l - 1 ) .. 10**$l ) { + if ( is_prime($i) ) { + push @primes, $i; + } + } + + # Calculate all brilliant numbers + my @bril_nums = (); + while ( my ( $i, $v1 ) = each @primes ) { + foreach my $v2 ( @primes[ $i .. $#primes ] ) { + say "$i $v1 $v2"; + push @bril_nums, $v1 * $v2; + } + } + + # Return the sorted list + return sort numerical @bril_nums; +} + +sub main() { + my $l = 1; + my @bril_nums = (); + + # Keep increasing the length of primes until we have at least twenty + # solutions + while ( scalar(@bril_nums) < 20 ) { + push @bril_nums, get_bril_nums($l); + $l++; + } + + # Print the first 20 numbers + say join ', ', @bril_nums[ 0 .. 19 ]; +} + +main(); diff --git a/challenge-169/sgreen/perl/ch-2.pl b/challenge-169/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..c557782966 --- /dev/null +++ b/challenge-169/sgreen/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw(all any min); + +sub is_achilles_number ($num) { + # Get the prime factors of the number + my %factors = (); + + my $i = 2; + while ( $num > 1 ) { + if ( $num % $i == 0 ) { + $num /= $i; + if ( exists( $factors{$i} ) ) { + $factors{$i}++; + } + else { + $factors{$i} = 1; + } + } + else { + $i++; + } + } + + # If there is only one prime factor, it is not an achilles number + if ( scalar( keys %factors ) == 1 ) { + return; + } + + # Get a list of unique powers + my @powers = values(%factors); + + # It's also not an achilles number if any of the powers were 1, or the + # powers are all the same (a perfect sqaure) + if ( $#powers == 0 or any { $_ == 1 } @powers ) { + return; + } + + # Finally, the greatest common divisor of these numbers has to be one + foreach my $i ( 2 .. min(@powers) ) { + if ( all { $_ % $i == 0 } @powers ) { + return; + } + } + + return 1; +} + +sub main() { + my @solutions = (); + my $num = 2; + + # Get the first 20 achilles numbers + while ( $#solutions < 19 ) { + if ( is_achilles_number($num) ) { + push @solutions, $num; + } + $num++; + } + + # Print the list + say join ', ', @solutions; +} + +main();
\ No newline at end of file diff --git a/challenge-169/sgreen/python/ch-1.py b/challenge-169/sgreen/python/ch-1.py new file mode 100755 index 0000000000..dee277497c --- /dev/null +++ b/challenge-169/sgreen/python/ch-1.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +import math + + +def is_prime(number): + '''Return true or false if the number is a prime''' + if number < 2: + return False + + for i in range(2, int(math.sqrt(number)) + 1): + if number % i == 0: + return False + + # It's a prime + return True + + +def get_bril_nums(l): + '''Get all primes of length l''' + + primes = [] + for i in range(10**(l-1), 10**l): + if is_prime(i): + primes.append(i) + + # Calculate all brilliant numbers + bril_nums = [] + for i, v1 in enumerate(primes): + for v2 in primes[i:]: + bril_nums.append(v1*v2) + + # Return the sorted list + bril_nums.sort() + return bril_nums + + +def main(): + '''Find the first twenty brilliant numbers''' + l = 1 + bril_nums = [] + + # Keep increasing the length of primes until we have at least twenty + # solutions + while len(bril_nums) < 20: + bril_nums.extend(get_bril_nums(l)) + l += 1 + + # Print the first 20 numbers + print(*bril_nums[0:20], sep=', ') + + +if __name__ == '__main__': + main() diff --git a/challenge-169/sgreen/python/ch-2.py b/challenge-169/sgreen/python/ch-2.py new file mode 100755 index 0000000000..3cd0323f3a --- /dev/null +++ b/challenge-169/sgreen/python/ch-2.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +def is_achilles_number(num): + '''Determine if this number is an achilles numbers''' + + # Get the prime factors of the number + factors = {} + + i = 2 + while num > 1: + if num % i == 0: + num /= i + if i in factors: + factors[i] += 1 + else: + factors[i] = 1 + else: + i += 1 + + # If there is only one prime factor, it is not an achilles number + if len(factors) == 1: + return False + + # Get a list of unique powers + powers = set(factors.values()) + + # It's also not an achilles number if any of the powers were 1, or the + # powers are all the same (a perfect sqaure) + if 1 in powers or len(powers) == 1: + return False + + # Finally, the greatest common divisor of these numbers has to be one + for i in range(2, min(powers)+1): + if all(x % i == 0 for x in powers): + return False + + return True + + +def main(): + '''Find the first 20 achilles numbers''' + solutions = [] + num = 2 + + # Get the first 20 achilles numbers + while len(solutions) < 20: + if is_achilles_number(num): + solutions.append(num) + num += 1 + + # Print the list + print(*solutions, sep=', ') + + +if __name__ == '__main__': + main() |
