 # Apples, pears and brute force in Tiny BASIC

Some days ago, I came across a post on the BASIC programming language Facebook group about a challenge announced on the Liberty BASIC community forum. The objective is to create a program for solving the following mathematical puzzle:

```There was a box of apples and pears. Some are big, some are small; some are yellow, rest are green.
There are no small pears and no small green apples.
Some numbers are given:
25 Apples, 17 pears
32 big fruits
28 yellow ones.
There are two more green apples then green pears.
Find the number of big yellow apples.```

The problem can be represented with a linear system with 5 equations involving 5 variables, which can be solved by means of well-known mathematical algorithms (I might have implemented some when I was a student). However, in this case I decided to have some fun implementing a brute force algorithm in Tiny BASIC. The program listing is reported below:

``````1 REM ********************
2 REM * APPLES AND PEARS *
3 REM ********************
4 REM BRUTE FORCE TINYBASIC IMPLEMENTATION
5 REM BY MARCO'S RETROBITS
6 REM RETROBITS.ALTERVISTA.ORG
7 REM PLEASE TAKE A SEAT, THIS COULD BE SLOW
8 REM ********************
10 REM THERE WAS A BOX OF APPLES AND PEARS
11 REM SOME ARE BIG, SOME ARE SMALL;
12 REM SOME ARE YELLOW, REST ARE GREEN
13 REM THERE ARE NO SMALL PEARS
14 REM AND NO SMALL GREEN APPLES.
15 REM SOME NUMBERS ARE GIVEN:
16 REM 25 APPLES, 17 PEARS
17 REM 32 BIG FRUITS
18 REM 28 YELLOW ONES.
19 REM THERE ARE TWO MORE GREEN APPLES THAN GREEN PEARS.
20 REM FIND THE NUMBER OF BIG YELLOW APPLES.
30 REM C: SMALL YELLOW APPLES
40 REM D: BIG YELLOW APPLES
50 REM E: BIG GREEN APPLES
60 REM F: BIG YELLOW PEARS
70 REM G: BIG GREEN PEARS
80 REM A: APPLES; P: PEARS
90 REM B: BIG FRUITS; Y: YELLOW ONES
100 LET A = 25
110 LET P = 17
120 LET B = 32
130 LET Y = 28
200 REM FOR C = 1 TO A
201 LET C=1
210   REM FOR D = 1 TO A-C
211   LET D = 1
220     REM FOR E = 1 TO A-C-D
221     LET E = 1
230       REM FOR F = 1 TO P
231       LET F = 1
240         REM FOR G = 1 TO P-F
241         LET G = 1
250           GOSUB 2000
260           IF S = 0 THEN GOTO 460
270           PRINT "SOLVED!"
280           PRINT "SMALL YELLOW APPLES:", C
290           PRINT "BIG YELLOW APPLES:", D, "*"
300           PRINT "BIG GREEN APPLES:", E
310           PRINT "BIG YELLOW PEARS:", F
320           PRINT "BIG GREEN PEARS:", G
400           END
460         REM NEXT G
461         LET G = G+1
462         IF G <= P-F THEN GOTO 250
470       REM NEXT F
471       LET F = F+1
472       IF F <= P THEN GOTO 240
480     REM NEXT E
481     LET E = E+1
482     IF E <= A-C-D THEN GOTO 230
490   REM NEXT D
491   LET D = D+1
492   IF D <= A-C THEN GOTO 220
500 REM NEXT C
501 LET C = C+1
502 IF C <= A THEN GOTO 210
510 PRINT "NO SOLUTION!"
520 END
1990 REM ** CHECK ROUTINE **
2000 LET S = 0
2001 REM PRINT C, D, E, F, G
2010 REM 25 APPLES, 17 PEARS
2020 IF C + D + E <> A THEN RETURN
2030 IF F + G <> P THEN RETURN
2040 REM 32 BIG FRUITS
2050 IF D + E + F + G <> B THEN RETURN
2060 REM 28 YELLOW ONES
2070 IF C + D + F <> Y THEN RETURN
2080 REM THERE ARE TWO MORE GREEN APPLES THAN GREEN PEARS
2090 IF E <> G + 2 THEN RETURN
2100 REM ** SOLVED! **
2110 LET S = 1
2990 RETURN``````

The firtst thing that emerges from the listing is the lack of FOR..NEXT loops in Tiny BASIC (I always forget about that!). This is not a big deal, since they can be replaced with (less elegant) IFs and GOTOs. Another limitation of this dialect is that variable names can only consist of a capital letter.

I ran the “Apples and Pears” program on TinyBasicBlazor, my Tiny BASIC environment for web browsers based on TinyBasic.NET and described in this post; it took more than half an hour to find the solution (I must check why)! With the console version of TinyBasic.NET, the problem is solved in a few seconds.

I almost forgot: there are 7 big yellow apples!

``````SMALL YELLOW APPLES: 10
BIG YELLOW APPLES: 7 *
BIG GREEN APPLES: 8
BIG YELLOW PEARS: 11
BIG GREEN PEARS: 6``````