aboutsummaryrefslogtreecommitdiff
path: root/challenge-144
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2021-12-27 01:49:13 -0500
committerJaldhar H. Vyas <jaldhar@braincells.com>2021-12-27 01:49:13 -0500
commitc8d9925fb29d46b554c4727ceb655ccaacbf6d55 (patch)
tree4a757a4153461de1deaf41e63aaa46bb8e261db3 /challenge-144
parent7639615d4115f8f6f073da82b7eede0de1d1f9d9 (diff)
downloadperlweeklychallenge-club-c8d9925fb29d46b554c4727ceb655ccaacbf6d55.tar.gz
perlweeklychallenge-club-c8d9925fb29d46b554c4727ceb655ccaacbf6d55.tar.bz2
perlweeklychallenge-club-c8d9925fb29d46b554c4727ceb655ccaacbf6d55.zip
Whoops misunderstood the spec. Fixed challenge 1 solutions accordingly.
Diffstat (limited to 'challenge-144')
-rwxr-xr-xchallenge-144/jaldhar-h-vyas/perl/ch-1.pl21
-rwxr-xr-xchallenge-144/jaldhar-h-vyas/raku/ch-1.raku21
2 files changed, 25 insertions, 17 deletions
diff --git a/challenge-144/jaldhar-h-vyas/perl/ch-1.pl b/challenge-144/jaldhar-h-vyas/perl/ch-1.pl
index 717b188c9c..3266e5a9e6 100755
--- a/challenge-144/jaldhar-h-vyas/perl/ch-1.pl
+++ b/challenge-144/jaldhar-h-vyas/perl/ch-1.pl
@@ -43,16 +43,19 @@ sub isPrime {
return 1;
}
-my $n = shift // die "Need an integer.\n";
+my @semiprimes;
-my @factors = grep { $n % $_ == 0 && isPrime($_) } (2 .. $n / 2);
-if (scalar @factors == 1) {
- say $factors[0] * $factors[0] == $n ? 1 : 0;
-} else {
- say
+for my $n (2 .. 100) {
+ my @factors = grep { $n % $_ == 0 && isPrime($_) } (2 .. $n / 2);
+ if (scalar @factors == 1 && $factors[0] * $factors[0] == $n) {
+ push @semiprimes, $n;
+ } elsif(
scalar
(grep { $_ == $n }
(map { $_->[0] * $_->[1] }
- combinations (\@factors, 2)))
- ? 1 : 0;
-} \ No newline at end of file
+ combinations (\@factors, 2)))) {
+ push @semiprimes, $n;
+ }
+}
+
+say join q{, }, @semiprimes; \ No newline at end of file
diff --git a/challenge-144/jaldhar-h-vyas/raku/ch-1.raku b/challenge-144/jaldhar-h-vyas/raku/ch-1.raku
index 1b8f22b5ad..981258cee1 100755
--- a/challenge-144/jaldhar-h-vyas/raku/ch-1.raku
+++ b/challenge-144/jaldhar-h-vyas/raku/ch-1.raku
@@ -1,12 +1,17 @@
#!/usr/bin/raku
-sub MAIN(Int $n) {
- my @factors = (2 .. $n / 2).grep({ $n %% $_ && $_.is-prime });
- if @factors.elems == 1 {
- say @factors[0] * @factors[0] == $n ?? 1 !! 0;
- } else {
- say
- @factors.combinations(2).map({ [*] $_; }).grep({ $_ == $n;}).elems
- ?? 1 !! 0;
+sub MAIN() {
+ my @semiprimes;
+
+ for 2 .. 100 -> $n {
+ my @factors = (2 .. $n / 2).grep({ $n %% $_ && $_.is-prime });
+
+ if @factors.elems == 1 && @factors[0] * @factors[0] == $n {
+ @semiprimes.push($n);
+ } elsif @factors.combinations(2).map({ [*] $_; }).grep({ $_ == $n;}).elems {
+ @semiprimes.push($n);
+ }
}
+
+ @semiprimes.join(q{, }).say;
}