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_LIMIT (uint64_t)1e9 |
| modulo limit
|
|
#define | MAX_LENGTH 5000 |
| chunk size of array allocation
|
|
|
char | is_in (uint64_t N, uint64_t *D, uint64_t L) |
| Check if a number is present in given array. More...
|
|
uint64_t | get_divisors (uint64_t N, uint64_t *D) |
| Get all integer divisors of a number. More...
|
|
uint64_t | sigma2 (uint64_t N) |
| compute sum of squares of all integer factors of a number More...
|
|
uint64_t | sigma (uint64_t N) |
| sum of squares of factors of numbers from 1 thru N
|
|
int | main (int argc, char **argv) |
| Main function.
|
|
Problem 401 solution - Sum of squares of divisors
- Author
- Krishna Vedala
◆ 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
60 for (i = 1; i * i <= N + 1; i++)
68 if (!
is_in(i, D, num))
73 if (!
is_in(q, D, num))
82 D = (uint64_t *)realloc(D,
MAX_LENGTH *
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++)
◆ sigma2()
uint64_t sigma2 |
( |
uint64_t |
N | ) |
|
compute sum of squares of all integer factors of a number
- Parameters
-
- Returns
- sum of squares
97 uint64_t *D = (uint64_t *)malloc(
MAX_LENGTH *
sizeof(uint64_t));
100 for (i = 1; i <
L; i++)