aboutsummaryrefslogtreecommitdiff
path: root/challenge-164
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-05-16 02:29:27 +0100
committerGitHub <noreply@github.com>2022-05-16 02:29:27 +0100
commita39409aea356fdb3bc875334cc8783c8d251a4d6 (patch)
treeefcdf4bf8b0d9f727dde7abbfa22a7f582ef9e48 /challenge-164
parent25614f344662b6b26fb0efa236057da86292dff3 (diff)
parenta039d7ced25ed8bc65d1e98d4f95afe3c96cedac (diff)
downloadperlweeklychallenge-club-a39409aea356fdb3bc875334cc8783c8d251a4d6.tar.gz
perlweeklychallenge-club-a39409aea356fdb3bc875334cc8783c8d251a4d6.tar.bz2
perlweeklychallenge-club-a39409aea356fdb3bc875334cc8783c8d251a4d6.zip
Merge pull request #6114 from jaldhar/challenge-164
Challenge 164 by Jaldhar H. Vyas.
Diffstat (limited to 'challenge-164')
-rw-r--r--challenge-164/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-164/jaldhar-h-vyas/perl/ch-1.pl25
-rwxr-xr-xchallenge-164/jaldhar-h-vyas/perl/ch-2.pl42
-rwxr-xr-xchallenge-164/jaldhar-h-vyas/raku/ch-1.sh3
-rwxr-xr-xchallenge-164/jaldhar-h-vyas/raku/ch-2.raku30
5 files changed, 101 insertions, 0 deletions
diff --git a/challenge-164/jaldhar-h-vyas/blog.txt b/challenge-164/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..0aaec8e16e
--- /dev/null
+++ b/challenge-164/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2022/05/perl_weekly_challenge_week_164.html \ No newline at end of file
diff --git a/challenge-164/jaldhar-h-vyas/perl/ch-1.pl b/challenge-164/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..3ed797dbb8
--- /dev/null
+++ b/challenge-164/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+sub isPrime {
+ my ($n) = @_;
+
+ if ($n < 2) {
+ return undef;
+ }
+
+ if ($n == 2) {
+ return 1;
+ }
+
+ for my $i (2 .. sqrt($n)) {
+ if ($n % $i == 0) {
+ return undef;
+ }
+ }
+
+ return 1;
+}
+
+say join q{ }, grep { isPrime($_) && $_ == reverse $_ } 1 .. 1000; \ No newline at end of file
diff --git a/challenge-164/jaldhar-h-vyas/perl/ch-2.pl b/challenge-164/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..1567175a76
--- /dev/null
+++ b/challenge-164/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+sub sum {
+ my ($arr) = @_;
+ my $total = 0;
+
+ for my $elem (@{$arr}) {
+ $total += $elem;
+ }
+
+ return $total;
+}
+
+sub isHappy {
+ my ($i) = @_;
+ my $tries = 0;
+ my $s = $i;
+
+ while ($s != 1) {
+ if ($tries == 8) {
+ return undef;
+ }
+ $s = sum([map { $_ ** 2 } split //, $s]);
+ $tries++;
+ }
+
+ return 1;
+}
+
+my @happy;
+my $i = 1;
+
+while (scalar @happy < 8) {
+ if (isHappy($i)) {
+ push @happy, $i;
+ }
+ $i++;
+}
+
+say join q{ }, @happy;
diff --git a/challenge-164/jaldhar-h-vyas/raku/ch-1.sh b/challenge-164/jaldhar-h-vyas/raku/ch-1.sh
new file mode 100755
index 0000000000..4f47143857
--- /dev/null
+++ b/challenge-164/jaldhar-h-vyas/raku/ch-1.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+raku -e '(1 .. 1000).grep({ $_.is-prime && $_ == $_.flip }).join(q{ }).say;'
diff --git a/challenge-164/jaldhar-h-vyas/raku/ch-2.raku b/challenge-164/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..385e710283
--- /dev/null
+++ b/challenge-164/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,30 @@
+#!/usr/bin/raku
+
+sub isHappy(Int $i) {
+ my $tries = 0;
+ my $s = $i;
+
+ while $s != 1 {
+ if $tries == 8 {
+ return False;
+ }
+ $s = $s.comb.map({ $_ ** 2 }).sum;
+ $tries++;
+ }
+
+ return True;
+}
+
+sub MAIN() {
+ my @happy;
+ my $i = 1;
+
+ while @happy.elems < 8 {
+ if isHappy($i) {
+ @happy.push($i);
+ }
+ $i++;
+ }
+
+ @happy.join(q{ }).say;
+} \ No newline at end of file