code: public static int[] factor (int n, int fromfactors[]){
      int factors[] = new int[64]; // Cant be more than 64 factors.
      int nf = 0;
      int ntest = n;
      int factor;
  
      if (n <= 0)			// Error case
        throw new Error("Number ("+n+") must be positive integer");
  
      /* deal with the preferred factors first */
      for(int i = 0; i < fromfactors.length && ntest != 1; i++){
        factor = fromfactors[i];
        while ((ntest % factor) == 0) {
  	ntest /= factor;
  	factors[nf++] = factor; }}
  
      /* deal with any other even prime factors (there is only one) */
      factor = 2;
      while ((ntest % factor) == 0 && (ntest != 1)) {
        ntest /= factor;
        factors[nf++] = factor; }
  
      /* deal with any other odd prime factors */
      factor = 3;
      while (ntest != 1) {
        while ((ntest % factor) != 0) {
  	factor += 2; }
        ntest /= factor;
        factors[nf++] = factor; }
  
      /* check that the factorization is correct */
      int product = 1;
      for (int i = 0; i < nf; i++) {
        product *= factors[i]; }
      if (product != n)
        throw new Error("factorization failed for "+n);
  
      /* Now, make an array of the right length containing the factors... */
      int f[] = new int[nf];
      System.arraycopy(factors,0,f,0,nf);    
      return f; }
docs: Return the prime factors of n.
  The method first extracts any factors in fromfactors, in order (which
  needn't actually be prime).  Remaining factors in increasing order follow.