aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordcw <d.white@imperial.ac.uk>2023-01-08 23:44:13 +0000
committerdcw <d.white@imperial.ac.uk>2023-01-08 23:44:13 +0000
commit5de32402004ca70b47052d0253d2b72f091e3e30 (patch)
tree513cf958c6cb4d58878adc30b26928e0f8f33c9a
parent7df9192d09fb0a838dda2bb6a1a4647173f9b062 (diff)
downloadperlweeklychallenge-club-5de32402004ca70b47052d0253d2b72f091e3e30.tar.gz
perlweeklychallenge-club-5de32402004ca70b47052d0253d2b72f091e3e30.tar.bz2
perlweeklychallenge-club-5de32402004ca70b47052d0253d2b72f091e3e30.zip
committed my solutions to this week's challenge, in Perl and C. Skipped last week's as a NY break, but will do them soon..
-rw-r--r--challenge-198/duncan-c-white/C/.cbuild2
-rw-r--r--challenge-198/duncan-c-white/C/Makefile18
-rw-r--r--challenge-198/duncan-c-white/C/README10
-rw-r--r--challenge-198/duncan-c-white/C/args.c207
-rw-r--r--challenge-198/duncan-c-white/C/args.h11
-rw-r--r--challenge-198/duncan-c-white/C/ch-1.c108
-rw-r--r--challenge-198/duncan-c-white/C/ch-2.c51
-rw-r--r--challenge-198/duncan-c-white/C/parseints.c114
-rw-r--r--challenge-198/duncan-c-white/C/parseints.h1
-rw-r--r--challenge-198/duncan-c-white/C/primes.c67
-rw-r--r--challenge-198/duncan-c-white/C/primes.h1
-rw-r--r--challenge-198/duncan-c-white/C/printarray.c39
-rw-r--r--challenge-198/duncan-c-white/C/printarray.h1
-rw-r--r--challenge-198/duncan-c-white/README61
-rw-r--r--challenge-198/duncan-c-white/perl/MakePrimes.pm90
-rwxr-xr-xchallenge-198/duncan-c-white/perl/ch-1.pl79
-rwxr-xr-xchallenge-198/duncan-c-white/perl/ch-2.pl52
17 files changed, 878 insertions, 34 deletions
diff --git a/challenge-198/duncan-c-white/C/.cbuild b/challenge-198/duncan-c-white/C/.cbuild
index a14ec76520..fd6334a425 100644
--- a/challenge-198/duncan-c-white/C/.cbuild
+++ b/challenge-198/duncan-c-white/C/.cbuild
@@ -1,4 +1,4 @@
BUILD = ch-1 ch-2
CFLAGS = -Wall -g
-#LDFLAGS = -lm
+LDFLAGS = -lm
#CFLAGS = -g
diff --git a/challenge-198/duncan-c-white/C/Makefile b/challenge-198/duncan-c-white/C/Makefile
new file mode 100644
index 0000000000..1b34ccd3b2
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/Makefile
@@ -0,0 +1,18 @@
+# Makefile rules generated by CB
+CC = gcc
+CFLAGS = -Wall -g
+BUILD = ch-1 ch-2
+
+all: $(BUILD)
+
+clean:
+ /bin/rm -f $(BUILD) *.o core a.out
+
+args.o: args.c
+ch-1: ch-1.o args.o parseints.o printarray.o
+ch-1.o: ch-1.c args.h parseints.h printarray.h
+ch-2: ch-2.o args.o parseints.o printarray.o
+ch-2.o: ch-2.c args.h parseints.h printarray.h
+parseints.o: parseints.c args.h parseints.h printarray.h
+printarray.o: printarray.c
+
diff --git a/challenge-198/duncan-c-white/C/README b/challenge-198/duncan-c-white/C/README
new file mode 100644
index 0000000000..4568a10359
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/README
@@ -0,0 +1,10 @@
+Thought I'd also have a go at translating ch-1.pl and ch-2.pl into C..
+
+Both C versions produce near-identical (non-debugging and even debugging)
+output to the Perl originals.
+
+They use several of my regular support modules:
+- a command-line argument processing module args.[ch],
+- a csvlist-of-int parsing module parseints.[ch], and
+- an int-array printing module printarray.[ch].
+- a prime number generator module primes.[ch]
diff --git a/challenge-198/duncan-c-white/C/args.c b/challenge-198/duncan-c-white/C/args.c
new file mode 100644
index 0000000000..d4a2d38b9a
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/args.c
@@ -0,0 +1,207 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+
+bool debug = false;
+
+
+// process_flag_noarg( name, argc, argv );
+// Process the -d flag, and check that there are no
+// remaining arguments.
+void process_flag_noarg( char *name, int argc, char **argv )
+{
+ int arg=1;
+ if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
+ {
+ debug = true;
+ arg++;
+ }
+
+ int left = argc-arg;
+ if( left != 0 )
+ {
+ fprintf( stderr, "Usage: %s [-d]\n", name );
+ exit(1);
+ }
+}
+
+
+// int argno = process_flag_n_args( name, argc, argv, n, argmsg );
+// Process the -d flag, and check that there are exactly
+// n remaining arguments, return the index position of the first
+// argument. If not, generate a fatal Usage error using the argmsg.
+//
+int process_flag_n_args( char *name, int argc, char **argv, int n, char *argmsg )
+{
+ int arg=1;
+ if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
+ {
+ debug = true;
+ arg++;
+ }
+
+ int left = argc-arg;
+ if( left != n )
+ {
+ fprintf( stderr, "Usage: %s [-d] %s\n Exactly %d "
+ "arguments needed\n", name, argmsg, n );
+ exit(1);
+ }
+ return arg;
+}
+
+
+// int argno = process_flag_n_m_args( name, argc, argv, min, max, argmsg );
+// Process the -d flag, and check that there are between
+// min and max remaining arguments, return the index position of the first
+// argument. If not, generate a fatal Usage error using the argmsg.
+//
+int process_flag_n_m_args( char *name, int argc, char **argv, int min, int max, char *argmsg )
+{
+ int arg=1;
+ if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
+ {
+ debug = true;
+ arg++;
+ }
+
+ int left = argc-arg;
+ if( left < min || left > max )
+ {
+ fprintf( stderr, "Usage: %s [-d] %s\n Between %d and %d "
+ "arguments needed\n", name, argmsg, min, max );
+ exit(1);
+ }
+ return arg;
+}
+
+
+// process_onenumarg_default( name, argc, argv, defvalue, &n );
+// Process the -d flag, and check that there is a single
+// remaining numeric argument (or no arguments, in which case
+// we use the defvalue), putting it into n
+void process_onenumarg_default( char *name, int argc, char **argv, int defvalue, int *n )
+{
+ char argmsg[100];
+ sprintf( argmsg, "[int default %d]", defvalue );
+ int arg = process_flag_n_m_args( name, argc, argv, 0, 1, argmsg );
+
+ *n = arg == argc ? defvalue : atoi( argv[arg] );
+}
+
+
+// process_onenumarg( name, argc, argv, &n );
+// Process the -d flag, and check that there is a single
+// remaining numeric argument, putting it into n
+void process_onenumarg( char *name, int argc, char **argv, int *n )
+{
+ int arg = process_flag_n_args( name, argc, argv, 1, "int" );
+
+ // argument is in argv[arg]
+ *n = atoi( argv[arg] );
+}
+
+
+// process_twonumargs( name, argc, argv, &m, &n );
+// Process the -d flag, and check that there are 2
+// remaining numeric arguments, putting them into m and n
+void process_twonumargs( char *name, int argc, char **argv, int *m, int *n )
+{
+ int arg = process_flag_n_args( name, argc, argv, 2, "int" );
+
+ // arguments are in argv[arg] and argv[arg+1]
+ *m = atoi( argv[arg++] );
+ *n = atoi( argv[arg] );
+}
+
+
+// process_twostrargs() IS DEPRECATED: use process_flag_n_m_args() instead
+
+
+// int arr[100];
+// int nel = process_listnumargs( name, argc, argv, arr, 100 );
+// Process the -d flag, and check that there are >= 2
+// remaining numeric arguments, putting them into arr[0..nel-1]
+// and returning nel.
+int process_listnumargs( char *name, int argc, char **argv, int *arr, int maxel )
+{
+ int arg=1;
+ if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
+ {
+ debug = true;
+ arg++;
+ }
+
+ int left = argc-arg;
+ if( left < 2 )
+ {
+ fprintf( stderr, "Usage: %s [-d] list_of_numeric_args\n", name );
+ exit(1);
+ }
+ if( left > maxel )
+ {
+ fprintf( stderr, "%s: more than %d args\n", name, maxel );
+ exit(1);
+ }
+
+ // elements are in argv[arg], argv[arg+1]...
+
+ if( debug )
+ {
+ printf( "debug: remaining arguments are in arg=%d, "
+ "firstn=%s, secondn=%s..\n",
+ arg, argv[arg], argv[arg+1] );
+ }
+
+ int nel = 0;
+ for( int i=arg; i<argc; i++ )
+ {
+ arr[nel++] = atoi( argv[i] );
+ }
+ arr[nel] = -1;
+ return nel;
+}
+
+
+//
+// bool isint = check_unsigned_int( char *val, int *n );
+// Given an string val, check that there's an unsigned integer
+// in it (after optional whitespace). If there is a valid
+// unsigned integer value, store that integer value in *n and
+// return true; otherwise return false (and don't alter *n).
+bool check_unsigned_int( char *val, int *n )
+{
+ // skip whitespace in val
+ char *p;
+ for( p=val; isspace(*p); p++ )
+ {
+ /*EMPTY*/
+ }
+ if( ! isdigit(*p) ) return false;
+ *n = atoi(p);
+ return true;
+}
+
+
+//
+// bool ok = check_unsigned_real( char *val, double *n );
+// Given an string val, check that there's an unsigned real
+// in it (after optional whitespace). If there is a valid
+// unsigned real value, store that value in *n and
+// return true; otherwise return false (and don't alter *n).
+bool check_unsigned_real( char *val, double *n )
+{
+ // skip whitespace in val
+ char *p;
+ for( p=val; isspace(*p); p++ )
+ {
+ /*EMPTY*/
+ }
+ if( ! isdigit(*p) ) return false;
+ *n = atof(p);
+ return true;
+}
diff --git a/challenge-198/duncan-c-white/C/args.h b/challenge-198/duncan-c-white/C/args.h
new file mode 100644
index 0000000000..8844a8f9c4
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/args.h
@@ -0,0 +1,11 @@
+extern bool debug;
+
+extern void process_flag_noarg( char * name, int argc, char ** argv );
+extern int process_flag_n_args( char * name, int argc, char ** argv, int n, char * argmsg );
+extern int process_flag_n_m_args( char * name, int argc, char ** argv, int min, int max, char * argmsg );
+extern void process_onenumarg_default( char * name, int argc, char ** argv, int defvalue, int * n );
+extern void process_onenumarg( char * name, int argc, char ** argv, int * n );
+extern void process_twonumargs( char * name, int argc, char ** argv, int * m, int * n );
+extern int process_listnumargs( char * name, int argc, char ** argv, int * arr, int maxel );
+extern bool check_unsigned_int( char * val, int * n );
+extern bool check_unsigned_real( char * val, double * n );
diff --git a/challenge-198/duncan-c-white/C/ch-1.c b/challenge-198/duncan-c-white/C/ch-1.c
new file mode 100644
index 0000000000..3624d3d242
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/ch-1.c
@@ -0,0 +1,108 @@
+//
+// Task 1: Max Gap
+//
+// C version.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "args.h"
+#include "parseints.h"
+#include "printarray.h"
+
+
+static int int_compare( const void *a, const void *b )
+{
+ return (*(int *)a) - (*(int *)b);
+}
+
+
+typedef char shortstr[20];
+
+
+//
+// int maxgap;
+// char *pairsstr;
+// int npairs = find_max_gap( list, nel, &maxgap, &pairsstr );
+// Find the maximum gap (maxgap) between any 2 elements in the
+// sorted list (list, nel elements), and pairsstr (a dynamically
+// allocted CSV of "el-el" strings) of elements with that maxgap. Return
+// the number of such pairs found
+//
+int find_max_gap( int *list, int nel, int *maxgap, char **pairsstr )
+{
+ *maxgap = 0;
+ int npairs = 0;
+ char *p = *pairsstr = malloc( nel * 10 * sizeof(char) );
+ assert( p != NULL );
+ for( int i=0; i<nel; i++ )
+ {
+ int gap = list[i+1] - list[i];
+ if( debug )
+ {
+ printf( "debug %d: %d..%d, gap=%d\n",
+ i, list[i], list[i+1], gap );
+ }
+ if( gap > *maxgap )
+ {
+ *maxgap = gap;
+ sprintf( p, "%d-%d", list[i], list[i+1] );
+ npairs = 1;
+ } else if( gap == *maxgap )
+ {
+ char *end = p + strlen(p);
+ sprintf( end, ",%d-%d", list[i], list[i+1] );
+ npairs++;
+ }
+ }
+ return npairs;
+}
+
+
+int main( int argc, char **argv )
+{
+ int argno = process_flag_n_m_args( "max-gap", argc, argv,
+ 1, 1000, "intlist" );
+
+ int nel;
+ int *list = parse_int_args( argc, argv, argno, &nel );
+
+ if( nel < 1 )
+ {
+ fprintf( stderr, "max-gap: need a list of > 0 elements\n" );
+ exit(1);
+ }
+
+
+ qsort( list, nel, sizeof(int), &int_compare );
+
+ if( debug )
+ {
+ printf( "debug: initial sorted list: " );
+ print_int_array( 60, nel, list, ',', stdout );
+ putchar( '\n' );
+ }
+
+ int maxgap;
+ char *pairsstr;
+ int npairs = find_max_gap( list, nel, &maxgap, &pairsstr );
+
+ if( debug )
+ {
+ printf( "debug: maxgap=%d, npairs=%d, pairstr=%s\n",
+ maxgap, npairs, pairsstr );
+ } else
+ {
+ printf( "%d\n", npairs );
+ }
+
+ free( pairsstr );
+ free( list );
+
+ return 0;
+}
diff --git a/challenge-198/duncan-c-white/C/ch-2.c b/challenge-198/duncan-c-white/C/ch-2.c
new file mode 100644
index 0000000000..89591338c2
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/ch-2.c
@@ -0,0 +1,51 @@
+//
+// Task 2: Prime Count
+//
+// C version.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "args.h"
+#include "parseints.h"
+#include "printarray.h"
+#include "primes.h"
+
+
+int main( int argc, char **argv )
+{
+ int argno = process_flag_n_args( "prime-count", argc, argv, 1, "" );
+ char *arg = argv[argno];
+ int n = atoi(arg);
+
+ if( debug )
+ {
+ printf( "debug: arg=%s, n=%d\n", arg, n );
+ }
+
+ if( n < 1 )
+ {
+ fprintf( stderr, "prime-count: n %d must be > 0\n", n );
+ exit(1);
+ }
+
+ int numprimes;
+ int *primes = primes_upto(n-1,&numprimes);
+
+ if( debug )
+ {
+ printf( "debug: there are %d primes < %d: \n", numprimes, n );
+ print_int_array( 60, numprimes, primes, ',', stdout );
+ putchar( '\n' );
+ } else
+ {
+ printf( "%d\n", numprimes );
+ }
+
+ return 0;
+}
diff --git a/challenge-198/duncan-c-white/C/parseints.c b/challenge-198/duncan-c-white/C/parseints.c
new file mode 100644
index 0000000000..0fb9985633
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/parseints.c
@@ -0,0 +1,114 @@
+// Simple routine to parse one or more arguments,
+// looking for individual +ints or comma-separated
+// lists of +ints.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "args.h"
+#include "printarray.h"
+#include "parseints.h"
+
+typedef struct
+{
+ int nel; // current number of elements
+ int maxel; // maximum number of elements allocated
+ int *list; // malloc()d list of integers
+} intlist;
+
+
+//
+// intlist il.. then initialize il.. then:
+// add_one( element, &il );
+//
+static void add_one( int x, intlist *p )
+{
+ if( p->nel > p->maxel )
+ {
+ p->maxel += 128;
+ p->list = realloc( p->list, p->maxel );
+ assert( p->list );
+ }
+ #if 0
+ if( debug )
+ {
+ printf( "PIA: appending %d to result at "
+ "pos %d\n", x, p->nel );
+ }
+ #endif
+ p->list[p->nel++] = x;
+}
+
+
+//
+// intlist il.. then initialize il.. then:
+// add_one_arg( argstr, &il );
+//
+static void add_one_arg( char *argstr, intlist *p )
+{
+ int x;
+ if( !check_unsigned_int(argstr,&x) )
+ {
+ fprintf( stderr, "PIA: arg %s must be +int\n", argstr );
+ exit(1);
+ }
+ add_one( x, p );
+}
+
+
+//
+// int nel;
+// int *ilist = parse_int_args( argc, argv, argno, &nel );
+// process all arguments argv[argno..argc-1], extracting either
+// single ints or comma-separated lists of ints from those arguments,
+// accumulate all integers in a dynarray list, storing the total number
+// of elements in nel. This list must be freed by the caller.
+// Note that the list of elements used to be terminated by a -1 value,
+// but I've commented this out from now on.
+//
+int *parse_int_args( int argc, char **argv, int argno, int *nel )
+{
+ int *result = malloc( 128 * sizeof(int) );
+ assert( result );
+ intlist il = { 0, 128, result };
+
+ #if 0
+ if( debug )
+ {
+ printf( "PIA: parsing ints from args %d..%d\n", argno, argc-1 );
+ }
+ #endif
+ for( int i=argno; i<argc; i++ )
+ {
+ assert( strlen(argv[i]) < 1024 );
+ char copy[1024];
+ strcpy( copy, argv[i] );
+ char *com;
+ char *s;
+ for( s=copy; (com = strchr(s,',')) != NULL; s=com+1 )
+ {
+ *com = '\0';
+ add_one_arg( s, &il );
+ }
+ add_one_arg( s, &il );
+ }
+
+ //add_one( -1, &il );
+
+ #if 0
+ if( debug )
+ {
+ printf( "PIA: final list is " );
+ print_int_array( 80, il.nel, il.list, ',', stdout );
+ putchar( '\n' );
+ }
+ #endif
+
+ *nel = il.nel;
+ return il.list;
+}
diff --git a/challenge-198/duncan-c-white/C/parseints.h b/challenge-198/duncan-c-white/C/parseints.h
new file mode 100644
index 0000000000..da5e145a86
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/parseints.h
@@ -0,0 +1 @@
+extern int * parse_int_args( int argc, char ** argv, int argno, int * nel );
diff --git a/challenge-198/duncan-c-white/C/primes.c b/challenge-198/duncan-c-white/C/primes.c
new file mode 100644
index 0000000000..9e9d0ba0fb
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/primes.c
@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <math.h>
+
+
+// int *primes = primes_upto(n,&numprimes);
+// Generate a dynamic array of integers containing
+// all primes up to $n. The array is zero-terminated.
+// We also set numprimes to the number of primes found.
+// The caller should free(primes) when they've finished
+// using it.
+//
+int * primes_upto( int n, int *numprimep )
+{
+ bool *isprime = (bool *) malloc( (n+1) * sizeof(bool) );
+ assert( isprime != NULL );
+
+ int i;
+ for( i=1; i<=n; i++ )
+ {
+ isprime[i] = 1; // initially
+ }
+
+ int upper = (int)(sqrt((double)(n)));
+ //printf( "debug: n=%d, upper=%d\n", n, upper );
+ for( i=2; i<=upper; i++ )
+ {
+ if( isprime[i] )
+ {
+ //printf( "debug: crossing out multiples of %d\n", i );
+ int j;
+ for( j=i*i; j<=n; j+=i )
+ {
+ isprime[j] = 0;
+ }
+ }
+ }
+
+ // count how many primes there are
+ int np = 0;
+ for( i=2; i<=n; i++ )
+ {
+ if( isprime[i] )
+ {
+ np++;
+ }
+ }
+ *numprimep = np;
+
+ // dynamically allocate an array of np+1 ints, and fill it in
+ int *primes = malloc( (np+1) * sizeof(int) );
+ int p = 0;
+ for( i=2; i<=n; i++ )
+ {
+ if( isprime[i] )
+ {
+ primes[p++] = i;
+ }
+ }
+ primes[p] = 0;
+
+ free( isprime );
+
+ return primes;
+}
diff --git a/challenge-198/duncan-c-white/C/primes.h b/challenge-198/duncan-c-white/C/primes.h
new file mode 100644
index 0000000000..d9fd437aa7
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/primes.h
@@ -0,0 +1 @@
+extern int * primes_upto( int n, int * numprimep );
diff --git a/challenge-198/duncan-c-white/C/printarray.c b/challenge-198/duncan-c-white/C/printarray.c
new file mode 100644
index 0000000000..ddee597df3
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/printarray.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <string.h>
+
+
+// print_int_array( maxw, nelements, results[], sep, outfile );
+// format results[0..nelements-1] as a <sep> separated
+// list onto outfile with lines <= maxw chars long.
+// produces a whole number of lines of output - without the trailing '\n'
+void print_int_array( int maxw, int nel, int *results, char sep, FILE *out )
+{
+ int linelen = 0;
+ for( int i=0; i<nel; i++ )
+ {
+ char buf[100];
+ sprintf( buf, "%d", results[i] );
+ int len = strlen(buf);
+ if( linelen + len + 2 > maxw )
+ {
+ fputc( '\n', out );
+ linelen = 0;
+ } else if( i>0 )
+ {
+ fputc( ' ', out );
+ linelen++;
+ }
+
+ linelen += len;
+ fprintf( out, "%s", buf );
+ if( i<nel-1 )
+ {
+ fputc( sep, out );
+ linelen++;
+ }
+ }
+ //if( linelen>0 )
+ //{
+ // fputc( '\n', out );
+ //}
+}
diff --git a/challenge-198/duncan-c-white/C/printarray.h b/challenge-198/duncan-c-white/C/printarray.h
new file mode 100644
index 0000000000..40efb83277
--- /dev/null
+++ b/challenge-198/duncan-c-white/C/printarray.h
@@ -0,0 +1 @@
+extern void print_int_array( int maxw, int nel, int * results, char sep, FILE * out );
diff --git a/challenge-198/duncan-c-white/README b/challenge-198/duncan-c-white/README
index bce30687f2..dffa7605cf 100644
--- a/challenge-198/duncan-c-white/README
+++ b/challenge-198/duncan-c-white/README
@@ -1,63 +1,58 @@
-Task 1: Pattern 132
+Task 1: Max Gap
You are given a list of integers, @list.
-Write a script to find out subsequence that respect Pattern 132. Return
-empty array if none found.
-
-Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k
-and a[i] < a[k] < a[j].
+Write a script to find the total pairs in the sorted list where 2
+consecutive elements has the max gap. If the list contains less then 2
+elements then return 0.
Example 1
-Input: @list = (3, 1, 4, 2)
-Output: (1, 4, 2) respect the Pattern 132.
-
-Example 2
+Input: @list = (2,5,8,1)
+Output: 2
-Input: @list = (1, 2, 3, 4)
-Output: () since no subsequence can be found.
+Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8)
-Example 3
+Example 2
-Input: @list = (1, 3, 2, 4, 6, 5)
-Output: (1, 3, 2) if more than one subsequence found then return the first.
+Input: @list = (3)
+Output: 0
-Example 4
-Input: @list = (1, 3, 4, 2)
-Output: (1, 3, 2)
-
-MY NOTES: very easy; straightforward "non-clever" code.
+MY NOTES: very easy. sort, then sequence through the sorted list,
+finding the max gap so far and all pairs with that gap.
GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl
into C (look in the C directory for the translation)
-Task 2: Range List
-
-You are given a sorted unique integer array, @array.
+Task 2: Prime Count
-Write a script to find all possible Number Range i.e [x, y] represent
-range all integers from x and y (both inclusive). Each subsequence
-must be of two or more contiguous integers.
+You are given an integer $n > 0.
+Write a script to print the count of primes less than $n.
Example 1
-Input: @array = (1,3,4,5,7)
-Output: [3,5]
+Input: $n = 10
+Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
Example 2
-Input: @array = (1,2,3,6,7,9)
-Output: [1,3], [6,7]
+Input: $n = 15
+Output: 6
Example 3
-Input: @array = (0,1,2,4,5,6,8,9)
-Output: [0,2], [4,6], [8,9]
+Input: $n = 1
+Output: 0
+
+Example 4
+
+Input: $n = 25
+Output: 9
-MY NOTES: simple enough state machine while we walk over the array in 1-pass.
+MY NOTES: very easy, specially if you have a prime finding module lying
+around:-)
GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl
into C (look in the C directory for the translation)
diff --git a/challenge-198/duncan-c-white/perl/MakePrimes.pm b/challenge-198/duncan-c-white/perl/MakePrimes.pm
new file mode 100644
index 0000000000..e268fa06a3
--- /dev/null
+++ b/challenge-198/duncan-c-white/perl/MakePrimes.pm
@@ -0,0 +1,90 @@
+#
+# mkprimes module (converted from mkprimes.c)
+#
+
+use strict;
+use warnings;
+use Function::Parameters;
+
+my $debug = 0;
+my @foundprimes; # remember all primes we've found..
+
+
+#
+# my @primes = primes_upto( $n );
+# Find all primes up to N; return a list of all such primes
+# (note that 1 is not usually considered a prime)
+#
+fun primes_upto( $n )
+{
+ my @isprime;
+
+ for( my $i=1; $i<=$n; $i++ )
+ {
+ $isprime[$i] = 1; # initially
+ }
+
+ # now sieve the non-primes out..
+ my $upper = int(sqrt($n));
+ printf( "debug: n=%d, upper=%d\n", $n, $upper ) if $debug;
+ for( my $i=2; $i<=$upper; $i++ )
+ {
+ if( $isprime[$i] )
+ {
+ #printf( "debug: crossing out multiples of %d\n", $i );
+ for( my $j=$i*$i; $j<=$n; $j+=$i )
+ {
+ $isprime[$j] = 0;
+ }
+ }
+ }
+
+ # after sieving, extract the primes
+ my @primes = grep { $isprime[$_] } 2..$n;
+
+ # remember them
+ @foundprimes = @primes;
+
+ return @primes;
+}
+
+
+#
+# my @moreprimes = more_primes( $n, $m );
+# Need more primes! Have @foundprimes up to $n, but need
+# to sieve primes from $n+1..$m, so re-sieve, return
+# a list of all new primes (in the range $n+1..$m) that we find.
+#
+fun more_primes( $n, $m )
+{
+ my %isprime;
+
+ print "finding more primes from ", $n+1, "..$m\n";
+
+ for( my $i=$n+1; $i<=$m; $i++ )
+ {
+ $isprime{$i} = 1; # pre-sieving
+ }
+
+ # now sieve the non-primes out..
+ foreach my $prime (@foundprimes)
+ {
+ # find first multiple of $prime > $n
+ my $mult = $prime * (int($n/$prime)+1);
+
+ #print "debug: xo multiples of $prime from $mult to $m\n";
+
+ for( my $j=$mult; $j<=$m; $j+=$prime )
+ {
+ delete $isprime{$j};
+ }
+ }
+
+ # after sieving, extract the primes
+ my @primes = grep { $isprime{$_} } $n+1..$m;
+ push @foundprimes, @primes;
+ return @primes;
+}
+
+
+1;
diff --git a/challenge-198/duncan-c-white/perl/ch-1.pl b/challenge-198/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..cdcd94f4ec
--- /dev/null
+++ b/challenge-198/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+#
+# Task 1: Max Gap
+#
+# You are given a list of integers, @list.
+#
+# Write a script to find the total pairs in the sorted list where 2
+# consecutive elements has the max gap. If the list contains less then 2
+# elements then return 0.
+#
+# Example 1
+#
+# Input: @list = (2,5,8,1)
+# Output: 2
+#
+# Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8)
+#
+# Example 2
+#
+# Input: @list = (3)
+# Output: 0
+#
+# MY NOTES: very easy. sort, then sequence through the sorted list,
+# finding the max gap so far and all pairs with that gap.
+#
+# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl
+# into C (look in the C directory for the translation)
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Data::Dumper;
+
+my $debug=0;
+die "Usage: max-gap [--debug] intlist\n"
+ unless GetOptions( "debug"=>\$debug ) && @ARGV>0;
+
+=pod
+
+=head2 my( $maxgap, @pairs ) = find_max_gap( @list );
+
+Find the maximum gap ($maxgap) between any 2 elements in the sorted list
+@list, and all pairs (@pairs, array of "el-el" string pairs) of elements
+with that $maxgap.
+
+=cut
+sub find_max_gap
+{
+ my( @list ) = @_;
+ my $maxgap = 0;
+ my @pairs = ();
+ foreach my $i (0..$#list-1)
+ {
+ my $gap = $list[$i+1] - $list[$i];
+ say "$i: $list[$i]..$list[$i+1], gap=$gap" if $debug;
+ if( $gap > $maxgap )
+ {
+ $maxgap = $gap;
+ @pairs = ( "$list[$i]-$list[$i+1]" );
+ } elsif( $gap == $maxgap )
+ {
+ push @pairs, "$list[$i]-$list[$i+1]";
+ }
+ }
+ return ( $maxgap, @pairs );
+}
+
+
+my @list = split( /,/, join(',',@ARGV) );
+
+die "max-gaps: need at least 2 ints in list\n" unless @list>1;
+
+@list = sort { $a <=> $b } @list;
+
+my( $maxgap, @pairs ) = find_max_gap( @list );
+say "maxgap: $maxgap, #pairs: ". scalar(@pairs). ", pairs: ( ". join( ', ', @pairs ) . ' )' if $debug;
+say scalar(@pairs);
diff --git a/challenge-198/duncan-c-white/perl/ch-2.pl b/challenge-198/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..76bb8cc543
--- /dev/null
+++ b/challenge-198/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+#
+# Task 2: Prime Count
+#
+# You are given an integer $n > 0.
+#
+# Write a script to print the count of primes less than $n.
+# Example 1
+#
+# Input: $n = 10
+# Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
+#
+# Example 2
+#
+# Input: $n = 15
+# Output: 6
+#
+# Example 3
+#
+# Input: $n = 1
+# Output: 0
+#
+# Example 4
+#
+# Input: $n = 25
+# Output: 9
+#
+# MY NOTES: very easy, specially if you have a prime finding module lying
+# around:-)
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Function::Parameters;
+use Data::Dumper;
+
+use lib qw(.);
+use MakePrimes;
+
+my $debug=0;
+die "Usage: prime-count [--debug] N\n"
+ unless GetOptions( "debug"=>\$debug ) && @ARGV==1;
+
+my $n = shift;
+
+die "prime-count: N must be > 0\n" if $n < 1;
+
+my @primes = primes_upto( $n-1 );
+
+say scalar @primes;