aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-07-04 00:23:01 +0100
committerGitHub <noreply@github.com>2022-07-04 00:23:01 +0100
commit5330510ca5fcdb2f41f3c4f75e0f23a39bf07d3b (patch)
tree740d6a1f349b7e69c464c29863d274756a548eba
parentabfef7592734e287dd19907b72f8cfd1f0fad918 (diff)
parent51c79ee4dd6e43ac464d808918f53861dc07f210 (diff)
downloadperlweeklychallenge-club-5330510ca5fcdb2f41f3c4f75e0f23a39bf07d3b.tar.gz
perlweeklychallenge-club-5330510ca5fcdb2f41f3c4f75e0f23a39bf07d3b.tar.bz2
perlweeklychallenge-club-5330510ca5fcdb2f41f3c4f75e0f23a39bf07d3b.zip
Merge pull request #6382 from dcw803/master
imported my solutions to this week's tasks - 2 in Perl and 1 in C..
-rw-r--r--challenge-171/duncan-c-white/C/.cbuild (renamed from challenge-171/duncan-c-white/C/.build)1
-rw-r--r--challenge-171/duncan-c-white/C/Makefile13
-rw-r--r--challenge-171/duncan-c-white/C/ch-1.c107
-rw-r--r--challenge-171/duncan-c-white/README62
-rwxr-xr-xchallenge-171/duncan-c-white/perl/ch-1.pl62
-rwxr-xr-xchallenge-171/duncan-c-white/perl/ch-2.pl48
6 files changed, 250 insertions, 43 deletions
diff --git a/challenge-171/duncan-c-white/C/.build b/challenge-171/duncan-c-white/C/.cbuild
index 7a8295430e..7171ba4c65 100644
--- a/challenge-171/duncan-c-white/C/.build
+++ b/challenge-171/duncan-c-white/C/.cbuild
@@ -1,2 +1,3 @@
BUILD = ch-1
+CFLAGS = -Wall
#CFLAGS = -g
diff --git a/challenge-171/duncan-c-white/C/Makefile b/challenge-171/duncan-c-white/C/Makefile
new file mode 100644
index 0000000000..e14a397db5
--- /dev/null
+++ b/challenge-171/duncan-c-white/C/Makefile
@@ -0,0 +1,13 @@
+# Makefile rules generated by CB
+CC = gcc
+CFLAGS = -Wall
+BUILD = ch-1
+
+all: $(BUILD)
+
+clean:
+ /bin/rm -f $(BUILD) *.o core a.out
+
+ch-1: ch-1.o
+ch-1.o: ch-1.c
+
diff --git a/challenge-171/duncan-c-white/C/ch-1.c b/challenge-171/duncan-c-white/C/ch-1.c
new file mode 100644
index 0000000000..a102774c93
--- /dev/null
+++ b/challenge-171/duncan-c-white/C/ch-1.c
@@ -0,0 +1,107 @@
+/*
+ * TASK 1 - Odd Abundant Numbers
+ *
+ * GUEST LANGUAGE: THIS IS THE C VERSION OF ch-1.pl;
+ * I took the liberty of merging finding factors and summing them.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+//#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+#include <unistd.h>
+#include <ctype.h>
+
+
+bool debug=false;
+
+
+// int n = process_singlearg( name, argc, argv, def );
+// Process the -d flag, and check that there is a single
+// remaining argument (or return $def if there are no args),
+// return that argument's numeric value.
+int process_singlearg( char *name, int argc, char **argv, int def )
+{
+ int arg=1;
+ if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
+ {
+ debug = true;
+ arg++;
+ }
+
+ int left = argc-arg;
+ if( left == 0 )
+ {
+ return def; // default
+ }
+ if( left != 1 )
+ {
+ fprintf( stderr,
+ "Usage: %s [-d] firstN (default %d)\n", name, def );
+ exit(1);
+ }
+
+ // element is in argv[arg]
+
+ if( debug )
+ {
+ printf( "debug: remaining argument is in arg=%d, firstn=%s\n",
+ arg, argv[arg] );
+ }
+
+ return atoi( argv[arg] );
+}
+
+
+//
+// int sum_f = sum_factors_less_than_n( n );
+// Find all the factors of n
+// including 1 but less than n.
+// Sum them all and return the sum.
+//
+int sum_factors_less_than_n( int n )
+{
+ int sumf = 0;
+ for( int i=1; i<n; i++ )
+ {
+ if( n%i == 0 ) sumf += i;
+ }
+ return sumf;
+}
+
+
+int main( int argc, char **argv )
+{
+ int n = process_singlearg( "odd-abundant-nos", argc, argv, 20 );
+
+ int *oa; // the first n Odd Abundant numbers
+ oa = malloc( n * sizeof(int) );
+ int noa = 0;
+
+ for( int i = 3; noa<n; i+=2 )
+ {
+ int sum = sum_factors_less_than_n( i );
+ if( debug )
+ {
+ printf( "i=%d, sum=%d\n", i, sum );
+ }
+ if( sum > i )
+ {
+ if( debug )
+ {
+ printf( "found odd abdundant no %d\n", i );
+ }
+ oa[noa++] = i;
+ }
+ }
+
+ for( int i=0; i<noa; i++ )
+ {
+ printf( "%d\n", oa[i] );
+ }
+
+ free( oa );
+ return 0;
+}
diff --git a/challenge-171/duncan-c-white/README b/challenge-171/duncan-c-white/README
index c46a4e6435..755694b9fc 100644
--- a/challenge-171/duncan-c-white/README
+++ b/challenge-171/duncan-c-white/README
@@ -1,57 +1,33 @@
-TASK 1: Primorial Numbers
+Task 1: Abundant Number
-Write a script to generate first 10 Primorial Numbers:
-Primorial numbers are those formed by multiplying successive prime numbers.
+Write a script to generate first 20 Abundant Odd Numbers.
-For example,
+According to wikipedia: A number n for which the sum of proper divisors
+(divisors from 1 but less than n) s(n) > n.
-P(0) = 1 (1)
-P(1) = 2 (1x2)
-P(2) = 6 (1x2x3)
-P(3) = 30 (1x2x3x5)
+For example, 945 is the first Abundant Odd Number.
-MY NOTES: ok, sounds easy, but I'm getting very bored of
-"tasks to do with Number Theory and (especially) Primes".
+Sum of divisors:
+1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 105 + 135 + 189 + 315 = 975
+
+MY NOTES: ok, sounds easy, and at least it's nowt to do with Primes:-)
GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl
into C (look in the C directory for that).
-Task 2: Kronecker Product
-
-You are given 2 matrices.
-
-Write a script to implement Kronecker Product on the given 2 matrices.
-
-For more information, please refer to the wikipedia page
-https://en.wikipedia.org/wiki/Kronecker_product
+Task 2: First-class Function
-For example,
+Create sub compose($f, $g) which takes in two parameters $f and $g as
+subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x)
+= $f->($g->($x))
-A = [ 1 2 ]
- [ 3 4 ]
+e.g.
-B = [ 5 6 ]
- [ 7 8 ]
+$f = (one or more parameters function)
+$g = (one or more parameters function)
-A x B = [ 1 x [ 5 6 ] 2 x [ 5 6 ] ]
- [ [ 7 8 ] [ 7 8 ] ]
- [ 3 x [ 5 6 ] 4 x [ 5 6 ] ]
- [ [ 7 8 ] [ 7 8 ] ]
+$h = compose($f, $g)
+$f->($g->($x,$y, ..)) == $h->($x, $y, ..) for any $x, $y, ...
- = [ 1x5 1x6 2x5 2x6 ]
- [ 1x7 1x8 2x7 2x8 ]
- [ 3x5 3x6 4x5 4x6 ]
- [ 3x7 3x8 4x7 4x8 ]
-
- = [ 5 6 10 12 ]
- [ 7 8 14 16 ]
- [ 15 18 20 24 ]
- [ 21 24 28 32 ]
-
-MY NOTES: Ok, at least it has nothing to do with prime numbers:-)
-I note in the Wikipedia page the useful formula:
-(A x B)ij = Ai/p,j/q * Bi%p,j%q [where B is of size p x q]
-
-GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl
-into C (look in the C directory for that).
+MY NOTES: An interesting question at last. Think it's quite easy..
diff --git a/challenge-171/duncan-c-white/perl/ch-1.pl b/challenge-171/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..73a5519592
--- /dev/null
+++ b/challenge-171/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+#
+# Task 1: Abundant Number
+#
+# Write a script to generate first 20 Abundant Odd Numbers.
+#
+# According to wikipedia: A number n for which the sum of proper divisors
+# (divisors from 1 but less than n) s(n) > n.
+#
+# For example, 945 is the first Abundant Odd Number.
+#
+# Sum of divisors:
+# 1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 105 + 135 + 189 + 315 = 975
+#
+# MY NOTES: ok, sounds easy, and at least it's nowt to do with Primes:-)
+#
+# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl
+# into C (look in the C directory for that).
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Data::Dumper;
+use List::Util qw(sum0);
+
+my $debug=0;
+die "Usage: odd-abundant-numbers [--debug] [N default 20]\n"
+ unless GetOptions( "debug"=>\$debug ) && @ARGV<2;
+my $n = shift // 20;
+
+#
+# my @f = factors_less_than_n( $n );
+# Find and return an array of all the factors of $n
+# including 1 but less than $n.
+#
+sub factors_less_than_n
+{
+ my( $n ) = @_;
+ my @f;
+ for( my $i=1; $i<$n; $i++ )
+ {
+ push @f, $i if $n%$i==0;
+ }
+ return @f;
+}
+
+
+my @oa; # the first few Odd Abundant numbers we find
+
+for( my $i = 3; @oa<$n; $i+=2 )
+{
+ my @f = factors_less_than_n( $i );
+ my $sum = sum0(@f);
+ say "i=$i, factors = ". join(',',@f). ", sum=$sum" if $debug;
+ next if $sum <= $i;
+ say "found odd abdundant no $i" if $debug;
+ push @oa, $i;
+}
+
+say for @oa;
diff --git a/challenge-171/duncan-c-white/perl/ch-2.pl b/challenge-171/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..2fcb8c2360
--- /dev/null
+++ b/challenge-171/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+#
+# Task 2: First-class Function
+#
+# Create sub compose($f, $g) which takes in two parameters $f and $g as
+# subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x)
+# = $f->($g->($x))
+#
+# e.g.
+#
+# $f = (one or more parameters function)
+# $g = (one or more parameters function)
+#
+# $h = compose($f, $g)
+# $f->($g->($x,$y, ..)) == $h->($x, $y, ..) for any $x, $y, ...
+#
+# MY NOTES: An interesting question at last. Think it's quite easy..
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Function::Parameters;
+use Data::Dumper;
+use List::Util qw(sum0);
+
+my $debug=0;
+die "Usage: first-class-function [--debug] ARGS FOR FUNCS\n"
+ unless GetOptions( "debug"=>\$debug ) && @ARGV>0;
+
+#
+# my $fg = compose( $f, $g );
+# Higher order function composer. Return a newly minted
+# composed function (subroutine ref) that, when called, applies
+# $f first, then $g.
+#
+fun compose( $f, $g )
+{
+ return fun(@x) { return $f->($g->(@x)); }
+}
+
+
+my $sum = fun( @x ) { return sum0(@x); };
+my $double = fun( @x ) { return map { 2 * $_ } @x };
+my $sumdouble = compose( $sum, $double );
+my $n = $sumdouble->( @ARGV );
+say $n;