diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2022-06-16 18:07:18 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2022-06-16 18:07:18 -0400 |
| commit | e28b4f047990a21b3e98b4e7b6b9e21f499d388a (patch) | |
| tree | 39ed88bfdf9d2689042d534606601a19edc0bf9b /challenge-169 | |
| parent | b9ec9ea5dac6ae173154732b1c3f997676f798b9 (diff) | |
| parent | 5c4b8df3cd8a191dd7c42fc49e12b5dfc3a02cc2 (diff) | |
| download | perlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.tar.gz perlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.tar.bz2 perlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-169')
87 files changed, 4348 insertions, 121 deletions
diff --git a/challenge-169/dario-mazzeo/README b/challenge-169/dario-mazzeo/README new file mode 100644 index 0000000000..9ed3e21a7f --- /dev/null +++ b/challenge-169/dario-mazzeo/README @@ -0,0 +1 @@ +Solutions by Dario Mazzeo. diff --git a/challenge-169/dario-mazzeo/perl/ch-1.pl b/challenge-169/dario-mazzeo/perl/ch-1.pl new file mode 100755 index 0000000000..08fbc5a5b7 --- /dev/null +++ b/challenge-169/dario-mazzeo/perl/ch-1.pl @@ -0,0 +1,24 @@ +# Autore: Dario Mazzeo (dmazzeo@ingele.com)
+# THE WEEKLY CHALLENGE - 169
+# Task 1: Brilliant Numbers
+
+my @primi=(2,3,5,7,11,13,17,19,23);
+my %ris=();
+my $out="";
+
+foreach my $i (@primi){
+ foreach my $j (@primi){
+ if ($ris{$i*$j}==0 && length($i) eq length($j)){
+ $ris{$i*$j}=1;
+ }
+ }
+}
+
+my $c=0;
+foreach my $i (sort {$a <=> $b} keys %ris){
+ if ($c<20){$out.="$i, ";}
+ $c++;
+}
+
+$out=~s/, $//;
+print "$out\n";
\ No newline at end of file diff --git a/challenge-169/e-choroba/perl/ch-1.pl b/challenge-169/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..ef863b1cbc --- /dev/null +++ b/challenge-169/e-choroba/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Math::Prime::Util qw{ factor }; + +sub brilliant_numbers { + my ($tally) = @_; + my @brilliant_numbers; + my $n = 1; + while (@brilliant_numbers < $tally) { + my @f = factor(++$n); + push @brilliant_numbers, $n + if 2 == @f + && length($f[0]) == length($f[1]); + } + return \@brilliant_numbers +} + +use Test::More tests => 1; + +is_deeply brilliant_numbers(20), + [4, 6, 9, 10, 14, 15, 21, 25, 35, 49, 121, + 143, 169, 187, 209, 221, 247, 253, 289, 299]; diff --git a/challenge-169/e-choroba/perl/ch-2.pl b/challenge-169/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..48e9f7c4eb --- /dev/null +++ b/challenge-169/e-choroba/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Math::Prime::Util qw{ factor gcd }; + +sub achilles_numbers { + my ($tally) = @_; + my $n = 1; + my @achilles_numbers; + while (@achilles_numbers < $tally) { + my %factors; + ++$factors{$_} for factor(++$n); + next if grep $_ == 1, values %factors; # Powerful. + + push @achilles_numbers, $n + if 1 == gcd(values %factors); # Not perfect. + } + return \@achilles_numbers +} + +use Test::More tests => 1; +is_deeply achilles_numbers(20), + [72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, + 968, 972, 1125, 1152, 1323, 1352, 1372, 1568, 1800]; diff --git a/challenge-169/eric-cheung/python/ch-1.py b/challenge-169/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..aa1dde99b9 --- /dev/null +++ b/challenge-169/eric-cheung/python/ch-1.py @@ -0,0 +1,46 @@ +
+## Remarks
+
+import math
+
+def PrimeFact(nInput):
+
+ for nDiv in range(2, int(math.sqrt(nInput)) + 1):
+ if nInput % nDiv == 0:
+ return nDiv
+
+ return 0
+
+
+def IsBrillNum(nInput):
+
+ nFact = PrimeFact(nInput)
+
+ if nFact == 0:
+ return False
+
+ nFact_2 = int(nInput / nFact)
+
+ nNum = PrimeFact(nFact_2)
+
+ if nNum > 0:
+ return False
+
+ if len(str(nFact)) == len(str(nFact_2)):
+ return True
+
+ return False
+
+
+## nOrigInputNum = 5
+## print (IsBrillNum(nOrigInputNum))
+
+nCount = 0
+nLoop = 4
+
+while nCount < 20:
+ if IsBrillNum(nLoop):
+ print(nLoop)
+ nCount = nCount + 1
+
+ nLoop = nLoop + 1
diff --git a/challenge-169/eric-cheung/python/ch-2.py b/challenge-169/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..af7725e75b --- /dev/null +++ b/challenge-169/eric-cheung/python/ch-2.py @@ -0,0 +1,80 @@ +
+## Remarks
+## https://en.wikipedia.org/wiki/Achilles_number
+
+
+import numpy as np
+import math
+
+
+def GetUniqueCount(arrList):
+ arrObj = np.array(arrList)
+ return np.unique(arrObj, return_counts = True)
+
+
+def IsPrime(nInput):
+
+ for nDiv in range(2, int(math.sqrt(nInput)) + 1):
+ if nInput % nDiv == 0:
+ return False
+
+ return True
+
+
+def PrimeFact(nOrigInput):
+
+ nInput = nOrigInput
+ arrPrimeFact = []
+
+ if IsPrime(nInput):
+ arrPrimeFact.append(nInput)
+ return arrPrimeFact
+
+ for nDiv in range(2, nOrigInput):
+
+ while nInput % nDiv == 0 and nInput > 0:
+
+ nInput = nInput / nDiv
+ arrPrimeFact.append(nDiv)
+
+ if nInput == 0:
+ break
+
+ return arrPrimeFact
+
+
+def IsAchillesNum(nInput):
+
+ ## print (nInput)
+
+ arrPrime, arrCount = GetUniqueCount(PrimeFact(nInput))
+
+ ## print (arrPrime)
+ ## print (arrCount)
+
+ ## Check Is Powerful
+ if min(arrCount) == 1:
+ return False
+
+ ## Check Is Perfect
+ if math.gcd(*arrCount) > 1:
+ return False
+
+ return True
+
+
+## print (PrimeFact(37))
+## print (IsAchillesNum(36))
+## print (IsAchillesNum(72))
+## print (IsAchillesNum(144))
+
+
+nCount = 0
+nLoop = 2
+
+while nCount < 20:
+ if IsAchillesNum(nLoop):
+ print(nLoop)
+ nCount = nCount + 1
+
+ nLoop = nLoop + 1
diff --git a/challenge-169/habere-et-dispetire/raku/ch-1.raku b/challenge-169/habere-et-dispetire/raku/ch-1.raku new file mode 100644 index 0000000000..67400cc525 --- /dev/null +++ b/challenge-169/habere-et-dispetire/raku/ch-1.raku @@ -0,0 +1,18 @@ +#! /usr/bin/env raku + +# Brilliant numbers + +use Prime::Factor; + +sub is-brilliant ($n) { + + .elems == 2 and + [==] .map: *.chars + given prime-factors $n + +} + +say (1 .. ∞) + # https://oeis.org/A078972 + .grep(&is-brilliant) + .head(20) diff --git a/challenge-169/habere-et-dispetire/raku/ch-2.raku b/challenge-169/habere-et-dispetire/raku/ch-2.raku new file mode 100644 index 0000000000..b387e3905f --- /dev/null +++ b/challenge-169/habere-et-dispetire/raku/ch-2.raku @@ -0,0 +1,18 @@ +#! /usr/bin/env raku + +# Achilles numbers + +use Prime::Factor; + +sub is-achilles ($n) { + + 2 ≤ .min and + 1 == [gcd] $_ + given @(values bag prime-factors $n) + +} + +say (2 .. ∞) + # https://oeis.org/A052486 + .grep(&is-achilles) + .head(20) diff --git a/challenge-169/james-smith/README.md b/challenge-169/james-smith/README.md index e5e368c7c4..ba9c64a2b4 100644 --- a/challenge-169/james-smith/README.md +++ b/challenge-169/james-smith/README.md @@ -1,7 +1,7 @@ -[< Previous 167](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-167/james-smith) | -[Next 169 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-169/james-smith) +[< Previous 168](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-168/james-smith) | +[Next 170 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-170/james-smith) -# The Weekly Challenge 168 +# The Weekly Challenge 169 You can find more information about this weeks, and previous weeks challenges at: @@ -13,147 +13,199 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-168/james-smith +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-169/james-smith -# Challenge 1 - Perrin Prime +# Challenge 1 - Brilliant numbers -***The Perrin sequence is defined to start with [3, 0, 2]; after that, term N is the sum of terms N-2 and N-3. (So it continues 3, 2, 5, 5, 7, ...) A Perrin prime is a number in the Perrin sequence which is also a prime number. Calculate the first 13 Perrin primes*** +***Write a script to generate first 20 Brilliant Numbers. Brilliant numbers are numbers with two prime factors of the same length. The number should have exactly two prime factors, i.e. it’s the product of two primes of the same length.*** ## Solution -Using our favourite is_prime library we loop through the Perrin numbers and checking to see if they are prime, if they are we display them - if not we restart the loop with `redo`. +Again our favoured prime package `Math::Prime::Util` has the method we need `factor` which returns a list of prime factors, and its fast - especially for the magnitude of numbers we are looking at. -Rather than keep the whole array we only need the last 3 numbers in the sequence to generate the next so we have: `$s -> $r`, `$t -> $s` and `$r+$s -> $t`. Once nice feature of perl is that you can do multiple parallel definitions inside a list. The final quirk you see here is `$t!=$` - there is one duplicate value in the list (5) which would mean we saw 5 twice in our output which we don't want.... +We loop through all numbers, checking to see if (a) the number has exactly 2 prime factors and they are the same length. + +For flexibility we define the max count `$MAX` as the command-line argument if one is supplied (or 100 if not). ```perl -my ($r,$s,$t)=(3,0,2); -($r,$s,$t)=($s,$t,$r+$s), is_prime($t) && $t!=$s ? (say $t) : (redo) for 1..13; +for( my( $MAX, $c, $n, @f ) = ($ARGV[0] // 1e2, 0); |
