Skip to main content
1 of 3
qwr
  • 1.2k
  • 1
  • 7
  • 23

Function names

n and eq are very unclear names, unless they clearly correspond to a referenced formula. eq usually stands for "equals" or "equation", and it's not clear what "equals" would do for one input.

Input handling

I assume you have a consistent way to handle negative numbers. You can simply output -1 as a factor, test for 0, and then only consider positive integers. This would reduce the negative number and zero checks.

Trial Division Algorithm

I think a for loop testing divisors from 2 to n instead of unbounded incrementing is easier to understand. You also need to only try dividing out primes. (Why is this true?) In this way, sieving prime numbers first and only dividing by primes will be much faster for large inputs (prime number theorem). By the way, the largest factor to try division is sqrt(n); if n leftover after dividing is not 1 then that is the largest prime factor. (Why is this true?) This cuts down the number of checks from O(n) to O(sqrt n).

qwr
  • 1.2k
  • 1
  • 7
  • 23