素因数分解(Java)

区間[a、b]内のすべての整数の素因数分解を見つけます。

入力形式:
2つの整数a、bを入力しますデータスケールと規則2 <= a <= b <= 10000

出力形式:
各行は、k = a1a2a3 ...(a1 <= a2 <= a3 ...、kも小さいものから大きいものへ)のような数値の分解を出力します(詳細については、例を参照してください)。

入力サンプル:これ
が入力のセットです。例:
3 10

出力サンプル:
対応する出力をここに示します。例:
3 = 3
4 = 2 2
5 = 5
6 = 2
3
7 = 7
8 = 2 2 2
9 = 3 3
10 = 2
5

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int num ;
        int a = sc.nextInt();
        int b = sc.nextInt();
        for (int i = a; i <= b; i++) {
    
    
            System.out.print(i + "=");
            num = i ;
            for (int j = 2; j <= i/2; j++) {
    
    
                while (num % j == 0) {
    
    
                    num = num / j;
                    System.out.print(j);
                    if (num != 1)
                        System.out.print("*");
                }
            }
            if (num==i){
    
    
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

おすすめ

転載: blog.csdn.net/weixin_51430516/article/details/115102654