Skip to content

Allow long running factorizations to yieldΒ #167

@DannyKong12

Description

@DannyKong12

I'm trying to write a program where the solution requires a prime factorization of an integer from a list of many possible candidate solutions. Any factorization will do, but obviously some numbers are harder to factor than others. WHP at least one of the numbers has some small factors. My approach is simply to try to factor a random candidate for some amount of time, and abandon it to move on to another solution if its taking too long.

I would like to do this with a timeout macro/function see this for example, but since factor, in particular pollardfactor doesn't yield(), such a macro won't work. From limited testing, in hard instances, this inner loop can run quite long.

Primes.jl/src/Primes.jl

Lines 544 to 553 in 42b97a5

while k < r && G == 1
ys = y
for i in 1:(m > (r - k) ? (r - k) : m)
y = y^2 % n
y = (y + c) % n
q = (q * (x > y ? x - y : y - x)) % n
end
G = gcd(q, n)
k += m
end

It would be really helpful if pollardfactor could yield from time to time, (perhaps once every X iterations of that loop, and once at the end). Someone who knows the code a bit better could probably find a better spot to place the yields that minimizes the impact on runtime for smaller instances.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions