Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

calculate prime factors of a number java

public static void main(String[] args) {
   	int n=12;
    for(int i=2; i<=n; i++){ 
       while(n%i==0){
         n=n/i;
         System.out.println(i); //prints 2 2 3
       }
   }
}
Comment

How to find the prime factors of a number in Java?

public class PrimeFactorization {
	/*
	 * Aim is to find all the prime
	 * factors of a given number n
	 */
	public static void main(String[] args) {
		int value = 315;
		printPrimeFactors(value); // 3^2 5^1 7^1
	}

	private static void printPrimeFactors(int n) {
		int powerOfDivisor;
		// Identify the prime factors of n
		for (int div = 2; div * div <= n; div++) {
			if (n % div == 0) {
				powerOfDivisor = 0;
				// How many times prime factor divides n
				while (n % div == 0) {
					n = n / div;
					powerOfDivisor++;
				}
				// Print prime factor raised to a certain power
				System.out.print(div + "^" + powerOfDivisor + " ");
			}
		}
		// Last prime factor of n has a power = 1
		if (n > 1)
			System.out.println(n + "^" + 1);
		else
			System.out.println();
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: int array to set in java 
Java :: java random max and min 
Java :: set height of layout programmatically android 
Java :: spigot scheduler 
Java :: java random between 
Java :: java timestamp 
Java :: java chararray to int 
Java :: how to clear a text file in java 
Java :: read int from keyboard java 
Java :: collision java 
Java :: init cap java 
Java :: resultset get value 
Java :: java bubble sort 
Java :: activity as a splash screen java code 
Java :: remove autofocus edittext android 
Java :: pytho count avro file 
Java :: Recompile with -Xlint:deprecation 
Java :: convert object to map scala 
Java :: stackoverflow spring regex 
Java :: java get specific element from arraylisb 
Java :: taking date as input in java 
Java :: consolenausgabe java 
Java :: android how to switch between activities 
Java :: file to multipartfile in java 
Java :: java key pressed 
Java :: Get the first Monday of a month in Java 
Java :: button color xml 
Java :: java swing array of buttons 
Java :: spring value # vs $ 
Java :: ubuntu java development kit 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =