aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-198/zapwai/raku/ch-1.raku27
-rw-r--r--challenge-198/zapwai/raku/ch-2.raku21
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-198/zapwai/raku/ch-1.raku b/challenge-198/zapwai/raku/ch-1.raku
new file mode 100644
index 0000000000..5414f45bae
--- /dev/null
+++ b/challenge-198/zapwai/raku/ch-1.raku
@@ -0,0 +1,27 @@
+my @list = (2,5,8,1);
+#my @list = (3);
+say "Input: (" ~ join(",",@list) ~ ')';
+@list = sort @list;
+my $gap = 0;
+loop (my $i = 0; $i < @list.elems - 1; $i++) {
+ my $diff = @list[$i + 1] - @list[$i];
+ $gap = $diff if ($gap < $diff );
+}
+my $cnt = 0;
+my @ind;
+loop ($i = 0; $i < @list.elems - 1; $i++) {
+ my $diff = @list[$i + 1] - @list[$i];
+ if $gap == $diff {
+ $cnt++ ;
+ @ind.push($i);
+ }
+}
+
+say "Output: $cnt";
+unless ($cnt == 0) {
+ print "The sorted list " ~ @list ~ " contains: ";
+ for (@ind) {
+ print "(" ~ @list[$_] ~ "," ~ @list[$_+1] ~ ")";
+ }
+ say "";
+}
diff --git a/challenge-198/zapwai/raku/ch-2.raku b/challenge-198/zapwai/raku/ch-2.raku
new file mode 100644
index 0000000000..111ccc0d9b
--- /dev/null
+++ b/challenge-198/zapwai/raku/ch-2.raku
@@ -0,0 +1,21 @@
+my $n;
+loop {
+ $n = prompt "Please enter a number (or (q)uit): ";
+ last if ($n eq 'q');
+ redo unless ($n ~~ /^\d+$/);
+ my $tot = 0;
+ for 2 .. $n {
+ $tot++ if is_prime($_);
+ }
+ say "Input: \$n = $n";
+ say "Output: $tot";
+}
+my %primes;
+sub is_prime($n) {
+ return True if %primes{$n};
+ for 2 .. sqrt $n {
+ return False if $n % $_ == 0;
+ }
+ %primes{$n} = 1;
+ True;
+}