Add solutions for problem 31
This commit is contained in:
parent
e8f5980a55
commit
cadb92b671
55
C/p031.c
Normal file
55
C/p031.c
Normal file
@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
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;
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from numpy import zeros
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def main():
|
||||
|
33
Python/p031.py
Normal file
33
Python/p031.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user