Main function.
26{
27 unsigned short max_digits = 0, max_idx_number = 0;
28
29 clock_t start_time = clock();
30 short deno;
31#ifdef _OPENMP
32#pragma omp for
33#endif
34 for (deno = 2; deno <
MAX_DENO; deno++)
35 {
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));
40
41
42 unsigned short index = 0, num_digits;
43
44 while (rem != 0)
45 {
46 rem = (rem * 10) % deno;
47 if (rem == 0)
48 {
49 index = 0;
50 break;
51 }
52 rem_ptr = (unsigned short *)bsearch(
54
55
56 if (rem_ptr != NULL)
57 break;
58 remainders[index] = rem;
59 rem_ptr = remainders;
60 index++;
61 }
62
63 num_digits = index - (rem_ptr - remainders);
64
65
66#ifdef _OPENMP
67#pragma omp critical
68 {
69#endif
70 if (num_digits > max_digits)
71 {
72 max_digits = num_digits;
73 max_idx_number = deno;
74
75 }
76#ifdef _OPENMP
77 }
78#endif
79 }
80 clock_t end_time = clock();
81
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,
85 max_idx_number);
86
87 return 0;
88}
#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