Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Prime factors of a number

public List<Integer> factorsOf(int n) {
  ArrayList<Integer> factors = new ArrayList<>();

  for (int d = 2; n > 1; d++)
    for (; n % d == 0; n /= d)
      factors.add(d);

  return factors;
}
Source by blog.cleancoder.com #
 
PREVIOUS NEXT
Tagged: #Prime #factors #number
ADD COMMENT
Topic
Name
5+1 =