Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. package fromLeonard;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.  * A utility class to find the prime factorilization of positive integers.
  7.  *
  8.  * @author Pi-Nerd Fisher
  9.  * @version 1.0
  10.  */
  11. public class PrimeFactorilization {
  12.     /**
  13.      * Finds the prime factors of any positive integer.
  14.      *
  15.      * @param i
  16.      *            the number to find the prime factors of
  17.      * @return an ArrayList with the factors, ordered from least to greatest
  18.      */
  19.     public static ArrayList<Integer> primeFactors(int i) {
  20.         Primes foo = new Primes(i);
  21.         ArrayList<Integer> primes = foo.getList();
  22.         ArrayList<Integer> factors = new ArrayList<Integer>();
  23.         int k = 0;
  24.         while (k < primes.size()) {
  25.             if (i % primes.get(k) == 0) {
  26.                 factors.add(primes.get(k));
  27.                 i/=primes.get(k);
  28.             } else {
  29.                 k++;
  30.             }
  31.         }
  32.         return factors;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement