You are not logged in.
What version of Mathematica do you have?
Part of the problem is that you do not know the name of this process. This has prevented you from researching it yourself. I tried to lead you in that direction with p-adic numbers.
It is called balanced ternary notation.
http://mathworld.wolfram.com/Ternary.html
http://en.wikipedia.org/wiki/Balanced_ternary
Balanced ternary notation is mentioned in D Knuth's books and at the OEIS.
http://oeis.org/search?q=balanced+terna … &go=Search
But never a formula, this is why I said you will have to be satisfied with an algorithmic answer.
Go here for a small function to compute them!
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
hi wetsun
I'm three quarters through writing a program in BASIC* which will do the job (I hope). Once I've got it debugged I'll post the code plus explanation. It should be possible to convert it into any ALGOL like language.
I can compile it into a exe form too.
Watch this space!
Bob
* Young'ns reading this will be horrified that something so primitive is still in existence! Or may have only heard of it on their grandaddy's knee. But when I were a lad, computers still ran on valves and were programmed via punched cards!
Last edited by Bob (2011-06-22 02:08:56)
Children are not defined by school ...........The Fonz
You cannot teach a man anything; you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you! …………….Bob
Offline
Hi wetsun,
A direct implementation of what soroban says:
If you are okay with C language:
Tested with gcc version 4.4.5.
#include <stdio.h>
int main()
{
int n, b3[20];
int i=0;
scanf("%d",&n);
while ( n )
{
b3[i] = n%3;
n /= 3;
i++;
}
n = i;
for(i=0;i<=n; i++)
{
switch(b3[i])
{
case 2:
b3[i] = -1;
b3[i+1] += 1;
break;
case 3:
b3[i] = 0;
b3[i+1] += 1;
break;
default:
break;
}
}
for ( i=0 ; i<=n; i++)
{
if ( b3[i] == -1 )
printf("%d*3^%d ",b3[i],i);
else if( b3[i] == 1 )
printf("+%d*3^%d ",b3[i],i);
}
return 0;
}
"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense" - Buddha?
"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."
Offline
hi wetsun
BASIC:
10 INPUT"Choose how many terms you want";size
REM If you don't allow sufficient powers of 3, the results are unreliable.
20 DIM key(size)
30 DIM powerthree(size)
40 flag = FALSE
50 PROCworkoutpowers
REM This computes and stores the powers of three.
60 PROCfindkeys
REM This computes the keys {1,2,5,14,41,....} see post #15
70
80 answer$ = ""
90 INPUT"What number would you like to do", number
100 workingnumber=number
110
REM This next section is the heart of the routine. The PROCedure finds the highest power of 3 that is needed for a given number.
If it's a power of 3 already, the search ends.
Otherwise the remainder is calculated, and whether you need to add or subtract to get to the number. The result is stored as a string (answer$).
The routine loops back with that remainder as the new value to work with. Once again it is tested for being a power of 3 and again the search stops if it is. If not, the power of 3 is added to the string, the remainder calculated and the search continues. It will definitely stop when the remainder is 1.
120 REPEAT
130 PROCfindkeymax(workingnumber)
140 answer$ = answer$ + add$
150 value = EVAL(answer$)
160 workingnumber = number - value
170 IF workingnumber < 0 THEN answer$ = answer$ + " - " ELSE IF workingnumber > 0 answer$ = answer$ + " + " ELSE
180 workingnumber = ABS(workingnumber)
190 UNTIL flag = TRUE
200 PRINT number;" = ";answer$
210
220 END
230
240 DEFPROCworkoutpowers
250 REM This gets the powers you have asked for
260 powerthree(1)=1
270 j=1
280 REPEAT
290 powerthree(j+1)=3*powerthree(j)
300 j = j + 1
310 UNTIL j = size
320 ENDPROC
330
340 DEFPROCfindkeys
350 REM This gets the corresponding keys
360 i = 1
370 power = 0
380 sum = 0
390 key(1) = sum + 1
400 FOR i = 2 TO size
410 sum = sum + 3^power
420 nextkey = sum + 1
430 key(i) = nextkey
440 power = power + 1
450 NEXT i
460 ENDPROC
470
480 DEFPROCfindkeymax(j)
490 REM check if it's a power of three
500 i = 0
510 REPEAT
520 i = i + 1
530 UNTIL j = powerthree(i) OR i = size
540 IF j = powerthree(i)THEN flag = TRUE:highest = i:add$ = STR$(powerthree(highest)):ENDPROC
550 REM find the first key above j
560 i = 0
570 REPEAT
580 i = i + 1
590 UNTIL key(i) > j OR i = size
600 REM pick the power to go with this high key
610 highest = i - 1
620 add$ = STR$(powerthree(highest))
630 ENDPROC
Screen shot of sample output upload as a gif image.
If you want your own working program, post back and I'll put a downloadable exe on my website.
You'll need a version of windows (eg. XP)
Bob
Last edited by Bob (2011-06-22 19:59:30)
Children are not defined by school ...........The Fonz
You cannot teach a man anything; you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you! …………….Bob
Offline