Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: nextprime(::BigInt) #16314

Closed
wants to merge 1 commit into from
Closed

Conversation

mschauer
Copy link
Contributor

@mschauer mschauer commented May 11, 2016

On StackExchange someone asked for a wrapper of the GMP next_prime routine. If there is interest, here is a pull request for a new function nextprime. But compared with the naive

nextprime2(y) = let x = y+1; while(!isprime(x)) x += 1; end; x; end

I get

x = big(1931)*prod(big(primes(1,1933)))÷7230 - 30244 #location of a big prime gap of size 66520
julia> @time nextprime2(x) - x
 33.357414 seconds (199.57 k allocations: 25.378 MB, 0.02% gc time)
66520

julia> @time nextprime(x) - x
 49.900821 seconds (11 allocations: 3.398 KB)
66520

Am I missing something or is __gmpz_nextprime worse than __gmpz_probab_prime_p (both use 25 reps standard)?

@pabloferz

@kshyatt kshyatt added the domain:maths Mathematical functions label May 11, 2016
@tkelman
Copy link
Contributor

tkelman commented May 11, 2016

If we're going to define this, there should be definitions for more than just BigInts.

@pabloferz
Copy link
Contributor

pabloferz commented May 11, 2016

@mschauer AFAIK mpz_nextprime can be slow (though this comparison shows it can be really bad). I agree with Tony in that there should be definitions for other types.

Nonetheless it has been pointed out before that this kind of functions probably belong to a package (to which I agree).

@pabloferz
Copy link
Contributor

Actually the 'naive' loop is probably near to the best you can do, what you would actually need is a faster primality test (BPSW, ECPP or APR). For the loop version you can get rid of the allocations with

function nextprime2(y::BigInt)
    c = BigInt(1)
    x = y + c
    while(!isprime(x))
        ccall((:__gmpz_add,:libgmp), Void, (Ptr{BigInt}, Ptr{BigInt}, Ptr{BigInt}), &x, &x, &c)
    end
    x
end

@mschauer
Copy link
Contributor Author

Actually the 'naive' loop is probably near to the best you can do

Looks like it is: http://stackoverflow.com/questions/4475996/given-prime-number-n-compute-the-next-prime
Nice insofar that then there is also a natural way to make @tkelman happy and define nextprime for other types in the same way.

@pabloferz
Copy link
Contributor

Given that it seems hard to do much better than this, what we really should focus on is in improving isprime istead, as discussed here #11594.

@mschauer
Copy link
Contributor Author

#16357

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants