Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

prime factorization java

    public static ArrayList<Integer> prime_factors(int n){
        ArrayList<Integer> result = new ArrayList<>();
        
        while (n % 2 == 0){
            result.add(2);
            n /= 2;
        }

        for (int i = 3; i <= Math.sqrt(n); i += 2) {
            while (n % i == 0){
                result.add(i);
                n /= i;
            }
        }

        if (n > 2)
            result.add(n);
        
        return result;
    }
 
PREVIOUS NEXT
Tagged: #prime #factorization #java
ADD COMMENT
Topic
Name
3+5 =