Problem 23 solution
More...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
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...
|
|
◆ get_next_abundant()
unsigned long get_next_abundant |
( |
unsigned long |
N | ) |
|
Find the next abundant number after N and not including N.
unsigned long is_abundant(unsigned long N)
Is the given number an abundant number (1) or not (0)
Definition: sol1.c:47
◆ 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.
21 unsigned long sum = 1;
24 for (
unsigned long i = 2; i * i <= N; i++)
29 unsigned long tmp = N / i;
37 ret = sum == N ? 0 : (sum > N ? 1 : -1);
◆ is_abundant()
unsigned long is_abundant |
( |
unsigned long |
N | ) |
|
Is the given number an abundant number (1) or not (0)
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
◆ 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
83 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: sol1.c:55
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Main function.
94 unsigned long MAX_N = 28123;
96 unsigned long sum = 0;
99 MAX_N = strtoul(argv[1], NULL, 10);
103 printf(
"Using OpenMP parallleization with %d threads\n",
104 omp_get_max_threads());
106 printf(
"Not using parallleization!\n");
109 double total_duration = 0.f;
112#pragma omp parallel for reduction(+ : sum) schedule(runtime)
114 for (i = 1; i <= MAX_N; i++)
116 clock_t start_time = clock();
121 clock_t end_time = clock();
122 total_duration += (double)(end_time - start_time) / CLOCKS_PER_SEC;
124 printf(
"... %5lu: %8lu\r", i, sum);
131 printf(
"Time taken: %.4g s\n", total_duration);
133 "Sum of numbers that cannot be represented as sum of two abundant "
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