public static int [ ] factorsOf ( final int pn ) { int n = pn ; final boolean isPrime [ ] = primeSieve ( n ) ; // primes <= n final ICommonsList < Integer > v = new CommonsArrayList <> ( ) ; // Loop to try prime divisors. for ( int factor = 2 ; n > 1 ; ++ factor ) { if ( isPrime [ factor ] && ( n % factor == 0 ) ) { // Prime divisor found. v . add ( Integer . valueOf ( factor ) ) ; // Factor out multiples of the divisor. do { n /= factor ; } while ( n % factor == 0 ) ; } } // Create an array of the distinct prime factors. final int factors [ ] = new int [ v . size ( ) ] ; for ( int i = 0 ; i < v . size ( ) ; ++ i ) { factors [ i ] = v . get ( i ) . intValue ( ) ; } return factors ; }
Compute the prime factors of an integer value .