1-3 最多约数问题
问题描述
正整数x的约数是能整除x的正整数。正整数x的约数个数记为div(x)div(x)。例如,1,2,5,10都是正整数10的约数,且div(10)=4。 对于给定的2个正整数a<=b,编程计算a与b之间约数个数最多的数。
分析
设正整数x的质因子分解为 x=pN11pN22...pNkkx=p1N1p2N2...pkNk,则
div(x)=(N1+1)(N2+1)...(Nk+1)div(x)=(N1+1)(N2+1)...(Nk+1)
Java
import java.util.Scanner;
public class Main {
//1<=a<=b
private static int MAXP = 100005;
private static int PCOUNT = 0;//when MAXP=100005, we have PCOUNT=9593, then the largest prime number is prime[9592]=100003
private static int[] prime = new int[MAXP];
private static boolean[] isPrime = new boolean[MAXP];
public static void main(String[] args) {
// primes();
optimisedPrimes();
Scanner input = new Scanner(System.in);
while (true){
System.out.println("Please input the value of a:");
int a = input.nextInt();
System.out.println("Please input the value of b:");
int b = input.nextInt();
int max = 0;
int count = 0;
int minX = 0;
for(int i=a; i<=b; i++){
count = div(i);
if(count > max){
max = count;
minX = i;
}
}
System.out.println("There are at most "+max+" divisors of the number between "+a+" and "+b);
System.out.println("The smallest number which has the most divisors is "+minX);
System.out.println("------------------------------");
}
}
//get the number of divisors in number x
private static int div(int x){
int count;
int sum=1;
for(int i=0; i<PCOUNT && prime[i]<=x; i++){
count = 1;
if((x%prime[i]) == 0){
do{
count++;
x /= prime[i];
}while ((x%prime[i]) == 0);
sum *= count;
}
}
return sum;
}
//build primes array
private static void primes(){
int i,j;
for(i=0; i<MAXP; i++){
isPrime[i] = true;
}
for(i=2; i<MAXP; i++){
if(isPrime[i]){
j = i + i;
while (j < MAXP){
isPrime[j] = false;
j += i;
}
}
}
for(i=2; i<MAXP; i++){
if(isPrime[i]){
prime[PCOUNT++] = i;
}
}
}
//build optimised primes array
private static void optimisedPrimes()
{
int i,j;
for(i=0; i<MAXP; i++){
isPrime[i] = true;
}
for(i=2; i<Math.sqrt(MAXP); i++){
if(isPrime[i]){
j = i + i;
while (j < MAXP){
isPrime[j] = false;
j += i;
}
}
}
for(i=2; i<MAXP; i++){
if(isPrime[i]){
prime[PCOUNT++] = i;
}
}
}
}
Sample input & output
Please input the value of a:
1
Please input the value of b:
36
There are at most 9 divisors of the number between 1 and 36
The smallest number which has the most divisors is 36
------------------------------
Please input the value of a:
24
Please input the value of b:
25
There are at most 8 divisors of the number between 24 and 25
The smallest number which has the most divisors is 24
------------------------------
Please input the value of a:
1
Please input the value of b:
100005
There are at most 128 divisors of the number between 1 and 100005
The smallest number which has the most divisors is 83160
------------------------------
Please input the value of a:
Reference
王晓东《计算机算法设计与分析》(第3版)P7

这篇博客探讨了如何解决1-3最多约数问题,即在正整数a和b之间找到约数数量最多的数。通过分析质因子分解,得出div(x)的公式,并提供了Java实现来解决此问题。附带样例输入输出和参考书籍。
1300

被折叠的 条评论
为什么被折叠?



