# Simple BASIC program to count from 1 to x

**URL:** <https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871>\
**Category:** Factual Questions\
**Created:** [January 13, 2007, 10:07pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871 "2007-01-13T22:07:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![KlondikeGeoff](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@KlondikeGeoff](https://boards.straightdope.com/u/KlondikeGeoff)\
**Post date:** [January 13, 2007, 10:07pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/1 "2007-01-13T22:07:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [January 13, 2007, 10:17pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/2 "2007-01-13T22:17:04Z")

</div>

[Turorial on GBASIC](http://www.floodgap.com/retrobits/tomy/gbasic.html)

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [January 13, 2007, 11:06pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/3 "2007-01-13T23:06:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [January 13, 2007, 11:07pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/4 "2007-01-13T23:07:12Z")

</div>

print **j** , that should be. Just testing!

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [January 14, 2007, 1:14am UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/5 "2007-01-14T01:14:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bryan\_Ekers](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bryan_ekers/32/183_2.png) [@Bryan\_Ekers](https://boards.straightdope.com/u/Bryan_Ekers)\
**Post date:** [January 14, 2007, 1:51am UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/6 "2007-01-14T01:51:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [January 14, 2007, 2:07am UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/7 "2007-01-14T02:07:06Z")

</div>

Hey, I issued a Service Pack within one minute…

---

<div class="post-metadata">

**Author:** ![spinky](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@spinky](https://boards.straightdope.com/u/spinky)\
**Post date:** [January 14, 2007, 2:12am UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/8 "2007-01-14T02:12:29Z")

</div>

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:

```auto

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](http://msdn.microsoft.com) (search for “jscript language reference”), too.

---

<div class="post-metadata">

**Author:** ![zut](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zut/32/2875_2.png) [@zut](https://boards.straightdope.com/u/zut)\
**Post date:** [January 14, 2007, 3:30pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/9 "2007-01-14T15:30:47Z")

</div>

[QUOTE=Bryan Ekers]  
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.  
[/QUOTE]

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

---

<div class="post-metadata">

**Author:** ![Captain\_Carrot](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Captain\_Carrot](https://boards.straightdope.com/u/Captain_Carrot)\
**Post date:** [January 14, 2007, 3:43pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/10 "2007-01-14T15:43:34Z")

</div>

[QUOTE=zut]  
for j = 1 to sqrt(n)  
[/QUOTE]

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

---

<div class="post-metadata">

**Author:** ![robardin](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@robardin](https://boards.straightdope.com/u/robardin)\
**Post date:** [January 14, 2007, 3:43pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/11 "2007-01-14T15:43:59Z")

</div>

[QUOTE=LSLGuy]  
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.  
[/QUOTE]

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:

> [@](#):
>
> As soon as we started programming, we found to our surprise that it wasn’t as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.

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._

---

<div class="post-metadata">

**Author:** ![robardin](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@robardin](https://boards.straightdope.com/u/robardin)\
**Post date:** [January 14, 2007, 3:50pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/12 "2007-01-14T15:50:30Z")

</div>

[QUOTE=Bryan Ekers]  
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.  
[/QUOTE]

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.

---

<div class="post-metadata">

**Author:** ![zut](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zut/32/2875_2.png) [@zut](https://boards.straightdope.com/u/zut)\
**Post date:** [January 14, 2007, 4:37pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/13 "2007-01-14T16:37:00Z")

</div>

[QUOTE=Captain Carrot]  
Can you go through your logic there? I kind of see it, but mostly not.  
[/QUOTE]  
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](http://www.techinterview.org/Solutions/fog0000000095.html) to explanation.

---

<div class="post-metadata">

**Author:** ![KlondikeGeoff](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@KlondikeGeoff](https://boards.straightdope.com/u/KlondikeGeoff)\
**Post date:** [January 14, 2007, 4:58pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/14 "2007-01-14T16:58:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thudlow\_Boink](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/thudlow_boink/32/320_2.png) [@Thudlow\_Boink](https://boards.straightdope.com/u/Thudlow_Boink)\
**Post date:** [January 14, 2007, 5:00pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/15 "2007-01-14T17:00:47Z")

</div>

[QUOTE=KlondikeGeoff]  
Can anybody refresh my memory so I can write this again. Just as a DOS program will be fine, .exe or .com.  
[/QUOTE]  
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).

---

<div class="post-metadata">

**Author:** ![KlondikeGeoff](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@KlondikeGeoff](https://boards.straightdope.com/u/KlondikeGeoff)\
**Post date:** [January 14, 2007, 5:15pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/16 "2007-01-14T17:15:04Z")

</div>

[QUOTE=Thudlow Boink]  
So if you’re going to write BASIC programs, you’re going to need a BASIC interpreter and/or compiler,  
[/QUOTE]

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). 😃

---

<div class="post-metadata">

**Author:** ![robardin](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@robardin](https://boards.straightdope.com/u/robardin)\
**Post date:** [January 14, 2007, 5:17pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/17 "2007-01-14T17:17:00Z")

</div>

[QUOTE=zut]  
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](http://www.techinterview.org/Solutions/fog0000000095.html) to explanation.  
[/QUOTE]

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

Cool beans.

---

<div class="post-metadata">

**Author:** ![BJMoose](https://avatars.discourse-cdn.com/v4/letter/b/5fc32e/32.png) [@BJMoose](https://boards.straightdope.com/u/BJMoose)\
**Post date:** [January 14, 2007, 5:36pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/18 "2007-01-14T17:36:18Z")

</div>

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!)

---

<div class="post-metadata">

**Author:** ![Bryan\_Ekers](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bryan_ekers/32/183_2.png) [@Bryan\_Ekers](https://boards.straightdope.com/u/Bryan_Ekers)\
**Post date:** [January 14, 2007, 7:33pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/19 "2007-01-14T19:33:31Z")

</div>

[QUOTE=BJMoose]  
On my first 386 machine it moved too fast to be seen.  
[/QUOTE]

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.

---

<div class="post-metadata">

**Author:** ![Bryan\_Ekers](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bryan_ekers/32/183_2.png) [@Bryan\_Ekers](https://boards.straightdope.com/u/Bryan_Ekers)\
**Post date:** [January 14, 2007, 9:57pm UTC](https://boards.straightdope.com/t/simple-basic-program-to-count-from-1-to-x/387871/20 "2007-01-14T21:57:48Z")

</div>

After ruminating on this, I remembered the programming language was [Karel](http://en.wikipedia.org/wiki/Karel_(programming_language)) and though my description of the syntax is not perfectly accurate (according to the Wikipedia page) I got the gist of it.
