繁体   English   中英

所有数字都有8个除数

[英]All the numbers with exactly 8 divisors

给定n ,我需要具有正好8个除数的数字。

24 = 1, 2, 3, 4, 6, 8, 12, 24

低于100 ,有10个满足上述条件的数字。

24, 30, 40, 42, 54, 56, 66, 70, 78 and 88

给定n,在n以下满足上述条件的数量。

第一种方法:

我使用了素因子方法。

x = p1^k1 * p2 ^k2 * p3^k3 ...
n = (k1 + 1)(k2 + 1)(k3 + 1)...

处理大数字时,这种方法有点慢。

int max = 1000000000;
int count = 0;
for(int i = 0; i < max; ++i)
   if(check(i))count++;

private static boolean check(int num) {
    int ans = 1;
    int count = 0;
    while (num % 2 == 0) {
        num /= 2;
        count++;
    }
    ans *= (count + 1);
    if (ans > 8)
        return false;
    for (int i = 3; i * i <= num; ++i) {
        count = 0;
        while (num % i == 0) {
            count++;
            num /= i;
        }
        ans *= (count + 1);
        if (ans > 8)
            return false;
    }
    if (num != 1)
        ans *= 2;
    if (ans > 8)
        return false;
    return ans == 8;
}

第二种方法:

筛选类似的方法,它标记一个数字的所有倍数,然后检查计数是否为8

static int max = 100000000;
static int[] facs = new int[max];

for (int i = 2; i < max; ++i) {
    for (int j = i; j < max; j += i) {
        facs[j]++;
    }
}
int count = 0;
for (int i = 0; i < max; ++i)
    if (facs[i] == 7)//1 is a factor of all number so check for count 7
        count++;
System.out.println(count);

然而,这种方法有点快,但这不能用于10^9以上的更大数字。

如何计算10^9以上正好有8个除数的数字?

我有什么诡计吗? 我怎样才能改善这个?

重点是找到数字的数量,而不是从迭代到计算减少问题的数字。

对于具有8个除数的数字,它应该以3种形式之一书写。

p1 * p2 * p3

p1^3 * p2

p^7

因为,从公式8 = 2 * 2 * 2 or 4 * 2 or 8 * 1

因此,如果我们知道有多少素数低于n ,我们就可以找出可以有8除数的组合。

p1*p2*p3:

The maximum `p` can be `N / 2 * 3`.
So the number of numbers which have `8` divisors are nC3.

p1^3*p2:

The maximum `p` can be `N/8`.
So the number of numbers which have `8` divisors are nC2.

p^7:

The maximum `p` can be `N^(1/7)`.
So the number of numbers which have `8` divisors are `n`.

Here, `n` is the number of primes under `N`

因此,为了找出nN的素数,我们可以使用Prime计数函数而不是计算素数。

因此,一旦知道素数的数量,就可以通过选择性组合计算数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM