aboutsummaryrefslogtreecommitdiff
path: root/challenge-201/paulo-custodio/c/ch-2.c
blob: b3587f47f5a322937fa02547e5dc34311b72534e (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Challenge 201

Task 2: Penny Piles
Submitted by: Robbie Hatley

You are given an integer, $n > 0.

Write a script to determine the number of ways of putting $n pennies in a row
of piles of ascending heights from left to right.
Example

Input: $n = 5
Output: 7

Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles:

    1 1 1 1 1
    1 1 1 2
    1 2 2
    1 1 3
    2 3
    1 4
    5
*/

#include <stdio.h>
#include <stdlib.h>

int make_piles1(int prev, int n) {
    int count = 0;
    if (n == 0)
        count++;
    else if (n > 0) {
        int max = (prev < 0) ? n : prev;
        for (int i = 1; i <= max; i++)
            count += make_piles1(i, n-i);
    }
    return count;
}

int make_piles(int n) {
    return make_piles1(-1, n);
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        fputs("usage: ch-2 N", stderr);
        exit(EXIT_FAILURE);
    }

    printf("%d\n", make_piles(atoi(argv[1])));
}