Add HW01 exercises
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import java.util.Scanner;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class PrimeFactors {
|
||||
public static ArrayList<Integer> Factors(int num){
|
||||
ArrayList<Integer> factors = new ArrayList<>();
|
||||
|
||||
if (num <= 1){
|
||||
System.out.println("no prime factor!.");
|
||||
}
|
||||
|
||||
while (num%2 == 0){
|
||||
factors.add(2);
|
||||
num /= 2;
|
||||
}
|
||||
for(int i = 3; i*i <= num; i+=2){
|
||||
while (num%i == 0){
|
||||
factors.add(i);
|
||||
num /= i;
|
||||
}
|
||||
}
|
||||
if (num > 1 ){
|
||||
factors.add(num);
|
||||
}
|
||||
return factors;
|
||||
}
|
||||
public static void main(String[] args){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int number = sc.nextInt();
|
||||
ArrayList<Integer> factors = Factors(number);
|
||||
if (!factors.isEmpty()){
|
||||
System.out.println("factors = "+ factors);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user