Mangarevan Counting

June 20, 2017

Six hundred years ago, the people of the French Polynesian island of Mangareva developed a mixed-radix counting system that combined binary and decimal elements to count from 1 to 799. They had no zero. The digits 1 through 9 had their normal decimal value. Digits K, P, T and V had values 10, 20, 40 and 80, respectively, so they increased in a binary progression. A number N was represented as N = nV + T + P + K + m, where n and m were digits; note that T, P and K did not have modifiers. Thus, 73 is represented as TPK3, 219 is represented as 2VTK9, and 799 is represented as 9VTPK9 in Mangarevan. You might enjoy this article in Nature and this article in the Proceedings of the National Academy of Sciences. Arithmetic is interesting: 1VPK9 + 1 = 1VT, and 3VPK3 + 2VTK9 = 6VK2.

Your task is to write programs that translate to and from Mangarevan counting numbers. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisements

Pages: 1 2

4 Responses to “Mangarevan Counting”

  1. Rutger said

    In Python, abusing the eval() function.

    import string
    
    K = 10
    P = 20
    T = 40
    V = 80
    
    
    def to_Mangarevan(x):
    	result = ""
    	n, r = divmod(x, V)
    	if n:
    		result += str(n)+"V"
    	if r > T:
    		result += "T"
    		r -= T
    	if r > P:
    		result += "P"
    		r -= P
    	if r > K:
    		result += "K"
    		r -= K
    	if r:
    		result += str(r)
    	return result
    
    def from_Mangeravan(m):
    	m = m[::-1]
    	result = 0
    	for i,x in enumerate(m):
    		if i == 0 and x in string.digits:
    			result += eval(m[0])
    		if x in "KPT":
    			result += eval(x)
    		if i == len(m)-1 and x in string.digits:
    			result += eval(x)*V
    	return result
    
    
    for x in [73, 219, 799, 164]:
    	print(x, to_Mangarevan(x))
    	assert x == from_Mangeravan(to_Mangarevan(x))
    
    # output
    # 73 TPK3
    # 219 2VTK9
    # 799 9VTPK9
    # 164 2V4
    
  2. Jussi Piitulainen said

    (@Praxis, your parsing function does not look right. I see it missing newlines and cut short.)

    # In Julia 0.6.0 that was released just
    # Only notation mava"3VTPK1" and binary addition
    # Allow 0 digit and even mava"" as 0
    # No overflow check
    # Regex matching feels fragile, seems ok
    # Suboptimal error message on no match
    # Unsure of proper macro usage, returns actual value, seems ok
    # Unsure of printing mechanism, show() using print() seems ok
    
    module PracticalMangarevaCounting
    
    export Mava, @mava_str
    import Base: +, show
    
    primitive type Mava 16 end
    
    macro mava_str(digits)
        mo = match(r"^((\d)V)?(T)?(P)?(K)?(\d)?$", digits)
        _, v, t, p, k, r = mo.captures
        reinterpret(Mava, UInt16(+((v == nothing) ? 0 : parse(v) * 80,
                                   (t == nothing) ? 0 : 40,
                                   (p == nothing) ? 0 : 20,
                                   (k == nothing) ? 0 : 10,
                                   (r == nothing) ? 0 : parse(r))))
    end
    
    function show(io::IO, mava::Mava)
        w = reinterpret(UInt16, mava)
        v, r = divrem(w, 80)
        t, r = divrem(r, 40)
        p, r = divrem(r, 20)
        k, r = divrem(r, 10)
        print(io, "mava", '"',
                  v > 0 ? v : "",
                  v > 0 ? "V" : "",
                  t > 0 ? "T" : "",
                  p > 0 ? "P" : "",
                  k > 0 ? "K" : "",
                  r > 0 ? r : "",
                  '"')
    end
    
    function +(ma::Mava, va::Mava)
        reinterpret(Mava, reinterpret(UInt16, ma) +
                          reinterpret(UInt16, va))
    end
    
    end
    
    julia> include("mangareva.jl")
    PracticalMangarevaCounting
    
    julia> using PracticalMangarevaCounting
    
    julia> reinterpret(Mava, UInt16(73))
    mava"TPK3"
    
    julia> reinterpret(Mava, UInt16(219))
    mava"2VTK9"
    
    julia> reinterpret(Mava, UInt16(799))
    mava"9VTPK9"
    
    julia> mava"1VPK9" + mava"1"
    mava"1VT"
    
    julia> mava"3VPK3" + mava"2VTK9"
    mava"6VK2"
    
    
  3. programmingpraxis said

    @Jussi: Fixed. Thanks. WordPress recently updated its editor, again, and every time they do something breaks.

  4. Paul said

    In Python with checks on input.

    prog = re.compile("^([1-9]V)?(T)?(P)?(K)?([1-9])?$")
    VALUE = {str(i)+"V": i*80 for i in range(1, 10)}
    VALUE.update(dict(zip("TPK123456789", (40, 20, 10) + tuple(range(1, 10)))))
    
    def man_to_dec(man):
        m = prog.match(man)
        if not m:
            raise ValueError("not a valid Mangarevan format " + man)
        return sum(VALUE[group] for group in filter(None, m.groups()))
    
    def dec_to_man(n):
        if not 1 <= n <= 799:
            raise ValueError("Outside range [1, 799]")
        res = []
        d, m = divmod(n, 80)
        if d:
            res += [str(d), "V"]
        for i, s in zip((40, 20, 10), "TPK"):
            if m >= i:
                m -= i
                res.append(s)
        if m:
            res.append(str(m))
        return "".join(res)
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: