United-TI: Mega Simple Optimization Questions - United-TI

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Mega Simple Optimization Questions Finding Primes

#1 User is offline   mezz Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 30-January 07

Posted 30 January 2007 - 01:52 AM

I created a simple little program to find prime numbers today.
This is one of my first TI BASIC programs, but I have read a bit about optimization and how to code well on it. I don't have any other coding experience though =/



0->B \\prepare B, used to count number of primes found
2->LPR(1 \\Make sure 2 and 3 are given primes on custom list "L PR"
3->LPR(2

Input "FIND N PRIMES: ",N \\User Inputs number of primes to find and append to list LPR

startTmr->θ \\For timing the different programs. the variable is theta

Lbl A \\Program will look back to here until N primes are found

dim(LPR->D \\number of primes in the list. The list isn't cleared every time so it can add up for later

2+LPR(D->X \\Starting value X is highest known prime + 2 (evens aren't prime)

1->A \\Prep A, used to go up and down the primes list to check for divisibility

Repeat LPR(A)>=(.5LPR(D \\repeats until all the primes up to half the number have been tried. Higher than 1/2 of X and they surely can't divide.

If not(fPart(X/LPR(A \\Checks to see if X is evenly divisible by a known prime. If it is divisible X is incremented by 2 and we start dividing by 3 again.
Then
2+X->X \\not going to deal with evens at all...
1->A \\A will be 2 by the time it tries again
End
A+1->A \\Goes to the next known prime to use as a divisor
End \\repeat ends if all known primes up to 1/2 of X have been tried, so X must be prime

X->LPR(D+1 \\stores a new prime to the end of the primes list

Disp X \\So you can see them whiz by :D, optional

1+B->B \\increments so we can stop it at the desired number of primes

If B!=N
Goto A \\loops everything

checkTmr(θ->L1(1 \\outputs my time




Starting with a blank LPR list:

Without the "disp X" my time is 23 seconds! (for the first 50 primes. setting N to 48, it starts with 2 and 3)
With disp x my time is 26 seconds

I checked this with a known primes list and it works. I am just wondering if I can optimize it more without learning ASM. So far this is the fastest one I've used, and I downloaded a couple so I'm pretty happy :)

This post has been edited by mezz: 30 January 2007 - 01:56 AM


#2 User is offline   Harrierfalcon Icon

  • The Raptor of Calcs
  • PipPipPipPipPipPipPipPipPipPip
  • Group: UTI Members
  • Posts: 2,535
  • Joined: 26-October 06
  • Gender:Male
  • Location:From a nest near Chicago
  • Interests:Programming, video games, programming, origami, programming, eating, programming, reading, programming, spinning pens, programming, palming quarters, programming, etc. I prefer "geek" instead of "nerd."

Posted 30 January 2007 - 01:55 AM

First, you can replace the goto loop with "Repeat B=N". Save a few bytes and time.
Rather than using LPR, use something like L1 or L2, the system lists.

Welcome to UnitedTI!
===== Status =====
Ambitious game w/nitacku, luby, and Super Speler.
Z-Rox 15% (Level Editor: everything's planned out, I just need to find time to do it.)

#3 User is offline   mezz Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 30-January 07

Posted 30 January 2007 - 01:58 AM

Ah thanks, the repeat should help :)
Are the system lists any different? I have plenty of memory and was worried because I have to use my regular system lists sometimes.
I guess it would save bytes in the program though.. hmm. I'll use L6.

Thanks :D!'

Still it's at a healthy 23 seconds. This is kicking the pants off of any of the ones I downloaded and tested the time for.

It should matter a lot more for longer runs of say... N= 997 heh

This post has been edited by mezz: 30 January 2007 - 02:05 AM


#4 User is offline   luby Icon

  • I want to go back to Philmont!!
  • PipPipPipPipPipPipPip
  • Group: UTI Members
  • Posts: 1,477
  • Joined: 23-April 06
  • Location:Somewhere north of Mexico and south of Canada
  • Interests:Mock Trial, Soccer, Programming, Running, Lock Picking, Hanging out here

Posted 30 January 2007 - 02:45 AM

0->b can be delvar b
2->LPR(1
3->LPR(2
can be {2,3->pr(1 // don't need the L

those I see right off the bat.
my first: post program game website
projects:
Linerider Basic: (Done. Download here)
Learning ASM: 97%(Getting some experience with item below)
Linerider ASM: 27% (Status: Compiler broken)
The thing I'm working on with other people: 23% (Back on track)
Helpful symbols(with help from boarder54 and Harrierfalcon): → ÷ ∟ ≥ ≠ ≤ π θ √ ≅ ℕ ∞ ∑ ► ∆ º Ω π ±
No files below this point...
*cough* *achoo!*
developing a sense of humor: ???% (use your imagination)

#5 User is offline   Weregoose Icon

  • Authentic INTJ
  • Icon
  • Group: UTI Staff
  • Posts: 3,957
  • Joined: 25-November 04
  • Gender:Male
  • Location:CA, USA
  • Interests:TI-Basic and Web Development

Posted 30 January 2007 - 02:45 AM

luby said:

2->LPR(1
3->LPR(2
can be {2,3->pr(1 // don't need the L
Not quite. ERR:DATA TYPE on that one.

Use [font="courier new;font-size:9pt;line-height:100%;color:darkblue"]DelVar B{2,3→PR[/font] instead.

mezz said:

Higher than 1/2 of X and they surely can't divide.
Higher than the square root of X and they surely can't divide.

mezz said:

So far this is the fastest one I've used, and I downloaded a couple so I'm pretty happy :)
It's a savvy implementation. Be proud, and welcome to United-TI. :)

#6 User is offline   mezz Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 30-January 07

Posted 30 January 2007 - 02:53 AM

I can see the DelVar, but there may be a problem with "{2,3->L6"
I don't want the whole list to be overwritten, so I can continue it later.

Changed to :
DelVar B2->L6(1
3->L6(2

Changed to: Repeat L6(A)>Sqrt(X

I was trying that before, but I had >= and didn't notice so 9 was "prime" and it messed the whole thing up. With just > it works really, really fast.


13 seconds!!!!!!!

Thanks guys!

EDIT: 11 seconds without Disp X. Jeez! What a difference

Here's the most current version. I decided to use a For( loop because I was incrementing B anyway.
Also, I stored Sqrt(X to Y to save computation
2->L6(1
3->L6(2
Prompt N
For(B,0,N-1
dim(L6->D
2+L6(D->X
1->A
Repeat L6(A)>Sqrt(X
If not(fPart(X/L6(A
Then
2+X->X
1->A
End
1+A->A
End
X->L6(D+1
End

At 111 bytes it's half the size and twice the speed of the rest~
9 seconds for first 50 primes

This post has been edited by mezz: 30 January 2007 - 07:46 AM


#7 User is offline   thornahawk Icon

  • μολών λαβέ
  • PipPipPipPipPip
  • Group: UTI Members
  • Posts: 568
  • Joined: 27-March 05
  • Location:only slightly above the equator
  • Interests:chemical experimentation and computer programming. Reads and writes essays, poems and short stories every so often. :)

Posted 02 February 2007 - 06:20 PM

Reminds me... here's a prime number sieve I wrote a while back, returning all primes less than or same as N:

PROGRAM:SIEVE
Prompt N
int(N/2)→M
M→dim(∟T)
Fill(1,∟T)
2→P:3→K
While K²≤N
For(J,P+K,M,K)
0→∟T(J)
End
Repeat ∟T(P)
P+1→P
End
2P–1→K
End
{2}→P:2→J
For(P,2,M)
∟T(P)→H
If H
2P–1→∟P(J)
J+H→J
End
DelVar ∟T
∟P


thornahawk
"If things are nice, there is probably a good reason why they are nice, and if you do not know at least one reason for this good fortune, then you still have work to do."
- R. Askey
"Life may toss us some ill-conditioned problems, but there is no good reason to settle for an unstable algorithm."
- D.P. O'Leary

E-mail me! | Equation Editor | Comments/Suggestions? | FooPlot for your immediate graphing needs

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users