Advertisement
Recrux

Navigating the pythagorean-triple tree, an alogrithm

Dec 3rd, 2022
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | Science | 0 0
  1. Pythagorean triple algorithm:
  2.  
  3. For given x^2 + y^2 = z^2 calculate the path to take along the pythagorean-triple-tree to get there.
  4.  
  5. (0) Reduce triple to primitive triple by dividing by their greatest common divisor gcd(x, y, z)
  6. x = x / gcd(x, y, z)
  7. y = y / gcd(x, y, z)
  8. z = z / gcd(x, y, z)
  9.  
  10. (1) let a, b:
  11. b = sqrt((z - x) / 2), a = sqrt((z + x) / 2) - b
  12.  
  13. if a is even, either of them is 0 or either of them is not an integer repeat the calculation with y instead of x!
  14.  
  15. (2) Calculate the Category of a and b:
  16. C: N^2\{1,1} -> N
  17.  
  18. C(a,b) = 2 - floor(2 / ceil(2 * b / a)) for a != 1 or b != 1
  19.  
  20. if C(a, b) is undefined, jump to step (6)
  21.  
  22. (3) Save C(a, b) in memory
  23.  
  24. (4) Calculate a' and b'
  25. let c = C(a, b)
  26.  
  27. { a-2b for c = 0
  28. a' = { 2b-a for c = 1
  29. { a for c = 2
  30.  
  31. { b for c = 0
  32. b' = { a-b for c = 1
  33. { b-a for c = 2
  34.  
  35. (5) Repeat from (2) with a = a' and b = b' as long as a != b and a * b != 0
  36.  
  37. (6) Read the saved C(a, b) from (3) in reverse order
  38. 0 corresponds to going left,
  39. 1 corresponds to going straigt,
  40. 2 corresponds to going right!
  41.  
  42. DONE!
  43.  
  44.  
  45. Algorithm implemented in C:
  46.  
  47. #include <stdio.h>
  48. #include <math.h>
  49.  
  50. int C(double a_, double b_) {
  51. double a = a_;
  52. double b = b_;
  53. int result = (int) (2 - floor(2.0 / ceil(2.0 * b / a)));
  54. return result;
  55. }
  56.  
  57. int lcm(int a, int b) {
  58. int temp = a;
  59. while (1) {
  60. if (temp % b == 0 && temp % a == 0) {
  61. break;
  62. }
  63. temp++;
  64. }
  65. return temp;
  66. }
  67.  
  68. int gcd(int a, int b) {
  69. return a * b / lcm(a, b);
  70. }
  71.  
  72. int gcd_3(int a, int b, int c) {
  73. return gcd(a, gcd(b, c));
  74. }
  75.  
  76. int main() {
  77. printf("Fibonacci-Tree path calculator for perfect pythagorean triple!\nPlease enter such that x^2+y^2=z^2!\n");
  78. int x, y, z;
  79.  
  80. printf("\n Enter x:");
  81. scanf("%d", &x);
  82. printf("\n Enter y:");
  83. scanf("%d", &y);
  84. printf("\n Enter z:");
  85. scanf("%d", &z);
  86.  
  87. if (x * x + y * y != z * z) {
  88. printf("%d^2 + %d^2 = %d^2 is not true!", x, y, z);
  89. return 0;
  90. }
  91.  
  92. int multiple = gcd_3(x, y, z);
  93. if (multiple != 1) {
  94. printf("Reducing triple to primitive triple! Has common factor %d\n", multiple);
  95. x /= multiple;
  96. y /= multiple;
  97. z /= multiple;
  98. printf("new x, y, z: %d, %d, %d\n\n", x, y, z);
  99. }
  100.  
  101. b = (int) (sqrt((z - x) / 2));
  102. a = (int) (sqrt((z + x) / 2) - b);
  103.  
  104. if (a % 2 == 0 || a*b == 0) {
  105. x = y;
  106. b = (int) (sqrt((z - x) / 2));
  107. a = (int) (sqrt((z + x) / 2) - b);
  108. printf("Switched x^2 and y^2 around!\n");
  109. }
  110.  
  111. int iteration = 0;
  112. int nums[100];
  113.  
  114. while (a != b && a * b != 0) {
  115. printf("a: %d \t b: %d\n", a, b);
  116.  
  117. int c = C(a, b);
  118. int a_ = a;
  119. int b_ = b;
  120.  
  121. printf("c: %d \n\n", c);
  122. if (c == 0) {
  123. a_ = a - 2 * b;
  124. b_ = b;
  125. } else if (c == 1) {
  126. a_ = 2 * b - a;
  127. b_ = a - b;
  128. } else if (c == 2) {
  129. a_ = a;
  130. b_ = b - a;
  131. } else {
  132. printf("c > 3 ????\n");
  133. break;
  134. }
  135. a = a_;
  136. b = b_;
  137.  
  138. nums[iteration++] = c;
  139. }
  140.  
  141. for ( int i = iteration - 1; i >= 0; i--) {
  142. printf("%c ", nums[i] == 0 ? 'l' : (nums[i] == 1 ? 'm' : 'r'));
  143. }
  144.  
  145. return 0;
  146. }
  147.  
  148. For Input x=25, y=312, z=313 the program outputs: r r r r r r r r r r r r
  149. For Input x=275, y=252, z=373 the program outputs: l r m
  150. For Input x=39, y=80, z=89 the program outputs: m r
  151.  
  152. --Jakob Lenke, 03.12.2022
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement