Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: meishe91 on September 10, 2011, 03:58:09 pm

Title: Pseudo-Recursive Binary Converter
Post by: meishe91 on September 10, 2011, 03:58:09 pm
Last semester I took a logic and program design class and had to make a recursive program that turned a decimal number into it's binary equivalent using recursion. This gave me the idea a couple days ago to come up with one in TI-BASIC (don't ask me why, just seemed fun). However, true recursion (or at least the way I was doing it in C++) isn't really possible in TI-BASIC so I had to come up with a way of simulating it, which is what I did. This has no real use or anything, I just thought it was a neat little thing that someone might want to see. Also, in no way is it probably optimized as much as it could be.

Code: (prgmC) [Select]
C→dim(L1
2fPart(A/2→L1(C
iPart(A/2→A
If A>0
Then
C+1→C
prgmC
End
If C>0
Disp L1(C
C-1→C
Output(1,1,"

To use this you need to set C to one and A to the decimal number you want to convert before you run the program.

(http://img.removedfromgame.com/imgs/PseudorRecurse.gif)

Edit:
Here is the code I'm basically trying to simulate.

Note: I know it may not be the best written but I'm still new to C++ and it works so I'm happy.

Code: [Select]
#include <iostream>
using namespace std;
void convert(int num)
{
if(num>0)
{
convert(num/2);
}
else {;}
cout<<num%2;
return;
}
int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
convert(num);
cout<<endl;
system("pause");
return 0;
}
Title: Re: Pseudo-Recursive Binary Converter
Post by: Xeda112358 on September 10, 2011, 04:06:36 pm
Here is a code (that can be optimised, I bet). Also, I use programs like this all the time and my RAM clears a lot, so I make this a lot. This returns the digits backwards:
Code: [Select]
While Ans
Disp 2fPart(Ans/2
iPart(Ans/2
End
It converts the number in Ans to binary.

EDIT: O.O Oh I see what you did there O.O I was looking for your loop when I saw your code and that is a cool method!

EDIT2: Here is a code like that:
Code: [Select]
Disp 2fPart(Ans/2
iPart(Ans/2
If Ans
prgmC
Title: Re: Pseudo-Recursive Binary Converter
Post by: meishe91 on September 10, 2011, 04:09:41 pm
Ya, I know you can do that. I wasn't trying to create the smallest binary converter (especially since Weregoose has that accomplishment under his belt already :P). I was just trying to simulate what I had written in C++.

Edit:
Thanks :)
Title: Re: Pseudo-Recursive Binary Converter
Post by: DrDnar on September 10, 2011, 04:12:29 pm
TI-BASIC programs can recursively call themselves.

The Turing completeness theorem lets us know that anything that can be done with recursion can be done without it, but it might be a lot uglier.
Title: Re: Pseudo-Recursive Binary Converter
Post by: Xeda112358 on September 10, 2011, 04:13:07 pm
It is still cool! I wish I knew how to program in C++. I would love to make a bunch of math programs in it...

EDIT:
Is math Turing Complete?
Title: Re: Pseudo-Recursive Binary Converter
Post by: meishe91 on September 10, 2011, 04:14:27 pm
Well I don't know the exact definition of what recursion is or what it truly is but I just meant how TI-BASIC can't make multiple instances of the same variable and call them back as it fall backs to the original run of the program. If that made any sense.

Edit:
Added the C++ code (or something similar since I wrote it off the top of my head) I was trying to replicate.