Looking for a logic to keep a fraction in a range - math

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.

Related

Finding the closest pentagonal number, 𝑝𝑛 given a positive integer, S where S ≥ 1

I need to create a function in R that takes as input an integer, S ≥ 1 and returns as output the pentagonal number which is closest to S.The output of my function should be the pentagonal number 𝑝𝑛 which satisfies |𝑝𝑛−𝑠|≤|𝑝𝑚−𝑠| for all positive integers m.
However if I could get two different pentagonal numbers which happens when the integer, s is literally in the middle of them. Then it doesn't matter which one it takes (greater or lesser value) which is like when S is 17 and the pentagonal number closest to 17 is 12 and 22 so it can take either one.
Here is the following code that I have created which is used to find the pentagonal number 𝑝𝑛 for a given positive integer, n:
P_n=function(n){
x=(3*n^2-n)/2
if(n == 0){
return (0)
}else{
return(x)
}
}
After writing the code to find pn, I am now stuck with finding the closest pentagonal number for integer, s. I know that the main idea is to distinguish Pm and Pn using ceiling and floor function but I don't really know how to link it to the equation |𝑝𝑛−𝑠|≤|𝑝𝑚−𝑠|.
You can try the code below
P_n <- Vectorize(function(n) max((3 * n^2 - n) / 2, 0))
k <- floor((1 + sqrt(1 + 24 * x)) / 6)
(n <- k - 1 + which.min(abs(P_n(c(k,k+1)) - x)))
Example 1
> x <- 18
> k <- floor((1 + sqrt(1 + 24 * x)) / 6)
> (n <- k - 1 + which.min(abs(P_n(c(k,k+1)) - x)))
[1] 4
Example 2
> x <- 17
> k <- floor((1 + sqrt(1 + 24 * x)) / 6)
> (n <- k - 1 + which.min(abs(P_n(c(k,k+1)) - x)))
[1] 3
You don't need loops, just solve following problem:
For input S find minimum n such that: 3n^2-n-2S >= 0
By doing that you get your two candidates:
n <- (1 + sqrt(1 + 24 * S)) / 6
p1 <- P_n(floor(n))
p2 <- P_n(ceiling(n))
c(p1, p2)[which.min(c(S - p1, p2 - S))]
In the case when the difference is same this will prefer lower pentagonal number (because of the way which.min works in case of equal numbers).

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

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

Math for Buy X Get Y For Z

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.

Separate a number into fixed part

What is the algorithm to find the number way to separate number M to p part and no two part equal.
Example:
M = 5, P = 2
they are (1,4) (2,3). If P = 3 then no partition availabe, i.e
not (1,2,2) because there two 2 in partition.
In the expanded product
(1+x)(1+x2)(1+x3)...(1+xn)
find the coefficient of x^n. This gives the number of any possibility to represent n as sum of different numbers, i.e., a variable number of terms.
You want the number of possibilites to have
n = i1+i2+...+iP with i1 < i2 < ... < iP
which can be realized by setting
i1=j1, i2=i1+j2=j1+j2, ...
iP=iP-1+jP=j1+j2+...+jP with all jk > 0
so that the original task is the same as counting all the ways that one can solve
n = P * j1+(P-1) * j2+...+1 * jP with all jk > 0, but unrelated among each other.
The corresponding generator function is the product of the geometric series of the powers of x, omitting the constant term,
(x+x2+x3+...) * (x2+x4+x6+...) * (x3+x6+x9+...) * ... * (xP+x2*P+x3*P+...)
= xP*(P+1)/2 * (1+x+x2+...) * (1+x2+x4+...) * (1+x3+x6+...) * ... * (1+xP+x2*P+...)
Clearly, one needs n >= P*(P+1)/2 to get any solution at all. For P=3 that bound is n >= 6, so that n=5 has indeed no solutions in that case.
Algorithm
count = new double[N]
for k=0..N-1 do count[k] = 1
for j=2..P do
for k=j..N-1 do
count[k] += count[k-j]
Then count[k] contains the number of combinations for n=P*(P+1)/2+k.

Resources