Kodewick
Start learning →
Starter←→0 / 50

Monkey and peaches

#024Starterx = (x + 1) * 25 min

A monkey picks a pile of peaches. Each day it eats half of what is left, plus 1 more. On the morning of day days, only 1 is left. Write peaches(days): how many peaches were there on day 1? If days is 2: 1 is left on day 2, so day 1 had (1 + 1) × 2 = 4. If days is 1, the answer is 1.

Examples
peaches(10)→1534
peaches(2)→4
peaches(3)→10
The functionint peaches(int days)
main.c
int peaches(int days) {
    int x = 1;
    for (int d = days - 1; d >= 1; d--) {
        // your code here: from the next day's count, work out the day before
    }
    return x;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.