Problem 401 solution - Sum of squares of divisors
More...
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
|
#define | __STDC_FORMAT_MACROS |
|
#define | MOD (uint64_t)1e9 |
|
#define | MAX_L 5000 |
|
|
char | is_in (uint64_t N, uint64_t *D, uint64_t L) |
|
uint64_t | get_divisors (uint64_t N, uint64_t *D) |
|
uint64_t | sigma2 (uint64_t N) |
|
uint64_t | sigma (uint64_t N) |
|
int | main (int argc, char **argv) |
|
Problem 401 solution - Sum of squares of divisors
- Author
- Krishna Vedala
◆ MAX_L
chunk size of array allocation
◆ MOD
#define MOD (uint64_t)1e9 |
◆ get_divisors()
uint64_t get_divisors |
( |
uint64_t |
N, |
|
|
uint64_t * |
D |
|
) |
| |
Get all integer divisors of a number
- Parameters
-
[in] | N | number to find divisors for |
[out] | D | array to store divisors in |
- Returns
- number of divisors found
56 for (i = 1; i * i <=
N + 1; i++)
64 if (!
is_in(i, D, num))
69 if (!
is_in(q, D, num))
77 D = (uint64_t *)realloc(D,
MAX_L *
sizeof(uint64_t) << 1);
◆ is_in()
char is_in |
( |
uint64_t |
N, |
|
|
uint64_t * |
D, |
|
|
uint64_t |
L |
|
) |
| |
Check if a number is present in given array
- Parameters
-
[in] | N | number to check |
[in] | D | array to check |
[in] | L | length of array |
- Returns
- 1 if present
-
0 if absent
31 for (i = 0; i <
L; i++)
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Main function
131 N = strtoll(argv[1], NULL, 10);
134 fprintf(stderr,
"Wrong number of input arguments!\n");
135 printf(
"Usage:\t ./sol1.c [N=1000]");
139 clock_t start_time = clock();
140 uint64_t result =
sigma(
N);
141 double dtime = clock() - start_time;
143 printf(
"N = %" PRIu64
"\nSum: %" PRIu64
"\n",
N, result);
144 printf(
"Time taken: %.4gms\n", dtime * 1e3 / CLOCKS_PER_SEC);
◆ sigma()
uint64_t sigma |
( |
uint64_t |
N | ) |
|
sum of squares of factors of numbers from 1 thru N
115 #pragma omp parallel for reduction(+ : sum)
117 for (i = 0; i <=
N; i++)
◆ sigma2()
uint64_t sigma2 |
( |
uint64_t |
N | ) |
|
compute sum of squares of all integer factors of a number
- Parameters
-
- Returns
- sum of squares
91 uint64_t *D = (uint64_t *)malloc(
MAX_L *
sizeof(uint64_t));
94 for (i = 1; i <
L; i++)
96 uint64_t DD = (D[i] * D[i]) %
MOD;