Advertisement
Sorceress

my algorithm

Sep 3rd, 2022
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. _DEFINE A-Z AS _UNSIGNED _INTEGER64
  2.  
  3. N = 13537 * 21019 'Semiprime to factorise
  4.  
  5. prs = 16 'prime parity bits in power vector
  6.  
  7. 'generate small primes
  8. DIM pr(255)
  9. i = 0: a = 2
  10. WHILE i < prs: ok = 1
  11. FOR j = 1 TO i
  12. IF a MOD pr(j) = 0 THEN ok = 0
  13. NEXT
  14. IF ok = 1 THEN i = i + 1: pr(i) = a
  15. a = a + 1
  16. WEND
  17.  
  18. 'look for squares composed solely of these prs primes
  19. DIM hi(65535), hs(65535)
  20. i0 = INT(SQR(N) + 1): s = i0 * i0 - N: a = 2 * i0 + 1
  21. PRINT i0; "->"; N, i0; "^ 2 ="; s: SLEEP
  22. i = i0: WHILE i < N
  23. s0 = s
  24. pv = 0: p = 1 'set bits of pv equal to the parity of prime occurance
  25. FOR j = 1 TO prs
  26. WHILE s0 MOD pr(j) = 0: s0 = s0 \ pr(j): pv = pv XOR p: WEND
  27. p = p * 2
  28. NEXT
  29.  
  30. IF s0 = 1 THEN 's is prs-smooth, pv is it's power vector
  31. PRINT i; "^ 2 ="; s; TAB(50); bin$(pv, prs)
  32. IF pv = 0 THEN 'direct root
  33. im = i: sm = SQR(s): GOSUB roots
  34. END IF
  35. 'NB: looking for matching pv only is naive.
  36. 'the great power of this method is realised when we xor elements of our
  37. 'table together to find matching combos. ie, where xor(combo)=0
  38. IF hi(pv) > 0 THEN 'matching root
  39. im = (i * hi(pv)) MOD N: sm = SQR(s * hs(pv)): GOSUB roots
  40. ELSE
  41. hi(pv) = i: hs(pv) = s
  42. END IF
  43. END IF
  44. s = s + a: IF s >= N THEN s = s - N
  45. a = a + 2: IF a >= N THEN a = a - N
  46. i = i + 1
  47. WEND
  48. END
  49.  
  50. roots:
  51. PRINT "after"; i - i0; "iterations:";
  52. s1 = im + sm: GOSUB gcd: PRINT s2;
  53. s1 = im - sm: GOSUB gcd: PRINT "*"; s2; TAB(50); im; "+/-"; sm
  54. IF s2 > 1 AND s2 < N THEN END
  55. RETURN
  56.  
  57. gcd:
  58. s2 = N
  59. WHILE s1 > 0
  60. s0 = s1: s1 = s2 - (s2 \ s1) * s1: s2 = s0
  61. WEND
  62. RETURN
  63.  
  64. FUNCTION bin$ (pv, bits)
  65. x = pv: bin$ = ""
  66. FOR k = 1 TO bits
  67. IF x MOD 2 = 0 THEN ch$ = "0" ELSE ch$ = "1"
  68. bin$ = ch$ + bin$: x = x \ 2
  69. NEXT
  70. END FUNCTION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement