From cadb92b671d54d6c217e2a655e8e25f817105d6a Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Fri, 20 Sep 2019 15:12:47 +0200 Subject: [PATCH] Add solutions for problem 31 --- C/p031.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ Python/p030.py | 2 -- Python/p031.py | 33 ++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 C/p031.c create mode 100644 Python/p031.py diff --git a/C/p031.c b/C/p031.c new file mode 100644 index 0000000..41982ab --- /dev/null +++ b/C/p031.c @@ -0,0 +1,55 @@ +#include +#include +#include + +int count(int value, int n, int i); + +int coins[8] = {1, 2, 5, 10, 20, 50, 100, 200}; + +int main(int argc, char **argv) +{ + int n; + double elapsed; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + n = count(0, 0, 0); + + clock_gettime(CLOCK_MONOTONIC, &end); + + elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; + + printf("Project Euler, Problem 31\n"); + printf("Answer: %d\n", n); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} + +int count(int value, int n, int i) +{ + int j; + + for(j = i; j < 8; j++) + { + value += coins[j]; + + if(value == 200) + { + return n+1; + } + else if(value > 200) + { + return n; + } + else + { + n = count(value, n, j); + value -= coins[j]; + } + } + + return n; +} diff --git a/Python/p030.py b/Python/p030.py index c86cba5..c0e86a1 100644 --- a/Python/p030.py +++ b/Python/p030.py @@ -1,7 +1,5 @@ #!/usr/bin/python3 -from numpy import zeros - from timeit import default_timer def main(): diff --git a/Python/p031.py b/Python/p031.py new file mode 100644 index 0000000..c56f4e4 --- /dev/null +++ b/Python/p031.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 + +from timeit import default_timer + +def count(coins, value, n, i): + for j in range(i, 8): + value = value + coins[j] + if value == 200: + return n + 1 + elif value > 200: + return n + else: + n = count(coins, value, n, j) + value = value - coins[j] + + return n + +def main(): + start = default_timer() + + coins = [1, 2, 5, 10, 20, 50, 100, 200] + + n = count(coins, 0, 0, 0) + + end = default_timer() + + print('Project Euler, Problem 30') + print('Answer: {}'.format(n)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main()