TweetFollow Us on Twitter

Feb 00 Challenge

Volume Number: 16 (2000)
Issue Number: 2
Column Tag: Programmer's Challenge

Programmer's Challenge

by Bob Boonstra, Westford, MA

Web apps with Lasso and FileMaker Pro

Latin Squares

A Latin Square of order N is an NxN array of numbers, where each number from 0..N-1 occurs exactly once in each row and in each column. Your Challenge this month is to construct Latin Squares that are minimal, in the sense that each must form the smallest NxN digit base N number, where the digits are read off column by column, one row at a time.

The prototype for the code you should write is:

#if defined(__cplusplus)
extern "C" {
#endif

void LatinSquares(
  short n,	/* dimension of the Latin Square to be generated */
  short *latinSquare	/* set latinSquare[c + r*n] to square value row r, col c */
);

#if defined(__cplusplus)
}
#endif

For example, 

				1234
				2341
				4123
				3412

is a Latin Square with the value 1234234141233412. It is not the minimal Latin Square of order 4, however, since the following Latin Square,

				1234
				2143
				3412
				4321

forms the number 1234214334124321.

The dimension of the squares you must generate will be less than 2**16. The winner will be the solution that correctly computes minimal Latin Squares in the least amount of execution time. Code size and code elegance, in that order, will be the tiebreakers, in the event two solutions are within 1% of one another in execution time. All Latin Squares must be computed at execution time - entries that precompute solutions will be disqualified.

This month's Challenge was suggested by Aaron Montgomery, who earns 2 Challenge points for making the suggestion

This will be a native PowerPC Challenge, using the CodeWarrior Pro 5 environment. Solutions may be coded in C, C++, or Pascal. Solutions in Java will also be accepted, but Java entries must be accompanied by a test driver that uses the interface provided in the problem statement.

Three Months Ago Winner

Congratulations to Tom Saxton for taking first place in the November, 1999, Putting Green Challenge. While I'm not sure that the next Ryder Cup teams will be beating a path to his door for his four-putting green skills, Tom did soundly beat the three other entries.

The Putting Green Challenge required contestants to analyze a sequence of greens described as three-dimensional points connected into adjoining triangles. Each green had a fixed pin position, and several holes were played on each green from varying initial ball positions. Complicating the Challenger's job was the fact that the propagation model was unknown, available only by observing the results of a putt. For each green, the contestants were given several practice holes that could be used to puzzle out the propagation model. Scoring was based on an award of 100 points for each hole successfully completed, minus 10 points for each stroke taken (not counting practice holes), and minus 10 points for each second of execution time (including practice time).

Two of the solutions submitted did not manage to sink a putt for any of the holes played in my test match. One of those solutions simply hit the ball at the hole very hard, trying to take advantage of a possible ambiguity about ball velocity in the problem statement. I heard from enough real golfers who had rimmed the cup a few times that I had to close the velocity loophole. Tom's winning solution is fairly straightforward. He takes 10 practice shots on the first hole of the first green to estimate the drag coefficients. It does not attempt to analyze the contours of the green or to model the effect of gravity. But the drag model is good enough to put the ball in the hole with only one putt on a level green, and with one or two putts on a flat but gently sloping green.

I used three greens in my test cases, a perfectly flat and level green, a flat but gently sloping green, and a green with more varied terrain. Five practice holes were played on each green, five scored holes on each of the first two greens, and 20 holes on the final green. On the interesting terrain of the last hole, Tom's solution completed each hole successfully, taking up to seven putts to complete the hole.

The table below lists, for each of the solutions submitted, the number of holes successfully completed, the total number of strokes taken on nonpractice holes, total execution time, the total score, and the size and language code parameters. As usual, the number in parentheses after the entrant's name is the total number of Challenge points earned in all Challenges prior to this one.

Name Holes Strokes Time Score Code Data Lang
Tom Saxton (138) 30 117 3.148 1829.969 2224 432 C
Brady Duga 10 49 2.435 509.976 1660 380 C
R. S. 0 60 0.42 -600.004 568 93 C++
J. T. 0 210 197.47 -2101.975 2580 255 C

Top Contestants

Listed here are the Top Contestants for the Programmer's Challenge, including everyone who has accumulated 10 or more points during the past two years. The numbers below include points awarded over the 24 most recent contests, including points earned by this month's entrants.

Rank Name Points
1. Munter, Ernst 237
2. Saxton, Tom 146
3. Maurer, Sebastian 67
4. Rieken, Willeke 51
5. Heithcock, JG 43
6. Shearer, Rob 41
7. Boring, Randy 39
8. Brown, Pat 20
9. Hostetter, Mat 20
10. Jones, Dennis 12
11. Hart, Alan 11
12. Duga, Brady 10
13. Hewett, Kevin 10
14. Murphy, ACC 10
14. Selengut, Jared 10
16. Smith, Brad 10
17. Strout, Joe 10
18. Varilly, Patrick 10

There are three ways to earn points: (1) scoring in the top 5 of any Challenge, (2) being the first person to find a bug in a published winning solution or, (3) being the first person to suggest a Challenge that I use. The points you can win are:

1st place 20 points
2nd place 10 points
3rd place 7 points
4th place 4 points
5th place 2 points
finding bug 2 points
suggesting Challenge 2 points

Here is Tom's winning Putting Green solution:

PuttingGreen.c
Copyright © 1999 Tom Saxton

#include "PuttingGreen.h"

#include <stdlib.h>
#include <math.h>

enum
{
	fFalse = 0,
	fTrue = TRUE
};
#define DIM(a) (sizeof(a)/sizeof((a)[0]))

// disable asserts
#define Assert(f)

// vector types and constants
typedef struct Point3DDouble VEC;
static const double epsilon = 1.0e-7;
static const VEC s_normalZ = { 0, 0, 1.0 };
static const VEC s_vecZero;

// this green

static Point3DDouble *s_paposGrid;
static int s_cposGrid;
static MyTriangle *s_patri;
static int s_ctri;
static int s_itriPin;
static Point3DDouble s_posPin;

static int s_cholePractice;			// number of practice putts
static int s_choleScored;				// number of scored putts

// computed physical constants for this green
static int s_fGetDrag = fTrue;
static double mDrag, bDrag;

// current state

static int s_ihole;						// which hole are we on?
static int s_cputt;						// which putt is this
static int s_fPractice;				// is this a practice putt?
static Point3DDouble s_posCur;	// where's the ball?

// the current putt

static double s_svInitial;
static double s_distExpect;

// vector utilities

static void _SubVec(VEC pos1, VEC pos2, VEC *pposResult);
static double _DotVec(VEC vec1, VEC vec2);
static double _LenVec(VEC vec);
static void _ScaleVec(VEC vec, double s, VEC *pvecResult);

// triangle utilities

static void _GetMidTriPos(int itri, Point3DDouble *ppos);

//
// Entry Points
//
#if 0
static void ___Entry_Points(){}
#endif

InitGreen

void InitGreen(
	Point3DDouble paposGrid[],	/* green terrain description */
	long cposGrid,	/* number of points */
	MyTriangle patri[],	/* triangles comprising the green */
	int ctri,	/* number of triangles */
	long itriPin,	/* index in triangles[] of the pin on this green */
	long cholePractice,	/* number of unscored (but timed) holes to 			   practice on this green */
	long choleScored	/* number of holes to be scored on this green */
)

{
	s_paposGrid = paposGrid;
	s_cposGrid = cposGrid;
	s_patri = patri;
	s_ctri = ctri;
	s_itriPin = itriPin;
	
	s_cholePractice = cholePractice;
	s_choleScored = choleScored;
	
	_GetMidTriPos(itriPin, &s_posPin);
	
	s_ihole = 0;
}
	
#pragma warn_unusedarg off

StartHole

void StartHole(	/* called to start play on this hole */
	Point3DDouble posStart, 	/* initial ball position on the green */
	Boolean fPractice	/* TRUE if this hole is practice */
)
{
	++s_ihole;
	
	s_posCur = posStart;
	s_cputt = 0;
	s_fPractice = fPractice;
}

#pragma warn_unusedarg reset

#define cputtPractice 10
#define cputtScore    10

typedef struct SD SD;
struct SD
{
	double speed;
	double dist;
};
static SD s_asd[cputtPractice];
static int s_csd;

MakePutt

Boolean 		/* quit */ MakePutt(
	Velocity2DDouble *pvXY		/* return initial ball velocity in the z==0 plane */
)
{
	double dist;
	Point3DDouble vXY;
		
	if (s_cputt >= (s_fPractice ? cputtPractice : cputtScore))
	{
		if (s_fGetDrag)
		{
			int isd;
			double sumX, sumY, sumXY, sumXX;
			
			Assert(s_csd > 0);

			// do linear regression on the data

			sumX = sumY = sumXY = sumXX = 0;
			for (isd = 0; isd < s_csd; ++isd)
			{
				SD *psd = &s_asd[isd];
				double y = psd->dist;
				double x = fabs(psd->speed);
				
				sumXX += x * x;
				sumXY += x * y;
				sumX += x;
				sumY += y;
			}
			
			mDrag = (sumX*sumY - s_csd*sumXY)/(sumX*sumX - s_csd*sumXX);
			bDrag = (sumY - mDrag*sumX)/s_csd;
			
			Assert(0.0 <= mDrag && mDrag <= 5.0);
			Assert(-5.0 <= bDrag && bDrag <= 5.0);

			s_fGetDrag = fFalse;
		}
		
		// give up

		return fTrue;
	}
	
	// just give up after we've finished with practice shots

	if (s_fPractice && !s_fGetDrag)
		return fTrue;

	_SubVec(s_posPin, s_posCur, &vXY);
	vXY.z = 0;
	dist = _LenVec(vXY);
	
	if (s_fGetDrag)
	{
		s_svInitial = 3.0*(1.0 + s_cputt/2);
		if (s_cputt % 2)
			s_svInitial = -s_svInitial;
	}
	else
	{
		s_svInitial = (dist + 0.03 - bDrag) / mDrag;
		s_distExpect = dist;
	}
	_ScaleVec(vXY, s_svInitial/dist, &vXY);
	
	pvXY->x = vXY.x;
	pvXY->y = vXY.y;

	return fFalse; // keep going
}

BallMovement

void BallMovement(
	BallPosition papos4[],	/* provides ball movement in response to MakePutt */
	int cpos,	/* number of ball positions provided */
	Boolean fInHole	/* true if the ball went into the hole */
)
{
	Point3DDouble posNew;
	
	posNew = papos4[cpos-1].pt;
	
	if (!fInHole)
	{
		double dist;
		Point3DDouble vecMove;
			
		_SubVec(posNew, s_posCur, &vecMove);
		dist = _LenVec(vecMove);

		if (s_fGetDrag)
		{
			Assert(0 <= s_csd && s_csd < DIM(s_asd));
			s_asd[s_csd].speed = fabs(s_svInitial);
			s_asd[s_csd].dist = dist;
			++s_csd;
		}
	}

	s_posCur = posNew;
		
	++s_cputt;
}

//
// Vector Functions
//

#if 0
static void ___Vector_Functions(){}
#endif

_SubVec

static void _SubVec(VEC pos1, VEC pos2, VEC *pposResult)
{
	pposResult->x = pos1.x - pos2.x;
	pposResult->y = pos1.y - pos2.y;
	pposResult->z = pos1.z - pos2.z;
}

_DotVec

static double _DotVec(VEC vec1, VEC vec2)
{
	return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
}

_LenVec

static double _LenVec(VEC vec)
{
	return sqrt(_DotVec(vec, vec));
}
_ScaleVec

static void _ScaleVec(VEC vec, double s, VEC *pvecResult)
{
	pvecResult->x = s * vec.x;
	pvecResult->y = s * vec.y;
	pvecResult->z = s * vec.z;

}

_GetMidTriPos

static void _GetMidTriPos(int itri, Point3DDouble *pposMid)
{
	MyTriangle tri;
	int iipos;
	
	Assert(0 <= itri && itri < s_ctri);
	tri = s_patri[itri];
	
	*pposMid = s_vecZero;
	
	for (iipos = 0; iipos < DIM(tri.pointIndices); ++iipos)
	{
		Point3DDouble *ppos;
		
		Assert(0 <= tri.pointIndices[iipos] && 
										tri.pointIndices[iipos] < s_cposGrid);
		ppos = &s_paposGrid[tri.pointIndices[iipos]];
		
		pposMid->x += ppos->x;
		pposMid->y += ppos->y;
		pposMid->z += ppos->z;
	}
	
	pposMid->x *= 1.0/3.0;
	pposMid->y *= 1.0/3.0;
	pposMid->z *= 1.0/3.0;
}
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »

Price Scanner via MacPrices.net

You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more

Jobs Board

Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.