title: Java8 Why is my solution faster than the neat solution? (hackerrank Chocolate Feast)
body: <p>Edit: Simplified my solution.
  Edit: Removed opinion based secondary question.</p>
  
  <p>Background: Atarted learning java a week or two ago using hackerranks problems as exercises and stackoverflow search + google as my teacher, I've had some limited experience learning other languages.</p>
  
  <p>I did the exercise my own "noobish learner way" which I can't help but feel is a "botched job" when I see "neat &amp; short" solutions.</p>
  
  <p>However, when submitting both solutions one after another a couple of times I found the "neat" solution was quite a bit slower. </p>
  
  <p>I vaguely remember something about % operations being costly, is mine faster because of no % operations or is there more to it than just that?</p>
  
  <p>Exercise: <a href="https://www.hackerrank.com/challenges/chocolate-feast" rel="nofollow">https://www.hackerrank.com/challenges/chocolate-feast</a></p>
  
  <p>Neat solution from discussion:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class Solution {
      static int cc; 
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          int t,n,c,m,r;
              t = in.nextInt();
              while(t--&gt;0){
               n = in.nextInt();
              c = in.nextInt();
               m = in.nextInt();
                  r=n/c;
                  cc=r;
  
                      while(r&gt;=m){
                          cc=cc+r/m;
                          r=r%m+r/m;
                      }
  
                  System.out.println(cc); 
              }
  
      }
  }
  </code></pre>
  
  <p>My Solution:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class Solution {
  
      public static void main(String[] args) {
  
          Scanner sc = new Scanner(System.in);
          int t = Integer.parseInt(sc.nextLine());    //t = number of test cases
          int[][] tc = readInput(sc, t);              //tc[t][0] = money. tc[t][1] = price. tc[t][2] = wrappers per free bar
  
          for (int i = 0; i&lt;t; i++){                  //loop for all test cases
              int choc = calcChoc(tc,i);              //work out how much choc can be bought
              System.out.println(choc);               //print result for the test case
          }
      }
      //calculate how much choc he can buy with m $ at p price with w wrappers needed for a free bar
      public static int calcChoc(int[][] tc,int i){
  
          int m = tc[i][0];       //money he has
          int p = tc[i][1];       //price of choc
          int w = tc[i][2];       //wrappers per free bar
  
          int bars = m/p;         //how many bars he can buy initially
          int wrappers = bars;    //each bar is a wrapper from initial purpose
  
          //loop to turn in all wrappers while it is possible to do so
          while (w&lt;=wrappers){
  
              int barsFromTurnIn = wrappers/w;                //bars from turning in current wrappers.
              bars = bars + barsFromTurnIn;                   //new bar count
              wrappers = wrappers - (barsFromTurnIn * (w-1)); //wrapper count reduced by amount of wrappers turned in -1 wrapper per bar recieved from turn in.
  
              if (w==1){ //break out of infinite loop when you get 1 bar for 1 wrapper!
                  System.out.print("Infinite Bars, exiting infinite loop at bars = ");
                  break;
              }
          }
          return bars;
      }
      //read input for each test case and make 2d array of the info
      public static int[][] readInput(Scanner sc, int t){
  
          int[][] input = new int[t][3];
  
          for (int i = 0; i&lt;t; i++){
              String[] inputLine = sc.nextLine().split(" ");
  
              input[i][0] = Integer.parseInt(inputLine[0]);
              input[i][1] = Integer.parseInt(inputLine[1]);
              input[i][2] = Integer.parseInt(inputLine[2]);
          }
          return input;
      }
  }
  </code></pre>