Java分解素因数
1243 ワード
問題の説明
区間[a,b]におけるすべての整数の素因数分解を求めた.
入力フォーマット
2つの整数a,bを入力します.
出力フォーマット
行ごとに1つの数の分解を出力して、形はk=a 1*a 2*a 3のようです...(a 1<=a 2<=a 3…,kも小さい頃から大きい)(具体的にはサンプルを見ることができる)
サンプル入力
3 10
サンプル出力
3=34=2*25=56=2*37=78=2*2*29=3*310=2*5
ヒント
すべての素数をふるい出してから分解します.
データ規模と約定
2<=a<=b<=10000
コード:
区間[a,b]におけるすべての整数の素因数分解を求めた.
入力フォーマット
2つの整数a,bを入力します.
出力フォーマット
行ごとに1つの数の分解を出力して、形はk=a 1*a 2*a 3のようです...(a 1<=a 2<=a 3…,kも小さい頃から大きい)(具体的にはサンプルを見ることができる)
サンプル入力
3 10
サンプル出力
3=34=2*25=56=2*37=78=2*2*29=3*310=2*5
ヒント
すべての素数をふるい出してから分解します.
データ規模と約定
2<=a<=b<=10000
コード:
import java.util.Scanner;
public class Test {
public static boolean sushu(int x) {
for (int i = 2; i < x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
for (int i = a; i <= b; i++) {
System.out.print(i + "=");
int temp = i;
while (sushu(temp)==false){
for (int j = 2; j < temp; j++){
if (temp % j == 0) {
System.out.print(j+"*");
temp = temp/j;
}
}
}
System.out.print(temp);
System.out.println();
}
}
}