What's wrong with the code?
Forum » Programming Help / TI-83 Programming » What's wrong with the code?
Started by: YatzyYatzy
On: 1265748356|%e %b %Y, %H:%M %Z|agohover
Number of posts: 18
rss icon RSS: New posts
Summary:
Prime Number
What's wrong with the code?
YatzyYatzy 1265748356|%e %b %Y, %H:%M %Z|agohover

Hello, I've been trying to make a program that lists all prime numbers in as few rows as possible. Now what I wrote lists all numbers instead of just the prime numbers but I cant see the mistake I made….

ClrHome
SetupEditor L(PRIME)
50->dim(LPRIME)

For (A,2,50) 
For (B,2,A-1)
(fPart(A/B)=0)+A->A (I wan't to go to the next A if I found out that the current isn't a prime number. I think I did something wrong here)
If B=A-1
A-> LPRIME(A)
End
End
LPRIME

Now the List order becomes :0,2,3,4,5,6. etc, for some strange reason.

Help Appreciated

Thanks

Last edited on 1265748650|%e %b %Y, %H:%M %Z|agohover By Yatzy + Show more
Reply  |  Options
Unfold What's wrong with the code? by YatzyYatzy, 1265748356|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
ztrumpetztrumpet 1265755834|%e %b %Y, %H:%M %Z|agohover

Look carefully at the variables you're using.

Then you realize "Oh no! A is used in the For loop and elsewhere! I may have to change the "extra A" to C."
Basicly you are storing A back onto itself, where you need to change one of the variables to C. It's a pretty simple mistake, but it's so easy to mis. Good luck! :)


Find more info about my projects on Omnimaga:
http://www.omnimaga.org/index.php

Reply  |  Options
Unfold Re: What's wrong with the code? by ztrumpetztrumpet, 1265755834|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
Edward HEdward H 1265758841|%e %b %Y, %H:%M %Z|agohover

Few things:

  • When you initialize with 50→dim(LPRIME), it could start out with anything. What you want to do is delete it, or store {0} to it so that when you change it's dimension to 50, it'll be all 0s.
  • You don't need to divide by every number before 2 to A-1 to find out whether A is prime or composite: in fact, you only need to check whether A is divisible by every prime less than or equal to √A. Doing so basically reduces the number of divisions you need to make by a ridiculous amount.
  • When you initialize a For loop like For(X,1,Y), and the value of Y changes inside the loop, the loop will still end on the same number. The loop evaluates the expression of the end-value once, and saves its value. which is why it makes sense to write loops like For(X,1,X). So, for your second loop, even if you increment A, The end value of that loop isn't being incremented at all. What you need to do is to somehow manually exit the loop.
  • For your outermost loop, you can limit yourself to only checking odd numbers, so you can write that loop For(A,3,49,2), which will skip every even number.
  • You'll be able to store a lot more primes in your list if you write LPRIME as {2,3,5,7,11,13,…} rather than {0,2,3,0,5,0,7,0,0,0,11,0,13,…}. So when you discover that A is prime, store it to the next element of LPRIME, rather than the Ath element. A→LPRIME(1+dim(LPRIME))
  • This isn't really important, but you can save a few bytes by omitting end paragraphs.

Good luck, son.

Last edited on 1265758889|%e %b %Y, %H:%M %Z|agohover By Edward H + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by Edward HEdward H, 1265758841|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
YatzyYatzy 1265801700|%e %b %Y, %H:%M %Z|agohover

Wow, looks like I've got alot to learn. I got some followup questions just; the number represents the dots:
1. Isn't setupeditor supposed to do that for me?
2. Ok, but how do I pick these out from the list?
5. Oh, I misunderstood the Wikipage. But why 1+dim??

I really appreciate your help, I'm here to learn.

Thanks, dad.

Last edited on 1265802386|%e %b %Y, %H:%M %Z|agohover By Yatzy + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by YatzyYatzy, 1265801700|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
SkwerlmanSkwerlman 1265808445|%e %b %Y, %H:%M %Z|agohover

SetUpEditor simply unarchives the list and moves it to the editor if it exists, and creates it if not. You still have to zero it out with another command.


184909387445f32626a63bf.gif
Reply  |  Options
Unfold Re: What's wrong with the code? by SkwerlmanSkwerlman, 1265808445|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
ztrumpetztrumpet 1265839135|%e %b %Y, %H:%M %Z|agohover

Thanks for the awesome reply. =) It's a lot better than mine, plus I'm pretty sure Yatzy learned a lot form it. Great job!


Find more info about my projects on Omnimaga:
http://www.omnimaga.org/index.php

Reply  |  Options
Unfold Re: What's wrong with the code? by ztrumpetztrumpet, 1265839135|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
YatzyYatzy 1265826757|%e %b %Y, %H:%M %Z|agohover

Ok,thanks for the answers! 2 problems left.

1. When limiting B to the root of A, my program fails at 21. Root of 21 is 4. It never gets to try out 7, because its larger than the root of 7. Did I do something wrong?
2. When I try display A , the prime numbers are correct. Although the list only shows 0's in the beginning, then comes the prime number, then more 0's. Why's that?
3. See code, just a question

Here's my new code:

ClrHome
SetUpEditor LPRIME
50->dim (LPRIME)
ClearList LPRIME
For (A,3,49,2)
For(B,2,iPart(sqrt A)
(fPart(A/B)=0)->C (For some reason, I have to do +C in the left part, can anyone explain why?
End                       
If C=0:Then
A->LPRIME(1+dim(LPRIME)
Else:
0->C
End
End
LPRIME

And about end paragraphs,please, if it's a short explanation or you have a link for this, I would like to know. Sorry for all the questions, if you prefer answering them on ventrilo or msn or whatever, just tell me.

Last edited on 1265831903|%e %b %Y, %H:%M %Z|agohover By Yatzy + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by YatzyYatzy, 1265826757|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
calccryptocalccrypto 1265834248|%e %b %Y, %H:%M %Z|agohover

here's a bit of an optimization:

ClrHome
1->D
{0->dim(LPRIME

For (A,3,49,2)
0->C
For(B,2,iPart(sqrt A)
(fPart(A/B)=0)+C->C         \\ Ans to #3: you need the +C because every time the B loop is run, C is written over, so it might be 0->0->->1->0->0
                            \\ so the output you see is 0. when you do +C, C will include all the answers from before, like 0->0->0->1->1->1->2->2,etc
End                       

If C=0                    \\ Ans to #2: in your code, its making the list bigger, adding 3,5,7,etc to the end of fifty 0s. this will add to the end of the 
Then                      \\ the shortest possible list that LPRIME can be, so at 5, the list will be 2 long, not 52
C->LPRIME(D
D+1->D
End

End
LPRIME

Visit Calccrypto for info on crypto

Last edited on 1265838014|%e %b %Y, %H:%M %Z|agohover By calccrypto + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by calccryptocalccrypto, 1265834248|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
YatzyYatzy 1265839627|%e %b %Y, %H:%M %Z|agohover

Ah, thats brilliant! But for some reason I get data type error at {0->dim(LPRIME. It works great without the { though, so finally my program is complete! :D. Thanks for the help everyone, think I got a good understanding on lists now :D. I'm still just doing rookie programs, but I'm progressing as fast as I can!

Since you guys got understanding for this, you know the answer for #1 also? For future knowledge.

Last edited on 1265840127|%e %b %Y, %H:%M %Z|agohover By Yatzy + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by YatzyYatzy, 1265839627|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
calccryptocalccrypto 1265840795|%e %b %Y, %H:%M %Z|agohover

oh oops. i meant

{0->PRIME

no little L or dim(

and for 21, it shouldnt need to go to 7. since 21/3 = 7, C will not be 0. at least it shouldnt be 0


Visit Calccrypto for info on crypto

Last edited on 1265840994|%e %b %Y, %H:%M %Z|agohover By calccrypto + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by calccryptocalccrypto, 1265840795|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
Edward HEdward H 1265844557|%e %b %Y, %H:%M %Z|agohover

Good job. There's still some room for improvement in your algorithm, though. First, since you're only going through odd values of A, there's no need to test for division by 2. In fact, there's no need to test for division by 2, 4, 6, 8, 10, etc. because we already know that an even number can't divide an odd number. So starting B at 3 and skipping every other value can basically halve the number of trial divisions needed.

That's an easy optimization. If you think about it, there's a lot more divisions you can skip. For example, you never need to trial divide by 9. Why is this? Because you already divided by 3 and failed; if A is not a multiple of 3 then it is for sure not a multiple of 9. Basically, you don't need to do trial division with any composite numbers, because if you're dividing A by a composite number, you've already tried dividing A by all of its prime factors and failed. So the loop For(B,2,iPart(√(A))) could be hugely optimized if you could somehow only divide by prime numbers.

Now how would you do that? It sure would be helpful to have a list of all the primes less than or equal to √(A), wouldn't it…?

So here's my version of the prime finding program:

:{2,3→PRM
:For(N,5,50,2
:1
:While Ans and ∟PRM(Ans+1)²≤N
:Ans+1
:Ansnot(not(fPart(N/∟P(Ans
:End
:If Ans
:N→∟PRM(1+dim(∟PRM
:End
:∟PRM

The way I've written this program is concise and fairly optimized, but it's hard to understand and kind of hides the way the algorithm works. I don't recommend trying to program all your programs this way when you start out, and I'm not suggesting that this is the best or the only way to do it.

Last edited on 1266179385|%e %b %Y, %H:%M %Z|agohover By Edward H + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by Edward HEdward H, 1265844557|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
YatzyYatzy 1265905395|%e %b %Y, %H:%M %Z|agohover

Sorry for the late answer, since I live in Europe , I'm usually asleep when you're not. Now I tried to test your program in my calculator, but I got dimension error at ∟PRM(Ans+1). Anyway, the code seems like an awesome optimization, although for me it's indeed very hard to understand. I tried as well as I could, but the 2 main rows are kind of too advanced for me.

I would gladly listen if you would take time to explain it if you want, otherwise I'm grateful you put your time to do this work.

Reply  |  Options
Unfold Re: What's wrong with the code? by YatzyYatzy, 1265905395|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
Edward HEdward H 1265936660|%e %b %Y, %H:%M %Z|agohover

Whoops, I entered the first line wrong. It should be {2,3→PRM, not {2,3→P It makes sense there'd be a dimension error if list PRM doesn't even exist.

Anyway, try and implement the optimization I mentioned, it's good practice for using lists.

Reply  |  Options
Unfold Re: What's wrong with the code? by Edward HEdward H, 1265936660|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
YatzyYatzy 1266098936|%e %b %Y, %H:%M %Z|agohover

Ok here's my finished code:

{2,3->LPRIME
For (A,5,50,2)
0->C
1->E
While LPRIME(E)<iPart(sqrtA)
((fPart(A/LPRIME(E))=0)+C-> C
1+E->E
End
If C=0
A->LPRIME(1+dim(LPRIME)
End
LPRIME
Last edited on 1266140598|%e %b %Y, %H:%M %Z|agohover By Yatzy + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by YatzyYatzy, 1266098936|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
Edward HEdward H 1266177086|%e %b %Y, %H:%M %Z|agohover

Nice job. You do want to change the While condition to use a less than or equal to sign (≤).

Otherwise, you'll never end up dividing 9 by 3, because 3 < iPart(sqrt(9)) is false. And then 9 will be a prime. Also, the iPart is extraneous, because if 19≤iPart(19.6), then also 19≤19.6 and vice versa.

A brief explanation on how my program works: I combine your "E" and "C" variables into one variable in Ans. The while loop makes sure that Ans is not zero, and that the prime is less than the square root of the number. Then, inside my loop, I increment Ans by 1, unless the division works, in which case I set Ans to 0. If the number is composite, Ans will be 0 as we exit the loop, and if the number is prime, then it'll be not zero.

Reply  |  Options
Unfold Re: What's wrong with the code? by Edward HEdward H, 1266177086|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
YatzyYatzy 1266178520|%e %b %Y, %H:%M %Z|agohover

Sorry, I should have pointed out that it was a less or equal to sign, I just didn't know how to print the symbol. But thanks for pointing that out, aswell as the iPart.

Now I used the list to make a program which divides a number into it's prime factors :D. Thanks again for all the help.

Reply  |  Options
Unfold Re: What's wrong with the code? by YatzyYatzy, 1266178520|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
 brt93yoda brt93yoda 1266212993|%e %b %Y, %H:%M %Z|agohover

If you wanted to print less than or equal to without using the little char you can type: <= For greater than or equal to you type: >=

Reply  |  Options
Unfold Re: What's wrong with the code? by  brt93yoda brt93yoda, 1266212993|%e %b %Y, %H:%M %Z|agohover
Re: What's wrong with the code?
YatzyYatzy 1266140435|%e %b %Y, %H:%M %Z|agohover

UPDATE: Please see previous post. I tried using "Ans" buut I thought this way was easier. Now you probably find another way of optimizing it even more :P, but if you don't, any ideas for another practise program would be great!

/Yatzy

Last edited on 1266170465|%e %b %Y, %H:%M %Z|agohover By Yatzy + Show more
Reply  |  Options
Unfold Re: What's wrong with the code? by YatzyYatzy, 1266140435|%e %b %Y, %H:%M %Z|agohover
New Post