Add solution for problem 76 in C and python
This commit is contained in:
46
C/p076.c
46
C/p076.c
@ -12,23 +12,29 @@ How many different ways can one hundred be written as a sum of at least two posi
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int count(int value, int n, int i);
|
||||
|
||||
int integers[99];
|
||||
#include "projecteuler.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, n;
|
||||
long int *partitions;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
for(i = 0; i < 99; i++)
|
||||
integers[i] = i + 1;
|
||||
if((partitions = (long int *)calloc(100, sizeof(long int))) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error while allocating memory\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = count(0, 0, 0);
|
||||
/* The number of ways a number can be written as a sum is given by the partition function
|
||||
* (-1 because the partition function includes also the number itself).
|
||||
* The function is implemented in projecteuler.c*/
|
||||
n = partition_fn(100, partitions) - 1;
|
||||
|
||||
free(partitions);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
@ -41,29 +47,3 @@ int main(int argc, char **argv)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count(int value, int n, int i)
|
||||
{
|
||||
int j;
|
||||
|
||||
for(j = i; j < 99; j++)
|
||||
{
|
||||
value += integers[j];
|
||||
|
||||
if(value == 100)
|
||||
{
|
||||
return n + 1;
|
||||
}
|
||||
else if(value > 100)
|
||||
{
|
||||
return n;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = count(value, n, j);
|
||||
value -= integers[j];
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
@ -783,3 +783,45 @@ int phi(int n, int *primes)
|
||||
|
||||
return (int)ph;
|
||||
}
|
||||
|
||||
/* Function implementing the partition function.*/
|
||||
long int partition_fn(int n, long int *partitions)
|
||||
{
|
||||
int k, limit;
|
||||
long int res = 0;
|
||||
|
||||
/* The partition function for negative numbers is 0 by definition.*/
|
||||
if(n < 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The partition function for zero is 1 by definition.*/
|
||||
if(n == 0)
|
||||
{
|
||||
partitions[n] = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* If the partition for the current n has already been calculated, return the value.*/
|
||||
if(partitions[n] != 0)
|
||||
{
|
||||
return partitions[n];
|
||||
}
|
||||
|
||||
k = -ceil((sqrt(24*n+1)-1)/6);
|
||||
limit = floor((sqrt(24*n+1)+1)/6);
|
||||
|
||||
while(k <= limit)
|
||||
{
|
||||
if(k != 0)
|
||||
{
|
||||
res += pow(-1, k+1) * partition_fn(n-k*(3*k-1)/2, partitions);
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
partitions[n] = res;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@ -24,5 +24,6 @@ int pell_eq(int i, mpz_t x);
|
||||
int is_semiprime(int n, int *p, int *q, int *primes);
|
||||
int phi_semiprime(int n, int p, int q);
|
||||
int phi(int n, int *primes);
|
||||
long int partition_fn(int n, long int *partitions);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user