diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2022-05-15 18:50:57 -0400 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2022-05-15 18:50:57 -0400 |
| commit | a039d7ced25ed8bc65d1e98d4f95afe3c96cedac (patch) | |
| tree | a13fc726b1e0645fc623c507e4651aecacd6f5f6 | |
| parent | 4a5f38a2d4cd826e084132cf3599d75396535b05 (diff) | |
| download | perlweeklychallenge-club-a039d7ced25ed8bc65d1e98d4f95afe3c96cedac.tar.gz perlweeklychallenge-club-a039d7ced25ed8bc65d1e98d4f95afe3c96cedac.tar.bz2 perlweeklychallenge-club-a039d7ced25ed8bc65d1e98d4f95afe3c96cedac.zip | |
Challenge 164 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-164/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-164/jaldhar-h-vyas/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-164/jaldhar-h-vyas/perl/ch-2.pl | 42 | ||||
| -rwxr-xr-x | challenge-164/jaldhar-h-vyas/raku/ch-1.sh | 3 | ||||
| -rwxr-xr-x | challenge-164/jaldhar-h-vyas/raku/ch-2.raku | 30 |
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 |
