This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's TI-BASIC subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. TI-Basic => TI-BASIC
United-TI Archives -> TI-Basic
 
    » Goto page 1, 2  Next
» View previous topic :: View next topic  
Author Message
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 18 Feb 2010 11:25:55 pm    Post subject:

So, first off. Brand new to the site, loving what I have seen so far here Smile Just wanted to say that in my first thread. Ok, now to my question.

I'm using a TI-84+ and have been working on a way to make a decimal to binary program (I know it has been done, but its just something that I'm trying to work out on my own. Yay motivation Razz). I have a theory on how to complete this but the only issue I can't think of a way to program is an efficient way of reversing the variables inside a String. Like say you have "1234" stored to Str1, what I'm asking is if there is a way of restoring the information in that variable to either the same one or another one (so instead of "1234" being stored to Str1 it would be "4321"). I'm sorry if that doesn't make sense, I can try to rephrase it.
I have used the search engine and the routine pages and couldn't find anything. I am sorry if it is out there and I simply missed it.

Thanks for any help Smile
Back to top
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 18 Feb 2010 11:59:50 pm    Post subject:

Now that is all that I have to do to make this program workable really. Everything works fine, you just have to read the final output backwards. Another quick question though, though I wouldn't be surprised if it has been asked before. How this program is working really is that I have "1" stored to Str1 and 0 to Str2 and " " (a space) stored to Str3 (the final outputed String) and I'm adding the strings to make the appropriate binary code. What I'm curious about is if there is a reason that when I had "" (no space) stored to Str3 (instead of the space) it wouldn't work and I would get a Invalid Dimentions Error. I just don't really understand why it is happening. (Its not a huge deal, it just leaves an extra space at the beginning right now, but if I figure out that String reverse thing it won't be an issue.)

Again, thanks for anything.
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 19 Feb 2010 12:41:00 am    Post subject:

Empty strings are not supported in TI-BASIC, so you usually need a dummy character to start with. sub( is the function to use to get rid of the space or whatever other dummy string you use.

thornahawk
Back to top
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 19 Feb 2010 01:11:23 am    Post subject:

thornahawk wrote:
Empty strings are not supported in TI-BASIC, so you usually need a dummy character to start with. sub( is the function to use to get rid of the space or whatever other dummy string you use.

thornahawk

Oh ok, that makes sense. Thank you.
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 19 Feb 2010 01:23:40 am    Post subject:

Welcome to United-TI, Meishe91! :)

Here's how I would go about it:

Supposing you have a string in Str1 that you want to reverse, you'll first want to store a separate string at the start of your program with nothing but a buffer character, (e.g., a space, question mark, or whatever). This is because, as you've discovered, the interpreter doesn't like working with empty strings (and that's really the only reason here that's worth knowing).

Then, for X starting at 1 and incrementally going towards the length( of Str1, you'll want to sub( out the (length(Str1)-X+1)th character from the input string and add it to the tail end of the buffer string.

So, for the well-famed "Hello World", the algorithm would work as follows:

´X´´Substring´´´´Buffer String
´1´´Hello World´´?d
´2´´Hello World´´?dl
´3´´Hello World´´?dlr
´4´´Hello World´´?dlro
´5´´Hello World´´?dlroW
´6´´HelloWorld´´?dlroW
´7´´Hello World´´?dlroW o
´8´´Hello World´´?dlroW ol
´9´´Hello World´´?dlroW oll
10´´Hello World´´?dlroW olle
11´´Hello World´´?dlroW olleH

After the loop, you may use sub( again to remove the "dummy" first character in the output string.

If you're brave, there is another approach which uses only one string, and which effectively concatenates the initial string onto itself in reverse order, and this requires use of the sub( command at the end to extract the entire latter half of the output.

As a final challenge: if you have only one line inside of the For( loop (did I forget to mention that you should use one of these?), you may not need a second variable at all, but instead just keep using Ans in a clever way.

Go forth, and make code. Very Happy


Last edited by Guest on 01 Jul 2010 09:45:13 am; edited 1 time in total
Back to top
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 19 Feb 2010 01:34:07 am    Post subject:

Weregoose wrote:
Welcome to United-TI, Meishe91! Smile

Here's how I would go about it:

Supposing you have a string in Str1 that you want to reverse, you'll first want to store a separate string at the start of your program with nothing but a buffer character, (e.g., a space, question mark, or whatever). This is because, as you've discovered, the interpreter doesn't like working with empty strings (and that's really the only reason here that's worth knowing).

Then, for X starting at 1 and incrementally going towards the length( of Str1, you'll want to sub( out the (length(Str1)-X+1)th character from the input string and add it to the tail end of the buffer string.

So, for the well-famed "Hello World", the algorithm would work as follows:

´X´´Substring´´´´Buffer String
´1´´Hello World´´?d
´2´´Hello World´´?dl
´3´´Hello World´´?dlr
´4´´Hello World´´?dlro
´5´´Hello World´´?dlroW
´6´´HelloWorld´´?dlroW
´7´´Hello World´´?dlroW o
´8´´Hello World´´?dlroW ol
´9´´Hello World´´?dlroW oll
10´´Hello World´´?dlroW olle
11´´Hello World´´?dlroW olleH

After the loop, you may use sub( again to remove the "dummy" first character in the output string.

If you're brave, there is another approach which uses only one string, and which effectively concatenates the initial string onto itself in reverse order, and this requires use of the sub( command at the end to extract the entire latter half of the output.

As a final challenge: if you have only one line inside of the For( loop (did I forget to mention that you should use one of these?), you may not need a second variable at all, but instead just keep using Ans in a clever way.

Go forth, and make code. Very Happy

Thanks Weregoose.

As for understanding all that, I think I got most of it (I'm not the most experienced programmer haha) but I think I kinda get what you mean. I will post some code if I figure some out and see what you think about it when I get to that point.

Thanks for your help.

P.S. I would like to hear more about this "brave" approach if you have the time to explain that a little more.

Edit:
I said I would post the code I created for reversing a String so here I go. It currently is 82 bytes (with the name).


Code:
PROGRAM:REVERSE
"123456→Str1
" →Str2
For(X,1,length(Str1
Str2+sub(Str1,length(Str1)-X+1,1→Str2
End
sub(Str2,2,length(Str2)-1

Along with making this I was able to finally finish my Decimal to Binary program. It is currently 143 bytes (with the name).


Code:
PROGRAM:DECTOBIN
"1→Str1
"0→Str2
" →Str3
" →Str4
Prompt A
While A≥1
A/2→A
If fPart(A)=0
Then
Str3+Str2→Str3
Else
Str3+Str1→Str3
End
iPart(A)→A
End
For(X,1,length(Str3
Str4+sub(Str3,length(Str3)-X+1,1→Str4
End
sub(Str4,2,length(Str4)-1

Note: Neither one of these has been looked at for optimization so I'm sure things can be tweaked. These were just made and then transfered here haha.


Last edited by Guest on 01 Jul 2010 09:46:06 am; edited 1 time in total
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 19 Feb 2010 02:49:04 am    Post subject:

Essentially, this "brave approach" Goose is speaking of uses the input string as the dummy instead of a single character; with that example above, you end up with something like "Hello WorldlroW olleH" before applying sub( to it.

thornahawk
Back to top
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 19 Feb 2010 03:26:39 am    Post subject:

thornahawk wrote:
Essentially, this "brave approach" Goose is speaking of uses the input string as the dummy instead of a single character; with that example above, you end up with something like "Hello WorldlroW olleH" before applying sub( to it.

thornahawk



Oh, ok. I think I get it. So basically instead of having two seperate Strings you would have a single one that would mirror itself in the same one?


Code:
PROGRAM:BRAVE
"123456→Str1
length(Str1→A
For(X,1,A
Str1+sub(Str1,A-X+1,1→Str1
End
sub(Str1,A+1,A

This is 70 bytes (with the name).


Last edited by Guest on 19 Feb 2010 03:29:16 am; edited 1 time in total
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 19 Feb 2010 05:03:40 am    Post subject:

The program for string reversal would work like that, but there's a version hidden somewhere in the forums that hinges on the unique properties of Ans.

As for decimal to binary, witness the following:


Code:
Prompt A
-int(-log(A)/log(2))→C
2^C→F
" →Str0 \\ that's a space
A→M
For(J,1,C
F/2→F:F≤M→T
Str0+sub("01",T+1,1→Str0
M-TF→M
End
sub(Str0,2,C→Str0


or alternatively


Code:
Prompt A
DelVar C:1→F
While F≤A
C+1→C:2F→F
End
" →Str0 \\ that's a space
A→M
For(J,1,C
F/2→F:F≤M→T
Str0+sub("01",T+1,1→Str0
M-TF→M
End
sub(Str0,2,C→Str0


thornahawk
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 19 Feb 2010 11:58:35 am    Post subject:

Edit: I submitted this and then went back and saw your edits at post #6 for the first time. This was not at all a response to your programs nor was it an endeavor to show how to do them differently. And yet, your code does give me further confidence in your merits as a programmer.

Disclaimer: [2nd] [ENTER] on the home screen will probably be useful throughout this post. I don't want to be accountable for any repetitive stress injuries.

Start out with 210 as the input. This is even. Divide it by two to get 105, which is an odd number. Divide that by two to get 52.5 – fractional part notwithstanding, we'll call this "even". Continuing along through 26.25, 13.125, etc., all the way down until no digits linger to the left of the decimal point, the parities will eventually be recorded as: even, odd, even, even, odd, even, odd, odd.

All odd numbers leave a remainder of 1 when divided by two. All even numbers leave a remainder of 0 when divided by two. If we make these substitutions, reverse the order, and then condense the digits, it becomes 11010010. This is exactly the binary representation of our original quantity.

Rather than recursively dividing by two, we can use seq( to rebuild each step individually by dividing by the appropriate power of two. This translates to seq(210/2^(X-1),X,1,B) (for an arbitrary number of bits). We know that we'll have eight bits this time, so we'll use 8→B for now.

It's noteworthy that seq( allows for a negative increment – as each step no longer depends on the one right before it, we can obtain the same line of results coming in from the opposite direction. This saves us from having to reposition the final digits later:

8→B:seq(210/2^(X-1),X,B,1,-1

Compare with the original:

8→B:seq(210/2^(X-1),X,1,B

To determine odd-or-even, we only need to address the integer parts, so we'll use int( or iPart( on the sequence itself.

Come to think of it, let's filter as much out of that seq( command as possible in the meantime.

8→B:int(210/2^seq(X-1,X,B,1,-1

As stated earlier, a number that leaves a remainder when divided by two must be an odd number. So, we'll first use that operation on our list (by multiplying it by one-half) and then take the fractional parts for evaluation:

8→B:fPart(.5int(210/2^seq(X-1,X,B,1,-1

Here we are given a list of two different numbers. We could do Ans≠0 to turn all the nonzeros into ones, or we could simply double all the values inside the list. One-half will essentially become one, and zero will remain zero.

8→B:2fPart(.5int(210/2^seq(X-1,X,B,1,-1

So, that works. But now for the small matter of generalizing B...

It's always going to equal the exponent of the smallest power of two that can fully contain our input (N) – anything less, and we'd be losing digits; anything more, and we'd be tacking on leading zeros. To avoid this kind of loss/redundancy, we'd have to solve 2^B = N beforehand.

Skipping the math lesson, the correct number of digits is given by 1+int(log(N)/log(2)). However, because of issues like this one, we really ought to make that 1+int(round(log(N)/log(2))) instead. Using that as our B, the sequence should maintain the correct length.

This...

1+int(round(log(N)/log(2→B
2fPart(.5int(N/2^seq(X-1,X,B,1,-1

...is not half bad. But we can toy with it to improve it a little.

We'll begin by shifting X's start and end values by negative one, and then compensating for the difference in the first argument:

1+int(round(log(N)/log(2→B
2fPart(.5int(N/2^seq(X,X,B-1,0,-1

Thus we have the same powers of two without explicitly subtracting one each time.

But we see that B, having already been given a plus-one term in the top line, is now losing the same amount in line two. Intuitively, this tell us that this dissociative addend/subtend can be omitted from both lines. But if you want an algebraic excuse, then replace B with its expression and simplify – the ones will simply cancel each other out.

int(round(log(N)/log(2→B
2fPart(.5int(N/2^seq(X,X,B,0,-1

An obvious change here is to remove the →B and use Ans in place of the second B, but I'm leaving it in for your purposes. Think you can expand it into a For( loop and make a string out of it? I'm pretty sure you're capable enough.

–Goose


Last edited by Guest on 01 Jul 2010 09:46:33 am; edited 1 time in total
Back to top
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 19 Feb 2010 07:07:57 pm    Post subject:

thornahawk wrote:
The program for string reversal would work like that, but there's a version hidden somewhere in the forums that hinges on the unique properties of Ans.

As for decimal to binary, witness the following:


Code:
Prompt A
-int(-log(A)/log(2))→C
2^C→F
" →Str0 \\ that's a space
A→M
For(J,1,C
F/2→F:F≤M→T
Str0+sub("01",T+1,1→Str0
M-TF→M
End
sub(Str0,2,C→Str0


or alternatively


Code:
Prompt A
DelVar C:1→F
While F≤A
C+1→C:2F→F
End
" →Str0 \\ that's a space
A→M
For(J,1,C
F/2→F:F≤M→T
Str0+sub("01",T+1,1→Str0
M-TF→M
End
sub(Str0,2,C→Str0


thornahawk

Oh ok. What kind of unique properties does Ans have? If ya don't mind me asking.

For your codes though, the first one doesn't seem to work, though I could have typed it in wrong. It works to an extent but I was getting 11 for both 3 and 4.
The second one seems to work though. Thanks for posting those, they gave me some ideas of how to shorten mine.

Weregoose wrote:
Edit: I submitted this and then went back and saw your edits at post #6 for the first time. This was not at all a response to your programs nor was it an endeavor to show how to do them differently. And yet, your code does give me further confidence in your merits as a programmer.

Disclaimer: [2nd] [ENTER] on the home screen will probably be useful throughout this post. I don't want to be accountable for any repetitive stress injuries.

Start out with 210 as the input. This is even. Divide it by two to get 105, which is an odd number. Divide that by two to get 52.5 – fractional part notwithstanding, we'll call this "even". Continuing along through 26.25, 13.125, etc., all the way down until no digits linger to the left of the decimal point, the parities will eventually be recorded as: even, odd, even, even, odd, even, odd, odd.

All odd numbers leave a remainder of 1 when divided by two. All even numbers leave a remainder of 0 when divided by two. If we make these substitutions, reverse the order, and then condense the digits, it becomes 11010010. This is exactly the binary representation of our original quantity.

Rather than recursively dividing by two, we can use seq( to rebuild each step individually by dividing by the appropriate power of two. This translates to seq(210/2^(X-1),X,1, (for an arbitrary number of bits). We know that we'll have eight bits this time, so we'll use 8→B for now.

It's noteworthy that seq( allows for a negative increment – as each step no longer depends on the one right before it, we can obtain the same line of results coming in from the opposite direction. This saves us from having to reposition the final digits later:

8→B:seq(210/2^(X-1),X,B,1,-1

Compare with the original:

8→B:seq(210/2^(X-1),X,1,B

To determine odd-or-even, we only need to address the integer parts, so we'll use int( or iPart( on the sequence itself.

Come to think of it, let's filter as much out of that seq( command as possible in the meantime.

8→B:int(210/2^seq(X-1,X,B,1,-1

As stated earlier, a number that leaves a remainder when divided by two must be an odd number. So, we'll first use that operation on our list (by multiplying it by one-half) and then take the fractional parts for evaluation:

8→B:fPart(.5int(210/2^seq(X-1,X,B,1,-1

Here we are given a list of two different numbers. We could do Ans≠0 to turn all the nonzeros into ones, or we could simply double all the values inside the list. One-half will essentially become one, and zero will remain zero.

8→B:2fPart(.5int(210/2^seq(X-1,X,B,1,-1

So, that works. But now for the small matter of generalizing B...

It's always going to equal the exponent of the smallest power of two that can fully contain our input (N) – anything less, and we'd be losing digits; anything more, and we'd be tacking on leading zeros. To avoid this kind of loss/redundancy, we'd have to solve 2^B = N beforehand.

Skipping the math lesson, the correct number of digits is given by 1+int(log(N)/log(2)). However, because of issues like this one, we really ought to make that 1+int(round(log(N)/log(2))) instead. Using that as our B, the sequence should maintain the correct length.

This...

1+int(round(log(N)/log(2→B
2fPart(.5int(N/2^seq(X-1,X,B,1,-1

...is not half bad. But we can toy with it to improve it a little.

We'll begin by shifting X's start and end values by negative one, and then compensating for the difference in the first argument:

1+int(round(log(N)/log(2→B
2fPart(.5int(N/2^seq(X,X,B-1,0,-1

Thus we have the same powers of two without explicitly subtracting one each time.

But we see that B, having already been given a plus-one term in the top line, is now losing the same amount in line two. Intuitively, this tell us that this dissociative addend/subtend can be omitted from both lines. But if you want an algebraic excuse, then replace B with its expression and simplify – the ones will simply cancel each other out.

int(round(log(N)/log(2→B
2fPart(.5int(N/2^seq(X,X,B,0,-1

An obvious change here is to remove the →B and use Ans in place of the second B, but I'm leaving it in for your purposes. Think you can expand it into a For( loop and make a string out of it? I'm pretty sure you're capable enough.

–Goose

Ok, I think I followed that haha. It was a little hard because I don't fully understand the sec( function. But from what I did understand I think that it would be interesting to try it out.

As an overall thing to both of you, thanks

Update:
I have been working on my code and have brought it down to 98 bytes (without name). Very Happy
It is no where near as small as it could be, considering I'm still going over the things Weregoose said. I did however finally see Teaser #28 "Reverse String" that had that code Thornahawk was talking about with the properties of Ans which helped (the code is Darkerline's). Also, I just did some minor tweaking too.


Code:
PROGRAM:DECTOBIN
" →Str3
Prompt A
While A≥1
iPart(A→A
A/2→A
If not(fPart(A
Then
Str3+"0→Str3
Else
Str3+"1→Str3
End
End
For(X,1,length(Ans)-1
sub(Ans,2X,1)+Ans
End
sub(Ans,1,X

(If it isn't that clear yet, I'm trying to do this all myselft. So I'm not like just copy/pasting random code I don't understand and such. Won't become a better programmer otherwise.) Smile


Last edited by Guest on 01 Jul 2010 09:47:25 am; edited 1 time in total
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 24 Feb 2010 04:08:33 am    Post subject:


Code:
If not(fPart(A
Then
Str3+"0→Str3
Else
Str3+"1→Str3
End


can be


Code:
Str3+sub("10",1+not(fPart(A)),1→Str3


instead.

Good luck learning! :)

thornahawk
Back to top
Xeda112358


Active Member


Joined: 19 May 2009
Posts: 520

Posted: 24 Feb 2010 01:10:29 pm    Post subject:

Here is a code I have made. It doesn't do the string reverse thing, but you can add that. Also, instead of a prompt, use something like this:

10:prgmDECTOBIN

10 is the number to convert. This program makes use of "the unique properties of Ans." The string is also stored in Ans at the end of the program.

PROGRAM:DECTOBIN
:Ans→B
:iPart(log(Ans)/log(2→C
:"0→Str1
:For(A,C,0,-1
:"0
:If B≥2^A
:Then
:B-2^A→B
:"1
:End
:Str1+Ans→Str1
:End
:sub(Str1,2,length(Str1)-1
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 24 Feb 2010 05:14:00 pm    Post subject:

If the only reason you want to reverse a string is to make the output list the digits in a correct order, there's an easier way to do it. Replace Str1+Ans→Str1 with Ans+Str1→Str1 -- this way, the digits are added to the front, instead of to the back. At the end, you'll have to replace sub(Str1,2,length(Str1)-1 with sub(Str1,1,length(Str1)-1 because now, you want to remove the last character instead of the first.

Last edited by Guest on 01 Jul 2010 09:49:21 am; edited 1 time in total
Back to top
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 24 Feb 2010 10:03:31 pm    Post subject:

thornahawk wrote:

Code:
If not(fPart(A
Then
Str3+"0→Str3
Else
Str3+"1→Str3
End


can be


Code:
Str3+sub("10",1+not(fPart(A)),1→Str3


instead.

Good luck learning!

thornahawk

Sweet! Thanks, I never thought about that. I knew there would be a different, more efficient, way to do that but couldn't think of how to do it. Thank you, Thornahawk. Very Happy

DarkerLine wrote:
If the only reason you want to reverse a string is to make the output list the digits in a correct order, there's an easier way to do it. Replace Str1+Ans→Str1 with Ans+Str1→Str1 -- this way, the digits are added to the front, instead of to the back. At the end, you'll have to replace sub(Str1,2,length(Str1)-1 with sub(Str1,1,length(Str1)-1 because now, you want to remove the last character instead of the first.

ThunderBolt's program doesn't use a string reverse section in his code. That's mine that uses it. His is storing Ans (which is either 1 or 0, depending on the conditions) onto the end of Str1. Storing the numbers in order from the start. I think you over looked that (no offence).

By the way, I made a Binary to Decimal program that I'm currently optimizing. Can post the code if you guys would like.


Last edited by Guest on 01 Jul 2010 09:39:06 am; edited 1 time in total
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 25 Feb 2010 09:03:01 am    Post subject:

"Binary to Decimal Program"

- You might appreciate this hint. :)

thornahawk
Back to top
kinkoa


Member


Joined: 28 Jul 2009
Posts: 103

Posted: 25 Feb 2010 11:16:50 am    Post subject:

hmm this is cool
Back to top
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 25 Feb 2010 07:50:57 pm    Post subject:

thornahawk wrote:
"Binary to Decimal Program"

- You might appreciate this hint. Smile

thornahawk

What exactly do you mean? Sorry, I just don't see how that helps (no offence, just don't understand the formula haha).

shadowking wrote:
hmm this is cool

This thread? Haha.

Oh, and here is the program code for the Binary to Decimal just to show what I have.


Code:
PROGRAM:BINTODEC
DelVar AInput Str1
For(X,1,length(Str1
A+2^(length(Str1)-X)(sub(Str1,X,1)="1→A
End
Ans

It is currently 66 bytes with the name, 58 without.


Last edited by Guest on 25 Feb 2010 07:53:19 pm; edited 1 time in total
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 26 Feb 2010 02:39:30 am    Post subject:

Meishe91 wrote:
Sorry, I just don't see how that helps.


Well, the good thing about Horner is that you trade off having to do an exponentiation and an addition for every pass of your loop into a multiplication and an addition, which is less work. Your program usually ends up cleaner and faster.

Instead of doing something like b0×20+b1×21+b2×22+b3×23, which requires four exponentiations, four multiplications, and three additions, you do b0+2(b1+2(b2+2b3)), which has no exponentiations. (I leave the counting of the additions and multiplications to you.)

To use your example,


Code:
PROGRAM:BINTODEC
DelVar A
Input Str1
For(X,1,length(Str1
A+2^(length(Str1)-X)(sub(Str1,X,1)="1→A
End
Ans


becomes


Code:
PROGRAM:BINTODEC
Input Str1
0
For(X,1,length(Str1
2Ans+sub(Str1,X,1)="1
End
Ans


which, if you use the expr( function, becomes even simpler:


Code:
PROGRAM:BINTODEC
Input Str1
0
For(X,1,length(Str1
2Ans+expr(sub(Str1,X,1
End
Ans


:)

thornahawk


Last edited by Guest on 26 Feb 2010 02:46:23 am; edited 1 time in total
Back to top
meishe91


Newbie


Joined: 06 Dec 2009
Posts: 49

Posted: 26 Feb 2010 07:07:34 pm    Post subject:

thornahawk wrote:
Meishe91 wrote:
Sorry, I just don't see how that helps.


Well, the good thing about Horner is that you trade off having to do an exponentiation and an addition for every pass of your loop into a multiplication and an addition, which is less work. Your program usually ends up cleaner and faster.

Instead of doing something like b0×20+b1×21+b2×22+b3×23, which requires four exponentiations, four multiplications, and three additions, you do b0+2(b1+2(b2+2b3)), which has no exponentiations. (I leave the counting of the additions and multiplications to you.)

To use your example,


Code:
PROGRAM:BINTODEC
DelVar A
Input Str1
For(X,1,length(Str1
A+2^(length(Str1)-X)(sub(Str1,X,1)="1→A
End
Ans


becomes


Code:
PROGRAM:BINTODEC
Input Str1
0
For(X,1,length(Str1
2Ans+sub(Str1,X,1)="1
End
Ans


which, if you use the expr( function, becomes even simpler:


Code:
PROGRAM:BINTODEC
Input Str1
0
For(X,1,length(Str1
2Ans+expr(sub(Str1,X,1
End
Ans




thornahawk

Ok. So a couple things haha.
I see how the code that you provided works, which is good. Which I thank you for.
The only thing I don't understand is how exactly you got it haha. I understand (I think) how Horner's Rule works
(ex. y=x4+x3+x2+x1+1=x(x(x(x(1)+1)+1)+1)+1)
but I don't quite understand how you got
Ans+2^(length(Str1)-X)(sub(Str1,X,1)="1
to be equal to
2Ans+sub(Str1,X,1)="1.
Well, that isn't entirely true. I was able to solve it out knowing that
Ans+2^(length(Str1)-X)=2Ans (using substitution).
I just don't get how to do it without knowing that.
Sorry about asking for a math lesson here haha. You definitely don't have to explain it if you don't want to/have the time.
Again though, thanks for all the help. Smile


Last edited by Guest on 01 Jul 2010 09:38:29 am; edited 1 time in total
Back to top
Display posts from previous:   
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
    » Goto page 1, 2  Next
» View previous topic :: View next topic  
Page 1 of 2 » All times are UTC - 5 Hours

 

Advertisement