Math for Buy X Get Y For Z - math

I'm working on a POS software that require a Buy X Get Y For Z discount schema, i.e: Buy 5 Get 2 For 5$, it means if you buy 7 items, 5 items are normal price and 2 items (6th, 7th) are 5$.
This is the spreadsheet for this https://docs.google.com/spreadsheets/d/1ym93Xqnw6wupBEp9ei711wQPpt3s6QONjcqBO4Xc5X4/edit#gid=0
I want to a algorithm to get X and Y (discounted item) when input quantity
i.e: input quantity and it will return X and Y for Buy 5 Get 2
input 7 return X = 5, Y= 2
input 8 return X = 6, Y= 2
..
input 17 return X= 13,Y= 4
I'm trying to find formula for this one but I'm failed. Please help me thanks

x = 5
y = 2
i = input
r = i % (x + y)
n = (i - r) / (x + y)
py = max(0, r - x) + (n * y)
px = i - py
return x = px, y = py
To explain, I'm setting r with the modulus/remainder of input / (x + y). This is the number remaining after completed offers are removed. I am then setting n to be the number of complete offers by subtracting the remainder from the input and dividing by (x + y). The variable py is then set using n * y for the number of items at the discounted price for completed offers and adding r - x if that is > 0. Finally px is the number of items at full price which is simply the input value - py.
In your spreadsheet, you have not implemented this correctly. Change as follows:
G2 =A2-F2
H2 =G2/($L$1+$L$2)
D2 =MAX(0,F2-$L$1)+H2*$L$2
E2 =A2-D2

For the offer "Buy x for $P and get y for $Q" you want to work out how many items can be bought at each price if you are buying q items in total.
The simplest approach is to iterate through each item, and figure out if it is bought at the cheaper price or the more expensive price -
qx = 0
qy = 0
for i = 0 : (q-1)
m = mod(i, x + y)
if m < x
qx = qx + 1
else
qy = qy + 1
end
end
Each item will be counted exactly once, so you are guaranteed that qx + qy = q.

I think it could work like this(for the option of BUY 5 GET 2 discounted, it could be generalized for other options):
int x = (input/7)*5;
int y = (input/7)*2;
if((input % 7) == 6){
x+=5;
y++;
}
else
x += (input % 7);
Where input is your total number of items x is number of full priced items and y of discounted items.
I'm treating the situation of having only one item discounted separately, but there might be way to deal with it easier.

Related

calculate the sum of a series with limit of x tends to 1

I want to calculate the sum of the series as below
Lim
X->1 (2/3 - x/3 -(x^2)/3 +(x^3)*2/3 -..). I am not sure whether we have a formula for finding the sum of this kind of series. Tried a lot but couldn't find any. Any help is appreciated.
This seems to be more maths than computing.
It factorises as (1 + x^3 + x^6 + ...)(2 - x - x^2)/3
If x = 1-d (where d is small), then to first order in d, the (2 - x - x^2) term becomes (2 - (1-d) - (1-2d)) = 3d
And the (1 + x^3 + x^6 + ...) term is a geometric progression, with sum 1/(1-x^3), or here 1/(1-(1-d)^3), and the denominator to first order in d is (1 - (1-3d)) = 3d
Hence the whole thing is (1/3d) (3d) / 3 = 1/3
But we can also verify computationally with a value close to 1 (Python code here):
x = 0.999999
s = 0
f = (2 - x - x*x) / 3.
x3 = x ** 3
s_prev = None
while s != s_prev:
s_prev = s
s += f
f *= x3
print(s)
gives:
0.33333355556918565

How to solve z = (((m / k) / j) / x) for x optimizing z towards 2ˣ

So say I have these variables:
m is the amount of memory available in bits
k is a dividing factor
j is another dividing factor, kept as a separate variable instead of combining with k
x is the value we want to figure out.
z is the value we want to be a closest to 2^x
Then we have
let z = (((m / k) / j) / x)
So for example, say we have this:
m = 2000000
k = 5
j = 10
x = ?
z = ?
Then we have
let z = ((2000000 / 5) / 10) / x
I would like to figure out what x is given that z should be as close to 2 to the power of x as possible. The way I am currently doing this is by just plugging in numbers and trying to get them close to matching. But I'm wondering a generic way to programmatically solve this. But for example, I might try plugging in x = 10, which equals:
4000 = ((2000000 / 5) / 10) / 10
Then 2¹⁰ = 1024 which is decently close to 4000, but I don't know what would be closer. Trying x = 11 gives:
3636 = ((2000000 / 5) / 10) / 11
And 2¹¹ = 2048, so x = 11 is a better solution.
Wondering how I can programmatically solve this. I have tried to factor the equation out, but it's been a while since I've done this so I don't know.
z = (((m / k) / j) / x)
x * z = (m / k) / j
j * (x * z) = m / k
k * (j * (x * z)) = m
...
A bit lost now, not sure how to get it to the point of this:
f(k, j) = ... => [ x, z ]
Generally I'm trying to figure out how to solve an equation programmatically. Just seeing an implementation would allow me to understand without making it too broad.
What I've been doing currently is basically going into the debugger and typing in some JavaScript to find values, but there's gotta be a better way.
You can do an iterative search:
x = 1
best_error = infinity
best_x = 0
while True:
z = (((m / k) / j) / x)
error = abs(z - pow(2,x))
if error > best_error
return best_x
best_error = error
best_x = x
x = x+1
For other relationships there are better ways of choosing the next x, but for this particular problem a linear search seems fine.

Looking for a logic to keep a fraction in a range

I need to write some code that can calculate a variable which shows the preference of a consumer to buy a component for his laptop. The preference changes by the tax (T) and the importance of prices on people's purchases (PriceI). I need to include both T and PriceI to find the person's willingness (W) for purchasing a laptop. Tax changes in a slider ranging from 50 Cent to $6 . I want to keep the variable W in a range from 1 to 2, where 1 is when the tax is on its default, minimum values which is 50 cent.
So There are 2 variables that have influence on W:
50<T<600
0.6 < PriceI < 9
Since I want 1<W<2, I thought it should work if I first normalize all the data by dividing them by their max, then in order to find a fraction to be between 1 and 2, I made the numerator to be less than 4 and the denominator to be less than 2, hoping to have the result between 1 to 2 :
to setup-WCalculator
ask consumers [
set PP ((PriceI / 9) * 2)
set TT ((T / 600) * 4)
set W TT / PP
]
end
However, Netlogo makes both PP and TT zero while they should be a small value like 0.15! Does the logic for finding W make sense?
Thanks,
Normalization is normally done with a formula such as
TT = (T - Tmin) / (Tmax - Tmin)
or here
TT = (T - 50) / (600 - 50)
That gives a normalized value between 0 and 1 as T ranges between 50 and 600. If you want TTT to range between 1 and x, where x is > 1, then you can set
TTT = 1.0 + TT * (x - 1.0)
So
TTT = 1.0 + TT * (4.0 - 1.0) = 1.0 + TT * 3.0
will give you a value between 1 and 4.

Find number of iterations until a value reaches a limit

I have two numbers X and Y and the following pseudocode:
i = 0
While X < Y:
X = X + complex_formula
i += 1
Print i
complex_formula is independent from the X and its previous value.
So, I was wondering if there is any way to calculate the i without doing the iterations.
Is complex_formula also independent of i and timing? If so then it's a constant and this is just simple math:
i = Ceiling( (Y - X)/complex_formula)
X = X + i*complex_formula

Find row of pyramid based on index?

Given a pyramid like:
0
1 2
3 4 5
6 7 8 9
...
and given the index of the pyramid i where i represents the ith number of the pyramid, is there a way to find the index of the row to which the ith element belongs? (e.g. if i = 6,7,8,9, it is in the 3rd row, starting from row 0)
There's a connection between the row numbers and the triangular numbers. The nth triangular number, denoted Tn, is given by Tn = n(n-1)/2. The first couple triangular numbers are 0, 1, 3, 6, 10, 15, etc., and if you'll notice, the starts of each row are given by the nth triangular number (the fact that they come from this triangle is where this name comes from.)
So really, the goal here is to determine the largest n such that Tn ≤ i. Without doing any clever math, you could solve this in time O(√n) by just computing T0, T1, T2, etc. until you find something bigger than i. Even better, you could binary search for it in time O(log n) by computing T1, T2, T4, T8, etc. until you overshoot, then binary searching on the range you found.
Alternatively, we could try to solve for this directly. Suppose we want to find the choice of n such that
n(n + 1) / 2 = i
Expanding, we get
n2 / 2 + n / 2 = i.
Equivalently,
n2 / 2 + n / 2 - i = 0,
or, more easily:
n2 + n - 2i = 0.
Now we use the quadratic formula:
n = (-1 &pm; √(1 + 8i)) / 2
The negative root we can ignore, so the value of n we want is
n = (-1 + √(1 + 8i)) / 2.
This number won't necessarily be an integer, so to find the row you want, we just round down:
row = ⌊(-1 + √(1 + 8i)) / 2⌋.
In code:
int row = int((-1 + sqrt(1 + 8 * i)) / 2);
Let's confirm that this works by testing it out a bit. Where does 9 go? Well, we have
(-1 + √(1 + 72)) / 2 = (-1 + √73) / 2 = 3.77
Rounding down, we see it goes in row 3 - which is correct!
Trying another one, where does 55 go? Well,
(-1 + √(1 + 440)) / 2 = (√441 - 1) / 2 = 10
So it should go in row 10. The tenth triangular number is T10 = 55, so in fact, 55 starts off that row. Looks like it works!
I get row = math.floor (√(2i + 0.25) - 0.5) where i is your number
Essentially the same as the guy above but I reduced n2 + n to (n + 0.5)2 - 0.25
I think ith element belongs nth row where n is number of n(n+1)/2 <= i < (n+1)(n+2)/2
For example, if i = 6, then n = 3 because n(n+1)/2 <= 6
and if i = 8, then n = 3 because n(n+1)/2 <= 8

Resources