Advertisement
Guest User

Pythagorean Triples Code

a guest
Sep 2nd, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. function Point(a,b,c){
  2. 'use strict';
  3.  
  4. if (! (this instanceof Point)) return new Point(x,y);
  5. Object.defineProperties(this, {
  6. a : {
  7. value: a || 0,
  8. writable: true,
  9. configurable: false,
  10. enumerable: true
  11. },
  12. b : {
  13. value: b || 0,
  14. writable: true,
  15. configurable: false,
  16. enumerable: true
  17. },
  18. c : {
  19. value: c || 0,
  20. writable: true,
  21. configurable: false,
  22. enumerable: true
  23. }
  24. });
  25. }
  26.  
  27. function findPythagoreanTriples(max) {
  28. 'use strict';
  29.  
  30. var results = [],
  31. a = 0,
  32. b = 0,
  33. c = 0,
  34. limit = Math.ceil(Math.sqrt(max)/2);
  35.  
  36. for (var m = 0; m <= limit; m++) {
  37. for (var n = m; 2*n*(m+n) < max; n++) {
  38. if (m && n && m !== n) {
  39. a = n*n-m*m;
  40. b = 2*m*n;
  41. c = m*m+n*n;
  42. if (isPythagoreanTriple(a,b,c)) {
  43. results.push(new Point(a, b, c))
  44. }
  45. }
  46. }
  47. }
  48.  
  49. return results;
  50. }
  51.  
  52. function isPythagoreanTriple(a,b,c) {
  53. 'use strict';
  54. return c*c === a*a + b*b;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement