aboutsummaryrefslogtreecommitdiff
path: root/challenge-008/duncan-c-white
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-008/duncan-c-white')
-rw-r--r--challenge-008/duncan-c-white/README24
-rwxr-xr-xchallenge-008/duncan-c-white/perl5/ch-1.pl55
-rwxr-xr-xchallenge-008/duncan-c-white/perl5/ch-2.pl59
3 files changed, 127 insertions, 11 deletions
diff --git a/challenge-008/duncan-c-white/README b/challenge-008/duncan-c-white/README
index fb3a863c80..7adef4a715 100644
--- a/challenge-008/duncan-c-white/README
+++ b/challenge-008/duncan-c-white/README
@@ -1,14 +1,16 @@
-Challenge 1: "Create a script which takes a list of numbers from
-command line and print the same in the compact form. For example, if
-you pass 1,2,3,4,9,10,14,15,16 then it should print the compact form
-like 1-4,9,10,14-16.."
+Challenge 1: "Write a script that computes the first five perfect
+numbers. A perfect number is an integer that is the sum of its positive
+proper divisors (all divisors except itself). Please check Wiki for
+more information. First 4 are 6, 28, 496 and 8128"
-Quite simple and dull problem. But ok, let's have a go.
+My notes: Cute little problem, let's have a go.
-Challenge 2: "Create a script to calculate Ramanujan's constant with at
-least 32 digits of precision."
-Never heard of this constant, seems to be e^(pi*sqrt(163)), which is
-"very nearly an integer", I don't particularly care about abtruse mathematical
-formulae. But ok, Perl's built in module biggrat will let you do this anyway,
-specifying accuracy 32; see ch-2.sh for the oneliner.
+Challenge 2: "Write a function, center, whose argument is a list of
+strings, which will be lines of text. The function should insert spaces
+at the beginning of the lines of text so that if they were printed,
+the text would be centered, and return the modified lines."
+
+My notes:
+
+Another well-specified problem:-)
diff --git a/challenge-008/duncan-c-white/perl5/ch-1.pl b/challenge-008/duncan-c-white/perl5/ch-1.pl
new file mode 100755
index 0000000000..5dcc9cfc12
--- /dev/null
+++ b/challenge-008/duncan-c-white/perl5/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+# Challenge 1: "Write a script that computes the first five perfect
+# numbers. A perfect number is an integer that is the sum of its positive
+# proper divisors (all divisors except itself). Please check Wiki for
+# more information. First 4 are 6, 28, 496 and 8128"
+
+
+use strict;
+use warnings;
+use Function::Parameters;
+use Data::Dumper;
+
+die "Usage: ch-1.pl [N|5]]\n" if @ARGV > 1;
+
+
+#
+# my $isprime = is_prime( $x );
+# Return true iff the integer $x is prime.
+fun is_prime( $x )
+{
+ my $limit = int(sqrt($x));
+ #print "is_prime($x): limit=$limit\n";
+ foreach my $i (2..$limit)
+ {
+ return 0 if $x % $i == 0;
+ }
+ return 1;
+}
+
+
+
+
+my $limit = shift // 5;
+
+for( my $p=2, my $found=0; $found<$limit; $p++ )
+{
+ if( is_prime( $p ) )
+ {
+ my $twop1 = 2**($p-1);
+ my $twop = 2*$twop1;
+ my $twopminus1 = $twop-1;
+ if( is_prime( $twopminus1 ) )
+ {
+ print "$p is prime and 2^$p-1 ($twopminus1) is prime\n";
+ my $perfect = ($twop-1) * $twop1;
+ print " so $perfect (2^$p-1 * 2^(p-1) is perfect\n";
+ $found++;
+ }
+ else
+ {
+ print "$p is prime but 2^$p-1 ($twopminus1) is not prime\n";
+ }
+ }
+}
diff --git a/challenge-008/duncan-c-white/perl5/ch-2.pl b/challenge-008/duncan-c-white/perl5/ch-2.pl
new file mode 100755
index 0000000000..d813df3174
--- /dev/null
+++ b/challenge-008/duncan-c-white/perl5/ch-2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+# Challenge 2: "Write a function, center, whose argument is a list of
+# strings, which will be lines of text. The function should insert spaces
+# at the beginning of the lines of text so that if they were printed,
+# the text would be centered, and return the modified lines."
+
+use strict;
+use warnings;
+use Function::Parameters;
+use File::Slurp;
+use Data::Dumper;
+
+die "Usage: ch-2.pl [DATAFILE]\n" if @ARGV > 1;
+my $datafilename = shift // $0;
+
+
+#
+# my $maxlen = maxlength( @data );
+# Find and return the maximum length of any string
+# element of @data.
+#
+fun maxlength( @data )
+{
+ my $maxlen = 0;
+ foreach my $s (@data)
+ {
+ my $l = length($s);
+ $maxlen = $l if $l>$maxlen;
+ }
+ return $maxlen;
+}
+
+
+#
+# my @centred = center( @data );
+# Center every element of @data, building
+# and returning a new @centred array, in
+# which every element is the original element
+# of @data, padded with leading spaces to centere it.
+#
+fun center( @data )
+{
+ my @result;
+ my $maxlen = maxlength( @data );
+ foreach my $s (@data)
+ {
+ my $l = length($s);
+ my $pad = ($maxlen - $l)/2;
+ my $centred = (' 'x$pad).$s;
+ push @result, $centred;
+ }
+ return @result;
+}
+
+
+my @data = read_file( $datafilename );
+my @centered = center( @data );
+map { print } @centered;