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)
 Returns: -1 if N is deficient 1 if N is abundant 0 if N is perfect.
 
unsigned long is_abundant (unsigned long N)
 Is the given number an abundant number (1) or not (0)
 
unsigned long get_next_abundant (unsigned long N)
 Find the next abundant number after N and not including N.
 
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.
 

Variables

unsigned long MAX_N = 28123
 upper limit of numbers to check
 

Detailed Description

Problem 23 solution

Author
Krishna Vedala

Function Documentation

◆ 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
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:
get_next_abundant
unsigned long get_next_abundant(unsigned long N)
Find the next abundant number after N and not including N.
Definition: sol1.c:55
N
#define N
number of digits of the large number
Definition: sol1.c:109
is_abundant
unsigned long is_abundant(unsigned long N)
Is the given number an abundant number (1) or not (0)
Definition: sol1.c:47