-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeFactorization.java
108 lines (92 loc) · 2.07 KB
/
PrimeFactorization.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.*;
/**
*
* Solution for Numbers / Prime Factorization problem on Karan projects - Github: https://github.com/karan/Projects.
*
* Prime Factorization - Have the user enter a number and find all Prime Factors (if there are any) and display them.
*
*
* @author Hedgehog01
* @version 14.07.2014
*
*/
public class PrimeFactorization {
/**
* Method that returns the prime factors of a number
* @param num the number to find prime factors for
* @return int array with the prime factors found for the number
*/
public static int[] findPrimeFactor (int num)
{
//array that will keep the prime factorizations
int [] factors= new int [num/2];
int prime = 2; // first prime to try
int index = 0; //array index
while (num !=1)
{
if (num % prime == 0)
{
num = num / prime;
factors[index] = prime; //save the fac
index++;
}
else
prime = getNextPrime (prime, num);
}
return factors;
}
/*
* private method that return the next prime number to try.
*/
private static int getNextPrime (int prime, int num)
{
if (isPrime(num)) //check if the current number is already a prime - if so return it.
prime = num;
else //find next best prime
{
prime++;
while (!isPrime(prime) && prime < num)
{
prime++;
}
}
return prime;
}
/*
* private method that checks if a number is a prime number.
*/
private static boolean isPrime(int num)
{
boolean bool = true;
for (int i=2; i<num && bool;i++)
{
if (num%i==0)
bool = false;
}
return bool;
}
/*
* private method to print an array
*/
private static void printArr (int [] arr,int num)
{
for(int i=0; i<arr.length;i++)
{
if (arr[i] != 0)
System.out.print(arr[i] + " X ");
}
System.out.print("1 = " + num);
System.out.println();
}
/*
* class driver
*/
public static void main(String[] args)
{
int num = 0;
Scanner scan = new Scanner (System.in);
System.out.println ("Please enter number to find Prime factors of: ");
num = scan.nextInt();
printArr (findPrimeFactor (num),num);
}
}