Primes by Trial Division - Atari 2600 BASIC
7 September 2016
This version was quite an adventure to produce. For quite a while it didn't look like it would be possible—not because the capabilities weren't there, but because there just wasn't enough memory to both hold the program and execute it. The 64-symbol workspace is exceptionally stingy as it has to not only hold your code, but to have enough room to store your variables (three symbols each), to hold anything you print on the screen, and to provide temporary storage for the parser.
There was enough room, for example, to add a line of code to exit the program after finding the 25th prime number (97, the largest prime number Atari 2600 BASIC is capable of representing), but then not enough memory leftover to run it. That there is no array to hold results is immaterial since there's no memory to hold it even if there were. That none of the screen areas can scroll is immaterial, since every character you print to the Output window consumes memory for as long as it stays in the Output window.
In the end, removing the exit condition freed up just enough memory (8 symbols) to run, with two symbols leftover to add a Clear statement to line 7, which keeps the Output window from ever consuming more than one symbol of workspace. If 2600 BASIC were capable enough to limit the scope of trial divisors just to prior results, the 2600 would deliver a faster result than the V-Tech Laser 50. But, alas.
There isn't even any memory to provide error conditions! If you run out of memory, first, assignment silently fails to happen, then eventually you get garbage on the screen and maybe your program stops. If you overflow the integer type, it just rolls over to 0. If you divide by zero, the answer is simply 0. As a result, this program will keep running (and correctly identify 0 and 1 as not prime!) forever... if you let it.
1 C←3 2 I←2 3 If C=I Then Goto 7 4 If C Mod I=0 Then Goto 8 5 I←I+1 6 Goto 3 7 Clear,Print C 8 C←C+1 9 Goto 2
One other funny thing: every symbol you add to the Program window causes the lines below it to shift down the screen a pixel. In this way, lines 7 through 9 become inaccessible before you have a chance to add them to your program. In order to avoid entering them blindly, you have to start entering your program on line 9 and make your way through it, backward.