import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter as FF
import numpy as np
import pandas as pd
from random import choice as RC
import seaborn as sns
#g(n).
def g(n):
return(4*n)
#h(n).
def h(n):
return(1-2*n)
#Every number n yields two new unique values, f(n) and g(n).
def getWhole(nums) :
new = []
for n in nums:
new.append(g(n))
new.append(h(n))
new.sort()
return(new)
#Return table of values.
def to1024():
#Define table columns.
data = pd.DataFrame(columns = ['n',
'Total',
'L0',
'G0L1025',
'G1024'])
data = data.append({'n' : 0,
'Total' : 1,
'L0' : 0,
'G0L1025' : 1,
'G1024' : 0},
ignore_index = True)
#Start with 0.
old = [0]
#Get new numbers
new = getWhole(old)
iteration = 1
#Update table.
data = data.append({'n' : iteration,
'Total' : len(new),
'L0' : 0,
'G0L1025' : len(new),
'G1024' : 0},
ignore_index = True)
while (iteration < 11) :
#Next iteration.
iteration += 1
#New becomes old.
old = new.copy()
#New new is spawned from new old.
new = getWhole(old)
new_neg = [n for n in new if n < -1024]
new_useful = [n for n in new if (-1024 <= n and n <= 1024)]
data = data.append({'n' : iteration,
'Total' : len(new),
'L0' : len(new_neg),
'G0L1025' : len(new_useful),
'G1024' : len(new) - len(new_neg) - len(new_useful)},
ignore_index = True)
return(data, new_useful)
data, final = to1024()
print(final)
data
[-1024, -511, -509, -508, -503, -501, -500, -497, -496, -479, -477, -476, -471, -469, -468, -465, -464, -455, -453, -452, -449, -448, -383, -381, -380, -375, -373, -372, -369, -368, -351, -349, -348, -343, -341, -340, -337, -336, -327, -325, -324, -321, -320, -287, -285, -284, -279, -277, -276, -273, -272, -263, -261, -260, -257, -256, -127, -125, -124, -119, -117, -116, -113, -112, -95, -93, -92, -87, -85, -84, -81, -80, -71, -69, -68, -65, -64, -31, -29, -28, -23, -21, -20, -17, -16, -7, -5, -4, -1, 0, 1, 3, 4, 9, 11, 12, 15, 16, 33, 35, 36, 41, 43, 44, 47, 48, 57, 59, 60, 63, 64, 129, 131, 132, 137, 139, 140, 143, 144, 161, 163, 164, 169, 171, 172, 175, 176, 185, 187, 188, 191, 192, 225, 227, 228, 233, 235, 236, 239, 240, 249, 251, 252, 255, 256, 513, 515, 516, 521, 523, 524, 527, 528, 545, 547, 548, 553, 555, 556, 559, 560, 569, 571, 572, 575, 576, 641, 643, 644, 649, 651, 652, 655, 656, 673, 675, 676, 681, 683, 684, 687, 688, 697, 699, 700, 703, 704, 737, 739, 740, 745, 747, 748, 751, 752, 761, 763, 764, 767, 768, 897, 899, 900, 905, 907, 908, 911, 912, 929, 931, 932, 937, 939, 940, 943, 944, 953, 955, 956, 959, 960, 993, 995, 996, 1001, 1003, 1004, 1007, 1008, 1017, 1019, 1020, 1023, 1024]
n | Total | L0 | G0L1025 | G1024 | |
---|---|---|---|---|---|
0 | 0 | 1 | 0 | 1 | 0 |
1 | 1 | 2 | 0 | 2 | 0 |
2 | 2 | 4 | 0 | 4 | 0 |
3 | 3 | 8 | 0 | 8 | 0 |
4 | 4 | 16 | 0 | 16 | 0 |
5 | 5 | 32 | 0 | 32 | 0 |
6 | 6 | 64 | 0 | 64 | 0 |
7 | 7 | 128 | 5 | 122 | 1 |
8 | 8 | 256 | 47 | 186 | 23 |
9 | 9 | 512 | 167 | 223 | 122 |
10 | 10 | 1024 | 422 | 233 | 369 |
11 | 11 | 2048 | 934 | 234 | 880 |
sns.set()
fig = plt.figure(figsize = (17, 8))
ax = fig.add_subplot(xlim = (0, 11.9),
ylim = (0, 1048))
#Title setup.
ax.set_title('Making Something and Negative Something out of Nothing', fontsize = 24)
#X-axis setup.
ax.set_xlabel('Number of Compositions', fontsize = 22)
ax.set_xticks(ticks = range(12))
#Y-axis setup.
ax.set_ylabel('Size of Set', fontsize = 22)
ax.tick_params(axis = 'both', which = 'major', labelsize = 18)
#Histogram.
#Numbers < 0.
bars_neg = plt.bar(x = data['n'] - 0.27,
height = data['L0'],
color = '#75D05466',
width = 0.27,
edgecolor = 'grey',
label = 'N < -1024')
#0 ≤ Numbers ≤ 1024.
bars0 = plt.bar(x = data['n'],
height = data['G0L1025'],
color = '#21908CFF',
width = 0.27,
edgecolor = 'grey',
label = '-1024 ≤ N ≤ 1024')
#Numbers > 1024.
bars_pos = plt.bar(x = data['n'] + 0.27,
height = data['G1024'],
color = '#40468866',
width = 0.27,
edgecolor = 'grey',
label = 'N > 1024')
#Labels.
for i in range(12):
#0 ≤ Numbers ≤ 1024.
plt.text(x = bars0[i].get_xy()[0] + 0.15,
y = bars0[i].get_height() + 5,
s = format(int(bars0[i].get_height()), ','),
ha = 'center',
size = 'xx-large',
weight = 'bold',
c = '#481567FF')
if bars_neg[i].get_height() > 0 :
#Numbers < 0.
plt.text(x = bars_neg[i].get_xy()[0] + 0.15,
y = bars_neg[i].get_height() + 5,
s = format(int(bars_neg[i].get_height()), ','),
ha = 'center',
size = 'large',
weight = 'bold',
c = '#48156766')
if bars_pos[i].get_height() > 0 :
#1024 < Numbers.
plt.text(x = bars_pos[i].get_xy()[0] + 0.15,
y = bars_pos[i].get_height() + 5,
s = format(int(bars_pos[i].get_height()), ','),
ha = 'center',
size = 'large',
weight = 'bold',
c = '#48156766')
plt.legend(loc = 2,
fontsize = 'x-large')
#Save.
fig.savefig("2023.07.28 Extra Credit.png",
bbox_inches = 'tight')