diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-05-13 18:53:28 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-05-13 18:53:28 +0100 |
| commit | 1f27abafe15d8fcb9dc21a82345350b04643bcf2 (patch) | |
| tree | bd73419729269595b067bfd2e89c78fb76b69b52 /challenge-164 | |
| parent | e62eefc9992b68e97006d5f0744790ac97b06d4d (diff) | |
| download | perlweeklychallenge-club-1f27abafe15d8fcb9dc21a82345350b04643bcf2.tar.gz perlweeklychallenge-club-1f27abafe15d8fcb9dc21a82345350b04643bcf2.tar.bz2 perlweeklychallenge-club-1f27abafe15d8fcb9dc21a82345350b04643bcf2.zip | |
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-164')
| -rwxr-xr-x | challenge-164/pete-houston/lua/ch-2.lua | 42 | ||||
| -rwxr-xr-x | challenge-164/pete-houston/perl/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-164/pete-houston/perl/ch-2.pl | 52 |
3 files changed, 127 insertions, 0 deletions
diff --git a/challenge-164/pete-houston/lua/ch-2.lua b/challenge-164/pete-houston/lua/ch-2.lua new file mode 100755 index 0000000000..24b7817fcb --- /dev/null +++ b/challenge-164/pete-houston/lua/ch-2.lua @@ -0,0 +1,42 @@ +#!/usr/bin/env lua
+--[[
+#===============================================================================
+#
+# FILE: 16402.lua
+#
+# USAGE: ./16402.lua N
+#
+# DESCRIPTION: First N decimal Happy numbers
+#
+# NOTES: N must be a natural number
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 13/05/22
+#===============================================================================
+]]
+
+function is_happy (x)
+ local seen = {}
+ seen[x] = 1
+ while x ~= 1 do
+ sum = 0
+ for c in string.gmatch (x, "%d") do
+ sum = sum + c * c
+ end
+ if seen[sum] then return 0 end
+ seen[sum] = 1
+ x = sum
+ end
+ return 1
+end
+
+m = tonumber (arg[1])
+i = 1
+while m > 0 do
+ if is_happy (i) == 1 then
+ print (i)
+ m = m - 1
+ end
+ i = i + 1
+end
diff --git a/challenge-164/pete-houston/perl/ch-1.pl b/challenge-164/pete-houston/perl/ch-1.pl new file mode 100755 index 0000000000..563a5f12e8 --- /dev/null +++ b/challenge-164/pete-houston/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 16401.pl +# +# USAGE: ./16401.pl [ N ] +# +# DESCRIPTION: Palindromic primes up to N (default 999) +# +# OPTIONS: N defaults to 999 if unset +# REQUIREMENTS: Math::Prime::Util +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 09/05/22 +#=============================================================================== + +use strict; +use warnings; + +use Math::Prime::Util 'is_prime'; + +my $max = shift // 999; +die "Argument must be natural number.\n" unless $max =~ /^[1-9][0-9]*$/; + +my @pp; + +for my $n (2 .. $max) { + next unless $n eq reverse $n; + push @pp, $n if $n eq reverse $n && is_prime ($n); +} + +print "@pp\n"; diff --git a/challenge-164/pete-houston/perl/ch-2.pl b/challenge-164/pete-houston/perl/ch-2.pl new file mode 100755 index 0000000000..8a706693c4 --- /dev/null +++ b/challenge-164/pete-houston/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 16402.pl +# +# USAGE: ./16402.pl [ N ] +# +# DESCRIPTION: Print out the first N decimal happy numbers. +# +# OPTIONS: N defaults to 8 if unset +# REQUIREMENTS: Perl 5.10.0 for // +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 13/05/22 +#=============================================================================== + +use strict; +use warnings; + +my $n = shift // 8; +die "Argument must be natural number.\n" unless $n =~ /^[1-9][0-9]*$/; + +my @happy; +my $i = 1; + +while ($n > @happy) { + push @happy, $i if is_happy ($i); + $i++; +} + +print "@happy\n"; + +sub is_happy { + my $x = shift; + my %seen = ($x => 1); + + while ($x != 1) { + + # Sum the squares of the digits + my $sum = 0; + $sum += chop ($x) ** 2 for 1 .. length $x; + + # Have we looped? + return 0 if $seen{$sum}; + + # Store it and go again + $seen{$x = $sum} = 1; + } + + return 1; +} |
