分解质因数

xiaoxiao2021-02-28  15

package homework; import java.util.Scanner; public class Sum { public static boolean isprime(int n) { boolean isprime = true; for( int i = 2; i < n ; i++ ) { if ( n % i == 0 ) { isprime = false ; break; } } return isprime; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); System.out.println("分解质因数,请输入一个[2,100000]范围内的整数"); int n; n = in.nextInt(); if( isprime(n)) { System.out.println(n+"="+n); } else { System.out.print(n+"="); for ( int j = 2; j <= n; j++) { if (isprime(j)) { int m; do { m = n % j; if (m == 0) { n = n / j; if( n >= j) { System.out.print(j+"x"); } else { System.out.print(j); } } }while ( m == 0); } } } } }
转载请注明原文地址: https://www.6miu.com/read-1649996.html

最新回复(0)