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 Brain Teasers => TI-BASIC
Author Message
bfr


Member


Joined: 13 Feb 2006
Posts: 108

Posted: 03 Jan 2009 11:52:45 pm    Post subject:

Given a positive real number in A (it might not be an integer), "rotate" it by the amount of places in N to the direction in D (if D=-1, A is rotated left, if D=1, A is rotated right). The result should be returned in Ans. It should work with any positive integer value in N. For example, if A=123.45, D=-1, and N=3 (or N=8, etc.), then 451.21 end up in Ans.

Mine is edit: 94 bytes

EDIT: A is at least 1

EDIT AGAIN (for clarification): All of the digits should be rotated, so if you have something like: 1.000123, after being rotated right once, the result should be 3.100012. With decimals being allowed, some weird things can happen with digits disappearing and stuff...so just make sure it works in general.

I guess it's OK if a few certain cases don't work, since mine isn't perfect (which I don't have time to correct now because I have homework to do, but maybe later this week), but again, just try to have it work in general. I'll try to test and look over my code a bit more before posting BASIC teasers in the future. Razz


Last edited by Guest on 04 Jan 2009 10:08:55 pm; edited 1 time in total
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 04 Jan 2009 12:16:32 am    Post subject:

I suppose you're rotating significant digits only? Otherwise, something like 0.0000000000000000000000000000000000375 would become 5 when rotated right, when 0.0000000000000000000000000000000000537 seems like a more meaningful answer.
Back to top
bfr


Member


Joined: 13 Feb 2006
Posts: 108

Posted: 04 Jan 2009 12:34:06 am    Post subject:

[s]Yeah, only significant digits.[/s] It should work for at least 9 decimal places or so.

Also, for some reason, a certain part stopped working, even though it worked before, so I added an additional token to make it work again (I'll point out which part once I post my solution). I compromised by taking out another token and decreasing the amount of digits it worked for though, so it's still at 120 bytes.

EDIT: Sorry for not including/noticing this earlier, but A is at least 1

EDIT: OK, I should really look over these more before posting them. It's down to 94 bytes.

EDIT AGAIN: Oh, and I didn't read your post carefully enough. All of the digits should be rotated, so if you have something like: 1.000123, after being rotated right once, the result should be 3.100012. With decimals being allowed, some weird things can happen with digits disappearing and stuff...so just make sure it works in general. Razz


Last edited by Guest on 04 Jan 2009 07:31:58 pm; edited 1 time in total
Back to top
Ed H


Member


Joined: 30 Nov 2007
Posts: 138

Posted: 04 Jan 2009 10:32:03 pm    Post subject:

The best I can do right now is 59 bytes, but I'm not sure if that's optimal.

Here goes.
Quote:
[font="Courier New"]:A→B
:While fPart(B
:10Ans→B
:End
:1+int(log(B→M
:B10^(-DN+Mint(DN/M
:A/B(int(Ans)+fPart(Ans)10^(M
Back to top
bfr


Member


Joined: 13 Feb 2006
Posts: 108

Posted: 04 Jan 2009 11:00:23 pm    Post subject:

Heh, I got owned.

Yours looks pretty interesting. I guess I might as well post mine now because yours is better anyway. I tried to do something a little different (although I haven't closely looked at your code yet because I have to get back to my homework :P):

[quote name='"BASIC Code"']:min(seq(E9fPart(A10^B))+B,B,0,9→B
:1+int(log(int(A→C
:B+C→L
:"int(Ans10^(1-C→u
:A
:For(I,1,abs(LfPart(N/L)-Lnot(D-1
:Ans10+u*(10^(‾B)-10^(C
:End
Generated by SourceCoder, © 2005 Cemetech

Where B holds the amount of digits that are in decimal places, and C holds the amount of digits that are in the ones, tens, etc. places. I think my main problem might have been min(seq(E9fPart(A10^B))+B,B,0,9
[/quote]

"Also, for some reason, a certain part stopped working, even though it worked before, so I added an additional token to make it work again (I'll point out which part once I post my solution)." -> This isn't in the code I just posted (this was before I looked over the code again). I think the code looked something like: [LL(LfPart(N/L)-Lnot(D-1 (which I know doesn't work for sometimes when D=1, but that isn't the issue here). It worked with the number 123.45, but not some other similar fractional numbers, giving a domain error or something, which I fixed by adding int(: LL(int(LfPart(N/L)-Lnot(D-1 . In other words, it seemed like sometimes the LfPart(N/L)-Lnot(D-1 expression, even though the result was an integer, treated the result as a fraction. I don't feel like looking further into this right now, but in case anybody else finds this interesting and has an explanation....

EDIT: I fixed the parentheses SourceCoder messed up with 10^(


Last edited by Guest on 04 Jan 2009 11:09:27 pm; edited 1 time in total
Back to top
Ed H


Member


Joined: 30 Nov 2007
Posts: 138

Posted: 05 Jan 2009 01:03:59 am    Post subject:

The idea behind mine is [whiteout]that it's easier to rotate integers, so first I turn A into an integer by repeatedly multiplying by 10. When I'm done, I store the finished integer in B. Then, the other basic idea is, a rotation is a rotation is a rotation. For the number 12345, a rotation right by 3 is a rotation left by 2. So basically I took D * N modulo the number of digits in B, and just rotated B according to that number. After I've rotated the number, I multiply by A/B to get the decimal back. I do this all together on the last 3 lines.[/whiteout]

Edit: Ah, I understand what you mean. You used [whiteout]MfPart(N/M to take N (mod M)[/whiteout], right? Well, sometimes, you might get something like 8.0000000000001, which looks like an integer on the homescreen but throws an error if you use it as a list index. I [whiteout]took N (mod M) using N-Mint(N/M[/whiteout], which doesn't have that problem, and has the advantage of working for negative numbers. That way, I could just take [whiteout]D*N (mod Number of Digits)[/whiteout] and deal with it as one case.


Last edited by Guest on 12 Jul 2010 01:23:03 am; edited 1 time in total
Back to top
bfr


Member


Joined: 13 Feb 2006
Posts: 108

Posted: 05 Jan 2009 06:46:56 pm    Post subject:

Yeah. I actually was originally going to do something like you did ([whiteout]multiply by 10 repeatedly first and then rotate the chunk [/whiteout]), but for some reason I thought that might get the numbers out of order....

And I actually was using [whiteout]N-Mint(N/M for mod[/whiteout] too, but then I switched to [whiteout]MfPart(N/M[/whiteout] because I didn't really think about any advantages that [whiteout]N-Mint(N/M[/whiteout] had, but just that it was larger.

I still probably would have used seq( if I had gone with your method (like I did in my final code) instead of a while loop like you did because I have a habit of automatically using seq( for everything (seq( didn't really work though for my recursive rotating method so I had to use a For loop).


Last edited by Guest on 12 Jul 2010 01:22:47 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
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement