Count the number of ways to make a given sum using one number from each array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I have three arrays, each of which contains only numbers. Given a target number, I need to return the number of ways I can make that sum, using exactly one number from each of the arrays.



For example: If I have 2 arrays [[1,2,3], [1,2,3]] and the target sum is 4, I must return the number of combinations that will sum to 4. In this case, they are:



1+3 = 4
2+2 = 4
3+1 = 4


So the function will return 3.



I wrote the function below, and it works well, but I am looking for a way to make more efficient, as well as make it scale to more input arrays - I have less than 6 arrays, but I want it to work if I have a hundred arrays. Is there any array function that can help me here?



This is the code:



<?php
function get_sum ($dice, $sum)
$sumcount = array();
$num_of_dice = count($dice);
foreach($dice[0] as $die1)
foreach($dice[1] as $die2)
if($num_of_dice == 5)
foreach($dice[2] as $die3)
foreach($dice[3] as $die4)
foreach($dice[4] as $die5)
if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
$good_res = array();
array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
array_push($sumcount, $good_res);





if($num_of_dice == 4)
foreach($dice[2] as $die3)
foreach($dice[3] as $die4)
if($die1 + $die2 + $die3+ $die4 == $sum)
$good_res = array();
array_push( $good_res, $die1, $die2, $die3, $die4);
array_push($sumcount, $good_res);



elseif ($num_of_dice == 3)
foreach($dice[2] as $die3)
if($die1 + $die2 + $die3 == $sum)
$good_res = array();
array_push( $good_res, $die1, $die2, $die3);
array_push($sumcount, $good_res);


else
if($die1 + $die2 == $sum)
$good_res = array();
array_push( $good_res, $die1, $die2);
array_push($sumcount, $good_res);




;



echo count($sumcount);




get_sum([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]], 9)
?>






share|improve this question



























    up vote
    1
    down vote

    favorite












    I have three arrays, each of which contains only numbers. Given a target number, I need to return the number of ways I can make that sum, using exactly one number from each of the arrays.



    For example: If I have 2 arrays [[1,2,3], [1,2,3]] and the target sum is 4, I must return the number of combinations that will sum to 4. In this case, they are:



    1+3 = 4
    2+2 = 4
    3+1 = 4


    So the function will return 3.



    I wrote the function below, and it works well, but I am looking for a way to make more efficient, as well as make it scale to more input arrays - I have less than 6 arrays, but I want it to work if I have a hundred arrays. Is there any array function that can help me here?



    This is the code:



    <?php
    function get_sum ($dice, $sum)
    $sumcount = array();
    $num_of_dice = count($dice);
    foreach($dice[0] as $die1)
    foreach($dice[1] as $die2)
    if($num_of_dice == 5)
    foreach($dice[2] as $die3)
    foreach($dice[3] as $die4)
    foreach($dice[4] as $die5)
    if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
    $good_res = array();
    array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
    array_push($sumcount, $good_res);





    if($num_of_dice == 4)
    foreach($dice[2] as $die3)
    foreach($dice[3] as $die4)
    if($die1 + $die2 + $die3+ $die4 == $sum)
    $good_res = array();
    array_push( $good_res, $die1, $die2, $die3, $die4);
    array_push($sumcount, $good_res);



    elseif ($num_of_dice == 3)
    foreach($dice[2] as $die3)
    if($die1 + $die2 + $die3 == $sum)
    $good_res = array();
    array_push( $good_res, $die1, $die2, $die3);
    array_push($sumcount, $good_res);


    else
    if($die1 + $die2 == $sum)
    $good_res = array();
    array_push( $good_res, $die1, $die2);
    array_push($sumcount, $good_res);




    ;



    echo count($sumcount);




    get_sum([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]], 9)
    ?>






    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have three arrays, each of which contains only numbers. Given a target number, I need to return the number of ways I can make that sum, using exactly one number from each of the arrays.



      For example: If I have 2 arrays [[1,2,3], [1,2,3]] and the target sum is 4, I must return the number of combinations that will sum to 4. In this case, they are:



      1+3 = 4
      2+2 = 4
      3+1 = 4


      So the function will return 3.



      I wrote the function below, and it works well, but I am looking for a way to make more efficient, as well as make it scale to more input arrays - I have less than 6 arrays, but I want it to work if I have a hundred arrays. Is there any array function that can help me here?



      This is the code:



      <?php
      function get_sum ($dice, $sum)
      $sumcount = array();
      $num_of_dice = count($dice);
      foreach($dice[0] as $die1)
      foreach($dice[1] as $die2)
      if($num_of_dice == 5)
      foreach($dice[2] as $die3)
      foreach($dice[3] as $die4)
      foreach($dice[4] as $die5)
      if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
      $good_res = array();
      array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
      array_push($sumcount, $good_res);





      if($num_of_dice == 4)
      foreach($dice[2] as $die3)
      foreach($dice[3] as $die4)
      if($die1 + $die2 + $die3+ $die4 == $sum)
      $good_res = array();
      array_push( $good_res, $die1, $die2, $die3, $die4);
      array_push($sumcount, $good_res);



      elseif ($num_of_dice == 3)
      foreach($dice[2] as $die3)
      if($die1 + $die2 + $die3 == $sum)
      $good_res = array();
      array_push( $good_res, $die1, $die2, $die3);
      array_push($sumcount, $good_res);


      else
      if($die1 + $die2 == $sum)
      $good_res = array();
      array_push( $good_res, $die1, $die2);
      array_push($sumcount, $good_res);




      ;



      echo count($sumcount);




      get_sum([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]], 9)
      ?>






      share|improve this question













      I have three arrays, each of which contains only numbers. Given a target number, I need to return the number of ways I can make that sum, using exactly one number from each of the arrays.



      For example: If I have 2 arrays [[1,2,3], [1,2,3]] and the target sum is 4, I must return the number of combinations that will sum to 4. In this case, they are:



      1+3 = 4
      2+2 = 4
      3+1 = 4


      So the function will return 3.



      I wrote the function below, and it works well, but I am looking for a way to make more efficient, as well as make it scale to more input arrays - I have less than 6 arrays, but I want it to work if I have a hundred arrays. Is there any array function that can help me here?



      This is the code:



      <?php
      function get_sum ($dice, $sum)
      $sumcount = array();
      $num_of_dice = count($dice);
      foreach($dice[0] as $die1)
      foreach($dice[1] as $die2)
      if($num_of_dice == 5)
      foreach($dice[2] as $die3)
      foreach($dice[3] as $die4)
      foreach($dice[4] as $die5)
      if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
      $good_res = array();
      array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
      array_push($sumcount, $good_res);





      if($num_of_dice == 4)
      foreach($dice[2] as $die3)
      foreach($dice[3] as $die4)
      if($die1 + $die2 + $die3+ $die4 == $sum)
      $good_res = array();
      array_push( $good_res, $die1, $die2, $die3, $die4);
      array_push($sumcount, $good_res);



      elseif ($num_of_dice == 3)
      foreach($dice[2] as $die3)
      if($die1 + $die2 + $die3 == $sum)
      $good_res = array();
      array_push( $good_res, $die1, $die2, $die3);
      array_push($sumcount, $good_res);


      else
      if($die1 + $die2 == $sum)
      $good_res = array();
      array_push( $good_res, $die1, $die2);
      array_push($sumcount, $good_res);




      ;



      echo count($sumcount);




      get_sum([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]], 9)
      ?>








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 22 at 9:42









      Toby Speight

      17.2k13487




      17.2k13487









      asked Jun 22 at 5:29









      DavSev

      1083




      1083




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted











          function get_sum ($dice, $sum) 
          ...
          echo count($sumcount);




          Both the stated specification and the function name say that it will return the count, not print it.



          Well, the function name get_XXX says it will return something. If you gave me the signature get_sum ($dice, $sum) and asked me to implement the body I would ask what $dice was for, since the signature clearly says that the body will be return $sum;. Surely there's a better name?





           $sumcount = array();
          ...
          echo count($sumcount);



          That's a rather memory-inefficient way of counting something. Why not increment an integer counter instead of adding an array to an array?





           $good_res = array();
          array_push( $good_res, $die1, $die2, $die3, $die4, $die5);



          Why not $good_res = array($die1, $die2, $die3, $die4, $die5)?





           if($num_of_dice == 5)
          foreach($dice[2] as $die3)
          foreach($dice[3] as $die4)
          foreach($dice[4] as $die5)
          if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
          $good_res = array();
          array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
          array_push($sumcount, $good_res);








          Again, the aim is to count. If $die1 + $die2 + $die3 + $die4 + $die5 == $sum, why does the loop over $die5 care what $die3 is? You could as well say



          foreach ($dice[0] as $die1) 
          $counter += get_sum(array_slice($dice, 1), $sum - $die1);



          You need to handle a couple of base cases, but that gives you code which should have fairly similar performance to the current code but is much much simpler. Then ask yourself how to memoise. Then ask yourself how to replace the recursion with iteration. The final result will be an iterative method which is about 10 lines long, handles any number of dice, and runs much faster than your current code.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197030%2fcount-the-number-of-ways-to-make-a-given-sum-using-one-number-from-each-array%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted











            function get_sum ($dice, $sum) 
            ...
            echo count($sumcount);




            Both the stated specification and the function name say that it will return the count, not print it.



            Well, the function name get_XXX says it will return something. If you gave me the signature get_sum ($dice, $sum) and asked me to implement the body I would ask what $dice was for, since the signature clearly says that the body will be return $sum;. Surely there's a better name?





             $sumcount = array();
            ...
            echo count($sumcount);



            That's a rather memory-inefficient way of counting something. Why not increment an integer counter instead of adding an array to an array?





             $good_res = array();
            array_push( $good_res, $die1, $die2, $die3, $die4, $die5);



            Why not $good_res = array($die1, $die2, $die3, $die4, $die5)?





             if($num_of_dice == 5)
            foreach($dice[2] as $die3)
            foreach($dice[3] as $die4)
            foreach($dice[4] as $die5)
            if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
            $good_res = array();
            array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
            array_push($sumcount, $good_res);








            Again, the aim is to count. If $die1 + $die2 + $die3 + $die4 + $die5 == $sum, why does the loop over $die5 care what $die3 is? You could as well say



            foreach ($dice[0] as $die1) 
            $counter += get_sum(array_slice($dice, 1), $sum - $die1);



            You need to handle a couple of base cases, but that gives you code which should have fairly similar performance to the current code but is much much simpler. Then ask yourself how to memoise. Then ask yourself how to replace the recursion with iteration. The final result will be an iterative method which is about 10 lines long, handles any number of dice, and runs much faster than your current code.






            share|improve this answer

























              up vote
              5
              down vote



              accepted











              function get_sum ($dice, $sum) 
              ...
              echo count($sumcount);




              Both the stated specification and the function name say that it will return the count, not print it.



              Well, the function name get_XXX says it will return something. If you gave me the signature get_sum ($dice, $sum) and asked me to implement the body I would ask what $dice was for, since the signature clearly says that the body will be return $sum;. Surely there's a better name?





               $sumcount = array();
              ...
              echo count($sumcount);



              That's a rather memory-inefficient way of counting something. Why not increment an integer counter instead of adding an array to an array?





               $good_res = array();
              array_push( $good_res, $die1, $die2, $die3, $die4, $die5);



              Why not $good_res = array($die1, $die2, $die3, $die4, $die5)?





               if($num_of_dice == 5)
              foreach($dice[2] as $die3)
              foreach($dice[3] as $die4)
              foreach($dice[4] as $die5)
              if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
              $good_res = array();
              array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
              array_push($sumcount, $good_res);








              Again, the aim is to count. If $die1 + $die2 + $die3 + $die4 + $die5 == $sum, why does the loop over $die5 care what $die3 is? You could as well say



              foreach ($dice[0] as $die1) 
              $counter += get_sum(array_slice($dice, 1), $sum - $die1);



              You need to handle a couple of base cases, but that gives you code which should have fairly similar performance to the current code but is much much simpler. Then ask yourself how to memoise. Then ask yourself how to replace the recursion with iteration. The final result will be an iterative method which is about 10 lines long, handles any number of dice, and runs much faster than your current code.






              share|improve this answer























                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted







                function get_sum ($dice, $sum) 
                ...
                echo count($sumcount);




                Both the stated specification and the function name say that it will return the count, not print it.



                Well, the function name get_XXX says it will return something. If you gave me the signature get_sum ($dice, $sum) and asked me to implement the body I would ask what $dice was for, since the signature clearly says that the body will be return $sum;. Surely there's a better name?





                 $sumcount = array();
                ...
                echo count($sumcount);



                That's a rather memory-inefficient way of counting something. Why not increment an integer counter instead of adding an array to an array?





                 $good_res = array();
                array_push( $good_res, $die1, $die2, $die3, $die4, $die5);



                Why not $good_res = array($die1, $die2, $die3, $die4, $die5)?





                 if($num_of_dice == 5)
                foreach($dice[2] as $die3)
                foreach($dice[3] as $die4)
                foreach($dice[4] as $die5)
                if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
                $good_res = array();
                array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
                array_push($sumcount, $good_res);








                Again, the aim is to count. If $die1 + $die2 + $die3 + $die4 + $die5 == $sum, why does the loop over $die5 care what $die3 is? You could as well say



                foreach ($dice[0] as $die1) 
                $counter += get_sum(array_slice($dice, 1), $sum - $die1);



                You need to handle a couple of base cases, but that gives you code which should have fairly similar performance to the current code but is much much simpler. Then ask yourself how to memoise. Then ask yourself how to replace the recursion with iteration. The final result will be an iterative method which is about 10 lines long, handles any number of dice, and runs much faster than your current code.






                share|improve this answer














                function get_sum ($dice, $sum) 
                ...
                echo count($sumcount);




                Both the stated specification and the function name say that it will return the count, not print it.



                Well, the function name get_XXX says it will return something. If you gave me the signature get_sum ($dice, $sum) and asked me to implement the body I would ask what $dice was for, since the signature clearly says that the body will be return $sum;. Surely there's a better name?





                 $sumcount = array();
                ...
                echo count($sumcount);



                That's a rather memory-inefficient way of counting something. Why not increment an integer counter instead of adding an array to an array?





                 $good_res = array();
                array_push( $good_res, $die1, $die2, $die3, $die4, $die5);



                Why not $good_res = array($die1, $die2, $die3, $die4, $die5)?





                 if($num_of_dice == 5)
                foreach($dice[2] as $die3)
                foreach($dice[3] as $die4)
                foreach($dice[4] as $die5)
                if($die1 + $die2 + $die3+ $die4 + $die5 == $sum)
                $good_res = array();
                array_push( $good_res, $die1, $die2, $die3, $die4, $die5);
                array_push($sumcount, $good_res);








                Again, the aim is to count. If $die1 + $die2 + $die3 + $die4 + $die5 == $sum, why does the loop over $die5 care what $die3 is? You could as well say



                foreach ($dice[0] as $die1) 
                $counter += get_sum(array_slice($dice, 1), $sum - $die1);



                You need to handle a couple of base cases, but that gives you code which should have fairly similar performance to the current code but is much much simpler. Then ask yourself how to memoise. Then ask yourself how to replace the recursion with iteration. The final result will be an iterative method which is about 10 lines long, handles any number of dice, and runs much faster than your current code.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jun 22 at 10:08









                Peter Taylor

                14k2454




                14k2454






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197030%2fcount-the-number-of-ways-to-make-a-given-sum-using-one-number-from-each-array%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation