[Solved] Count how many days it will take using the while loop


I apologize in advance, for I’m still actively learning C++, but think I can reproduce your desired output in PHP while loops. From what I could gather, the formula you would need is n = n – i with i being the iterator that doubles each day and n being the number of bags…

  $n = 10; // number of bags gathered
  $i = 1; // iterator for day 1
  $days = array($n); // create an array to help keep track of the days
  $n = $n - $i; // day 1 has to be done outside the loop
  array_unshift($days, $n); // prepend new n value to array

  // while loop
  while ($n > 0) {
    $i = ($i * 2); // double the iterator each day (run)
    $n = $n - $i;
    if ($n > 0) {
      array_unshift($days, $n); // prepend each new n value to array
    }
  }
  // count each item in the array
  echo $numberOfDays = count($days); // outputs 4

Checked it on pen and paper a few times with some small numbers and each turned out correct. I’m assuming this same concept could be applied to C++.

2

solved Count how many days it will take using the while loop