================================================================================================

tc.exe - Windows tiny-C interpreter

tc - Linux tiny-C interpreter

Note: If a program has a line like #include pps/library.tc you need to
download what is to be included (and place it in this case in a folder
named pps). The following four files are examples of included files.

a. library.tc - this needs to be in a folder named pps

b. mathLib.tc - this needs to be in a folder named pps

c. lrblib.tc - 5/25/19 [v 1.14]

d. pyGraphicsLib.tc - this needs to be in a folder named pps

================================================================================================

The Book

http://primepuzzle.com/lee-bradley/SamplePrograms/The_Book.html

Interesting and often challenging problems solved in tiny-C and C with
an occasional use of Python and JavaScript and Mouse and Pascal and ...

by Lee R. Bradley
The definitive guide to the tiny-C language

0. the_new_classical_tiny-C.doc

Two programs that draw stars

1. starc_no_color.c - 5/25/19

2. starc.c - 5/26/19

e. cairopy.py - launched by applications that use Cairo graphics (for example, the next two)

When you need to multiply (or add) two Roman numerals (e.g. XIV*XIV=CXCVI)

3. roman.tc

4. spirala.tc, spiralpi.c

5. sieve.c - 5/24/19

D:\Tom Gibson\tiny-c-master\SamplePrograms>sieve
sieve.c - 5/24/19 - lrb
usage: sieve [-v]

Enter a number to factor : 23372

2 2 5843

5a. njtc_sieve_v2.c - 5/27/19

5b. factor.6.2.tc

H:\>tc factor.6.2.tc -r main(0) -l
Sizes: of pr 100000 fun 200 stack 200 var 300
pps\library.tc loaded
  (default)
  endlibrary
factor.6.2.tc loaded
***  TINY-C VERSION 1.0,  COPYRIGHT 1977, T A GIBSON  ***
        This C version copyright 2017, T A Gibson

2020-02-14 10:52:16

factor.tc - 2/13/20 - lrb - source code has 1647 bytes

Enter number to factor 2123123123
 11 23 8391791 = 2123123123

2020-02-14 10:53:14

H:\>

12. star.tc - 5/15/19

You've got $15. Chocolate bars cost $1. You can turn in 3 wrappers and
get another chocolate bar. How many chocolate bars can you get?
The following program is called van.tc because choc.tc had already been taken :-)

13. van.tc - 5/28/19

D:\Tom Gibson\tiny-c-master>tc SamplePrograms\van.tc
***  TINY-C VERSION 1.0,  COPYRIGHT 1977, T A GIBSON  ***
        This C version copyright 2017, T A Gibson

van.tc - lrb - 5/29/18

dollars : 123

number of wrappers to get extra bar : 5

 123 147 152 153 wrappers left = 3

done
D:\Tom Gibson\tiny-c-master>

14. colcircles.c

How many socks do you need to pull from a drawer in order to get two pair?

15. socks.tc
15a. socks.out

The next one analyses a tiny-C program and shows its structure (the depth of the ifs and whiles).

16. matched_pair.tc
16a. tchelp.matched_pair.txt

Given a list of possible books to read, a group of people vote on what
they'd like to be picked. The books are listed in descending vote
order.

17. page_turners.tc
17a. page_turners.tc.out
17b. page_turners.c
17c. page_turners.c.out

A generalized "When's the next Friday the 13th?" program. The calendars
below show when the next Thursday the 6th occurs.

18a. next_c.tc
18b. next_c_doc.html

This program is fed two paces (minutes per mile for first 10 and last
16.2 miles in a marathon) and produces a chart showing you what time you
should be at at each mile.

19. chas.tc

***  TINY-C VERSION 1.0,  COPYRIGHT 1977, T A GIBSON  ***
        This C version copyright 2017, T A Gibson

chas.tc - 4/14/18

Pace for the first 10 miles (for example 9:39)?
Minutes first, then seconds.

minutes 9
seconds 10

Pace for last 16.2 miles (for example 9:28)?
Minutes first, then seconds.

minutes 12
seconds 13

  1	 0 : 9 : 10	 11 : 24 : 10
  2	 0 : 18 : 20	 11 : 33 : 20
  3	 0 : 27 : 30	 11 : 42 : 30
  4	 0 : 36 : 40	 11 : 51 : 40
  5	 0 : 45 : 50	 12 : 0 : 50
  6	 0 : 55 : 0	 12 : 10 : 0
  7	 1 : 4 : 10	 12 : 19 : 10
  8	 1 : 13 : 20	 12 : 28 : 20
  9	 1 : 22 : 30	 12 : 37 : 30
  10	 1 : 31 : 40	 12 : 46 : 40
  11	 1 : 43 : 53	 12 : 58 : 53
  12	 1 : 56 : 6	 1 : 11 : 6
  13	 2 : 8 : 19	 1 : 23 : 19
  14	 2 : 20 : 32	 1 : 35 : 32
  15	 2 : 32 : 45	 1 : 47 : 45
  16	 2 : 44 : 58	 1 : 59 : 58
  17	 2 : 57 : 11	 2 : 12 : 11
  18	 3 : 9 : 24	 2 : 24 : 24
  19	 3 : 21 : 37	 2 : 36 : 37
  20	 3 : 33 : 50	 2 : 48 : 50
  21	 3 : 46 : 3	 3 : 1 : 3
  22	 3 : 58 : 16	 3 : 13 : 16
  23	 4 : 10 : 29	 3 : 25 : 29
  24	 4 : 22 : 42	 3 : 37 : 42
  25	 4 : 34 : 55	 3 : 49 : 55
  26	 4 : 47 : 8	 4 : 2 : 8
  26.2	 4 : 49 : 34	 4 : 4 : 34

done

A tutee had a problem some time back that involved amortization. This
has to do with how many equal payments you would have to make of a
specified amount if you borrowed say $2000 at say 8%.

21. amort.tc
21a. amortpi.c

D:\Tom Gibson\tiny-c-master>tc SamplePrograms\amort.tc
***  TINY-C VERSION 1.0,  COPYRIGHT 1977, T A GIBSON  ***
        This C version copyright 2017, T A Gibson

amort.tc - lrb - 11/19/18

Today's date and time are 2019-06-08 05:06:38

monthly payment amount (e.g. 100) ? 234
principle (amount borrowed) (e.g. 2000) ? 10000
annual percent interest rate (e.g. 8) ? 6

number of monthly payments is 48.19

HARBRAD is a toy assembly language. An assembler and an interpreter and
a sample program written in this language follow.

22. hasm.tc
22a. hrun.tc
22b. nedanted.hbs

Here's the story behind Ned and Ted ...

Each day of the week (thus the loop uses a 7) Ned, a bubble gum card
collector, enters into the computer the number of baseball cards he's
collected from bubble gum purchases. At the end of the week, his friend
Ted gives him 3 cents per card if there were 25 or fewer collected. If
more than 25 were collected, he gets a buck (100 cents) plus a nickel
for each card over the 25 he collected.

If, for example, he collected 4 cards on each of the days he'd earn
$1.15 because 4 + 4 + 4 + 4 + 4 + 4 + 4 = 28 = 25 + 3 and 100 + 3*5 = 115

00,+000     DS        NEDANTED.HBS
01,+000     DS
02,+123     L    SV   INITIALIZE DAYS
03,+224     ST   DL   LEFT WITH A 7.
04,+700     RD        GET FIRST DAY'S
05,+225  L1 ST   CT   TAKE. SAVE IT.
06,+124     L    DL   UPDATE DAY'S
07,+426     S    ON   LEFT.
08,-313     BRZ  CP   IF WEEK OVER, GO
09,+224     ST   DL   TO CALC PAY ELSE
10,+700     RD        GET NEXT DAY'S
11,+325     A    CT   TAKE AND UPDATE
12,-105     BR   L1   TOTAL.
13,+125  CP L    CT   CALCULATE PAY:
14,+427     S    TF   IF <= 25, GET 3
15,-219     BRP  L2   CENTS PER CARD
16,+125     L    CT
17,+529     M    TH
18,-121     BR   PT
19,+530  L2 M    FV   ELSE GET 5 CENTS
20,+328     A    OH   PER CARD OVER
21,+800  PT PT        25 + $1.  PRINT
22,+900     STOP      AND STOP.
23,+007  SV DW   +007 7 DAYS IN A WEEK
24,+000  DL DS        DAYS LEFT AREA
25,+000  CT DS        CARD TOTAL AREA
26,+001  ON DW   +001 THE VALUE 1
27,+025  TF DW   +025 THE VALUE 25
28,+100  OH DW   +100 ONE DOLLAR
29,+003  TH DW   +003 3 CENTS
30,+005  FV DW   +005 5 CENTS

D:\Tom Gibson\tiny-c-master>tc SamplePrograms\hrun.tc
***  TINY-C VERSION 1.0,  COPYRIGHT 1977, T A GIBSON  ***
        This C version copyright 2017, T A Gibson

hrun.tc - 4/28/19 - lrb

object filename (.hbo will be appended) sampleprograms\nedanted

turn tracing on (y/n) ? n ? 4
 ? 4
 ? 4
 ? 4
 ? 4
 ? 4
 ? 4
 115

memory dump (y/n) ? n
As tiny-C developed it became useful to keep track of the features it
supported. What libraries were available? What operating system
dependencies are there? How is the interpreter invoked? What was the
build date? What was the date and time of the run of a program? What
version of a library is being used?

23. ver.tc

D:\Tom Gibson\tiny-c-master>tc SamplePrograms\ver.tc -r "main\"...\""
***  TINY-C VERSION 1.0,  COPYRIGHT 1977, T A GIBSON  ***
        This C version copyright 2017, T A Gibson

ver.tc - lrb - 6/24/19

The tiny-C build date is 2019-06-23  4:07
The operating system is Windows_NT
The program length is 40000
tiny-C authors ..., ..., ...
The current date and time is 2019-06-24 14:38:40
Enter a number : 31415

Your entry and a message for the day follow.

          31415
  tiny-C rocks!

Using functions from the libraries

http://primepuzzle.com/lee-bradley/lrblib.tc
http://primepuzzle.com/lee-bradley/pps/library.tc

lrblib version 1.14

http://primepuzzle.com/lee-bradley/ver.tc

Your command tail was ...
3.1415926535897932384
yummm
John Conway's game of Life.

This is a very simple implementation of this famous "game." The "board"
is small (50x50). It starts by randomly populating the board and then
runs for a user-supplied number of generations.

24. slr.tc

24a. Click the link to see the "movie" life.mp4

For when you need to dump a file in hex and character.

25. rdump.tc

99 bottles of beer on the wall (Halloween version)

26. pieces.tc

./tc SamplePrograms\lee-bradley\pieces.tc

Enter number of verses : 10
Enter delay in seconds between verses : 2

10 piece(s) of pumpkin pie on the shelf,
10 piece(s) of pumpkin pie.
If one of those pieces of pumpkin pie should happen to fall ...
9 piece(s) of pumpkin pie on the shelf.

9 piece(s) of pumpkin pie on the shelf,
9 piece(s) of pumpkin pie.
If one of those pieces of pumpkin pie should happen to fall ...
8 piece(s) of pumpkin pie on the shelf.

...

2 piece(s) of pumpkin pie on the shelf,
2 piece(s) of pumpkin pie.
If one of those pieces of pumpkin pie should happen to fall ...
1 piece(s) of pumpkin pie on the shelf.

1 piece(s) of pumpkin pie on the shelf,
1 piece(s) of pumpkin pie.
If one of those pieces of pumpkin pie should happen to fall ...
0 piece(s) of pumpkin pie on the shelf.

This next program computes the value of 3^n-3*n for large n.

27. multseq.tc

The 955 digit value of 3^2000-3*2000 is

1747871251722651609659974619164660570529
0624874351885178118880118106862662272754
8929148646986468111107560895069614527658
8771368435875508647514414202093638481872
9123800899771793815296284783205235193191
4268150442405941089021450050064781393581
8925701905402605484098137956979368551025
8252394113186439979165236770447696626286
4640654033562797532961926424507975047086
2462474091105444437355302146151475348090
7553301532690679330916994798890898246508
4179556747860639697566455714373765702708
0403239977757865296846740093712377915770
5360942236880491080232441391830279624844
1107846443951684522796193522126981475341
6782576455507316073751985374046064592546
7960431507378083145016846797580569059487
5924636864441615186313808527660359581641
0945157599742077617618911601185155602080
7717467859593598794901919333899652712754
0312792543224796326967591264610315634395
4375442792688936047041533537523137941310
6908339497677642900813339003803104061547
23157882112449991673819054110434001

Richard Pattis wrote "Karel the Robot" in 1981.

Karel lives in a grid of avenues and streets.

He can move forward a single unit. He can turn left 90 degrees. He can
pick up beepers that he runs into and he can put down beepers if he has
any beepers in his beeper bag. He can see walls immediately in front of
him, immediately to his left, or to his right. He can detect what
direction he is facing in (N, S, E or W). Finally, he can tell if he's
at a beeper location.

Here is a simulation of Karel running around a lake, picking up beepers along the way.

28. mykarel.lw.tc - Windows version
28a. pospi.c - plugin supporting cursor positioning
28b. mykarel.lw.linux.tc - Linux version
28c. lw.kw.txt - world file
28d. lw_linux.mp4 - This is a screencast of his run.

Another sample program which illustrates a few more features


28e. mykarel.p1.tc
28f. mykarel.p1.linux.tc
28g. p1.kw.txt - world file

Storm Nemo

28h. nemo.tc
28i. nemo.kw - world file
28j. nemo.mp4 - screencast

During Storm Nemo, Karel the Robot was watching Lee solve the problem of
where to put the snow. The only way to get rid of it was to shovel a
swath and pile it up. Then shovel another swath and pile it in another
pile. These piles would then be moved onto Lee's lawn. The piles could
only be four feet high. Here's what the piles looked like after one day
of shoveling.



S A
      *********************************
T 9   *                               *
      *                               *
R 8   *         p                   l *
      *                             a *
E 7   *         q                   w *
      *                             n *
E 6   *                               *
      *                               *
T 5   *         v       r             *
      *                               *
S 4   *     @   @   @   @             *
      *                               *
  3   *     @   @   @   @             *
      *                               *
  2   *                               *
      *********************************
  1

    1   2   3   4   5   6   7   8   9   A
    A   V   E   N   U   E   S

Put the 4 beepers that are nearest Karel in a pile at location
(avenue,street) = (4,8).  Put the remaining beepers in a pile at
location (4,7). Move the pile you 1st built to some place on the lawn
and move the 2nd pile to another place on the lawn.  The p, q, r and
lawn are just there to help you locate key places.

k 4 5 S
w 1 1 9 1
w 1 9 9 9
w 1 1 1 9
w 9 1 9 9
b 3 4 1
b 4 4 1
b 5 4 1
b 6 4 1
b 3 3 1
b 4 3 1
b 5 3 1
b 6 3 1
g 0

Here's another Karel program. A maze.

http://primepuzzle.com/lee-bradley/SamplePrograms/maze.html

I made a stand-up jigsaw puzzle of a "Christmas tree" and then
identified the coordinates of the lines and peaks and turned it into a
graphics application.

29. xmas.tc

Amoeba!

30. amoeba.linux.tc.

Graphic by DeeDee

primepzl@november:~/public_html/lee-bradley$ ./tc SamplePrograms/amoeba.linux.tc
***  TINY-C VERSION 1.0,  COPYRIGHT 1977, T A GIBSON  ***
        This C version copyright 2017, T A Gibson

amoeba.linux.tc - tct - 7/10/19

An amoeba either dies, lives, splits into two or splits into three,
each with probability 1/4. It can be proved that out of 10
simulations, about 10*(sqrt(2)-1), which equals about 4,
populations will die out. It can also be proved that 1.5^n
is the expected number of amoebas after n generations. If n=10,
58 amoebas are expected.

seed? 5

simulation  1: population 0
simulation  2: population 0
simulation  3: population 33
simulation  4: population 0
simulation  5: population 0
simulation  6: population 74
simulation  7: population 0
simulation  8: population 12
simulation  9: population 91
simulation  10: population 137

 34 amoebas, on average, were living after 10 generations.
 5 population(s) died out.

done

Binary search trees

31. bst2.c

31a. tree-128.tc

31b. TREE-16.TC

This next application was developed some time ago and written in tiny-C.
It went through literally hundreds of changes. Finally, in just a couple
of days, it was translated into JavaScript!
32. http://primepuzzle.com/lee-bradley/SamplePrograms/jspeek.html

Here's a sample run.

This next application involves compressing and decompressing files

33. http://primepuzzle.com/lee-bradley/compressor.html

33a. http://primepuzzle.com/lee-bradley/SamplePrograms/compress.tc

33b. http://primepuzzle.com/lee-bradley/SamplePrograms/dompress.tc

Determine the next date that has the same day number and day of week
given a valid dow, month, day and year e.g. 

dow (0=Sunday, 1=Monday, etc.) : 5
month (0=January, 1=February, etc.) : 11
day (1..31) : 13
year (e.g. 2019) : 2019

Fri Dec 13
11 5
12 1
13 4
14 5
Mar 2020
34. next_c.tc

RYEMASH is a solution to Laplace's Equation as applied to a still in
Kentucky. We use it here as an example of how to run a CP/M program.

0. Do this once. Click putty.exe and save it to your computer.

1. Open a DOS box and navigate to where you've saved it.
   Run it by typing
   
   putty
   
   The Host Name to use is november.he.net (type this in the dialog box)

2. log in as: primepzl and use run413life when prompted for a password
   You will now be at a Linux terminal (which is like a DOS box)

3. change to the linux directory by typing

   cd ~/public_html/linux

4. run Peter Schorn's Altair Z80 Simulator by typing

   ./altairz80l64

5. at the sim> prompt type

   do nzcom

You will now be in an emulated CP/M environment. See the Note below for
how to get a much more powerful environment.

6. run the compiled BASIC program by typing at the prompt

   ryemash

7. leave CP/M by typing
 
   halt

8. leave the simulator by typing

   bye

9. leave Linux by typing

   exit

Note: A more powerful environment (called Z-System) is available. At the A> type

   beginc

to enter this environment.

A fancier prompt will be seen.

A0:COMMANDS>>

Z-System is beyond the scope of this document.

There are 2 "hard discs," I: and J:. There are 6 "floppies," A: thru F:.
To navigate to different areas you may type things like J5:

ff, (Find File), is a program which locates files. It "auto-wildcards."

For example

ff .bas abcdefij/s

would tell you where all the BASIC programs are on all drives, including
so-called "system" file.
 
35. RYEMASH.BAS
An application that turns text files into handwriting.

To run this, follow the directions to line item 35 and at the A> type leetrym
36. LEETRYM.BAS, LEE.TXT, LEETRY.HTM
Two programs that treat the problem of solving the relation y^x=x^y, one
for reals, the other for rationals.
37. xyyx.c, yxxyr.c

38. SOLVE.MSE, COWTRY.MSE

39. CPMDIR.PAS

This is a good example of cursor positioning in C under Windows
40. hillary.c, https://www.desmos.com/calculator/smtlehysff

41. double.tc

42. getproperty.tc

43a. dup.tc

43b. dup.c

44. http://primepuzzle.com/tunxis/integration.c, http://primepuzzle.com/tunxis/integration.amy.try.c

45. http://primepuzzle.com/tc/entropya.c

46. http://primepuzzle.com/tc/easter.tc

print pythagorean triples given a hypotenuse
47. http://primepuzzle.com/lee-bradley/SamplePrograms/danm.tc

48. http://primepuzzle.com/lee-bradley/SamplePrograms/signatur.plus.tc

49. http://primepuzzle.com/lee-bradley/SamplePrograms/totient.tst

50. http://primepuzzle.com/tc/pnut.1.c

50a. !jtc

50b. http://primepuzzle.com/tc/pnut.htm

H:\>pnut.1 -n 1000001 -v 0

largest prime was 999983

n / # primes <= n = 1000001 / 78498 = 12.7391908

ln(1000001) = 13.8155116

51. http://primepuzzle.com/tunxis/delta.epsilon.c

51a. http://primepuzzle.com/tunxis/feb.challenge.html

52. A Programmer's Guide to the Meaning of Life

53. http://primepuzzle.com/tc/tiny-c-programs.html

Appendix

https://github.com/tgibson37/tiny-c - GitHub repository of the tiny-C interpreter

buildPlugIn.bat - script used to build plugins under Windows

buildPlugIn.sh - script used to build plugins under Linux