Nov 9, 2013

A generalization of a number pattern from base 10 to base 16, and base 20, and base 32


First looking at the decimals number patterns:

12*9+3 : 111
123*9+4 : 1111
1234*9+5 : 11111
12345*9+6 : 111111
123456*9+7 : 1111111
1234567*9+8 : 11111111
12345678*9+9 : 111111111
123456789*9+10 : 1111111111

And the python generator for this:

def get_first(p1, mynum):
   
    p2 = p1 + str(mynum)
    n2 = int(p2) * 9 + (mynum+1)
    print "%s*9+%d : %d"%(p2,mynum+1, n2)
    get_first(p2, mynum+1)

get_first("1", 2)
   
Can it be generalize to base 16?

Yes:

12*15+0x3 : 0x111
123*15+0x4 : 0x1111
1234*15+0x5 : 0x11111
12345*15+0x6 : 0x111111
123456*15+0x7 : 0x1111111
1234567*15+0x8 : 0x11111111
12345678*15+0x9 : 0x111111111L
123456789*15+0xa : 0x1111111111L
123456789a*15+0xb : 0x11111111111L
123456789ab*15+0xc : 0x111111111111L
123456789abc*15+0xd : 0x1111111111111L
123456789abcd*15+0xe : 0x11111111111111L
123456789abcde*15+0xf : 0x111111111111111L
123456789abcdef*15+0x10 : 0x1111111111111111L

And the program is:

def pattern_base16(p1, mynum):
    p2 = p1 + hex(int(str(mynum)))[2:]
    n2 = int(p2,16) * 15 + (mynum+1)
    print "%s*15+%s : %s"%(p2,hex(mynum+1), hex(n2))
    pattern_base16(p2, mynum+1)

pattern_base16("1", 2)

And for base 20:

12*19+3 : 111
123*19+4 : 1111
1234*19+5 : 11111
12345*19+6 : 111111
123456*19+7 : 1111111
1234567*19+8 : 11111111
12345678*19+9 : 111111111
123456789*19+a : 1111111111
123456789a*19+b : 11111111111
123456789ab*19+c : 111111111111
123456789abc*19+d : 1111111111111
123456789abcd*19+e : 11111111111111
123456789abcde*19+f : 111111111111111
123456789abcdef*19+g : 1111111111111111
123456789abcdefg*19+h : 11111111111111111
123456789abcdefgh*19+i : 111111111111111111
123456789abcdefghi*19+j : 1111111111111111111
123456789abcdefghij*19+10 : 11111111111111111111

and the program is:

from baseconv import BaseConverter

def base10_to_20(nos):
    base20 = BaseConverter('0123456789abcdefghij')
    return base20.encode(nos)

def base20_to_10(mystring):
    base20 = BaseConverter('0123456789abcdefghij')
    return int(base20.decode(mystring))

def pattern_base20(p1, mynum):
    p2 = p1 + base10_to_20(mynum)
    #print "p2=%s"%p2
    n2 = base20_to_10(p2) * 19 + (mynum+1)
    print "%s*19+%s : %s"%(p2, base10_to_20(mynum+1), base10_to_20(n2))
    pattern_base20(p2, mynum+1)

pattern_base20("1", 2)

And for base 32:

12*31+3 : 111
123*31+4 : 1111
1234*31+5 : 11111
12345*31+6 : 111111
123456*31+7 : 1111111
1234567*31+8 : 11111111
12345678*31+9 : 111111111
123456789*31+a : 1111111111
123456789a*31+b : 11111111111
123456789ab*31+c : 111111111111
123456789abc*31+d : 1111111111111
123456789abcd*31+e : 11111111111111
123456789abcde*31+f : 111111111111111
123456789abcdef*31+g : 1111111111111111
123456789abcdefg*31+h : 11111111111111111
123456789abcdefgh*31+i : 111111111111111111
123456789abcdefghi*31+j : 1111111111111111111
123456789abcdefghij*31+k : 11111111111111111111
123456789abcdefghijk*31+l : 111111111111111111111
123456789abcdefghijkl*31+m : 1111111111111111111111
123456789abcdefghijklm*31+n : 11111111111111111111111
123456789abcdefghijklmn*31+p : 111111111111111111111111
123456789abcdefghijklmnp*31+q : 1111111111111111111111111
123456789abcdefghijklmnpq*31+r : 11111111111111111111111111
123456789abcdefghijklmnpqr*31+s : 111111111111111111111111111
123456789abcdefghijklmnpqrs*31+t : 1111111111111111111111111111
123456789abcdefghijklmnpqrst*31+u : 11111111111111111111111111111
123456789abcdefghijklmnpqrstu*31+v : 111111111111111111111111111111
123456789abcdefghijklmnpqrstuv*31+w : 1111111111111111111111111111111
123456789abcdefghijklmnpqrstuvw*31+10 : 11111111111111111111111111111111

And the program is:

from baseconv import BaseConverter

def base10_to_32(nos):
        base32 = BaseConverter('0123456789abcdefghijklmnpqrstuvw')
        return base32.encode(nos)

def base32_to_10(mystring):
        base32 = BaseConverter('0123456789abcdefghijklmnpqrstuvw')
        return int(base32.decode(mystring))

def pattern_base32(p1, mynum):
    p2 = p1 + base10_to_32(mynum)
    #print "p2=%s"%p2
    n2 = base32_to_10(p2) * 31 + (mynum+1)
    print "%s*31+%s : %s"%(p2, base10_to_32(mynum+1), base10_to_32(n2))
    pattern_base32(p2, mynum+1)

The BaseConverter python module is downloaded following the two articles below:

http://stackoverflow.com/questions/2267362/convert-integer-to-a-string-in-a-given-numeric-base-in-python

https://bitbucket.org/semente/baseconv






No comments: