Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol1.c File Reference

Problem 23 solution More...

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

Functions

char get_perfect_number (unsigned long N)
 Returns: -1 if N is deficient 1 if N is abundant 0 if N is perfect. More...
 
unsigned long is_abundant (unsigned long N)
 Is the given number an abundant number (1) or not (0) More...
 
unsigned long get_next_abundant (unsigned long N)
 Find the next abundant number after N and not including N. More...
 
char is_sum_of_abundant (unsigned long N)
 check if a given number can be represented as a sum of two abundant numbers. More...
 
int main (int argc, char **argv)
 Main function. More...
 

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 ;
61 }
62 return i;
63}
unsigned long is_abundant(unsigned long N)
Is the given number an abundant number (1) or not (0)
Definition: sol1.c:47
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.

20{
21 unsigned long sum = 1;
22 char ret = 0;
23
24 for (unsigned long i = 2; i * i <= N; i++)
25 {
26 if (N % i == 0)
27 {
28 sum += i;
29 unsigned long tmp = N / i;
30 if (tmp != i)
31 {
32 sum += tmp;
33 }
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}
char get_perfect_number(unsigned long N)
Returns: -1 if N is deficient 1 if N is abundant 0 if N is perfect.
Definition: sol1.c:19
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
72{
73 /* optimized logic:
74 * i + j = N where both i and j should be abundant
75 * hence we can simply check for j = N - i as we loop through i
76 */
77 for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
78 i = get_next_abundant(i))
79 {
80 if (is_abundant(N - i))
81 {
82#ifdef DEBUG
83 printf("\t%4lu + %4lu = %4lu\n", i, N - i, N);
84#endif
85 return 1;
86 }
87 }
88 return 0;
89}
unsigned long get_next_abundant(unsigned long N)
Find the next abundant number after N and not including N.
Definition: sol1.c:55
Here is the call graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Main function.

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