TI-BASIC program - Need suggestions.

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

TI-BASIC program - Need suggestions.

Post by Patz QuickBASIC Creations » Mon Apr 03, 2006 4:41 pm

I'm not sure if TI-BASIC questions are allowed on the forum, but here it goes.

I recently made a program to generate random numbers that would never repeat.

Here is the dreadfully slow (not mine) version.

Notes: > specifies a STORE function, not GREATER THAN
<> specifies the TI-BASIC Is Not Equal To
These are both using minimal eye-candy
LGROUP and LGRPB are lists.
I can't use code tags...

Input A
{0}>LGROUP
0>D
0>F
While dim(LGROUP) <> A
int(rand*A)+1>B
For(C,1,dim(LGROUP)
IF B=LGROUP(C)
1>D
END
IF D <> 1
THEN
F+1>F
B>LGROUP(F)
END
Output(8,1,dim(LGROUP))
0>D
END
Output(1,1,LGROUP)



And my new (faster) version.


Input A
{0}>LGROUP
seq(B,B,1,A)>LGRPB
For(C,1,A)
int(rand*dim(LGRPB)+1>D
LGRPB(D)>LGROUP(C)
0>LGRPB
SortD(LGRPB)
dim(LGRPB)-1>dim(LGRPB)
End
DelVar LGRPB
Output(1,1,LGROUP)


Anyone have any tips for it? Speed or memory leak tips? (I already have muchos eye-candy, but didn't want to post it.) (if you can't use TI-BASIC then these programs are pretty useless)

User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim » Thu Apr 06, 2006 11:43 am

This is really interesting. Could you explain a bit about what this code is supposed to do and how it does it? Also, why is your code better (faster?) than the other code. One question in particular, where does the value of B come from?

I do a little programming in TI-BASIC. I own a TI-83+ SE and my son owns a TI-84+ SE. We use TI-83+'s in the classroom and many of the students have their own calculator.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Re: TI-BASIC program - Need suggestions.

Post by Patz QuickBASIC Creations » Thu Apr 06, 2006 4:13 pm

A quick overview:
Both codes generate random numbers in a random order and store them in LGROUP. However, none of the numbers ever repeat.

Differences:
First code: Generates a random number, checks if it exists in the list already. If it doesn't, it adds the number to the list.
Second code: I'll do a walkthrough.

Input A
- Prompts for your maximum number
{0}>LGROUP
- Clears whatever may already be in the list already
seq(B,B,1,A)>LGRPB
-Generates a second list of numbers from 1 to A. (B is used as a counter.)
For(C,1,A)
- The ever wonderful FOR loop!
int(rand*dim(LGRPB)+1>D
-Generates a random location in the second list to use that number
LGRPB(D)>LGROUP(C)
-Stores the number in position C (used in the FOR loop)
0>LGRPB
-Replaces the value you just used with a 0
SortD(LGRPB)
- Sorts the list in descending order. (0 is put in the last spot)
dim(LGRPB)-1>dim(LGRPB)
-Remove the last number in the list. (Always going to be the 0)
End
-End the FOR loop.
DelVar LGRPB
-Delete the (blank) second list tom save RAM
Output(1,1,LGROUP)
- Shows the list.



The reason my code is faster is because it is guarenteed to never generate the same number, so it doesn't have to check if the number exists.
Quick $$$:
To group 28: Old program-30 seconds. New program-3 seconds.
To group 400: Old program-Close to 4 hours. New program-23 minutes.

The syntax of seq() (where the B is used) is as follows:

Code: Select all

seq(pattern,counter,start,end)
Pattern: The pattern to follow. An equation usually goes in this spot
Counter: The variable that changes.
Start: Where to start counting.
End: Where to end counting.

So, seq(B,B,1,A) generates a list that increases in size by 1 for every entry, until it reaches the number you gave it (where Input A is).


Too long of an explanation :-P If I'm unclear about something just tell me.

User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim » Fri Apr 07, 2006 12:34 pm

Ok... so this could be useful for modeling what they call "Sampling Without Replacement" such as in drawing numbers (or names) out of a hat where you don't want the same number (name) called twice.

Very useful! I'll have to give it a look.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Fri Apr 07, 2006 4:16 pm

That's what it was originally made for.

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Thu Apr 13, 2006 4:49 pm

In fact, that analogy is very close. Here is an example using it.

FIRST CODE:
1. Draws a random number.
2. Asks 'Have I used this number before?'
3. If not, add it to the list.
4. Put the number back in the hat. (every time)
5. Repeat.


SECOND CODE:
1. Get 2 hats. (One with numbers, one empty)
2. Draw a random number out of the first hat.
3. Records that number.
4. Puts it into the second hat. (so it will not be drawn again.)
5. Repeat.



Hopefully that shows you how my code is more efficient.
PATZ

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Thu Apr 13, 2006 4:58 pm

OK, I just finished a diagnostic on my new code. To group 800, the program takes: 10771 seconds (about 3 hours.) I have yet to try the old code with 800 as the maximum number, however I will start it and tell you the results on Monday. (if it's done by then :lol: )



*-just started-*

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Mon Apr 17, 2006 5:52 pm

To group 800 the old program took 32 hours. See how mine's better?

New: 3 hours
Old: 32 hours.

User avatar
Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:

Post by Theophage » Sun May 07, 2006 7:54 pm

You young whippersnappers and your speedy code! Why in my day, we used code that let us come back a couple of days later to see if it was done or not, and we liked it!

Now, GET OFF MY LAWN!!!
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re » Mon May 08, 2006 5:35 am

Theophage wrote:You young whippersnappers and your speedy code! Why in my day, we used code that let us come back a couple of days later to see if it was done or not, and we liked it!

Now, GET OFF MY LAWN!!!
haha :D
I have left this dump.

User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim » Wed May 10, 2006 12:18 pm

In MY day, we'd turn in our stack of punch cards at the desk and come back in a couple of days to see if it had run.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Wed May 10, 2006 3:38 pm

Theophage wrote:You young whippersnappers and your speedy code! Why in my day, we used code that let us come back a couple of days later to see if it was done or not, and we liked it!

Now, GET OFF MY LAWN!!!
Get into the 21st century. People don't keep track of who's on who's lawn... Oh, you mean the code...

User avatar
Deleter
Veteran
Posts: 119
Joined: Sat May 07, 2005 7:31 pm

Post by Deleter » Wed May 10, 2006 5:26 pm

Lawn? isnt that that flat expanse outside my house? What color was it again? Blue? I only go out at night as the sunlight is lethal...why do you make references to this odd thing in dealing with code, the ultimate source of logic?

:lol:

User avatar
Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:

Post by Theophage » Thu May 11, 2006 4:10 am

Fear the Lawn!

It is the true and original source of the Almighty Grass Tile!

(besides, we're so off topic now, I'm sure we're on somebody's lawn...)
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Re: TI-BASIC program - Need suggestions.

Post by Patz QuickBASIC Creations » Mon Sep 11, 2006 4:53 pm

I feel like an idiot.
An idiot once wrote:To group 800 the old program took 32 hours. See how mine's better?

New: 3 hours
Old: 32 hours
Newest: 80 seconds (thats roughly 1500x faster than the old, and 135x faster than the 'new'



NEW SOURCE: (It's also got a smaler file size! I feel sooo accomplished!)

Input A
{0}>LGROUP
seq(B,B,1,A)>LGRPB
For(C,1,A)
int(rand*dim(LGRPB)+1>D
LGRPB(D)>LGROUP(C)
LGRPB(dim(LGRPB))>LGRPB(D)
dim(LGRPB)-1>dim(LGRPB)
End
DelVar LGRPB
Output(1,1,LGROUP)


Well, it's faster. Anyone care to take a guess at the logic?

Well, I first wanted to speed it up more. So, I thought "Hey, the SortD( is the slowest part, right? Since all I was using it for was getting rid of a single number, I could just move the last number in the list and replace the number that had just been used with it. Now, it doesn't have to go through a sloooowwww... sorting proccess.

User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} » Mon Sep 11, 2006 7:48 pm

Funny timing. I just got a TI-84+ SE and am re-learning TI-BASIC, attempting to make a rather crappy text-game to be expanded later, but my RAM accidentally got cleared earlier today.
Image

User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim » Tue Sep 12, 2006 11:08 am

Using the software and cable that came with it, you can back up the whole thing to a computer and restore it later if you have to. I did this when I upgraded the operating system on my 83+SE.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Tue Sep 12, 2006 4:10 pm

Although, for some odd reason, the TI-84 Plus Silver Edition doesn't run TI-BASIC files as well as the TI-84 Plus. (I have an 84P)

User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} » Wed Sep 13, 2006 2:25 pm

I also noticed that the scrolling on the APPS menu is MUCH slower than on the 83+/84+. It kinda annoys me when scrolling to NoteFolio to write down my homework, but the 1 megabyte of flash memory is worth it.
Image

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Wed Sep 13, 2006 5:04 pm

Aight, here we go, 84+ n00b...
Nathan1993 wrote:I also noticed that the scrolling on the APPS menu is MUCH slower than on the 83+/84+.
2 solutions: Since the 84SE comes with a ton of apps, erase the ones you'll never use. Download OMNICALC from Detached Soultions and activate the Fast App Menu option. It's LIGHTNING fast!
http://detachedsolutions.com/omnicalc/
Nathan1993 wrote:It kinda annoys me when scrolling to NoteFolio to write down my homework
Press ALPHA+N to go straight to the apps that start with N.
Nathan1993 wrote:but the 1 megabyte of flash memory is worth it.
Pffffftt... I can use a flash drive on an 84+.
http://www.ticalc.org/archives/news/art ... 39572.html
(it's fairly new)

Post Reply