My Grades Are Suffering ... I Can't Do This

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

(1, 2, 4, 8, ...)

(Yes this is homework, don't judge me.)

1
2
3
4
5
# include <iostream>
int main() { 
  int n = 0; std::cin >> n;  
  std::cout << (n*(n + 1)*(2*n + 1) / 6) << '\n';
}
Last edited on
It doesn't work right ...

If I input 2, I should get 2 instead of 5.

If I input 4, I should get 8 instead of 30.

I'm confused.

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).

Right?
My mistake, sorry.
That formula is the sum of the first n squares. @jonnin's got it right.
1
2
3
4
5
6
# include <iostream>
int main() { 
  int n = 0; std::cin >> n;  
  // std::cout << (n*(n + 1)*(2*n + 1) / 6) << '\n'; // wrong 
  std::cout << (1ull << n) - 1 << '\n';
}
Last edited on
Thanks, @mbozzi and @ jonnin! It really helps!
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
using namespace std;
int main()
{ 
  int n = 0;
  cout << "Input days worked: ";   cin >> n;  
  cout << "Pay = " << stoull( string(n,'1'), 0, 2 ) << " pence\n";
}

Last edited on
That's clever. Since it's the sum of powers of 2.
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.
Last edited on
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.
@lastchance
It's a professor/teacher
so what would I do for a user inputted number?

I also need to show the final amount in dollars
The answers you got were already user-input programs.

To convert cents to dollars, divide by 100.0 when displaying.
Last edited on
... 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.


Last edited on
@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.
Last edited on
I tried to submit this and it needs a loop.

sorry.
1
2
3
4
5
6
7
8
9
10
# 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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int main()
{
    const int INITIAL_VALUE = 1 ; // value for day #1
    const int MULTIPLIER = 2 ;
    const int MAX_DAYS = 15 ;

    std::cout << "number of days [1-" << MAX_DAYS << "]? " ;
    int ndays ;
    std::cin >> ndays ;

    // sanity check to make sure ndays is not too big (or too small)
    if( ndays > MAX_DAYS ) ndays = MAX_DAYS ;
    if( ndays < 1 ) ndays = 1 ;

    unsigned long long wage_for_the_day = INITIAL_VALUE ;
    unsigned long long total_wages = 0 ;

    for( int i = 0 ; i < ndays ; ++i )
    {
        std::cout << "day #" << i+1 << " : " << wage_for_the_day << '\n' ;
        total_wages += wage_for_the_day ;

        wage_for_the_day *= MULTIPLIER ;
    }

    std::cout << "\ntotal for all days: " << total_wages << '\n' ;
}
Topic archived. No new replies allowed.