Simple BASIC program to count from 1 to x

Too many years ago to even mention, I wrote a simple BASIC program that would count from 1 to whatever nunber I entered, and display the numbers on the screen.

Just idle fun, mainly to see how computers got faster over the years, and to use as a smoke test when I built a new computer. By the time got to the 386s, they were too fast for this anyway, so never kept it.

Now a friend’s kid wants something like that, but BASIC has totally vanished from my brain. Well, it’s probably in there somewhere, but the problem is retrieval. Or, that my brain is down to only 512K of memory.

Can anybody refresh my memory so I can write this again. Just as a DOS program will be fine, .exe or .com.

While thinking about this, might also use it to run constantly to test how long my laptop battery lasts. Or is there some other program that would do for that? I Googled a bunch of things without luck.

Turorial on GBASIC

It hardly qualifies as a program:

input n
for j = 1 to n
print n
next j

Depending on the version of BASIC, you could throw a few TIMER statements in to see how fast it runs. If it’s so ancient that it doesn’t have FOR/NEXT loops, you can achieve the same thing with IFs and GOTOs.

print j, that should be. Just testing!

Without trying to be a jerk, I think you just demonstrated why software is hard. Given a trivial & well-understood requirement, the first solution is program 4 lines long that contains fundamental bug.

Folks, it doesn’ get easier as the programs grow from 4 lines to 400,000.

Well, one key element is that Usram wasn’t charging for his services. Microsoft does.

Just for laughs, try programming this scenario: A jail has 100 cells. On his birthday, the King has a ritual of walking through and unlocking each cell. Then he makes a second pass and relocks each second cell (i.e. #2, #4, #6…). Then he makes a third pass and changes the state (i.e. locks it if it’s unlocked, unlock it if it’s locked) of every third cell. His fourth pass visits every fourth cell and so forth until his 100th pass stops only at the 100th cell.

Write a program to find out which cells are open at the end.

This was an actual college programming-course question I got hit with circa 1990.

Hey, I issued a Service Pack within one minute…

If you have a windows machine, a fun way to do some simple programming is to use javascript. You can execute it using cscript.exe, which is builtin. For example, if you save this answer to Bryan Ekers’ challenge in a file called cells.js:



var cells = new Array();

// initialize cells to locked
for(var i=0; i<100; i++)
{
    cells* = true;
}

// begin king's ritual
for(var i=0; i<100; i++)
{
    for(var j=(i); j<100; j += (i+1))
    {
        cells[j] = !cells[j];
    }
}

// ritual over; print results
for(var i=0; i<100; i++)
{
    if(!cells*)
    {
        print("cell " + (i+1) + " is unlocked");
    }
}

function print(message)
{
    //WScript is part of the Windows Scripting Host API
    //provided when running scripts with cscript.exe
    WScript.Echo(message);
}


And then run “cscript cells.js”, it will run the program and print the answer. There is a language reference at msdn.microsoft.com (search for “jscript language reference”), too.

That simplifies immensely if you recognize it as a logic problem. In fact, I can just tweak the pre-existing Usram module (remembering to include the bug fix!)

input n
for j = 1 to sqrt(n)
print j
next j

Can you go through your logic there? I kind of see it, but mostly not.

This made me laugh in sad recognition.

I have a framed quote from circa 1949 attributed to [Maurice Wilkes](maurice wilkes), a pioneer of computer programming, in my office that reads:

Supposedly he also disliked the term Computer Science to describe, well, what is now called “Software Engineering”: I have been a scientist and I have been an engineer: I know the difference.

The brute-force double-loop code seems obvious enough. All you need is to remember about the modulo operator to implement the “skip <passnum> cells at pass number <passnum>” in the inner loop. I think it is more interesting to consider it as a math problem first though. Can this be solved without an n^2 algorithm? It seems like it should be solvable algebraically.

Heh. Well, continuing with the theme in the thread, there’s a bug in the code. Should be:

input n
for j = 1 to sqrt(n)
print j^2
next j

Point is, the answer is “the open doors are all perfect squares.” That’s because a door state will change once for every factor of the number. Frex, door six will change state four times, at pass 1, 2, 3, and 6. For any non-perfect square (like six) the factors are all paired, so there’s an even number of factors, so the door will return to its original state (ie, closed). For the perfect squares, the square root are not paired, so there are an odd number of factors, so the door will change state.

Link to explanation.

This has become a very interesting thread.

OK, to answer my own OP question, this .bat file works, sort of:

FOR /L %%G IN (1,1,1000000) DO echo %%G

This sentence has me a little confused. In the good old days, DOS included some version or other of BASIC, but unless I’m misinformed, Microsoft stopped including BASIC with Windows some time ago. So if you’re going to write BASIC programs, you’re going to need a BASIC interpreter and/or compiler, which you may or may not have on your computer. (Roughly, with an interpreter, you’re writing and executing BASIC programs from within the BASIC software environment; a compiler turns your BASIC program into a stand-alone .exe or .com file.) If you don’t already have BASIC on your computer, I’m sure there are versions available for download. Some of the details of your program may depend on which version (i.e. which particular interpreter or compiler) you have (e.g. whether or not you have to number the lines of the program, which used to be the standard for BASIC many years ago).

Thanks, you are right, of course. I do have an old BASIC interpreter saved from an old computer on my second HDD. About the only thing I do remember is that I have to number te lines (plus a few commands such as GOTO, etc). :smiley:

Aha! I knew there was some truly insightful answer to this question.

Cool beans.

To clarify the delay routine, using Usram’s (corrected) program:

10 input n
20 for j = 1 to n
30 print j
40 for x=1 to z
50 next x
60 next j

where z is some number; the bigger the number, the longer the delay. As fast as computers are these days, you should probably start with something like 1000.

(This reminds me of an old database program called Nutshell, which started with “animation” of a file drawer opening. On my first 386 machine it moved too fast to be seen. Ah, the old days, when good programs could be written with less than 200k of code!)

Well, that’s what the turbo switch was for.
I remember another programming course where the language was entirely fictional and simplified. One controlled a robot with the following commands:

turnright
step
putbeeper
pickbeeper
end

And the following if/then functions:

next-to-beeper
bag-is-empty
wall-front
wall-right
wall-left
facing-north
facing-east
facing-west
facing-south

plus a generic “while” structure, i.e.

While not wall-front
{step}

…would tell the robot to walk a straight line until finding a wall.

And that was it. Navigate your robot around a two-dimensional grid. If he walks into a wall, puts down a “beeper” (miniature beacon) if his beeper bag is empty or tries to pick up a beeper if not standing next to one, crash. A basic programming assignment was the robot faces a building of random shape; walk up to it and around it once, then end. Since the language didn’t exist (though I suppose one could write an emulator easily enough), debugging was entirely a thought experiment - no bashing away at it until it worked.

I really should go back to school.

After ruminating on this, I remembered the programming language was Karel and though my description of the syntax is not perfectly accurate (according to the Wikipedia page) I got the gist of it.