-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decimal to Binary
95 lines (83 loc) · 1.79 KB
/
Decimal to Binary
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def isOdd(n):
if n%2==0:
return False
else:
return True
def numToBinary(n):
string = ''
while n != 0:
if n%2==0:
string = '0' + string
else :
string = '1'+ string
n=n/2
return string
def binaryToNum(string):
dec=0
count=0
while string != '':
if string[-1]=='1':
dec =dec + (2**count);
count = count +1
string = string[:-1]
return dec
def increment(string):
dec=binaryToNum(string)
dec=dec+1
st=numToBinary(dec)
st = "00000000" + st
st=st[-8:]
return st
def count(s,n):
print s
while n != 0:
s=increment(s)
print (s)
n=n-1;
return
def numToTernary(n):
string = ''
while n != 0:
if n%3==0:
string = '0' + string
elif n%3==1:
string = '1' + string
else :
string = '2'+ string
n=n/3
return string
def ternaryToNum(string):
dec=0
count=0
while string != '':
if string[-1]=='1':
dec =dec + (3**count);
elif string[-1]=='2':
dec=dec+ 2*(3**count)
count = count +1
string = string[:-1]
return dec
def balancedTernaryToNum(string):
dec=0
count=0
while string != '':
if string[-1]=='+':
dec =dec + (3**count);
elif string[-1]=='-':
dec=dec - (3**count)
count = count +1
string = string[:-1]
return dec
def numToBalancedTernary(n):
string = ''
while n != 0:
if n%3==0:
string = '0' + string
n=n/3
elif n%3==1:
string = '+' + string
n=n/3
else :
string = '-'+ string
n=(n+1)/3
return string