Skip to content

Commit f720bc3

Browse files
committed
Create PrimeFactors.java
1 parent 1790948 commit f720bc3

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class PrimeFactors {
2+
public static void primeFactors(int n){
3+
for(int i=2; i<=Math.sqrt(n);i++){
4+
while(n%i==0){
5+
System.out.print(i+" ");
6+
n=n/i;
7+
}
8+
}
9+
if(n>1){
10+
System.out.print(n+" ");
11+
}
12+
}
13+
public static boolean isPrime(int n){
14+
if(n<=1){
15+
return false;
16+
}
17+
for(int i=2;i<=Math.sqrt(n);i++){
18+
if(n%i==0)
19+
return false;
20+
}
21+
return true;
22+
}
23+
public static void main(String args[]){
24+
int n = 125;
25+
primeFactors(n);
26+
System.out.println();
27+
if(isPrime(n)){
28+
System.out.println(n + " is a prime number.");
29+
}else{
30+
System.out.println(n + " is not a prime number.");
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)