I once wrote an algorithm that I originally generated from a fractal. It was a lot easier for me to come up with the rules for the fractal and then use the image for the algorithm than it was to solve the algorithm problem directly. However, yesterday, after nt touching this problem in almost a year, it randomly popped into my head with a solution, so I have the algorithm below in TI-BASIC (since I had my calculator nearby, I tested it on that). What I am wondering is how to optimise it. I tried to generate it with a binary expansion because ultimately, that is what I will be using it with, but it got too messy without the fractal I was using as a crutch:
Input A
Input B
"A must be odd
While not(remainder(a,2
.5A→A
B-1→B
End
2A/2^B→A
"constraints on A and B will always cause this to be on [0,1)
B-2→B
".→Str1
While B
B-1→B
2A→A
int(A→R
A-Ans→A
If not(R
1-A→A
Str1+sub("01",R+1,1→Str1
End
Except for the first character, Str1 will be a sequence of 0s and 1s. Any ideas? It essentially maps an odd binary number to another binary number and the map is 1-1.
EDIT:
;given a,b such that a/2^(b+1) is on [0,1) and a is odd
a/2^(b+1)→a
b-2→b
""→string
While b>0
b-1→b
2*a→a
string & str(int(a))→string ;int(a) will either be 1 or 0, so append this to the string
if a<1
1-a→a
a-int(a)→a