Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

prime factorization java

    public static void prime_factors(int n){
        int power = 0;
        for (int i = 2; i <= Math.sqrt(n); i += 2) {
            if (n % i == 0) {
                while (n % i == 0) {
                    power++;
                    n /= i;
                }
                System.out.print(i);
                if (power > 1)
                    System.out.print("^" + power);
                if (n > 1)
                    System.out.print(" x ");
                power = 0;
            }
            if (i == 2) i--;
        }
        if (n > 1)
            System.out.print(n);
    }
Comment

prime factorization java

    public static void prime_factors(int n){

        while (n % 2 == 0){
            System.out.print(2 + " ");
            n /= 2;
        }

        for (int i = 3; i <= Math.sqrt(n); i += 2) {
            while (n % i == 0){
                System.out.print(i + " ");
                n /= i;
            }
        }

        if (n > 2)
            System.out.print(n);
    }
Comment

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;
    }
Comment

PREVIOUS NEXT
Code Example
Java :: t implements comparable 
Java :: change fab image programatically 
Java :: how to find odd and even in a array 
Java :: where to use findviewbyid in fragment 
Java :: java programme for fibonnaci series 
Java :: how to insert a 0 in an array java 
Java :: java null 
Java :: java take and save screenshot 
Java :: Could not resolve project :app 
Java :: refreshing method() android studio webview 
Java :: reverse a doubly linked list 
Java :: java 2d jagged array example 
Java :: LRU Cache java 
Java :: run java file 
Java :: java dictionary initialization 
Java :: java check prime number 
Java :: remove duplicates from arraylist in android 
Java :: input arraylist in java 
Java :: creating an object in java 
Java :: java collectors mapping 
Java :: switch in java 
Java :: what is encapsulation in java 
Java :: android hide and show bottom navigation 
Java :: can we serialize class in java 
Java :: interface declaration in java 
Java :: keycloak spring boot application.properties 
Java :: Is the main method compulsory in Java? 
Java :: multiple inputs java 
Java :: Java Custom Annotations 
Java :: java inser at index 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =