aboutsummaryrefslogtreecommitdiff
path: root/challenge-133
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-10 17:17:18 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-10 17:17:18 +0100
commita0d7e8d549e57b480ce2f27cd6df195475be4f89 (patch)
tree5d6c9ab2f9bf31ea2193086c6dca13a6bee5d891 /challenge-133
parent0fdcc6e6ed8a0b5565b6c966eb22b19550b21693 (diff)
downloadperlweeklychallenge-club-a0d7e8d549e57b480ce2f27cd6df195475be4f89.tar.gz
perlweeklychallenge-club-a0d7e8d549e57b480ce2f27cd6df195475be4f89.tar.bz2
perlweeklychallenge-club-a0d7e8d549e57b480ce2f27cd6df195475be4f89.zip
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-133')
-rwxr-xr-xchallenge-133/pete-houston/perl/ch-1.pl41
-rwxr-xr-xchallenge-133/pete-houston/perl/ch-2.pl44
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-133/pete-houston/perl/ch-1.pl b/challenge-133/pete-houston/perl/ch-1.pl
new file mode 100755
index 0000000000..1d7ca9703f
--- /dev/null
+++ b/challenge-133/pete-houston/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 13301.pl
+#
+# USAGE: ./13301.pl N [ N ... ]
+#
+# DESCRIPTION: Print the integer square root(s) of the argument(s)
+#
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 04/10/21
+#===============================================================================
+
+use strict;
+use warnings;
+
+while (my $sq = shift) {
+ next if $sq =~ /[^0-9]/;
+ print "Integer square root of $sq is " . isqrt ($sq) . "\n";
+}
+
+# Woo's Abacus method
+sub isqrt {
+ use integer;
+ my $in = shift;
+ my $root = 0;
+ my $top = 1 << 30;
+ $top >>= 2 while $top > $in;
+
+ while ($top) {
+ if ($in >= $root + $top) {
+ $in -= $root + $top;
+ $root += 2 * $top;
+ }
+ $root /= 2;
+ $top /= 4;
+ }
+ return $root;
+}
diff --git a/challenge-133/pete-houston/perl/ch-2.pl b/challenge-133/pete-houston/perl/ch-2.pl
new file mode 100755
index 0000000000..eb21a4fe47
--- /dev/null
+++ b/challenge-133/pete-houston/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 13302.pl
+#
+# USAGE: ./13302.pl
+#
+# DESCRIPTION: Generate the first 10 Smith Numbers
+#
+# REQUIREMENTS: List::Util, Math::Prime::Util, Perl 5.10 or higher
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 04/10/21
+#===============================================================================
+
+use strict;
+use warnings;
+use List::Util 'sum';
+use Math::Prime::Util qw/is_prime factor/;
+
+my $n = 0;
+my $i = 1;
+while ($n < 10) {
+ if (smith ($i)) {
+ print "$i\n";
+ $n++;
+ }
+ $i++;
+}
+
+sub smith {
+ my $x = shift;
+ return 0 if is_prime ($x);
+ return dsum ($x) == dsum (prime_factors ($x));
+}
+
+sub dsum {
+ return sum split //, shift;
+}
+
+sub prime_factors {
+ return join '', factor (shift);
+}