Ternary.info FAQ FORUM LOGIN REG
Ternary.info
special interest group on balanced ternary numeral system and trinary logic
Main Menu

Ternary.info Forum Index
   Ternary Software {ENG}
     Great algorithm
Register To Post

Flat Previous Topic | Next Topic
Poster Thread
eudoxie
Posted on: 2009/1/22 16:53
Home away from home
Joined: 2007/12/27
From: Sweden
Posts: 266
Great algorithm
This is fantastic. I've written an algorithm for host integer to trit array representation that is incredibly simple and fast. It's O(log(v)), all-integer and has no superfluous variables or overhead.

/* Convert a native integer v to
 * an array of at most n ternary digits, t. */
void to_ternary(int* t, int n, int v) {
        int i; 
        for(i = 0; i < n && v != 0; i++) {
                if(v >= 0) {
                        t[i] = v-3*((v+1)/3);
                        v = (v+1)/3;
                } else {
                        t[i] = v-3*((v-1)/3);
                        v = (v-1)/3;
                }
        }
        for(; i < n; i++) t[i] = 0; 
}


It's based on a mathematical insight I had on balanced bases when I was taking the bus earlier today. I wrote it down in a PDF if anyone is interested...

http://www.acc.umu.se/~achtt315/math.tools.for.balanced.bases.pdf

The algorithm takes advantage of what happens to equation (8) when you let kappa be 1, and from that it's just a matter of inserting values into equation (11) and making optimizations.
Flat Previous Topic | Next Topic

Subject Poster Date
 » Great algorithm eudoxie 2009/1/22 16:53
     Re: Great algorithm Mbr 2009/1/22 17:20
       Re: Great algorithm eudoxie 2009/1/22 17:26
         Re: Great algorithm hemuman 2009/1/23 5:35
           Re: Great algorithm eudoxie 2009/1/23 14:28
             Re: Great algorithm hemuman 2009/1/24 3:07
               Re: Great algorithm eudoxie 2009/1/24 18:06
                 Re: Great algorithm hemuman 2009/1/24 19:57
                   Re: Great algorithm eudoxie 2009/1/24 20:24
                     Re: Great algorithm hemuman 2009/1/24 20:45

Register To Post
 
Login
Username:

Password:


Lost Password?

Register now!


Copyright (c) 2005-2010 Ternary.info. All rights reserved.
Powered by XOOPS 2.0 (c) 2001-2003 The XOOPS Project
Theme & Graphics by Xoops Brasil