mfactor.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // For odd n, returns the largest factor less than or equal to sqrt(n)
  2. #include "stdafx.h"
  3. #include "defs.h"
  4. #if 0 // not used anymore
  5. unsigned int *
  6. mfactor(unsigned int *n)
  7. {
  8. unsigned int *r, *root, *t, *two, *x, *y;
  9. two = mint(2);
  10. root = msqrt(n);
  11. // y = 1;
  12. y = mint(1);
  13. // x = 2 isqrt(n) + 1
  14. t = madd(root, root);
  15. x = madd(t, y);
  16. mfree(t);
  17. // r = isqrt(n) ^ 2 - n
  18. t = mmul(root, root);
  19. r = msub(t, n);
  20. mfree(t);
  21. mfree(root);
  22. while (1) {
  23. if (MZERO(r)) {
  24. // n = (x - y) / 2
  25. t = msub(x, y);
  26. n = mdiv(t, two);
  27. mfree(t);
  28. mfree(r);
  29. mfree(x);
  30. mfree(y);
  31. mfree(two);
  32. return n;
  33. }
  34. // r = r + x
  35. t = madd(r, x);
  36. mfree(r);
  37. r = t;
  38. // x = x + 2
  39. t = madd(x, two);
  40. mfree(x);
  41. x = t;
  42. while (1) {
  43. // r = r - y
  44. t = msub(r, y);
  45. mfree(r);
  46. r = t;
  47. // y = y + 2
  48. t = madd(y, two);
  49. mfree(y);
  50. y = t;
  51. if (MSIGN(r) == -1 || MZERO(r))
  52. break;
  53. }
  54. }
  55. }
  56. void
  57. test_mfactor(void)
  58. {
  59. unsigned int *n;
  60. n = mint(377);
  61. n = mfactor(n);
  62. printf("%d", n[0]);
  63. }
  64. #endif