Advertisement
koki2000

coinChallenge

Oct 16th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.42 KB | None | 0 0
  1. <?php
  2.    
  3.     $coins = [1, 2];
  4.     $totalAmount = 4;
  5.  
  6.     function calculateCombo(array $coins, int $amount, int $currentindex)
  7.     {
  8.         if($amount == 0) { return 1; }
  9.        
  10.         if($amount < 0) { return 0; }
  11.        
  12.         $combos = 0;
  13.         for($i = $currentindex; $i < count($coins); $i++)
  14.         {
  15.             $combos = $combos + calculateCombo($coins, $amount - $coins[$i], $i);
  16.         }
  17.        
  18.         return $combos;
  19.     }
  20.    
  21.     echo calculateCombo($coins, $totalAmount, 0);
  22. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement