/
base.py
168 lines (118 loc) · 4.1 KB
/
base.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from stats import calcRegression,PRECISION
from util import lcm,factorial
from math import ceil
def lc(coins,args):
#Get the regression value for a list of coins
#Calculates for a range of values
start = int(args[0])
step = int(args[1])
iterations = int(args[2])
end = start+step*iterations
xAxis = list(range(start,end,step))
yAxis = []
#Create the amount of change
for i in xAxis:
yAxis.append(getChange(i,coins))
regression = calcRegression(xAxis,yAxis,len(coins)-1)
coef = regression[0]
#Pearson R Value between [-1,1]
r = regression[1]
#Create the equation based on the coefficients
equation = ""
for i in range(0,len(coef)):
#The last term is x^0, so subtract 1
equation+=str(coef[i])+"x^"+str(len(coef)-(i+1))+" "
equation+="\n"+str(r)
return equation
def ap(coins,args):
f = open("dump.txt","w")
start = int(args[0])
iterations = len(coins)
uniquePolys = int(lcm(coins))
for i in range(start,start+uniquePolys*iterations):
#Argument to Pass Into the LC Function (Starting Value,Step, Number of Iterations)
passArg = [str(i),str(uniquePolys),str(iterations)]
#Write the current number (Loops mod Polys because they all should be the same)
#Also write the actual polynomial
f.write(str(i%uniquePolys) + " "+lc(coins,passArg).split("\n")[0] + "\n")
f.close()
def cc(coins,args):
#Calculate the difference between Ceiling and Formula values
f = open("dump.txt","w")
start = int(args[0])
uniquePolys = int(lcm(coins))
for i in range(start,start+uniquePolys):
#Same as AP, Passing Arguments into LC
passArg = [str(i),str(uniquePolys),"4"]
equation = lc(coins,passArg).split("\n")[0]
# f.write(equation +" ")
equation = equation.split()
#Value when start is plugged in, using Ceiling
result = 0
for i in range(0,len(equation)-1):
coef = float(equation[i].split("x^")[0])
degree = int(equation[i].split("x^")[1])
result+=coef*start**degree
f.write(str(ceil(result)) + " ")
#Add the constant term
result+=float(equation[-1].split("x^")[0])
result = round(result,PRECISION)
f.write(str(result))
f.write("\n")
f.close()
def cs(coins,args):
#Compare to Schurs
start = 1
step = 1
iterations = int(args[0])
without = lc(coins,[start,step,iterations]).split("\n")[0]
xAxis = list(range(1,1+iterations))
yAxis = []
productOfCoins = 1
for i in range(0,len(coins)):
productOfCoins*=coins[i]
firstTerm = 1/(factorial(len(coins)-1)*productOfCoins)
degree = len(coins)-1
for i in xAxis:
yAxis.append(getChange(i,coins) - i**degree*firstTerm)
reg = calcRegression(xAxis,yAxis,degree-1)[0]
for i in reg:
i = round(i,PRECISION)
equation = str(round(firstTerm,PRECISION))+"x^"+str(degree) + " "
for i in range(0,len(reg)):
equation+=str(round(reg[i],5))+"x^"+str(degree-(1+i))
equation+=" "
return [without,equation]
def getChange(coin,coinValues):
#Make a reference table with the amount of change for each coin
base = []
for i in range(0,coin+1):
#Determine if using only base coins, change can be made
#Like for change for 99 cents, can it be made for 2 cents (No)
if(i%coinValues[0]) == 0:
#Change can be made using only Pennies (Or 2 cent...)
base.append(1)
else:
base.append(0)
#List of changes for each coin
#Matrix of length Coins by Change
coinMatrix = [base]
#We already figured out change for the base value
for currCoin in range(1,len(coinValues)):
#Change values for current
currentChange = []
#1 Way to make change for 0 cents
for currValue in range(0,coin+1):
if((currValue)<coinValues[currCoin]):
#For all coins less than the value (like 15 for a quarter)
#Change is just number of ways with dimes (because 0 for quarter)
currentChange.append(coinMatrix[currCoin-1][currValue])
else:
#Number of Change = Dn = Dn-25 (Or Coin Value) + Cn
change = currentChange[currValue-coinValues[currCoin]]
change+= coinMatrix[currCoin-1][currValue]
currentChange.append(change)
coinMatrix.append(currentChange)
return coinMatrix[len(coinValues)-1][coin]
if(__name__ == "__main__"):
print(getChange(100,[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]))