Skip to content

Instantly share code, notes, and snippets.

@torazuka
Last active April 28, 2022 03:13
Show Gist options
  • Save torazuka/0609a9c9b834a0748edca2f1ce2b26c0 to your computer and use it in GitHub Desktop.
Save torazuka/0609a9c9b834a0748edca2f1ce2b26c0 to your computer and use it in GitHub Desktop.
To plot ternary numbers which do not contain 1
import matplotlib.pyplot as plt
import math as math
def get_exp(n):
if n <= 0:
return 0
return math.floor(math.log(n, 3))
def conv_to_deci(n):
s = str(n)
l = len(s)
res = 0
for i in range(len(str(n))):
res += (3 ** (l - 1 - i)) * int(s[i])
return res
def test_conv_to_deci():
i = [0, 1, 10, 20, 21, 10202]
o = [0, 1, 3, 6, 7, 101]
for p in range(len(i)):
if conv_to_deci(i[p]) != o[p]:
print('ng with ' + str(i[p]))
test_conv_to_deci()
tmp = []
for i in range(100):
res = 0
n = i
while n > 2:
idx1 = get_exp(n)
res += 10 ** idx1
n = n - 3 ** idx1
if n > 0:
idx2 = get_exp(n)
res += 10 ** idx2
n = n - 3 ** idx2
res += n
if ('1' in str(res)) == False:
tmp.insert(len(tmp), res)
x = []
y = []
for el in tmp:
num = conv_to_deci(el)
x.insert(len(x), num)
y.insert(len(y), num)
plt.plot(x, y, 'o', color='black')
plt.title('Ternary Numbers which Do Not Contain 1')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment