fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* see http://e...content-available-to-author-only...a.org/wiki/Permutation#Generation_in_lexicographic_order */
  5. int permnext(char *p, int n) {
  6. int kh, k = -1;
  7. int lh, l = -1;
  8. char tmp;
  9.  
  10. for (kh = 0; kh < n - 1; kh++) {
  11. if (p[kh] < p[kh + 1]) k = kh;
  12. }
  13. if (k == -1) return 1;
  14. for (lh = k + 1; lh < n; lh++) {
  15. if (p[k] < p[lh]) l = lh;
  16. }
  17. if (l == -1) {
  18. fprintf(stderr, "This didn't happen.\n");
  19. exit(EXIT_FAILURE);
  20. }
  21. tmp = p[k]; p[k] = p[l]; p[l] = tmp;
  22. l = k + 1;
  23. lh = n - 1;
  24. while (l < lh) {
  25. tmp = p[l]; p[l] = p[lh]; p[lh] = tmp;
  26. l++;
  27. lh--;
  28. }
  29. return 0;
  30. }
  31.  
  32. void check(const char *p, int nda, int ndb, int ndc, int ndd) {
  33. int a = 0, b = 0, c = 0, d = 0;
  34. if (p[nda] == '0') return;
  35. if (p[nda + ndb] == '0') return;
  36. if (p[nda + ndb + ndc] == '0') return;
  37. while (nda--) { a *= 10; a += *p - '0'; p++; }
  38. while (ndb--) { b *= 10; b += *p - '0'; p++; }
  39. if (b < a) return;
  40. while (ndc--) { c *= 10; c += *p - '0'; p++; }
  41. if (c < a) return;
  42. while (ndd--) { d *= 10; d += *p - '0'; p++; }
  43. if (d < c) return;
  44. if (a*d + b*c == b*d) {
  45. printf("%d/%d + %d/%d = 1\n", a, b, c, d);
  46. }
  47. }
  48.  
  49. int main(void) {
  50. char perm[] = "1023456789";
  51. do {
  52. check(perm, 1, 1, 1, 7);
  53. check(perm, 1, 1, 2, 6);
  54. check(perm, 1, 1, 3, 5);
  55. check(perm, 1, 1, 4, 4);
  56. check(perm, 1, 2, 1, 6);
  57. check(perm, 1, 2, 2, 5);
  58. check(perm, 1, 2, 3, 4);
  59. check(perm, 1, 3, 1, 5);
  60. check(perm, 1, 3, 2, 4);
  61. check(perm, 1, 3, 3, 3);
  62. check(perm, 1, 4, 1, 4);
  63. check(perm, 1, 4, 2, 3);
  64. check(perm, 1, 5, 1, 3);
  65. check(perm, 1, 5, 2, 2);
  66. check(perm, 1, 6, 1, 2);
  67. check(perm, 1, 7, 1, 1);
  68. check(perm, 2, 2, 2, 4);
  69. check(perm, 2, 2, 3, 3);
  70. check(perm, 2, 3, 2, 3);
  71. check(perm, 2, 4, 2, 2);
  72. } while (permnext(perm, 10) == 0);
  73. return 0;
  74. }
Success #stdin #stdout 3.69s 1832KB
stdin
Standard input is empty
stdout
10/28 + 369/574 = 1
10/45 + 287/369 = 1
10/45 + 728/936 = 1
10/96 + 473/528 = 1
1/2 + 3485/6970 = 1
1/2 + 3548/7096 = 1
1/2 + 3845/7690 = 1
1/2 + 4538/9076 = 1
1/2 + 4685/9370 = 1
1/2 + 4835/9670 = 1
1/2 + 4853/9706 = 1
1/2 + 4865/9730 = 1
12/54 + 609/783 = 1
12/60 + 748/935 = 1
12/96 + 357/408 = 1
12/96 + 735/840 = 1
13/26 + 485/970 = 1
13/52 + 678/904 = 1
1/4 + 7365/9820 = 1
15/30 + 486/972 = 1
16/32 + 485/970 = 1
1/6 + 7835/9402 = 1
1/7 + 4362/5089 = 1
17/89 + 504/623 = 1
18/90 + 276/345 = 1
18/90 + 372/465 = 1
19/57 + 308/462 = 1
19/58 + 273/406 = 1
21/96 + 375/480 = 1
2/4 + 3079/6158 = 1
24/63 + 507/819 = 1
24/96 + 531/708 = 1
2/6 + 3190/4785 = 1
27/54 + 309/618 = 1
2/7 + 5940/8316 = 1
2/7 + 6810/9534 = 1
27/81 + 306/459 = 1
27/81 + 630/945 = 1
2/9 + 5803/7461 = 1
29/58 + 307/614 = 1
29/87 + 310/465 = 1
3/127 + 496/508 = 1
31/62 + 485/970 = 1
32/48 + 169/507 = 1
32/80 + 417/695 = 1
34/51 + 269/807 = 1
34/578 + 96/102 = 1
35/70 + 148/296 = 1
35/70 + 481/962 = 1
3/6 + 1485/2970 = 1
3/6 + 2079/4158 = 1
3/6 + 2709/5418 = 1
3/6 + 2907/5814 = 1
3/6 + 4851/9702 = 1
36/81 + 405/729 = 1
36/81 + 540/972 = 1
38/61 + 207/549 = 1
38/76 + 145/290 = 1
38/76 + 451/902 = 1
38/95 + 426/710 = 1
39/51 + 204/867 = 1
39/65 + 284/710 = 1
42/87 + 315/609 = 1
4/356 + 792/801 = 1
4/5 + 1278/6390 = 1
4/5 + 1872/9360 = 1
45/61 + 208/793 = 1
45/90 + 138/276 = 1
45/90 + 186/372 = 1
45/90 + 381/762 = 1
46/92 + 185/370 = 1
48/96 + 135/270 = 1
48/96 + 351/702 = 1
5/104 + 693/728 = 1
54/87 + 231/609 = 1
56/428 + 93/107 = 1
56/832 + 97/104 = 1
56/84 + 109/327 = 1
56/84 + 307/921 = 1
57/204 + 98/136 = 1
57/92 + 140/368 = 1
59/236 + 78/104 = 1
60/1245 + 79/83 = 1
6/324 + 795/810 = 1
63/728 + 95/104 = 1
6/534 + 792/801 = 1
70/96 + 143/528 = 1
74/89 + 105/623 = 1
7/54 + 893/1026 = 1
7/9 + 1208/5436 = 1
7/9 + 1352/6084 = 1
8/10 + 729/3645 = 1
8/10 + 927/4635 = 1
8/512 + 693/704 = 1
87/435 + 96/120 = 1
9/12 + 876/3504 = 1
9/351 + 684/702 = 1