Post Reply 
Interesting little loop optimization exercise
05-14-2015, 08:27 PM (This post was last modified: 05-14-2015 08:37 PM by Dave Britten.)
Post: #1
Interesting little loop optimization exercise
I stumbled across this earlier during some downtime:

http://i.imgur.com/E1kRdcv.jpg

Transcribed:

Quote:Liam has five coins.

Three of the coins add up to 30p.

Three of the coins add up to 40p.

All five coins add up to £1.

What are the coins that Liam has?

UK coins: 1p, 2p, 5p, 10p, 20p, 50p, £1, £2.

I came up with a valid solution pretty quickly messing around with some scratch paper, but wondered if there were more than one solution.

I did a relatively straight-forward GW-BASIC program on my 200LX, which has FOR loops nested 8 deep. Thanks to a few optimizations, it runs to completion in just 4.5 seconds. The C# version on my desktop is effectively instantaneous. I'll probably throw together a 48 version eventually. What interesting approaches can you guys come up with?

EDIT: Got the GW-BASIC version down to 3.2 sec. I imagine this should be pretty doable on anything that makes indirect addressing easy enough.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Interesting little loop optimization exercise - Dave Britten - 05-14-2015 08:27 PM



User(s) browsing this thread: 1 Guest(s)