Reputation: 3875
I have a scenerio that I want to achieve. Lets say there are 4 people A,B,C and D. Each user can deposit $10. And the deduction fee is $1. So initially we have
A: 10
Now when B will join, B will give $1 as deduction fee to the company and the company will share it in the previous clients. So.
A: 10+1
B: 9
Now person C joins in and the same thing happens. Now $1 deducted fee from C will be shared in A and B.
A: 10+1+0.5
B: 9+0.5
C: 9
There can be any n number of users and the starting free and deduction fee are variable. I am unable to do it successfully. This is my current code.
<?php
$i;
$y;
$people = 10;
$stake = 1;
$dev = 0.1;
$counter = 0;
$inner_counter = 0;
$total_money = $stake * $people;
$remaining = $total_money * $dev;
for ($y = $people; $y > 0; $y--) {
echo "Total Deposit: " . $stake . " | Profit: ";
$inner_counter = 0;
for ($i = 0; $i < $y; $i++) {
echo($stake * $dev);
$counter++;
$inner_counter++;
}
echo '<br>';
echo " Inner:" . $inner_counter . "";
echo '<br>';
}
echo $counter;
echo '<br>';
for ($x = 0; $x <= $people; $x++) {
echo "($x / $counter)*$dev ";
echo '<br>';
}
I am completely lost. If anyone can tell me a better approach, it'd help as well.
Upvotes: 2
Views: 60
Reputation: 23958
You can loop the count and foreach an array_slice to split the fee between the n number of people.
$people = ["A" => [], "B" => [], "C" => [], "D" => [], "E" => []];
$deposit = 10;
$fee =1;
$people["A"]["sum"] = $deposit;
for($i=1;$i<count($people);$i++){ // loop the same number of times as count of people
foreach(array_slice($people,0,$i) as $key => $p){ // only loop the number of people that should share the fee
if(!isset($people[$key]["sum"])) $people[$key]["sum"] = $deposit-$fee; // if it's the first round give them the starting sum
$people[$key]["sum"] += $fee/$i; // give them the percentage
}
}
$keys = array_keys($people);
$people[end($keys)]["sum"] = $deposit-$fee;
var_dump($people);
output:
array(5) {
["A"]=>
array(1) {
["sum"]=>
float(12.083333333333)
}
["B"]=>
array(1) {
["sum"]=>
float(10.083333333333)
}
["C"]=>
array(1) {
["sum"]=>
float(9.5833333333333)
}
["D"]=>
array(1) {
["sum"]=>
float(9.25)
}
["E"]=>
array(1) {
["sum"]=>
int(9)
}
}
Upvotes: 2
Reputation: 147156
Try this. I think the comments in the code and the variable names make it fairly self-explanatory:
$deposit = 10;
$fee = 1;
$people = 4;
$balances = array();
for ($p = 1; $p <= $people; $p++) {
// person p makes a deposit
$balances[$p] = $deposit;
// if more than one person, divide the fee by the preceding despositors
if ($p != 1) {
$balances[$p] -= $fee;
$split_fee = $fee / ($p - 1);
for ($i = 1; $i < $p; $i++) {
$balances[$i] += $split_fee;
}
}
}
print_r($balances);
Output: ($people = 4
):
Array
(
[1] => 11.833333333333
[2] => 9.8333333333333
[3] => 9.3333333333333
[4] => 9
)
Output ($people = 10
):
Array
(
[1] => 12.828968253968
[2] => 10.828968253968
[3] => 10.328968253968
[4] => 9.9956349206349
[5] => 9.7456349206349
[6] => 9.5456349206349
[7] => 9.3789682539683
[8] => 9.2361111111111
[9] => 9.1111111111111
[10] => 9
)
Edit
Based on OPs feedback, here is a version which takes an associative array as an input instead of a count of people, and populates that array with the final balances:
$deposit = 10;
$fee = 1;
$people = [ 'A' => ['total' => 0],
'B' => ['total' => 0],
'C' => ['total' => 0],
'D' => ['total' => 0],
'E' => ['total' => 0]
];
// get the names of the people
$names = array_keys($people);
for ($p = 1; $p <= count($people); $p++) {
// person p makes a deposit
$people[$names[$p-1]]['total'] = $deposit;
// if more than one person, divide the fee by the preceding despositors
if ($p != 1) {
$people[$names[$p-1]]['total'] -= $fee;
$split_fee = $fee / ($p - 1);
for ($i = 1; $i < $p; $i++) {
$people[$names[$i-1]]['total'] += $split_fee;
}
}
}
print_r($people);
Output:
Array
(
[A] => Array
(
[total] => 12.083333333333
)
[B] => Array
(
[total] => 10.083333333333
)
[C] => Array
(
[total] => 9.5833333333333
)
[D] => Array
(
[total] => 9.25
)
[E] => Array
(
[total] => 9
)
)
Upvotes: 4