Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
JT
Posts:
436
Registered:
4/7/12
|
|
Re: JS coding
Posted:
Feb 3, 2013 11:34 PM
|
|
On 3 Feb, 23:33, Virgil <vir...@ligriv.com> wrote: > In article > <6e315bac-9f19-4cee-ad90-7ec7bbe8b...@w7g2000yqo.googlegroups.com>, > > JT <jonas.thornv...@gmail.com> wrote: > > I do not want a counter, i want a general solution to re encode any > > decimal number where anybase(decimal,base) {} > > This is probably why people save code instead of posting it to > > usenet ;D > > The process involves repeated divisions with whole number quotients and > remainder > Example: Convert 99 base 10 to base 8 > > Step 1. 8 into 99 goes 12 times with 3 left over > Step 2. 8 into 12 goes 1 time with 4 left over > Step 3. 8 into 1 goes 0 times with 1 left over > Stop when base goes into dividend 0 times > > Then line up the leftovers from last to first: 1,4,3 > > Result 99(base 10) is 143(base 8) > > Check: 143(base 8) = 1*8^2 + 4*8^1 + 3*8^0 > = 1*64 + 4*8 + 3*1 > = 64 + 32 + 3 > = 99 (base 10) > -- (Sorry lines, were to long had to remove and repost) Thank you for helping out, but that is not the formula i am looking for see the serie above. Your idea use the formula creating standard base including zero. I ask for the general formula writing out the multiples without zeros. I do not want base 3 (normal way) 9=100=1*9 + 0*3 + 0*1 (I already have a general formula for anybase, see the javascript) I want base 3 (doing my way) 9=23=2*3 + 1*3 From script i want 2,3 out not 1,0,0 But i forgot how to change it to reflect my serie writing bases without zeros. But if you or anyone else could figure out howto change code to reflect the zeroless serie above, i would be greatful, right now i am lost. And if you find a way to improve code to make it readable (normal bases)please do so, this is in no way optimal just my hack. (Below you have boiled down code for basechange (works for all bases and numbers) just put your base and decimal number in function call. Thanks Virgil
<HTML> <SCRIPT language=Javascript> function anybase(bas,decimal) { multip=0; basemultip=1; unr=0; basestr=""; subtrahend=0; while (basemultip<decimal){ basemultip=basemultip*bas; multip=multip+1; } while (decimal>0){ unr++; set=0; for(i=bas;i>0;i--){ subtrahend=basemultip*i; document.write(subtrahend+"="+basemultip+"* (i)"+i+"<BR>"); if (decimal>=subtrahend) { document.write("decimal"+decimal+"- subtra"+subtrahend); decimal=decimal-subtrahend; document.write("="+decimal+"<BR>") set=1; basestr=basestr+i+","; } } if(set==0 && decimal!=0)basestr=basestr+0+","; basemultip=basemultip/bas; }
while (multip>=unr){ basestr=basestr+0+","; unr++; } document.write("basestr "+basestr+"<BR>"); }
anybase(4,81); </SCRIPT> <HTML>
|
|
|
|