0

This is what I got but it seems it's not working. When I test it, it just goes through and nothing gets returned.

Is there something I'm missing?

pollard[n_, B_] := 
Module[{a, g, i}, 
  i = 2;
  While[i < B,
    a = PowerMod[2, i!, n];
    g = GCD[(a - 1), n];
    If[g > 1 && g < n, Return[g]; Break[]]; 
    i = i + 1
  ]
]
Michael E2
  • 235,386
  • 17
  • 334
  • 747
r1z1a
  • 1
  • Are you sure you want a semicolon after the Return and not a comma? Also see the "possible issues" section on the Return doc page : Return exits only the innermost construct in which it is invoked. Note also that While only returns Null. – Sjoerd C. de Vries Jul 06 '15 at 14:59
  • Still don't get what you mean by Return exits only the innermost construct in which it is invoked. Kinda don't get why it can't return value of g and terminate whole loop, and yes if nothing is found that matches than Null would make sense. – r1z1a Jul 06 '15 at 15:01
  • Have you read the reference to the manual I pointed at? This answer may be helpful in this context. – Sjoerd C. de Vries Jul 06 '15 at 15:03
  • It seems that its actually working fine, but there was more or less an issue with some previous version i wrote that was still in running, and mathematica didn't abort the execution when updated and run again. – r1z1a Jul 06 '15 at 15:30
  • 1

1 Answers1

1

Frankly, I'd write this function differently, at least if B is small:

ClearAll[pollard];

Module[{p},
 p[n_, i_] := GCD[PowerMod[2, i!, n] - 1, n];
 pollard[n_, B_] := p[n, #] & /@ Select[Range[2, B], 1 < p[n, #] < n &, 1]]

I am not entirely sure what your function is intended to return, and how you define "most B iterations" (since your implementation conflicts with your own requirement). Anyway, above code is reformulated to select the smallest 2 <= i <= B which satisfies the Pollard condition, and to return the corresponding factor as a single-item list. If none is found in the range, an empty list is returned.

# -> pollard[#, 4] & /@ Range[100]

(* {1 -> {}, 2 -> {}, 3 -> {}, 4 -> {}, 5 -> {}, 6 -> {3}, 7 -> {}, 
 8 -> {}, 9 -> {3}, 10 -> {5}, 11 -> {}, 12 -> {3}, 13 -> {}, 
 14 -> {7}, 15 -> {3}, 16 -> {}, 17 -> {}, 18 -> {3}, 19 -> {}, 
 20 -> {5}, 21 -> {3}, 22 -> {}, 23 -> {}, 24 -> {3}, 25 -> {5}, 
 26 -> {13}, 27 -> {3}, 28 -> {7}, 29 -> {}, 30 -> {3}, 31 -> {}, 
 32 -> {}, 33 -> {3}, 34 -> {17}, 35 -> {7}, 36 -> {3}, 37 -> {}, 
 38 -> {}, 39 -> {3}, 40 -> {5}, 41 -> {}, 42 -> {3}, 43 -> {}, 
 44 -> {}, 45 -> {3}, 46 -> {}, 47 -> {}, 48 -> {3}, 49 -> {7}, 
 50 -> {5}, 51 -> {3}, 52 -> {13}, 53 -> {}, 54 -> {3}, 55 -> {5}, 
 56 -> {7}, 57 -> {3}, 58 -> {}, 59 -> {}, 60 -> {3}, 61 -> {}, 
 62 -> {}, 63 -> {3}, 64 -> {}, 65 -> {}, 66 -> {3}, 67 -> {}, 
 68 -> {17}, 69 -> {3}, 70 -> {7}, 71 -> {}, 72 -> {3}, 73 -> {}, 
 74 -> {}, 75 -> {3}, 76 -> {}, 77 -> {7}, 78 -> {3}, 79 -> {}, 
 80 -> {5}, 81 -> {3}, 82 -> {}, 83 -> {}, 84 -> {3}, 85 -> {}, 
 86 -> {}, 87 -> {3}, 88 -> {}, 89 -> {}, 90 -> {3}, 91 -> {7}, 
 92 -> {}, 93 -> {3}, 94 -> {}, 95 -> {5}, 96 -> {3}, 97 -> {}, 
 98 -> {7}, 99 -> {3}, 100 -> {5}} *)
kirma
  • 19,056
  • 1
  • 51
  • 93
  • Ahh that's interesting approach, i don't need too see all the factors, just get the prime factor so i can then find n=pq. It really seems i was having issues with mathematica program rather than the code. After i restarted and run it again it works fine for me. But thanks anyways – r1z1a Jul 06 '15 at 16:13