Showing posts with label basic. Show all posts
Showing posts with label basic. Show all posts

Saturday, 20 May 2017

ZX Spectrum BASIC Challenges

Recently I've entered a few of the programming challenges in the BASIC on the ZX Spectrum group on Facebook. They vary in difficultly but it's possible to write a program for most in under 30 minutes. If you're looking for a quick challenge or the opportunity to improve you BASIC, why not take a look.

Here are some examples of the challenges:

Japanese Pattern

Uwe Geiken asked us to recreate an intricate Japanese pattern. By mirroring, rotating and repeating a 4 line candy cane shape I squeezed this into 156 bytes.



Earth/Venus Orbits

David Saphier challenged us to write the fastest code to display the Earth and Venus orbit pattern in BASIC. Being a bit of a rebel I aimed to write the shortest code and managed to completely botch it before coming up with this working version:



Greenlandic Flag

Matthew Logue issued a challenge to accurately display the flag of Greenland. The flag is simple enough to be reduced to a formula x²+y² < 54² ⊻ y > 0:



Triangles

Uke Geiken showed a pattern of triangles and asked for the shortest code. The shortest implementation I found uses UDGs:



Grid

Matthew Logue asked us recreate a grid-like pattern with the shortest code. Surprisingly I actually managed to discover the smallest program:



Weaving

‎Uwe Geiken challenged us to recreate a pattern of weaving attributes. I found this one pretty tricky to reduce in size, but finally got it down to 109 bytes:



Flag

Matthew Logue asked for the shortest code to recreate a 31×21 attribute flag-like pattern. Uwe Geiken solved this is 67 bytes, easily beating my 74:



Rudimentary Gear

Matthew Logue challenged us again, this time to draw a rudimentary gear with 10 teeth and a circular radius. Here's what I came up with:



Monday, 6 December 2010

Programming Contest: Topswaps

Al Zimmermann's latest programming challenge asks up to arrange a deck of n cards numbered 1 .. n to maximise the number of swaps. Each swap looks at the value x on the top card and reverses the top x cards.

For n=4 the sequence 3, 1, 4, 2 requires 4 swaps:

3 1 4 2

4 1 3 2

2 3 1 4

3 2 1 4

1 2 3 4

Al challenges us to find the best solution for the first 25 primes, n=2, 3, 5 .. 97. Even a simple program which tests random combinations can throw out some reasonable scores:

10 rem setup array
20 input z
30 dim x(z)
40 for a=1 to z
50 x(a)=a
60 next a
70 h=0

100 rem shuffle array
110 for a=1 to z
120 r=int(rnd*z+1)
130 t=x(a)
140 x(a)=x(r)
150 x(r)=t
160 next a

200 rem remember order
210 a$=""
220 for a=1 to z
230 a$=a$+","+str$(x(a))
240 next a

300 rem swap until done
310 c=0

400 q=x(1)
410 for a=1 to int(q/2)
420 t=x(a)
430 x(a)=x(q+1-a)
440 x(q+1-a)=t
450 next a

500 c=c+1
510 if x(1)>1 then goto 400

600 rem display highscore
610 if c>h then h=c:print c;" : ";a$
620 goto 100

Are you planning to take part in the contest?

Saturday, 9 January 2010

8 Bit Home Computer Benchmarks

Over the years I've collected quite a few 8 bit home computers. Out of curiosity I decided to write a simple prime sieve benchmark to compare their implementations of BASIC.

10 LET W=500:DIM F(W):LET P=1:LET A=3
20 LET F(P)=A:LET P=P+1:IF P>W THEN STOP
30 LET A=A+2:LET X=1
40 LET S=A/F(X):IF S=INT(S) THEN 30
50 LET X=X+1:IF X<P AND F(X)*F(X)<=A THEN 40
60 GOTO 20

Here are the results from a few of the machines I have to hand:

System
CPU
Time
Acorn Electron
2.0MHz 6502
138
Amstrad CPC464
4.0MHz Z80A
140
Commodore C64
1.0MHz 6510
254
Commodore Plus/4
1.0 MHz 8501
267
Tandy 64K CoCo 2
0.895MHz 6809E
271
Atari 800XL
1.8MHz 6502
316
Sinclair Spectrum +3
3.55MHz Z80A
388

Let me know how long it takes to run on your favourite classic computer. :-)