2020-06-05 19:20:25 +03:00
|
|
|
/**
|
|
|
|
* \file
|
2020-06-06 21:51:49 +03:00
|
|
|
* \brief [Problem 401](https://projecteuler.net/problem=401) solution -
|
2020-06-05 19:20:25 +03:00
|
|
|
* Sum of squares of divisors
|
2020-06-06 21:51:49 +03:00
|
|
|
* \author [Krishna Vedala](https://github.com/kvedala)
|
2020-06-05 19:20:25 +03:00
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdint.h>
|
2020-04-23 23:31:59 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <time.h>
|
2020-04-24 14:41:24 +03:00
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#include <inttypes.h>
|
2020-04-23 23:31:59 +03:00
|
|
|
#ifdef _OPENMP
|
|
|
|
#include <omp.h>
|
|
|
|
#endif
|
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
#define MOD (uint64_t)1e9 /**< modulo limit */
|
|
|
|
#define MAX_L 5000 /**< chunk size of array allocation */
|
2020-04-23 23:31:59 +03:00
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
/**
|
|
|
|
* Check if a number is present in given array
|
|
|
|
* \param[in] N number to check
|
|
|
|
* \param[in] D array to check
|
|
|
|
* \param[in] L length of array
|
|
|
|
* \returns 1 if present
|
|
|
|
* \returns 0 if absent
|
|
|
|
*/
|
2020-04-23 23:31:59 +03:00
|
|
|
char is_in(uint64_t N, uint64_t *D, uint64_t L)
|
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
for (i = 0; i < L; i++)
|
|
|
|
if (D[i] == N)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
/**
|
|
|
|
* Get all integer divisors of a number
|
|
|
|
* \param[in] N number to find divisors for
|
|
|
|
* \param[out] D array to store divisors in
|
|
|
|
* \returns number of divisors found
|
|
|
|
*/
|
2020-04-23 23:31:59 +03:00
|
|
|
uint64_t get_divisors(uint64_t N, uint64_t *D)
|
|
|
|
{
|
|
|
|
uint64_t q, r;
|
|
|
|
int64_t i, num = 0;
|
|
|
|
|
|
|
|
if (N == 1)
|
|
|
|
{
|
|
|
|
D[0] = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
// search till sqrt(N)
|
|
|
|
// because after this, the pair of divisors will repeat themselves
|
2020-04-23 23:31:59 +03:00
|
|
|
for (i = 1; i * i <= N + 1; i++)
|
|
|
|
{
|
2020-06-05 19:20:25 +03:00
|
|
|
r = N % i; // get reminder
|
2020-04-23 23:31:59 +03:00
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
// reminder = 0 if 'i' is a divisor of 'N'
|
2020-04-23 23:31:59 +03:00
|
|
|
if (r == 0)
|
|
|
|
{
|
|
|
|
q = N / i;
|
2020-06-05 19:20:25 +03:00
|
|
|
if (!is_in(i, D, num)) // if divisor was already stored
|
2020-04-23 23:31:59 +03:00
|
|
|
{
|
|
|
|
D[num] = i;
|
|
|
|
num++;
|
|
|
|
}
|
2020-06-05 19:20:25 +03:00
|
|
|
if (!is_in(q, D, num)) // if divisor was already stored
|
2020-04-23 23:31:59 +03:00
|
|
|
{
|
|
|
|
D[num] = q;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 19:20:25 +03:00
|
|
|
|
|
|
|
if (num == MAX_L) // limit of array reached, allocate more space
|
2020-04-23 23:31:59 +03:00
|
|
|
D = (uint64_t *)realloc(D, MAX_L * sizeof(uint64_t) << 1);
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-05 19:20:25 +03:00
|
|
|
* compute sum of squares of all integer factors of a number
|
|
|
|
* \param[in] N
|
|
|
|
* \returns sum of squares
|
|
|
|
*/
|
2020-04-23 23:31:59 +03:00
|
|
|
uint64_t sigma2(uint64_t N)
|
|
|
|
{
|
2020-06-05 19:20:25 +03:00
|
|
|
uint64_t sum = 0, L;
|
2020-04-23 23:31:59 +03:00
|
|
|
int64_t i;
|
|
|
|
uint64_t *D = (uint64_t *)malloc(MAX_L * sizeof(uint64_t));
|
|
|
|
|
|
|
|
L = get_divisors(N, D);
|
|
|
|
for (i = 1; i < L; i++)
|
|
|
|
{
|
2020-06-05 19:20:25 +03:00
|
|
|
uint64_t DD = (D[i] * D[i]) % MOD;
|
2020-04-23 23:31:59 +03:00
|
|
|
sum += DD;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(D);
|
|
|
|
return sum % MOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sum of squares of factors of numbers
|
|
|
|
* from 1 thru N
|
2020-06-05 19:20:25 +03:00
|
|
|
*/
|
2020-04-23 23:31:59 +03:00
|
|
|
uint64_t sigma(uint64_t N)
|
|
|
|
{
|
2020-04-24 03:53:53 +03:00
|
|
|
uint64_t s, sum = 0;
|
|
|
|
int64_t i;
|
2020-04-23 23:31:59 +03:00
|
|
|
|
|
|
|
#ifdef _OPENMP
|
2020-06-05 19:20:25 +03:00
|
|
|
// parallelize on threads
|
2020-05-29 23:23:24 +03:00
|
|
|
#pragma omp parallel for reduction(+ : sum)
|
2020-04-23 23:31:59 +03:00
|
|
|
#endif
|
|
|
|
for (i = 0; i <= N; i++)
|
|
|
|
{
|
|
|
|
s = sigma2(i);
|
|
|
|
sum += s;
|
|
|
|
}
|
|
|
|
return sum % MOD;
|
|
|
|
}
|
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
/** Main function */
|
2020-04-23 23:31:59 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
uint64_t N = 1000;
|
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
N = strtoll(argv[1], NULL, 10);
|
2020-06-05 19:20:25 +03:00
|
|
|
else if (N > 2)
|
2020-04-23 23:31:59 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Wrong number of input arguments!\n");
|
2020-06-05 19:20:25 +03:00
|
|
|
printf("Usage:\t ./sol1.c [N=1000]");
|
2020-04-23 23:31:59 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_t start_time = clock();
|
|
|
|
uint64_t result = sigma(N);
|
|
|
|
double dtime = clock() - start_time;
|
|
|
|
|
2020-04-24 14:41:24 +03:00
|
|
|
printf("N = %" PRIu64 "\nSum: %" PRIu64 "\n", N, result);
|
2020-04-23 23:31:59 +03:00
|
|
|
printf("Time taken: %.4gms\n", dtime * 1e3 / CLOCKS_PER_SEC);
|
|
|
|
|
|
|
|
return 0;
|
2020-06-06 21:51:49 +03:00
|
|
|
}
|