So, I'm working on a sort of economics game. I'm having trouble with a certain part of it.

In the game, players can buy shares of a company's stock. Every time 1000 shares are sold, the company levels up and the value of each share goes up 10%. (I know this seems strange, but there's a reason I want it to work this way.)

As soon as I coded this, I realized I left open a problematic exploit: If a player comes to the company at 999 shares, he could buy two shares, which would raise the price, and sell them back which would lower it again, and do this repeatedly for infinite money.

So, I concluded that in order for this to work, the program has to offer multiple prices for one large order. For instance, if the player buys 3000 shares, the system needs to charge him one price for the first thousand, 10% more of the second, and 10% more than that for the third.

The thing is, I'm not sure how to represent this mathematically. It's somewhat similar to an acceleration problem in physics, but it's complicated by the fact that acceleration is exponential rather than linear. Anyone with a strong mathematical background able to come up with a formula that will return the correct total for a bulk order, taking into account the changes in the company's "level" that may take place over its course? Thanks for your time, sorry if my explanation of the problem isn't too clear.

# Need help designing an algorithm

Page 1 of 1## 7 Replies - 1364 Views - Last Post: 05 December 2013 - 03:39 PM

##
**Replies To:** Need help designing an algorithm

### #2

## Re: Need help designing an algorithm

Posted 05 December 2013 - 02:05 PM

It seems like a numerical approach. I would try something like this. A differential approach wouldn't break it up piecewise as you want.

initial_price := 10 share_amount := k num_thresholds = ceil(log(k, 10)) //log_{10}(k) total := 0 for i = 0 to num_thresholds price := initial_price + initial_price * 0.10 * i if(share_amount < 1000){ total += price * share_amount } else{ total += 1000 * share_amount } end for

### #3

## Re: Need help designing an algorithm

Posted 05 December 2013 - 02:05 PM

Let's see... I suppose the first step would be to figure out if the purchase will push the company to the next level. Figure out this value, and charge the current price for it.

The next step would be to figure out how many levels it will increase. Maybe a while loop, subtracting 1000 and adjusting the price each time.

Finally, you'd have to figure out how much is left over, and the correct price for that.

I guess what I can do is subtract from the order and start counting backwards. That actually doesn't seem to hard when I write it that way.

The next step would be to figure out how many levels it will increase. Maybe a while loop, subtracting 1000 and adjusting the price each time.

Finally, you'd have to figure out how much is left over, and the correct price for that.

I guess what I can do is subtract from the order and start counting backwards. That actually doesn't seem to hard when I write it that way.

### #4

## Re: Need help designing an algorithm

Posted 05 December 2013 - 02:12 PM

I am guessing if the stock sells off the company loses it's leveling?

As for the 3000 vs three 1000 stocks purchased you can add modifiers to push for better behavior.. like "fees" and such for a transaction. Push for a specific behavior and what not.

As for the 3000 vs three 1000 stocks purchased you can add modifiers to push for better behavior.. like "fees" and such for a transaction. Push for a specific behavior and what not.

### #5

## Re: Need help designing an algorithm

Posted 05 December 2013 - 02:52 PM

@macosxnerd101 - Sorry, I made that last post before I saw yours. Studying your solution now, thanks.

"I am guessing if the stock sells off the company loses it's leveling?"

Yes, that's correct.

" As for the 3000 vs three 1000 stocks purchased you can add modifiers to push for better behavior.. like "fees" and such for a transaction. Push for a specific behavior and what not."

Even if I did that, I'd have to have a way to handle the cases where users decide to act otherwise.

"I am guessing if the stock sells off the company loses it's leveling?"

Yes, that's correct.

" As for the 3000 vs three 1000 stocks purchased you can add modifiers to push for better behavior.. like "fees" and such for a transaction. Push for a specific behavior and what not."

Even if I did that, I'd have to have a way to handle the cases where users decide to act otherwise.

### #6

## Re: Need help designing an algorithm

Posted 05 December 2013 - 03:02 PM

@macosxnerd - Ok, I think I understand, but I have two questions.

What is the role of log in calculating num_thresholds? (Showing an embarrassing lack of mathematical literacy here, I know.)

Wouldn't this be linear growth instead of the exponential growth the problem calls for?

Nevertheless, I think with some more study, I can create a solution based on what you gave me. Thanks.

EDIT: Maybe

What is the role of log in calculating num_thresholds? (Showing an embarrassing lack of mathematical literacy here, I know.)

price := initial_price + initial_price * 0.10 * i

Wouldn't this be linear growth instead of the exponential growth the problem calls for?

Nevertheless, I think with some more study, I can create a solution based on what you gave me. Thanks.

EDIT: Maybe

initial_price * 1.10^iwould be better?

This post has been edited by **RuneChart**: 05 December 2013 - 03:03 PM

### #7

## Re: Need help designing an algorithm

Posted 05 December 2013 - 03:16 PM

Ok, what about this? (just assume the initial price is one)

existing_shares := s //bought prior to this transaction share_amount := k num_thresholds = ceil (k / 1000) -1 total := 0 for i = 0 to num_thresholds price := round(1.10^(i+(existing_shares/1000))) if(share_amount < 1000){ total += price * share_amount } else{ total += price * 1000 share_amount -= 1000 } end forOk, this is still incomplete. It doesn't work if existing_shares is not a multiple of 1000. Let me try again.

### #8

## Re: Need help designing an algorithm

Posted 05 December 2013 - 03:39 PM

Ok, I think I got it. Thanks for the advice.

existing_shares := s //the number of shares bought in previous transactions share_amount := k num_thresholds = ceil (k / 1000) -1 total := 0 while (share_amount > 0) price := round(1.10^(floor(existing_shares/1000))) shares_to_nextk = nearestk(share_amount) - share_amount shares_sold = min(share_amount, shares_to_nextk) total += price * shares_sold share_amount -= shares_sold existing_shares += shares_sold end while

Page 1 of 1