-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeFactors.java
64 lines (58 loc) · 1.2 KB
/
PrimeFactors.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
package codingProblems;
public class PrimeFactors
{
public static void getPrimeFact(int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
System.out.print(" "+2);
n = n/2;
}
// n is odd
for (int i = 3; i <= Math.sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
System.out.print(" "+i);
n = n/i;
}
}
// This condition is to handle the case when n is a prime number > 2
if (n > 2)
System.out.print(" "+n);
}
public static void main(String args[])
{
int n = 315;
getPrimeFact(n);
}
}
/*import java.util.*;
public class PrimeNumber
{
public static void main(String args [])
{
Scanner sc = new Scanner(System.in);
int num1, num2;
System.out.println("Please enter the first number");
num1 = sc.nextInt();
System.out.println("Please enter the Second number");
num2 = sc.nextInt();
System.out.println("Prime number: ");
for (int i=num1; i <= num2; i++ ){
int j;
for (j=2; j<i; j++){
int n = i%j;
if (n==0){
break;
}
}
if(i == j){
System.out.print(" "+i);
}
}
System.out.println();
}
}*/