计算一个数的每个质因数的个数
count the number of each prime factor of a number
我想计算一个整数的每个质因数的个数,然后打印出最大的个数。例如720 = 2^4 * 3^2 * 5^1。所以我想打印 Maximum Number of Time occurrence = 4 并且数字是 2。我尝试使用 Integer.max()
,但那没有用。
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
int num;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
TreeMap<Integer, Integer> factors = new TreeMap<>();
for (int i =2; i < num; i++){
int count = 0;
while (num%i == 0){
System.out.println(i + " ");
num = num/i;
count ++;
}
if (count > 0){
factors.put(i, count);
}
}
if (num > 2){
System.out.println(num + " ");
factors.put(num, 1);
}
System.out.println( "-------------" );
for( Integer factor : factors.keySet() ){
System.out.println( factor + "^" + factors.get( factor ) );
}
}
}
如果您只想要出现次数最多的素数,那么您可以在每个素数都经过测试后进行检查。为此,地图并不是真正必要的。请注意,多个素数可能出现相同的次数。如果有平局,这只保存遇到的第一个。
int num;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
int primeFactor = 0;
int count = 0;
int maxCount = 0;
int inc = 1; // initial increment.
int i = 2; // intitial prime
while (num > 1) {
count = 0;
while (num%i == 0) {
num = num/i;
count++;
}
// here check current count against maxCount and
// alter if required.
if (count > maxCount) {
maxCount = count;
primeFactor = i;
}
if (count > 0) {
System.out.printf("Prime = %d, count = %d%n", i, count);
}
i+=inc; // bump by 1 to get 3 and then
inc = 2; // start incrementing by 2
}
// and print the results.
System.out.println("First highest occuring prime = " + primeFactor);
System.out.println("MaxCount = " + maxCount);
我想计算一个整数的每个质因数的个数,然后打印出最大的个数。例如720 = 2^4 * 3^2 * 5^1。所以我想打印 Maximum Number of Time occurrence = 4 并且数字是 2。我尝试使用 Integer.max()
,但那没有用。
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
int num;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
TreeMap<Integer, Integer> factors = new TreeMap<>();
for (int i =2; i < num; i++){
int count = 0;
while (num%i == 0){
System.out.println(i + " ");
num = num/i;
count ++;
}
if (count > 0){
factors.put(i, count);
}
}
if (num > 2){
System.out.println(num + " ");
factors.put(num, 1);
}
System.out.println( "-------------" );
for( Integer factor : factors.keySet() ){
System.out.println( factor + "^" + factors.get( factor ) );
}
}
}
如果您只想要出现次数最多的素数,那么您可以在每个素数都经过测试后进行检查。为此,地图并不是真正必要的。请注意,多个素数可能出现相同的次数。如果有平局,这只保存遇到的第一个。
int num;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
int primeFactor = 0;
int count = 0;
int maxCount = 0;
int inc = 1; // initial increment.
int i = 2; // intitial prime
while (num > 1) {
count = 0;
while (num%i == 0) {
num = num/i;
count++;
}
// here check current count against maxCount and
// alter if required.
if (count > maxCount) {
maxCount = count;
primeFactor = i;
}
if (count > 0) {
System.out.printf("Prime = %d, count = %d%n", i, count);
}
i+=inc; // bump by 1 to get 3 and then
inc = 2; // start incrementing by 2
}
// and print the results.
System.out.println("First highest occuring prime = " + primeFactor);
System.out.println("MaxCount = " + maxCount);