19{
20 return (*(unsigned short *)a - *(unsigned short *)b);
21}
22
23
24int main(
int argc,
char *argv[])
25{
26 unsigned short max_digits = 0, max_idx_number = 0;
27
28 clock_t start_time = clock();
29 short deno;
30#ifdef _OPENMP
31#pragma omp for
32#endif
33 for (deno = 2; deno <
MAX_DENO; deno++)
34 {
35 unsigned short remainders[
MAX_LEN];
36 unsigned short rem = 1, *rem_ptr = remainders;
37 memset(remainders, (unsigned short)-1,
38 MAX_LEN *
sizeof(
unsigned short));
39
40
41 unsigned short index = 0, num_digits;
42
43 while (rem != 0)
44 {
45 rem = (rem * 10) % deno;
46 if (rem == 0)
47 {
48 index = 0;
49 break;
50 }
51 rem_ptr = (unsigned short *)bsearch(
53
54
55 if (rem_ptr != NULL)
56 break;
57 remainders[index] = rem;
58 rem_ptr = remainders;
59 index++;
60 }
61
62 num_digits = index - (rem_ptr - remainders);
63
64
65#ifdef _OPENMP
66#pragma omp critical
67 {
68#endif
69 if (num_digits > max_digits)
70 {
71 max_digits = num_digits;
72 max_idx_number = deno;
73
74 }
75#ifdef _OPENMP
76 }
77#endif
78 }
79 clock_t end_time = clock();
80
81 printf("Time taken: %.4g ms\n",
82 1e3 * (double)(end_time - start_time) / CLOCKS_PER_SEC);
83 printf("Maximum digits: %hu\t Denominator: %hu\n", max_digits,
84 max_idx_number);
85
86 return 0;
87}
int main()
Main function.
Definition sol1.c:12
#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