算法设计与分析: 1-3 最多约数问题

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值