Author Topic: Pseudo-Recursive Binary Converter  (Read 3676 times)

0 Members and 2 Guests are viewing this topic.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Pseudo-Recursive Binary Converter
« 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.



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;
}
« Last Edit: September 10, 2011, 04:35:20 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Pseudo-Recursive Binary Converter
« Reply #1 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

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Pseudo-Recursive Binary Converter
« Reply #2 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 :)
« Last Edit: September 10, 2011, 04:10:16 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: Pseudo-Recursive Binary Converter
« Reply #3 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.
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Pseudo-Recursive Binary Converter
« Reply #4 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?

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Pseudo-Recursive Binary Converter
« Reply #5 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.
« Last Edit: September 10, 2011, 05:23:26 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)