Problem 26 solution
More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
|
#define | MAX_DENO 2000 |
| limit of unit fractions
|
|
#define | MAX_LEN (MAX_DENO + 10) |
| length of resulting recurring fraction number
|
|
|
int | compare (const void *a, const void *b) |
| comparison function for use with internal qsort algorithm More...
|
|
int | main (int argc, char *argv[]) |
| Main function. More...
|
|
◆ compare()
int compare |
( |
const void * |
a, |
|
|
const void * |
b |
|
) |
| |
comparison function for use with internal qsort
algorithm
21 return (*(
unsigned short *)a - *(
unsigned short *)b);
◆ main()
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Main function.
27 unsigned short max_digits = 0, max_idx_number = 0;
29 clock_t start_time = clock();
34 for (deno = 2; deno <
MAX_DENO; deno++)
36 unsigned short remainders[
MAX_LEN];
37 unsigned short rem = 1, *rem_ptr = remainders;
38 memset(remainders, (
unsigned short)-1,
39 MAX_LEN *
sizeof(
unsigned short));
42 unsigned short index = 0, num_digits;
46 rem = (rem * 10) % deno;
52 rem_ptr = (
unsigned short *)bsearch(
58 remainders[index] = rem;
63 num_digits = index - (rem_ptr - remainders);
70 if (num_digits > max_digits)
72 max_digits = num_digits;
73 max_idx_number = deno;
80 clock_t end_time = clock();
82 printf(
"Time taken: %.4g ms\n",
83 1e3 * (
double)(end_time - start_time) / CLOCKS_PER_SEC);
84 printf(
"Maximum digits: %hu\t Denominator: %hu\n", max_digits,
#define MAX_DENO
limit of unit fractions
Definition: sol1.c:14
#define MAX_LEN
length of resulting recurring fraction number
Definition: sol1.c:15
int compare(const void *a, const void *b)
comparison function for use with internal qsort algorithm
Definition: sol1.c:19