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

Problem 23 solution More...

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

Functions

char get_perfect_number (unsigned long N)
 
unsigned long is_abundant (unsigned long N)
 
unsigned long get_next_abundant (unsigned long N)
 
char is_sum_of_abundant (unsigned long N)
 
int main (int argc, char **argv)
 

Variables

unsigned long MAX_N = 28123
 

Detailed Description

Problem 23 solution

Author
Krishna Vedala

Function Documentation

◆ get_next_abundant()

unsigned long get_next_abundant ( unsigned long  N)

Find the next abundant number after N and not including N

56 {
57  unsigned long i;
58  for (i = N + 1; !is_abundant(i); i++)
59  ;
60  return i;
61 }
Here is the call graph for this function:

◆ get_perfect_number()

char get_perfect_number ( unsigned long  N)

Returns: -1 if N is deficient 1 if N is abundant 0 if N is perfect

22 {
23  unsigned long sum = 1;
24  char ret = 0;
25 
26  for (unsigned long i = 2; i * i <= N; i++)
27  {
28  if (N % i == 0)
29  {
30  sum += i;
31  unsigned long tmp = N / i;
32  if (tmp != i)
33  sum += tmp;
34  }
35  }
36 
37  ret = sum == N ? 0 : (sum > N ? 1 : -1);
38  // #ifdef DEBUG
39  // printf("%5lu: %5lu : %d\n", N, sum, ret);
40  // #endif
41  return ret;
42 }

◆ is_abundant()

unsigned long is_abundant ( unsigned long  N)

Is the given number an abundant number (1) or not (0)

48 {
49  return get_perfect_number(N) == 1 ? 1 : 0;
50 }
Here is the call graph for this function:

◆ is_sum_of_abundant()

char is_sum_of_abundant ( unsigned long  N)

check if a given number can be represented as a sum of two abundant numbers.

Returns
1 - if yes
0 - if not

optimized logic: i + j = N where both i and j should be abundant hence we can simply check for j = N - i as we loop through i

70 {
71  /** optimized logic:
72  * i + j = N where both i and j should be abundant
73  * hence we can simply check for j = N - i as we loop through i
74  **/
75  for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
76  i = get_next_abundant(i))
77  if (is_abundant(N - i))
78  {
79 #ifdef DEBUG
80  printf("\t%4lu + %4lu = %4lu\n", i, N - i, N);
81 #endif
82  return 1;
83  }
84  return 0;
85 }
Here is the call graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Main function

89 {
90  unsigned long sum = 0;
91  if (argc == 2)
92  MAX_N = strtoul(argv[1], NULL, 10);
93 
94 #ifdef _OPENMP
95  printf("Using OpenMP parallleization with %d threads\n",
96  omp_get_max_threads());
97 #else
98  printf("Not using parallleization!\n");
99 #endif
100 
101  double total_duration = 0.f;
102  long i;
103 #ifdef _OPENMP
104 #pragma omp parallel for reduction(+ : sum) schedule(runtime)
105 #endif
106  for (i = 1; i <= MAX_N; i++)
107  {
108  clock_t start_time = clock();
109  if (!is_sum_of_abundant(i))
110  sum += i;
111  clock_t end_time = clock();
112  total_duration += (double)(end_time - start_time) / CLOCKS_PER_SEC;
113 
114  printf("... %5lu: %8lu\r", i, sum);
115  if (i % 100 == 0)
116  fflush(stdout);
117  }
118 
119  printf("Time taken: %.4g s\n", total_duration);
120  printf(
121  "Sum of numbers that cannot be represented as sum of two abundant "
122  "numbers : %lu\n",
123  sum);
124 
125  return 0;
126 }
Here is the call graph for this function:

Variable Documentation

◆ MAX_N

unsigned long MAX_N = 28123

upper limit of numbers to check

get_next_abundant
unsigned long get_next_abundant(unsigned long N)
Definition: sol1.c:55
N
#define N
Definition: sol1.c:109
is_abundant
unsigned long is_abundant(unsigned long N)
Definition: sol1.c:47
MAX_N
unsigned long MAX_N
Definition: sol1.c:13
get_perfect_number
char get_perfect_number(unsigned long N)
Definition: sol1.c:21
is_sum_of_abundant
char is_sum_of_abundant(unsigned long N)
Definition: sol1.c:69