#134354 - 2005-02-23 12:17 AM
Re: KiXgolf: Balanced Ternary Addition - Public Coding
|
maciep
Korg Regular
   
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
Saving best for last...
Score = 279 Code:
Function BTA($a,$b) dim $x,$,$s,$y $b = $a,$b,0 while $b[0] + $b[1] $s=3 for $a=0 to 2 $y = $b[$a] $x= right($y,2) $b[$a]=left($y,len($y)-1-($x<0)) $s = $s + $x mod 2 next $a=0,1,-1,0,1,-1,0,-1,-1,0,0,0,1,1 $ = "" + $a[$s] + $ $b[2] = $a[$s+7] loop $ = "0" + $b[2] + $ do $ = substr($,2) until left($,1)<>0 or len($)<2 $BTA=$ EndFunction
_________________________
Eric
|
|
Top
|
|
|
|
#134357 - 2005-02-23 11:06 AM
Re: KiXgolf: Balanced Ternary Addition - Public Coding
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Code:
function bta($a, $b)
dim $,$i,$m,$t
$a = $a+#+$b ; delim operands with #
$b = 1
for $i = 1 to len($a)
$t = left(right($a,$i),1)
if $t = "-" ; was n minus?
$ = $ - $m * 2 ; subtract last $m (*2 because we already added it previous)
else
$m = $b * $t ; multiply base x num (the # in stream has no effect)
$ = $ + $m ; accumulate n
$b = iif($t=#,1,$b*3) ; next base or reset to 1 on delim
endif
next
do
$b = $ mod 3
if $b/2 ; ternary 2 ?
$b = $b/-2 ; change 2 to -1 and -2 to 1
$ = $ - $b ; carry forward
endif
$bta = "" + $b + $bta ; answer (coerce b to string with "")
$ = $ / 3 ; next base
until $=0 ; done?
endfunction
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 837 anonymous users online.
|
|
|