Calculating bid price from total cost - algorithm

I have to following problem to solve. I have to calculate the bidding price from a total price. For example, a client wants to pay 2000$ as a total price, but from this price, there are added costs:
-usage price which is 10% from the bidding price with a minimum of 10$ and a maximum of 50$
-seller price: 2% from bidding price
-added price: 5$ for a bid between 1 and 500
10$ for a bid between 501 and 1000
15$ for a bid between 1001 and 3000
20$ for a bid over 3000$
-storing cost: 100$
from all this, I have to calculate the bidding price for a total of 2000$, for example. I kind of have no clue of how this can be done. Can anyone give me some hints or pieces of answer of what the algorithm should be?
EDIT: ok I got how to calculate the algebra, now where i'm stuck is how to write the algorithm in code or pseudo-code. Anyone got a hint?

You can express all those costs as a function of the bid price. Generate a giant equation that is the sum of all those functions and solve for a particular final value, e.g.:
usage cost(bid) = PIN(bid*0.10, 10, 50)
seller cost(bid) = bid*.02
added cost(bid) = PIN(ceiling(bid/500)*5, 5, 10) + PIN(ceiling((bid - 1000)/2000)*5, 0, 10)
storing cost(bid) = 100
So the final cost is something like:
final cost(bid) = PIN(bid*.1, 10, 50) + pin(ceiling(bid/500)*5, 5, 20) + PIN(ceiling((bid - 1000)/2000)*10, 0, 20) + bid*.02 + 100 + bid
Solve for a particular value and you're done.
For example, if you want the total cost to be $2000:
2000 = PIN(bid*.1, 10, 50) + pin(ceiling(bid/500)*5, 5, 10) + PIN(ceiling((bid - 1000)/2000)*5, 0, 10) + bid*.02 + 100 + bid.
Bid must be at least > 1500 and < 2000, which works out nicely since we can make those PIN sections constant:
2000 = 50 + 10 + 5 + 100 + bid*1.02
1835 = bid*1.02
bid = 1799.0196078431372549019607843137
The PIN expressions are the hardest to factor out, so you might have to guess a few times until you get something that narrows down the range of bids you want to calculate.

Related

Dynamic Programming Problem - Maximize Profit by Selling Stocks

I am looking at this code challenge:
You are selling stocks over n days and you have x number of stocks.
x = y1 + y2 + ... + yn denoting the number of stocks you sell per day over n days.
There is a list of default prices for each day p[1..n] and a list of selling deduction f[0..x].
Selling deduction depends on how many stocks you sell at a time hence 0 to x.
Actual price per day depends on how many stocks you sell.
For instance, p[1] is 500 and f[120] is 60. If you have 200 stocks and you sell 120 on day 1.
Profit = 120 * (500 - 60) = 50400
The deduction carries on into subsequent days, so if you sell the remaining stocks on day 2 with p[2] = 300 and f[80] = 40.
Profit = 80 * (300 - 40 - 60) = 16000
I'm trying to maximize total profit by deciding how many stocks to sell on each day.
I'm not very good at DP problems and have only practiced the traditional problems. I'm getting kind of stuck on this one. I think the approach might use a 3D array with x, n, and p being number of stocks, number of days, and price on that day after the deductions from previous days and the day of if applicable.
I'm trying to figure out the recurrence first for P(x,i,p) denoting maximum profit by selling x shares by day i with p being the price of stocks on day i, but I'm getting stuck as well trying to figure out patterns by working with small examples.
How can I approach this?
One simple recursive that could solve this problem in O(x * n) is like bellow (Where x is the total amount of stocks and n is the total number of days) :
p = [100, 200, 90, 190]
f = [1, 3, 4, 5, 6, 3, 5]
def max_profit(stocks, day):
if day == 1:
return stocks * (p[day - 1] - f[stocks - 1]), [stocks]
max = 0
history = []
for i in range(0, stocks + 1):
prev_profit, prev_history = max_profit(stocks - i, day - 1)
profit = prev_profit + i * (p[day - 1] * f[stocks - 1])
if profit > max:
max = profit
history = prev_history
history.append(i)
return max, history
print(max_profit(7, 4))
In this algorithm I've considered all the ways that is possible to sell stock, I mean 0 share , 1 share, 2 share , .... x shares, and then called max_profit for previouse day recursively.

Algorithm to distribute a jackpot between winners

I'm building a betting pool system and I have to split the jackpot between all participants given the number of hits (accurate predictions of a certain sport game) they achieved, where more hits means a bigger prize.
For example, if we want to distribute a 1000 coins jackpot for this betting pool, we could use this distribution:
Is there any algorithm to calculate the prize given to each winner given this conditions?
Without knowing how you want to split the prize, one option is to calculate the total number of hits by all users, and divide the jackpot by that number to find the prize awarded to each hit.
You can then just go through and give each user a prize that is this number multiplied by the number of hits.
You can simply define how big the share for which number of hits is
Hits, winWeight, numberOfWinners
5, 24, n(5)
4, 12, n(4)
3, 4, n(3)
2, 2, n(2)
1, 1, n(1)
than you multiply these values with number of winners and get:
total=24*n(5)+12*n(4)+4*n(3)+2*n(2)+1*n(1)
Now you calculate how many coins:
jackpot/total * 24 = pricePerWinner for 5 hits
jackpot/total * 12 = pricePerWinner for 4 hits
jackpot/total * 4 = pricePerWinner for 3 hits
jackpot/total * 2 = pricePerWinner for 2 hits
jackpot/total * 1 = pricePerWinner for 1 hit
Calculating the amount of total hits.
5*6 = 30
4*40 = 160
3*80 = 240
2*20 = 40
1*15 = 15
0*2 = 0
If you add them all up together it would total up to
30+160+240+40+15+0=485
Since there are 1000 coins for the jackpot.
1000/485 ~= 2
This means that for each hit, it would grant 2 coins.
Eg. 5 hits would mean 10 coins per winner

Algorithm: Fill different baskets

Let's assume I have 3 different baskets with a fixed capacity
And n-products which provide different value for each basket -- you can only pick whole products
Each product should be limited to a max amount (i.e. you can maximal pick product A 5 times)
Every product adds at least 0 or more value to all baskets and come in all kinds of variations
Now I want a list with all possible combinations of products fitting in the baskets ordered by accuracy (like basket 1 is 5% more full would be 5% less accurate)
Edit: Example
Basket A capacity 100
Basket B capacity 80
Basket C capacity 30
fake products
Product 1 (A: 5, B: 10, C: 1)
Product 2 (A: 20 B: 0, C: 0)
There might be hundreds more products
Best fit with max 5 each would be
5 times Product 1
4 times Product 2
Result
A: 105
B: 50
C: 5
Accuracy: (qty_used / max_qty) * 100 = (160 / 210) * 100 = 76.190%
Next would be another combination with less accuracy
Any pointing in the right direction is highly appreciated Thanks
Edit:
instead of above method, accuracy should be as error and the list should be in ascending order of error.
Error(Basket x) = (|max_qty(x) - qty_used(x)| / max_qty(x)) * 100
and the overall error should be the weighted average of the errors of all baskets.
Total Error = [Σ (Error(x) * max_qty(x))] / [Σ (max_qty(x))]

how to find maximum profit while selling some garbage item

I have one problem statement for which i need write an algo. can somebody help me?
Problem is :
i have different length of iron rod let say {26, 103, 59}, i want to sell the same length of iron rod so that i can earn maximum profit. Also i have to cutting charge lets say 10 Rs/unit.
Case 1, if cutting charge is Rs 100 and sell length of 51 feet with cost 100 per unit.
then 103/ 51 = 2 length ((51 * 100 * 2) - ((1 * 100) + 200 ) = 9900
59 / 51 = 1 length ((51 * 100 * 1) - ((8 * 100) + 1* 100) = 4200
26/52 = 0 length((0 * 100)) - (26 * 100 ) = -2600
now total profit is = 11500
But if cutting charge is vary then this calculation like failed, can some one tell how can develop algo to find the maximum profit.

Progressive non-linear algorithm for increasing discount

A system has to support 100 users and the price for support is 3
A system has to support 10 000 users and the price for support is 1
I have to devise an algorithm to give me the price in between so it will gradually rise with the number of users.
I tried to multiply the number of users by 0.0002 to get the discount value and I got
300 users * 0.0002 = 0.06 discount, so price for support = 2.94
total income = 300 * 2.94 = 882
5000 users * 0.0002 = 1 discount, so price for support = 2
total income = 5000 * 2 = 10 000
8000 users * 0.0002 = 1.6 discount, so price for support = 1.4
total income = 8000 * 1.4 = 11 200
10 000 users * 0.0002 = 2 discount, so price for support = 1
total income = 8000 * 1.4 = 10 000
So you see after a given point I am actually having more users but receiving less payment.
I am not a mathematician and I now this is not really a programming question, but I don't know where else to ask this. I will appreciate if someone can help me with any information. Thanks!
price = n * (5 - log10(n)) will work for 100 < n < 10000.
Just make sure you're using base-10 log and not natural (base-e) log. If your language doesn't have base-10 log normally, you can calculate it like this:
function log10(x) { return log(x)/log(10); }.
For 100 users, that's 100 * (5 - log10(100)), which gives 100 * (5 - 2), which is 300.
For 1000 users, that's 1000 * (5 - log10(1000)), which gives 1000 * (5 - 3), which is 2000.
For 10000 users, that's 10000 * (5 - log10(10000)), which gives 10000 * (5 - 4), which is 10000.
Let's pick some more random figures.
2500 users: 2500 * (5 - log10(2500)) gives us 2500 * (5 - 3.39794), which is 4005.
6500 users: 6500 * (5 - log10(6500)) gives us 6500 * (5 - 3.81291), which is 7716.
8000 users: 8000 * (5 - log10(8000)) gives us 8000 * (5 - 3.90309), which is 8775.
Should work out about right for what you're modelling.
Scaling the price per user linearly didn't work as you showed, but you can try scaling the total income linearly instead.
total income for 100 users = 300
total income for 10000 users = 10000
total income for n users = (n-100) / (10000-100) * (10000-300) + 300
You know that the total income for n users is the price for support per user times the number of users, that means, now you have to find the function f(n) such that f(n) * n = (n-100) / (10000-100) * (10000-300) + 300.
And if you have to show that as the total income always increase, the price for support always decrease, just show that f'(n) ≤ 0 when 100 ≤ n ≤ 10000.

Resources