My friend challenged me today at school, who can write the smallest quadratic solver? I'm not a TI-Basic fan, but something that easy shouldn't be that hard to put together?

Rules: Smallest program wins (number of bytes)
No calling of other programs/libs.
The program should be able to solve both quadratic equations and linear equation (by inputting a=0).
The program doesn't need to handle errors.

Here's what I've thrown together:

Code:
PROGRAM:A
Prompt A,B,C
If A:Then
-.5B/A→D
√D²-C/A
Disp D-Ans,D+Ans
Else
Disp C/B


Is there any way to shrink this program further? Thanks to all elite Basic hackers here. Smile
You're missing an "End" at the end.
Merthsoft, that is intentional. Smile
I didn't seem to need it, and it was taking up precious space. Smile
{1,-1} is useful where you see the ± operator. I should also note that two forms of the quadratic formula exist. One might end up using fewer bytes than the other. Good Idea
I suppose it does take a byte, but it's bad form to not end your if statements. It could possible cause memory leaks if called from another program.
merthsoft wrote:
I suppose it does take a byte, but it's bad form to not end your if statements.
Two bytes, because of the newline!
The point still stands.
If used inside another program, sure, but when called with the prgm command and subsequently terminated, all bytes that were allocated to it are restored, IIRC. Debate for another topic.
Dropping the End is a perfectly valid way to save space, especially if you just intend to let your program be called from the home screen.
Okay, I've dropped code size a bit with the clues from Weregoose. It's a lot smaller, but I think he have better improvements in mind. However, here's my current code:


Code:
PROGRAM:A
Prompt A,B,C
-.5(B+√(B²-4AC)(B/abs(B
Disp C/Ans
If A:Disp Ans/A


If there are better improvements, I'll need a clue. (Note: If it includes imaginary numbers, please tell me that. I've not learned that yet.)

EDIT: Some serious thought problems here, dunno if code is working
EDIT2: As of now, the code will choke on negative b with c=0.
EDIT3: Should work again, but now the code is on 54 bytes...
EDIT4: Still working, down to 49
add a A=Bi at the beggining. that will solve most problems and should only add a byte or 2. it is in the modes.
How would a+bi change anything? I've not used complex numbers (but I know the basics) so I can't really understand how they would make an impact.
a+bi would prevent the TI OS from giving an error when the parser evaluates an expression to a complex number.
Basically if there ended up being an imaginary number which for future reference is how you make an negative number under a root then it turns the answer to a number plus i

so say your answer is =-5.555555555553234234234i your calculator will only see that answer if you are in A+Bi mode.
I probably should have added that we only care about real numbers. Smile
Dr Phil wrote:
I probably should have added that we only care about real numbers. Smile

But, if the answer was indeed imaginary, you wouldn't want the TI OS breaking your program with ERR: NONREAL ANS. Wink
Hehe, Weregoose has already optimized the quadratic formula to death.


Code:

PROGRAM:QUADFORM
:‾2C/(B+{1,‾1}√(B²+4ACi²


Example calling syntax:

36→A:‾18→B:9/2→C:prgmQUADFORM ► {.25+.25i .25-.25i}

Taken from TI-BASIC vault.
Dr Phil wrote:
Rules: Smallest program wins (number of bytes)
No calling of other programs/libs.
The program should be able to solve both quadratic equations and linear equation (by inputting a=0).
The program doesn't need to handle errors.


Everything in the program is optimized for size.
That is not really handling errors. It is making it work. 50percent of the time you will get an imaginary number. 1 byte will not hurt the size much.
Qwerty.55 wrote:
Hehe, Weregoose has already optimized the quadratic formula to death.


Code:

PROGRAM:QUADFORM
:‾2C/(B+{1,‾1}√(B²+4ACi²


Example calling syntax:

36→A:‾18→B:9/2→C:prgmQUADFORM ► {.25+.25i .25-.25i}

Taken from TI-BASIC vault.


I'm quite sure my version is better than that. Try that code with something like a=2, b=1 and c=0. At least I'm getting a divide by zero error when trying.

EDIT: Might as well explain what I mean. If C or A is zero in that version, it'll throw an error. Dividing by (B±√(B²)) is not really okay. I can agree that having a=0 could be out of scope for a quadratic solver, but having c=0 shouldn't.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement