Skip to main content
Code Review

Return to Revisions

3 of 3
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/

On top of the answer by Emily L. you can add a simple primality test as an exit condition for the loop, this will also avoid a lot of iterations.

while(number!=1 && div <= sqrt(number))

Where sqrt(number) is the square root of the number in each iteration.

If \$ n \$ does not have divisors \$ < \sqrt{n} \$, then \$ n \$ is prime and thus you can stop looking for larger prime factors and output \$n\$.

pepelu
  • 111
  • 4
default

AltStyle によって変換されたページ (->オリジナル) /