Minor changes

This commit is contained in:
daniele 2019-10-02 14:23:01 +02:00
parent 9e247f1624
commit 899be1ccf5
Signed by: fuxino
GPG Key ID: 6FE25B4A3EE16FDA
5 changed files with 30 additions and 48 deletions

View File

@ -24,10 +24,10 @@ int main(int argc, char **argv)
{ {
/* Calculate the sum of proper divisors with the function /* Calculate the sum of proper divisors with the function
* implemented in projecteuler.c.*/ * implemented in projecteuler.c.*/
n = sum_of_divisors(i); n = sum_of_divisors(i, 1);
/* If i!=n and the sum of proper divisors of n=i, /* If i!=n and the sum of proper divisors of n=i,
* sum the pair of numbers and add it to the total.*/ * sum the pair of numbers and add it to the total.*/
if(i != n && sum_of_divisors(n) == i) if(i != n && sum_of_divisors(n, 1) == i)
{ {
sum += i + n; sum += i + n;
} }

View File

@ -84,5 +84,5 @@ int main(int argc, char **argv)
int is_abundant(int n) int is_abundant(int n)
{ {
return sum_of_divisors(n) > n; return sum_of_divisors(n, 1) > n;
} }

View File

@ -16,20 +16,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
#include "projecteuler.h"
#define N 1000000 #define N 1000000
int sum_proper_divisors(int i); int sociable_chain(int i, int *chain, int l, int *min);
int sociable_chain(int i, int start, int *min, int l);
/* Vector to save the current chain values. I started with a longer vector,
* but no chain is longer than 100 elements, so this is sufficient.*/
int c[100];
int divisors[N] = {0}; int divisors[N] = {0};
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i, min = 0, min_tmp, length, l_max = 0; int i, min = 0, min_tmp, length, l_max = 0;
int chain[100];
double elapsed; double elapsed;
struct timespec start, end; struct timespec start, end;
@ -39,14 +37,14 @@ int main(int argc, char **argv)
{ {
/* Calculate the divisors of i and save it. Ii is equal to the sum of its proper divisors, /* Calculate the divisors of i and save it. Ii is equal to the sum of its proper divisors,
* the length of the chain is 1 and we don't need to check it.*/ * the length of the chain is 1 and we don't need to check it.*/
if((divisors[i] = sum_proper_divisors(i)) == i) if((divisors[i] = sum_of_divisors(i, 1)) == i)
{ {
continue; continue;
} }
else else
{ {
min_tmp = i; min_tmp = i;
length = sociable_chain(i, i, &min_tmp, 0); length = sociable_chain(i, chain, 0, &min_tmp);
} }
if(length > l_max) if(length > l_max)
@ -68,38 +66,15 @@ int main(int argc, char **argv)
return 0; return 0;
} }
int sum_proper_divisors(int n)
{
int i, limit, sum = 1;
limit = floor(sqrt(n));
for(i = 2; i <= limit; i++)
{
if(n % i == 0)
{
sum += i;
sum += n / i;
}
}
if(n == limit * limit)
{
sum -= limit;
}
return sum;
}
/* Function to recursively find the length of the chain.*/ /* Function to recursively find the length of the chain.*/
int sociable_chain(int i, int start, int *min, int l) int sociable_chain(int i, int *chain, int l, int *min)
{ {
int n; int n;
/* Save current number in the chain.*/ /* Save current number in the chain.*/
c[l] = i; chain[l] = i;
/* If we reached 1, the chain will never return go anywhere.*/ /* If we reached 1, the chain will never go anywhere.*/
if(i == 1) if(i == 1)
{ {
return -1; return -1;
@ -112,7 +87,7 @@ int sociable_chain(int i, int start, int *min, int l)
} }
else else
{ {
n = sum_proper_divisors(i); n = sum_of_divisors(i, 1);
divisors[i] = n; divisors[i] = n;
} }
@ -123,7 +98,7 @@ int sociable_chain(int i, int start, int *min, int l)
} }
/* If the next number in the chain is equal to the starting one, the chain is finished.*/ /* If the next number in the chain is equal to the starting one, the chain is finished.*/
if(n == start) if(n == chain[0])
{ {
return l + 1; return l + 1;
} }
@ -132,7 +107,7 @@ int sociable_chain(int i, int start, int *min, int l)
* chain is stuck in a loop that will not return to the starting number.*/ * chain is stuck in a loop that will not return to the starting number.*/
for(i = l; i > 0; i--) for(i = l; i > 0; i--)
{ {
if(n == c[i]) if(n == chain[i])
{ {
return -1; return -1;
} }
@ -145,5 +120,5 @@ int sociable_chain(int i, int start, int *min, int l)
*min = n; *min = n;
} }
return sociable_chain(n, start, min, l+1); return sociable_chain(n, chain, l+1, min);
} }

View File

@ -238,7 +238,7 @@ int find_max_path(int **triang, int n)
return triang[0][0]; return triang[0][0];
} }
int sum_of_divisors(int n) int sum_of_divisors(int n, int proper)
{ {
int i, sum = 1, limit; int i, sum = 1, limit;
@ -254,13 +254,20 @@ int sum_of_divisors(int n)
if(n % i == 0) if(n % i == 0)
{ {
sum += i; sum += i;
sum += n / i;
}
}
/* If n is a perfect square, i=limit is a divisor and /* If n is a perfect square, i=limit is a divisor and
* has to be counted only once.*/ * has to be counted only once.*/
if(n != i * i) if(n == limit * limit)
{ {
sum += (n / i); sum -= limit;
}
} }
if(!proper)
{
sum += n;
} }
return sum; return sum;

View File

@ -12,7 +12,7 @@ long int lcmm(long int *values, int n);
int *sieve(int n); int *sieve(int n);
int count_divisors(int n); int count_divisors(int n);
int find_max_path(int **triang, int n); int find_max_path(int **triang, int n);
int sum_of_divisors(int n); int sum_of_divisors(int n, int proper);
void swap(void **vet, int i, int j); void swap(void **vet, int i, int j);
void insertion_sort(void **array, int l, int r, int (*cmp)(void *lv, void *rv)); void insertion_sort(void **array, int l, int r, int (*cmp)(void *lv, void *rv));
void quick_sort(void **array, int l, int r, int (*cmp)(void *lv, void *rv)); void quick_sort(void **array, int l, int r, int (*cmp)(void *lv, void *rv));