Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
sol1.c File Reference

Problem 401 solution - Sum of squares of divisors More...

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
Include dependency graph for sol1.c:

Macros

#define __STDC_FORMAT_MACROS
 
#define MOD_LIMIT   (uint64_t)1e9
 modulo limit
 
#define MAX_LENGTH   5000
 chunk size of array allocation
 

Functions

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.
 

Detailed Description

Problem 401 solution - Sum of squares of divisors

Author
Krishna Vedala

Function Documentation

◆ get_divisors()

uint64_t get_divisors ( uint64_t  N,
uint64_t *  D 
)

Get all integer divisors of a number.

Parameters
[in]Nnumber to find divisors for
[out]Darray to store divisors in
Returns
number of divisors found
48 {
49  uint64_t q, r;
50  int64_t i, num = 0;
51 
52  if (N == 1)
53  {
54  D[0] = 1;
55  return 1;
56  }
57 
58  // search till sqrt(N)
59  // because after this, the pair of divisors will repeat themselves
60  for (i = 1; i * i <= N + 1; i++)
61  {
62  r = N % i; // get reminder
63 
64  // reminder = 0 if 'i' is a divisor of 'N'
65  if (r == 0)
66  {
67  q = N / i;
68  if (!is_in(i, D, num)) // if divisor was already stored
69  {
70  D[num] = i;
71  num++;
72  }
73  if (!is_in(q, D, num)) // if divisor was already stored
74  {
75  D[num] = q;
76  num++;
77  }
78  }
79 
80  if (num == MAX_LENGTH)
81  { // limit of array reached, allocate more space
82  D = (uint64_t *)realloc(D, MAX_LENGTH * sizeof(uint64_t) << 1);
83  }
84  }
85  return num;
86 }
Here is the call graph for this function:

◆ 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]Nnumber to check
[in]Darray to check
[in]Llength of array
Returns
1 if present
0 if absent
29 {
30  uint64_t i;
31  for (i = 0; i < L; i++)
32  {
33  if (D[i] == N)
34  {
35  return 1;
36  }
37  }
38  return 0;
39 }

◆ sigma2()

uint64_t sigma2 ( uint64_t  N)

compute sum of squares of all integer factors of a number

Parameters
[in]N
Returns
sum of squares
94 {
95  uint64_t sum = 0, L;
96  int64_t i;
97  uint64_t *D = (uint64_t *)malloc(MAX_LENGTH * sizeof(uint64_t));
98 
99  L = get_divisors(N, D);
100  for (i = 1; i < L; i++)
101  {
102  uint64_t DD = (D[i] * D[i]) % MOD_LIMIT;
103  sum += DD;
104  }
105 
106  free(D);
107  return sum % MOD_LIMIT;
108 }
Here is the call graph for this function:
L
Definition: list.h:8
get_divisors
uint64_t get_divisors(uint64_t N, uint64_t *D)
Get all integer divisors of a number.
Definition: sol1.c:47
MOD_LIMIT
#define MOD_LIMIT
modulo limit
Definition: sol1.c:17
is_in
char is_in(uint64_t N, uint64_t *D, uint64_t L)
Check if a number is present in given array.
Definition: sol1.c:28
MAX_LENGTH
#define MAX_LENGTH
chunk size of array allocation
Definition: sol1.c:18