mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Formatted with Google Java Formatter
This commit is contained in:
@@ -1,36 +1,32 @@
|
||||
package Maths;
|
||||
|
||||
import java.lang.Math;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PrimeFactorization {
|
||||
public static void main(String[] args){
|
||||
System.out.println("## all prime factors ##");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.print("Enter a number: ");
|
||||
int n = scanner.nextInt();
|
||||
System.out.print(("printing factors of " + n + " : "));
|
||||
pfactors(n);
|
||||
scanner.close();
|
||||
public static void main(String[] args) {
|
||||
System.out.println("## all prime factors ##");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.print("Enter a number: ");
|
||||
int n = scanner.nextInt();
|
||||
System.out.print(("printing factors of " + n + " : "));
|
||||
pfactors(n);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
public static void pfactors(int n) {
|
||||
|
||||
while (n % 2 == 0) {
|
||||
System.out.print(2 + " ");
|
||||
n /= 2;
|
||||
}
|
||||
public static void pfactors(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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user