aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRick Bychowski <rick@hiranyaloka.com>2022-06-19 12:05:09 -0700
committerRick Bychowski <rick@hiranyaloka.com>2022-06-19 12:05:09 -0700
commit4e9141fe7c6ae0f1372d09ced4f32d3256efa8f0 (patch)
treecb87cd7d686ae5236380ce6e9a826a1463739a8c
parent5cf0768375d1e0df139eec109433726256ed489a (diff)
downloadperlweeklychallenge-club-4e9141fe7c6ae0f1372d09ced4f32d3256efa8f0.tar.gz
perlweeklychallenge-club-4e9141fe7c6ae0f1372d09ced4f32d3256efa8f0.tar.bz2
perlweeklychallenge-club-4e9141fe7c6ae0f1372d09ced4f32d3256efa8f0.zip
Brilliant and Achilles Numbers
-rwxr-xr-xchallenge-169/rick-bychowski/ch-1.raku44
-rwxr-xr-xchallenge-169/rick-bychowski/ch-2.raku78
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-169/rick-bychowski/ch-1.raku b/challenge-169/rick-bychowski/ch-1.raku
new file mode 100755
index 0000000000..4a008120a6
--- /dev/null
+++ b/challenge-169/rick-bychowski/ch-1.raku
@@ -0,0 +1,44 @@
+#!/usr/bin/env raku
+
+#Brilliant Numbers
+sub MAIN($n = 20) {
+ my @brilliant;
+ for 2 .. * -> $i {
+ @brilliant.push($i) if is-brilliant($i);
+ last if @brilliant.elems == $n
+ }
+ say @brilliant.join(", ");
+}
+
+sub is-brilliant($dividend) {
+ for 2 ..^ $dividend -> $divisor {
+ if $dividend %% $divisor {
+ my $quotient = $dividend/$divisor;
+ my $l= log10($divisor).truncate;
+ if $quotient.is-prime and $l == log10($quotient).truncate {
+ return True;
+ } else {
+ return False;
+ }
+ }
+ }
+}
+
+=begin comment
+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.
+
+For example,
+24287 = 149 x 163
+24289 = 107 x 227
+
+Therefore 24287 and 24289 are 2-brilliant numbers.
+These two brilliant numbers happen to be consecutive as there are no even
+ brilliant numbers greater than 14.
+
+Output
+4, 6, 9, 10, 14, 15, 21, 25, 35, 49, 121, 143, 169, 187, 209, 221, 247, 253, 289, 299
+
+=end comment
diff --git a/challenge-169/rick-bychowski/ch-2.raku b/challenge-169/rick-bychowski/ch-2.raku
new file mode 100755
index 0000000000..cf1b1f576d
--- /dev/null
+++ b/challenge-169/rick-bychowski/ch-2.raku
@@ -0,0 +1,78 @@
+#!/usr/bin/env raku
+
+# Achilles Numbers
+sub MAIN(Int $limit = 20 ) {
+# say is-achilles($limit);
+ my $i;
+ my @achilles-numbers;
+ for 4 .. * -> $n {
+ if is-achilles($n) {
+ @achilles-numbers.push($n);
+ $i++;
+ last if $i == $limit;
+ }
+ }
+ say @achilles-numbers.join(", ");
+}
+
+# Given Integer, Return True if Achilles
+sub is-achilles( Int $n where $n > 0 ) {
+ my %f = factor($n);
+ if %f and %f.values.all > 1 {
+ my $min = %f.values.min;
+ return True unless %f.values.all %% $min;
+ }
+ return False;
+}
+
+# Input: Positive Integer
+# return all prime factors as a HoH:
+# factor($dividend) # { $factor1 => $power1, $factor2 => $power2, ... }
+# Example: factor(24) # { 24 => { 2 => 3, 3 => 1 } }
+sub factor($dividend = 2, %factors?) {
+ my %f = %factors ?? %factors !! ();
+ my $quotient;
+ if $dividend.is-prime {
+ %f{$dividend}++;
+ return %f;
+ }
+ for 2 ..^ $dividend -> $i {
+ if $dividend %% $i {
+ $quotient = Int($dividend / $i);
+ %f{$i}++;
+ if $quotient.is-prime {
+ %f{$quotient}++;
+ return %f;
+ } else {
+ return factor($quotient, %f);
+ }
+ }
+ }
+}
+
+=begin comment
+Write a script to generate first 20 Achilles Numbers. Please checkout wikipedia
+for more information.
+
+ An Achilles number is a number that is powerful but imperfect (not a
+ perfect power). Named after Achilles, a hero of the Trojan war, who was
+ also powerful but imperfect.
+
+ A positive integer n is a powerful number if, for every prime factor p of n,
+ p^2 is also a divisor.
+
+ A number is a perfect power if it has any integer roots
+ (square root, cube root, etc.).
+
+For example 36 factors to (2, 2, 3, 3) - every prime factor (2, 3) also has
+ its square as a divisor (4, 9). But 36 has an integer square root, 6,
+ so the number is a perfect power.
+
+But 72 factors to (2, 2, 2, 3, 3); it similarly has 4 and 9 as divisors, but
+ it has no integer roots. This is an Achilles number.
+
+Output
+
+ 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125,
+ 1152, 1323, 1352, 1372, 1568, 1800
+=end comment