Problem 23 solution - optimization using look-up array
More...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
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). More...
|
|
Problem 23 solution - optimization using look-up array
- Author
- Krishna Vedala
Optimization applied - compute & store abundant numbers once into a look-up array.
◆ get_next_abundant()
unsigned long get_next_abundant |
( |
unsigned long |
N | ) |
|
Find the next abundant number after N and not including N.
char is_abundant(unsigned long N)
Is the given number an abundant number (1) or not (0)
Definition: sol2.c:59
◆ 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
33 unsigned long sum = 1;
36 for (
unsigned long i = 2; i * i <= N; i++)
41 unsigned long tmp = N / i;
49 ret = sum == N ? 0 : (sum > N ? 1 : -1);
51 printf(
"%5lu: %5lu : %d\n", N, sum, ret);
◆ is_abundant()
char is_abundant |
( |
unsigned long |
N | ) |
|
Is the given number an abundant number (1) or not (0)
char * abundant_flags
This is the global array to be used to store a flag to identify if a particular number is abundant (1...
Definition: sol2.c:24
◆ 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
99 printf(
"\t%4lu + %4lu = %4lu\n", i, N - i, N);
unsigned long get_next_abundant(unsigned long N)
Find the next abundant number after N and not including N.
Definition: sol2.c:70
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Main function.
112 unsigned long sum = 0;
115 MAX_N = strtoul(argv[1], NULL, 10);
124 perror(
"Unable to allocate memoey!");
129 printf(
"Using OpenMP parallleization with %d threads\n",
130 omp_get_max_threads());
132 printf(
"Not using parallleization!\n");
135 clock_t start_time = clock();
140#pragma omp for schedule(runtime)
142 for (N = 1; N <= MAX_N; N++)
148 int byte_offset = N & 7, index = N >> 3;
158 clock_t end_time = clock();
159 double t1 = 1e3 * (end_time - start_time) / CLOCKS_PER_SEC;
160 printf(
"Time taken to get abundant numbers: %.4g ms\n", t1);
165#pragma omp parallel for schedule(runtime) reduction(+ : sum)
167 for (i = 1; i < MAX_N; i++)
169 clock_t start_time1 = clock();
177 clock_t end_time1 = clock();
181 t2 += end_time1 - start_time1;
183 printf(
"... %5lu: %8lu\r", i, sum);
193 double t22 = 1e3 * t2 / CLOCKS_PER_SEC;
194 printf(
"Time taken for final sum: %.4g ms\nTotal Time taken: %.4g ms\n",
196 printf(
"Memory used: %lu bytes\n", MAX_N >> 3);
198 "Sum of numbers that cannot be represented as sum of two abundant "
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition: malloc_dbg.h:22
char get_perfect_number(unsigned long N)
Definition: sol2.c:31
char is_sum_of_abundant(unsigned long N)
check if a given number can be represented as a sum of two abundant numbers.
Definition: sol2.c:87
◆ abundant_flags
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