Algorithms_in_C
1.0.0
Set of algorithms implemented in C.
|
Problem 23 solution - optimization using look-up array More...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Functions | |
char | get_perfect_number (unsigned long N) |
char | 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 | |
long | MAX_N = 28123 |
char * | abundant_flags = NULL |
Problem 23 solution - optimization using look-up array
Optimization applied - compute & store abundant numbers once into a look-up array.
unsigned long get_next_abundant | ( | unsigned long | N | ) |
Find the next abundant number after N and not including N
char get_perfect_number | ( | unsigned long | N | ) |
char is_abundant | ( | unsigned long | N | ) |
Is the given number an abundant number (1) or not (0)
char is_sum_of_abundant | ( | unsigned long | N | ) |
check if a given number can be represented as a sum of two abundant numbers.
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
int main | ( | int | argc, |
char ** | argv | ||
) |
Main function
byte array to store flags to identify abundant numbers the flags are identified by bits
char* abundant_flags = NULL |
This is the global array to be used to store a flag to identify if a particular number is abundant (1) or not (0). Using a whole byte to store a binary info would be redundant. We will use each byte to represent 8 numbers by relying on bits. This saves memory required by 1/8
long MAX_N = 28123 |
Limit of numbers to check