Discount Calculation:
Product quantity and range
1 - 10 - 1%
11 - 20 - 2%
21 - 30 - 3%
31 - 40 - 4%
41 - 50 - 5%
the above are the quantity range and their discount% given,
for example:
each product cost is 100
if i purchase 50 product then 5% discount is 250
Now if i purchase 50 products at 2 terms let say 20 and 30 then
for 20 product 2% discount = 40
for 30 product 3% discount = 90
total discount = 130
but here i have to get discount as 250,
Problem description:
the product can be purchased in n number of terms for the maximum quantity, here maximum quantity is 50. discount% for the purchased product is given from the above range. when total discount is added it should be equal. here when 50 product is purchased 250 is given as discount same 250 should be total discount even when product purchased as 20, 10, 10 in terms or 25, 25 whatever.....
plz help me with the calculation part, with some formula or anything....
I assume that you want the discount rate to always increase as the number of purchased items increases, and if that's the case, there's no way to do this.
Here's the logic. The basic equation is:
n1d1 + n2d2 + n3d3 = (n1 + n2 + n3)dx
One obvious solution to this is to have all the d's being equal, that is, all the discount rates are the same. Otherwise, there's no general solution (that is, no set of d's that will work for all n combinations -- for example, whenever all but one of the n's are zero then the d's on both sides of the equation will have to be the same, so the only general solution is that all d's are the same), and if you want a specific solution with different d's, you could solve for the correct value of d given a set of n's, but when you do that, it's clear if one of the d's is smaller than dx, another will have to be larger, so you can't have an strictly increasing discount rate.
Calculate the discount for the previous item count. (How much discount has been given before.)
Calculate the discount for the new item count (previous + current order). (How much discount the customer should have.)
Give a final discount as the difference between the two values.
Store the new item count (of each type) for the customer to some database.
float SimpleDiscount(float cost, int count)
{
if (count <= 0) return 0;
if (count <= 10) return 0.01f * cost;
if (count <= 20) return 0.02f * cost;
if (count <= 30) return 0.03f * cost;
if (count <= 40) return 0.04f * cost;
return 0.05f * cost; // count > 40
}
float GetDiscount(int customerId, int itemId, int count)
{
float cost = GetItemCost(itemId);
int previousCount = GetCustomerOrderedItemCount(customerId, itemId);
float previousDiscount = SimpleDiscount(cost, previousCount);
int newCount = previousCount + count;
float newDiscount = SimpleDiscount(cost, newCount);
SaveCustomerOrderedItemCount(customerId, itemId, newCount);
return newDiscount - previousDiscount;
}
For example:
Item cost = 100
For 20 items: Discount = 40 (2%)
For 30 items: Discount = 210 (7%)
Total discount = 250 (5%)
Related
for example, I have 1 computer with a discount, the price of this computer is $ 450 with a 10% discount, I want to know the real price of it,
I want to learn this both over 10% and as 10% money.
Computer 10% off Price = 450$
Computer $10 off Price = 490$
$net_total = 450;
$discount_value = 10; < percent or amount
$gross_price = ?;
Well, let's solve the equations:
Computer 10% off Price = 450$
Computer $10 off Price = 490$
Which can be written as (let x be the initial price of the computer)
x - x * 10 / 100 = 450 # initial price - x without 10 % from x - x * 10% / 100%
x - 10 = 490 # just 10$ off
Or
0.9 * x = 450
x = 500
Finally
x = 450 / 0.9 = 500
x = 500
So from both equations we have that the initial computer's price is 500$
Edit: in general case,
if $discount_value stands for per cent (i.e. $discount_value = 10 means 10% discount) then
$gross_price = $net_total * 100.0 / (100.0 - $discount_value)
if $discount_value stands for money (i.e. $discount_value = 10 means 10$ discount), then
$gross_price = $net_total + $discount_value
I'm writing a diploma about vaccines. There is a region, its population and 12 month. There is an array of 12 values from 0 to 1 with step 0.01. It means which part of population should we vaccinate in every month.
For example if we have array = [0.1,0,0,0,0,0,0,0,0,0,0,0]. That means that we should vaccinate 0.1 of region population only in first month.
Another array = [0, 0.23,0,0,0,0,0,0, 0.02,0,0,0]. It means that we should vaccinate 0.23 of region population in second month and 0.02 of region population in 9th month.
So the question is: how to generate (using 3 loops) 12(months) * 12(times of vaccinating) * 100 (number of steps from 0 to 1) = 14_400 number of arrays that will contain every version of these combinations.
For now I have this code:
for(int month = 0;month<12;month++){
for (double step = 0;step<=1;step+=0.01){
double[] arr = new double[12];
arr[month] = step;
}
}
I need to add 3d loop that will vary number of vaccinating per year.
Have no idea how to write it.
Idk if it is understandable.
Hope u get it otherwise ask me, please.
You have 101 variants for the first month 0.00, 0.01..1.00
And 101 variants for the second month - same values.
And 101*101 possible combinations for two months.
Continuing - for all 12 months you have 101^12 variants ~ 10^24
It is not possible to generate and store so many combinations (at least in the current decade)
If step is larger than 0.01, then combination count might be reliable. General formula is P=N^M where N is number of variants per month, M is number of months
You can traverse all combinations representing all integers in range 0..P-1 in N-ric numeral system. Or make digit counter:
fill array D[12] with zeros
repeat
increment element at the last index by step value
if it reaches the limit, make it zero
and increment element at the next index
until the first element reaches the limit
It is similar to counting 08, 09, here we cannot increment 9, so make 10 and so on
s = 1
m = 3
mx = 3
l = [0]*m
i = 0
while i < m:
print([x/3 for x in l])
i = 0
l[i] += s
while (i < m) and l[i] > mx:
l[i] = 0
i += 1
if i < m:
l[i] += s
Python code prints 64 ((mx/s+1)^m=4^3) variants like [0.3333, 0.6666, 0.0]
So I have an array of coupons, each with a price and the quantity of items that can be bought from it. I can only buy the given item quantity from a coupon, no more, no less. How to find the minimum cost to get the required number of items with coupons (and return -1 if not possible)?
For example, on having 4 coupons: "Buy 3 at 10 dollars", "Buy 2 at 4 dollars", "Buy 2 at 4 dollars" and "Buy 1 at 3 dollars", and 4 items to buy, the minimum cost is 8 dollars.
Knapsack works on finding maximums, but for minimum it'll just keep on not considering any coupon and come up with an answer of 0.
Here's my code:
int minimumCost(coupon_t coupons[], int numCoupons, int units) {
if (units <= 0 || numCoupons <= 0)
return 0;
if (coupons[numCoupons-1].quantity > units)
return minimumCost(coupons, numCoupons-1, units);
coupon_t coupon = coupons[numCoupons-1];
return min(coupon.price + minimumCost(coupons, numCoupons-1, units-coupon.quantity),
minimumCost(coupons, numCoupons-1, units));
}
Had a little more think about this. The key, as you say, is handling of 0. In typical knapsack code, 0 has two meanings: both "not buying" and "can't buy". Splitting those seems to work:
def minimum_cost(coupons, units, coupon_no=0):
if units < 0 or coupon_no == len(coupons):
# special value for "impossible"
return None
if units == 0:
# no more space, so we're not buying anything else
return 0
quantity, price = coupons[coupon_no]
next_coupon = coupon_no + 1
if quantity > units:
return minimum_cost(coupons, units, next_coupon)
pre_purchase_value_when_used = minimum_cost(coupons, units - quantity, next_coupon)
value_when_unused = minimum_cost(coupons, units, next_coupon)
# return whichever is not impossible, or cheaper of two possibilities:
if pre_purchase_value_when_used is None:
return value_when_unused
elif value_when_unused is None:
return pre_purchase_value_when_used + price
else:
return min(pre_purchase_value_when_used + price, value_when_unused)
coupons = [[3, 10], [2, 4], [2, 4], [1, 3]]
units = 4
cost = minimum_cost(coupons, units)
print(cost)
# => 8
(Note that recursion is not dynamic-programming, unless you cache the function results, although it shouldn't be too hard to make it use a table. The key insight about dynamic programming is to use storage to avoid recalculating things we already calculated.)
In erlang:
cost(I, Miners) ->
BasePrice = lists:nth(I, prices()),
Owned = lists:nth(I, Miners),
Rate = increaseRate(I) / 100,
Multiplier = math:pow((1 + Rate), Owned),
floor(BasePrice * Multiplier).
for example, a base price of 8000, with an increase rate of 7, and I own 0
the price of the first one I expect to be: 8000
when buying my second one, with an increase rate of 7, and I own 1
the price of the second one I expect to be:
Multiplier = 1.07
8000 * 1.07 =
8560
This all works fine. Now I have to implement this in Solidity, which doesn't do decimal math very well. It auto rounds down such that 3/2 == 1 in Solidity.
I want to recreate my cost function in Solidity.
function cost(uint _minerIndex, uint _owned) public view returns (uint) {
uint basePrice = 8000;
uint increaseRate = 7;
return basePrice * ((1 + increaseRate / 100) ** _owned);
}
increaseRate / 100 will always return 0 if increaseRate is < 100.
How do I achieve this same effect?
From the documentation:
"Fixed point numbers are not fully supported by Solidity yet. They can be declared, but cannot be assigned to or from."
a simple solution is
(basePrice * ((100+increaseRate)** _owned))/(100 ** _owned)
but it may fail also because of arithmetic overflow, depending on your numbers and the MaxInt supported by solidity.
Let's say i have 3 sets of numbers and i want the % of their difference.
30 - 60
94 - 67
10 - 14
I want a function that calculate the percentage of the difference between each 2 numbers, and the most important is to support positive and negative percentages.
Example:
30 - 60 : +100%
94 - 67 : -36% ( just guessing )
10 - 14 : +40%
Thanks
This is pretty basic math.
% difference from x to y is 100*(y-x)/x
The important issue here is whether one of your numbers is a known reference, for example, a theoretical value.
With no reference number, use the percent difference as
100*(y-x)/((x+y)/2)
The important distinction here is dividing by the average, which symmetrizes the definition.
From your example though, it seems that you might want percent error, that is, you are thinking of your first number as the reference number and want to know how the other deviates from that. Then the equation, where x is reference number, is:
100*(y-x)/x
See, e.g., wikipedia, for a small discussion on this.
for x - y the percentage is (y-x)/x*100
Simple math:
function differenceAsPercent($number1, $number2) {
return number_format(($number2 - $number1) / $number1 * 100, 2);
}
echo differenceAsPercent(30, 60); // 100
echo differenceAsPercent(94, 67); // -28.72
echo differenceAsPercent(10, 14); // 40
If the percentage is needed for a voting system then Andrey Korolyov is the only who answered correctly.
Example
10 votes for 1 vote against = 90%
10 votes for 5 votes against = 50%
10 votes for 3 votes against = 70%
100 votes for 1 vote against = 99%
1000 votes for 1 vote against = 99.9%
1 votes for 10 vote against = -90%
5 votes for 10 votes against = -50%
3 votes for 10 votes against = -70%
1 votes for 100 vote against = -99%
1 votes for 1000 vote against = -99.9%
function perc(a,b){
console.log( (a > b) ? (a-b)/a*100 : (b - a)/b*-100);
}
$c = ($a > $b) ? ($a-$b)/$a*-100 : ($b-$a)/$b*100;
In Ukraine children learn these math calculations at the age of 12 :)