Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. /*slower method to find a rectange from a surface*/
  2. function findAllRectanglesBrutForce(area) {
  3. // let area = 558;
  4. let a = Math.sqrt(area), maxY = Math.ceil(area / 2) + 1;
  5. // if area= 18, a= 4.242, but 18=6*3
  6. let x = Math.floor(a); let y = Math.ceil(a);
  7. // this a match to return if no match was found
  8. let bismatched = { x: x, y: y, calc: x * y };
  9. const result = {
  10. matched: null,
  11. allMatched: null,
  12. matchedBis: bismatched
  13. };
  14. // check if already good
  15. if (x = + a && y == a) {
  16. result.matched = matched;
  17. }
  18. let matched = null;
  19. let allMatched = [];
  20. let allMatched_X = [];
  21. let allMatched_Y = [];
  22. let xi; let yi;
  23. xi = x; yi = y;
  24.  
  25. console.time('findAllRectanglesBrutForce_' + area);
  26. if (result.matched == null)
  27. for (let xi = 2; xi < area; xi++) {
  28. for (let yi = area; yi > 1; yi--) {
  29. let calc = xi * yi;
  30. if (calc == area) {
  31. let bx = allMatched_X.indexOf(yi) == -1;
  32. let bY = allMatched_Y.indexOf(xi) == -1;
  33. if (bx && bY) {
  34. allMatched_X.push(xi);
  35. allMatched_Y.push(yi);
  36. matched = { x: xi, y: yi };
  37. allMatched.push(matched);
  38. }
  39. }
  40. }
  41. }
  42. console.timeEnd('findAllRectanglesBrutForce_' + area);
  43. /**
  44. * area 558 = 9.28076171875ms
  45. * algo0_558: 6.281005859375ms
  46. */
  47. result.matched = matched;
  48. result.allMatched = allMatched;
  49. result.matchedBis = bismatched;
  50. return result;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement