fork download
  1. <?php
  2. $monthly = 5000;
  3. $percents = [1.04,1.03,1.02];
  4. $comission = [500, 1000, 0];
  5. $firstPay = 7777;
  6. $balance=39999;
  7. function credit($balance, $monthly, $percents, $comission, $firstPay){
  8. $totalPayed=$firstPay;
  9. $balance+=$firstPay;
  10. echo "$balance $totalPayed $percents $comission $monthly \n";
  11. for($i=1;$i<=20;$i++){
  12. $balance=($balance*$percents)+$comission-$monthly;
  13. $totalPayed=$totalPayed + $monthly;
  14. echo "$balance $totalPayed \n";
  15. if ($balance<=0){
  16. $absBalance=abs($balance);
  17. $totalPayed-=$absBalance;
  18. $balance+=$absBalance;
  19. echo "Enough! Total payed: {$totalPayed} current balance: {$balance} $i \n";
  20. break;
  21. }
  22.  
  23. }
  24. return $totalPayed;
  25. }
  26. $totalPayed1=credit($balance, $monthly, $percents[0], $comission[0], 0);
  27. $totalPayed2=credit($balance, $monthly, $percents[1], $comission[1], 0);
  28. $totalPayed3=credit($balance, $monthly, $percents[2], $comission[2], $firstPay);
  29. echo "first: $totalPayed1 second: $totalPayed2 third: $totalPayed3 \n";
  30. ?>
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
39999 0 1.04 500 5000 
37098.96 5000 
34082.9184 10000 
30946.235136 15000 
27684.08454144 20000 
24291.447923098 25000 
20763.105840022 30000 
17093.630073622 35000 
13277.375276567 40000 
9308.47028763 45000 
5180.8090991352 50000 
888.04146310056 55000 
-3576.4368783754 60000 
Enough! Total payed: 56423.563121625 current balance: 0 12 
39999 0 1.03 1000 5000 
37198.97 5000 
34314.9391 10000 
31344.387273 15000 
28284.71889119 20000 
25133.260457926 25000 
21887.258271663 30000 
18543.876019813 35000 
15100.192300408 40000 
11553.19806942 45000 
7899.7940115026 50000 
4136.7878318477 55000 
260.89146680313 60000 
-3731.2817891928 65000 
Enough! Total payed: 61268.718210807 current balance: 0 13 
47776 7777 1.02 0 5000 
43731.52 12777 
39606.1504 17777 
35398.273408 22777 
31106.23887616 27777 
26728.363653683 32777 
22262.930926757 37777 
17708.189545292 42777 
13062.353336198 47777 
8323.6004029218 52777 
3490.0724109802 57777 
-1440.1261408001 62777 
Enough! Total payed: 61336.8738592 current balance: 0 11 
first: 56423.563121625 second: 61268.718210807 third: 61336.8738592