I have to write a program that will calculate how much someone would earn if someone offered to pay them a penny a day, doubling every day for a user inputted amount of days. In other words, the number of pennies would go
day 1, n is zero, 2 to the zero is 1, answer is 1.
day 2, n is 1, 2 to the 1 is 2.
day 3, n is 2, n squared is 4
day 4, n is 3, 2 to the 3 is 8... so far we have 1,2,4,8,...
but on day 4 its the sum of all those that was EARNED. The sum is 15, so the answer is 15, not 8.
if you just want what the wage is on day n, its just 2 to that power as demonstrated. If you want the sum of all the days, the TOTAL earned, its a different value (that happens to be 2 to the n+1 -1).
its also a very old story... the king owed a favor to some guy and he said to pay him by putting a grain of rice on the first square of a chessboard, then 2 on the next, then 4 on the next, each year until the board is filled. A few years in the king just gave him the kingdom instead :)
To be fair, I prefer @mbozzi's solution with bit-shifting (it's one less than an exact power of two). There are some really fanciful ways of doing this problem, but I hope that the OP wasn't supposed to use a loop, or we've really misguided him/her!
I guess it comes down to whether this is marked by an automatic grader or an observant professor.
Err right, that's what I meant (one less than power of 2), my mistake. I mean we could just do pow(2, n)-1 but of course that can be made equivalent through bit-shifting.
I just liked yours because I've never actually seen sto* functions be used before.
... pennies / 100 is dollars. I don't know any way to make this any simpler.
the user needs to input the # of days. You can use that directly, due to happy circumstances:
day 1 is really day zero, because you need zero to get 1 penny on day 1 via 2 to the zero =1. But the total is 2 to the n+1 -1. So on day 1 you really want 0+1, which is 1, giving 2 to the 1 is 2, -1 is 1. All that to say that the +1 -1 junk cancels out, just use the # of days input from the user where 1 day is 1, 2 days is 2, and the value is 2 to the #of days -1.
@LilXeno,
Both coding solutions had user input (via cin). Both went directly for the answer to the problem: a geometric series with first term 1, common ratio 2 and number of terms n: that sum is exactly 2n-1. Both codes exploited binary representations to shortcut that: @mbozzi by bit-shifting to get 2n and then subtracting 1, mine simply noting that the sum was a series of powers of 2, or 111...111 (n lots of 1) in binary.
Both solutions assume that n is the actual number of days, starting at day=1. There is no point in confusing the issue by starting at day=0, as there are no array indices to worry about here.
If you use either of these codes then make sure that you know how they work, at least. Alternatively, you can just write a code with a loop to do the sum. Carry a variable e.g. dayPay (initialised as 1) and totalPay (initialised as dayPay) and loop a further n-1 times, multiplying dayPay by 2 and adding to totalPay on each loop.
If you want the final answer in unit of 100 then, as Ganado points out, divide your answer by 100.0 - that .0 is important: without it you may suffer from integer division if your sum in smaller amounts is an integer.
# include <iostream>
int main() {
int n = 0; std::cin >> n;
int sum = 0;
for (int i = 0; i < n; ++i)
sum += (1ull << i);
std::cout << sum / 100.0 << '\n';
}
Note: I hope this makes sense. Eventually the assignments will become less trivial, and solutions won't be so forthcoming.