-
Notifications
You must be signed in to change notification settings - Fork 32
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
Replace isprime #50
Replace isprime #50
Conversation
Replace isprime with weaker, but orders of magnitude faster test. Better to finish testing primes up to 2^16, then performing Miller-Rabin as many times as different prime factors.
that would do
@@ -264,9 +264,10 @@ function factor!{T<:Integer,K<:Integer}(n::T, h::Associative{K,Int}) | |||
n = div(n, p) | |||
end | |||
n == 1 && return h | |||
isprime(n) && (h[n] = 1; return h) | |||
p^2 >= n && (h[n] = 1; return h) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If p^2 >= n
, it means that n
is the factor of primes less than or equal to p
: hence n
should have already been factored out by that time, and the condition n == 1
already met!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more sensible change would be to test PRIMES[end]^2 >= n
as a condition to break out of the loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rfourquet no, that's exactly what I meant. Either n is 1, or n is prime by this moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But can you give me an exampleof initial value of n
such that p^2 >= n > 1
at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example is 63. After factoring out 3*3, we are left with 7, which satisfies conditions : 1< 7 < 3^2. As I said, it is a prime. Also, equal sign is here redundant, but the point still holds.
Let's put it this way:
You might want to check PRIMES[end]^2 >= n. Sure, if it is met, then n is at that point either prime or 1. But we could test something similar - p^2 >= n (p being the rithg now excluded factor). If this condition is met, then suppose n=ab with a, b proper divisors of n. Bot a and b are at this point greater then p. So n=ab>p*p>=n, a contradiction. So n has only trivial factorisation, hence it's a prime.
But it is a considerably stronger test! For if n falls into the latter, it falls also into the former, but nor in converse. That is why I propose such solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes of course, thank you and sorry, I should have analysed your change more carefully!
So you are proposing a different strategy: can you show some timings, both what speed-up or slow-down can be expected? |
@rfourquet i'll reply when I'll get to my notebook |
@rfourquet
And results:
Every factor in the lists is prime. I commented out As for
This is considerably faster that at least 15 minutes. |
OK thanks for benchmarking. What about a compromise: adding an |
The point is - numbers with large number of prime factors and numbers with very little prime factors, both are rather rare. You may be right. In fact, Primes.PRIMES table is about 6300 long. Doing Rabin-Miller after every iteration is not necessary, but if it was only 63 times, maybe it would make sense? I'd have to do timings. |
When there are very few factors from |
That is not true. What if |
Maybe there should be an optional argument which is how often make tests and default it to about 100? And make docs that say what is the specifics of such argument? And furthermore, don't perform Also, excuse me if my code is poor, this pr is literally first 3 lines I ever wrote in Julia. |
I would say it's a bit premature to offer an optional argument for that. And if someone has a wish that this option is available, meaning she has an insight how to optimize this value, maybe would be good then to include this heuristic in this package!
It seems like a good idea, I thought about that too. Whether you want to implement that in this PR or in another one is your call! |
I'd better if we finished this PR (separation of concerns). Then I will start with clean state. And as far as optional arg is concerned, I found that in very most cases, the less prime tests, the better. So I'd stick with about 200 probably. Btw, do you have any idea why "AppVeyor build failed"? Logs mention reducing over empty collection, I cannot locate where this function could yield empty Dict. |
OK.
Try |
Replace isprime with weaker, but orders of magnitude faster test. Better to finish testing primes up to 2^16, then performing Miller-Rabin as many times as different prime factors.
Related issue: #49