So I need to think of a good example of a program that can be carried through from planning, deciding on functions, to writing, unit-testing, testing, and debugging. TI-BASIC, of course, and it can use loops, jumps, conditionals, menus, and subprograms. I asked on IRC, and there were some good ideas, such as a Menu-based RPG and a random number-guessing game. I already discuss a number-guessing game somewhere, so I took the number-guessing game idea and thought of a dice-rolling game (game?) of some sort. I'm not 100% on that, though, so any other ideas? Ideally, this program wouldn't be more than 30 or 40 lines.
rfdave wrote:
Wow, that's actually a really awesome idea. ![Very Happy](images/smiles/icon_biggrin.gif)
http://projecteuler.net/problem=9
I think if I expanded it to ask the user for a number to look for the sum for, rather than just using 1000, and perhaps some kind of progress display, that that might be a neat example program.
pong. I didn't realize how many people have trouble with it, until there was recently an assignment on that in one of the other comp sci classes.
graphmastur wrote:
pong. I didn't realize how many people have trouble with it, until there was recently an assignment on that in one of the other comp sci classes.
I actually thought of that, but sadly, they (you?) don't learn getKey until the next chapter. I could make it be a bouncing ball, but no paddle. :S However, please keep on me to make Pong be an example in some later chapter!
I would look for more-concrete examples that aren't flush with avenues for people taking creative liberties with the designs. Pong can go in several directions, and a beginner might mistake a demonstration of one of them as the "right" way to go about it.
Weregoose wrote:
I would look for more-concrete examples that aren't flush with avenues for people taking creative liberties with the designs. Pong can go in several directions, and a beginner might mistake a demonstration of one of them as the "right" way to go about it.
Weregoose, with that in mind, could you suggest a (sane) sort of math or entertaining program that would fit the bill and be sufficiently concrete? What do you think about my Project Euler idea based on rfdave's excellent suggestion? KermMartian wrote:
Weregoose wrote:
I would look for more-concrete examples that aren't flush with avenues for people taking creative liberties with the designs. Pong can go in several directions, and a beginner might mistake a demonstration of one of them as the "right" way to go about it.
Weregoose, with that in mind, could you suggest a (sane) sort of math or entertaining program that would fit the bill and be sufficiently concrete? What do you think about my Project Euler idea based on rfdave's excellent suggestion?The Pythagorean Triple assignment should be a fun one (and it was; I just tried it). Ingenuity there makes the difference between the number of For( loops employed.
Hmm, now I'm intrigued how I might get better than O(n^2/2) = O(n^2) performance on that.
This should indeed be a fun example to present, I think.
![Very Happy](images/smiles/icon_biggrin.gif)
I would like this better than another game example because, at the end of the day, a calculator is intended to do mathematical computations, not play games...
rfdave wrote:
I would like this better than another game example because, at the end of the day, a calculator is intended to do mathematical computations, not play games...
What it's intended for and what people are able to stretch their imaginations to create are two different things, but for this particular chapter, I agree that a math program would be a good example.
*bump* (Premature bump, I know; sue me
). For what it's worth, here's the version that I came up with. I'm sure Weregoose might see some insane version that uses seq() to complete this in O(log(n)) or something, but here goes:
![Wink](images/smiles/icon_wink.gif)
BASIC Code wrote:
:ClrHome
:Disp "FINDS A PYTHAGOR","EAN TRIPLET","A B C (A²+B²=C²)","WHERE A+B+C=N","AND A<B<C
:Input "TARGET N=",N
:For(A,1,N-1
:For(B,A+1,N-1
:If A+B+√(A²+B²)=N
:Then
:ClrHome
:Disp "FOUND:","A=","B=","C=","N=
:Output(2,3,A
:Output(3,3,B
:Output(4,3,√(A²+B²
:Output(5,3,N
:Pause
:Return
:End
:End
:End
:ClrHome
:Disp "NO RESULTS
Generated by SourceCoder, © 2005-2012 Cemetech
:Disp "FINDS A PYTHAGOR","EAN TRIPLET","A B C (A²+B²=C²)","WHERE A+B+C=N","AND A<B<C
:Input "TARGET N=",N
:For(A,1,N-1
:For(B,A+1,N-1
:If A+B+√(A²+B²)=N
:Then
:ClrHome
:Disp "FOUND:","A=","B=","C=","N=
:Output(2,3,A
:Output(3,3,B
:Output(4,3,√(A²+B²
:Output(5,3,N
:Pause
:Return
:End
:End
:End
:ClrHome
:Disp "NO RESULTS
Generated by SourceCoder, © 2005-2012 Cemetech
One thing I notice, are some optimizations like removing end parens and quotation marks; wouldn't it be wise to inform the reader of the optimizations at a later time, to lead away from more potential bad-optimization tricks and general confusion? Plus, if the reader moves on to use a Computer language, they won't be all "WTF mate" when those tricks don't work.
Unless of course you wisely integrated a section on optimization earlier and I obviously don't know about it.
Unless of course you wisely integrated a section on optimization earlier and I obviously don't know about it.
![Razz](images/smiles/icon_razz.gif)
As you'll find out if you (please?) sign up for the MEAP that's launching in less than a week, I gradually introduced leaving off things like ending parentheses and quotes after first teaching them to write commands with all the closing "punctuation" intact.
But it's a good point, thanks for thinking of that.
![Smile](images/smiles/icon_smile.gif)
- Weregoose
- Cemetech Expert (Posts: 573)
-
- 22 Feb 2012 04:48:58 pm
- Last edited by Weregoose on 22 Feb 2012 06:12:23 pm; edited 5 times in total
Find integers u and v such that u+v+√(u²+v²) = k.
u+v+√(u²+v²) = k
√(u²+v²) = k-u-v
u²+v² = (k-u-v)²
u²+v² = k²-2ku+u²-2kv+2uv+v²
0 = k²-2ku-2kv+2uv
2ku-2uv = k²-2kv
u(2k-2v) = k²-2kv
u = (k²-2kv)/(2k-2v)
u = (k²/2-kv)/(k-v)
u = k(k/2-v)/(k-v)
u = k((k-k/2)/(v-k)+1) // (p-r)/(q-r) = (q-p)/(r-q)+1
u = k(k-k/2)/(v-k)+k
u = k(k/2)/(v-k)+k
u = k²/(2(v-k))+k
Our task now is to find a v for which u stays an integer.
With k = 1000, the smallest such pair is (200,375).
√(200²+375²) = 425, and 200+375+425 = 1000.
Another little fact is that if k isn't divisible by 3, you can increment v by 3 in the hunt for u. If k isn't divisible by 4, you can increment v by 4.
Code:
ETA: A009096
u+v+√(u²+v²) = k
√(u²+v²) = k-u-v
u²+v² = (k-u-v)²
u²+v² = k²-2ku+u²-2kv+2uv+v²
0 = k²-2ku-2kv+2uv
2ku-2uv = k²-2kv
u(2k-2v) = k²-2kv
u = (k²-2kv)/(2k-2v)
u = (k²/2-kv)/(k-v)
u = k(k/2-v)/(k-v)
u = k((k-k/2)/(v-k)+1) // (p-r)/(q-r) = (q-p)/(r-q)+1
u = k(k-k/2)/(v-k)+k
u = k(k/2)/(v-k)+k
u = k²/(2(v-k))+k
Our task now is to find a v for which u stays an integer.
With k = 1000, the smallest such pair is (200,375).
√(200²+375²) = 425, and 200+375+425 = 1000.
Another little fact is that if k isn't divisible by 3, you can increment v by 3 in the hunt for u. If k isn't divisible by 4, you can increment v by 4.
Code:
:Input N
:1+max({2,3}not(fPart(N/{3,4
:For(X,Ans,N-1,Ans
:N+N²/(2(X-N
:If fPart(Ans
:End
:If X<N-1
:{X,Ans,R►Pr(X,Ans
ETA: A009096
As a side note, I should point out that I generalized the problem to "Given some arbitrary N, find A, B, C such that 0<A<B<C (and obviously C<N) where A+B+C=N". Of course all the tricks you mentioned are awesome and valid, but do you think perhaps that might fly over their heads? Perhaps after I work them through the process of building to my solution I'll show how you can massively reduce the search space like that.
KermMartian wrote:
*bump* (Premature bump, I know; sue me
). For what it's worth, here's the version that I came up with. I'm sure Weregoose might see some insane version that uses seq() to complete this in O(log(n)) or something, but here goes:
![Wink](images/smiles/icon_wink.gif)
BASIC Code wrote:
:ClrHome
:Disp "FINDS A PYTHAGOR","EAN TRIPLET","A B C (A²+B²=C²)","WHERE A+B+C=N","AND A<B<C
:Input "TARGET N=",N
:For(A,1,N-1
:For(B,A+1,N-1
:If A+B+√(A²+B²)=N
:Then
:ClrHome
:Disp "FOUND:","A=","B=","C=","N=
:Output(2,3,A
:Output(3,3,B
:Output(4,3,√(A²+B²
:Output(5,3,N
:Pause
:Return
:End
:End
:End
:ClrHome
:Disp "NO RESULTS
Generated by SourceCoder, © 2005-2012 Cemetech
:Disp "FINDS A PYTHAGOR","EAN TRIPLET","A B C (A²+B²=C²)","WHERE A+B+C=N","AND A<B<C
:Input "TARGET N=",N
:For(A,1,N-1
:For(B,A+1,N-1
:If A+B+√(A²+B²)=N
:Then
:ClrHome
:Disp "FOUND:","A=","B=","C=","N=
:Output(2,3,A
:Output(3,3,B
:Output(4,3,√(A²+B²
:Output(5,3,N
:Pause
:Return
:End
:End
:End
:ClrHome
:Disp "NO RESULTS
Generated by SourceCoder, © 2005-2012 Cemetech
I may be missing the point, (or blind) but you could also add some code to find ALL of the sides. Would be easy. again, I MAY be blind and missing where you do this.
Sounds like you're in the right audience for me to bounce the material in this chapter off of you, then.
The idea is to find where A+B+C=N, and remember that A^2+B^2=C^2, or C=sqrt(A^2+B^2). Therefore, this line:
If A+B+√(A²+B²)=N
is equivalent to A²+B²+C².
Edit: Weregoose, I decided to show the one-loop solution as an extra add-on, a "never decide your program is complete" sort of thing. I even managed to work through the derivation and get the same equation as you!
![Smile](images/smiles/icon_smile.gif)
If A+B+√(A²+B²)=N
is equivalent to A²+B²+C².
Edit: Weregoose, I decided to show the one-loop solution as an extra add-on, a "never decide your program is complete" sort of thing. I even managed to work through the derivation and get the same equation as you!
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
» Go to Registration page
» Goto page 1, 2 Next
» View previous topic :: View next topic
» View previous topic :: View next topic
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
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