Skip to main content
9 events
when toggle format what by license comment
Nov 22, 2012 at 17:15 vote accept Adil Mughal
Oct 19, 2012 at 7:12 comment added Peter Taylor @GeneS, either pull the Sqrt out of the loop (and downcast to int) or invert the test to i * i < n (because processors have a single instruction for multiplication, but sqrt is expensive).
Oct 18, 2012 at 17:22 comment added Gene S @PeterTaylor So for my own education, what would be a better way of doing an early escape?
Oct 18, 2012 at 16:25 comment added Peter Taylor @GeneS, true, although that's the worst possible way of doing the early escape. I didn't focus too much on IsPrime because I knew I was going to point out that it's unnecessary.
Oct 18, 2012 at 16:20 comment added Gene S You could also skip a larger number of tests in the IsPrime function by using the Math.Sqrt(n) in the for statement ... for (int i = 2; i <= Math.Sqrt(n); i++)
Oct 18, 2012 at 16:14 comment added Adil Mughal since it's just for self-learning and fun, that's why I also tried that. But what I meant with alternative was any better approach to do with prime factorization
Oct 18, 2012 at 15:47 comment added Peter Taylor @AdilMughal, there's the obvious approach of not using prime factorisation, but you've explicitly chosen not to use gcd.
Oct 18, 2012 at 13:33 comment added Adil Mughal +1. Thanks Peter. Is there alternative approach you can think of for LeastCommonMultipleByPrimeFactorization.
Oct 18, 2012 at 12:48 history answered Peter Taylor CC BY-SA 3.0