计数正整数用除数的给定数量除数、数量、正整数

2023-09-11 07:27:48 作者:﹏1s、耂夫尐萋。

基本上我试图做的就是插入一个整数k的重新presents除数的数,然后找出所有的K因数从1-100000的数字

basically what i was trying to do is insert an integer k that represents the number of divisors and then finding all the numbers that have k divisors from 1-100000

#include <stdio.h>

int main(void)
{
    int k, x = 1, y = 100000, divisor, count;

    printf("Enter the target number of divisors:\n");
    scanf("%d", &k);



    for (divisor = 0; divisor <= 1; divisor++)
        if (x % divisor == 0 && y % divisor == 0)
           count++;

    printf("There are %d numbers between 1 and 100000 inclusive which have exactly %d   divisors\n", k, divisor);

    return 0;
}

不过,我不能似乎是能够做到这一点,请你帮我,我是相当新的编程现场并没有找到答案别处。

However I can't seem to be able to do it, please do help me as I'm fairly new to the programming scene and haven't found an answer elsewhere.

推荐答案

有一个定理,指出如果你有规范重$ P $整数psentation 是一个 1 B 1 *一个 2 B 2 ...一个 N B N 然后这个整数的除数的数量是(B 1 + 1)*(B 2 + 1)...(B N + 1 )。

There is a theorem that states if you have the canonical representation of an integer being a1b1 * a2b2 ... anbn then the number of divisors of this integer is (b1 + 1) * (b2 + 1) ... (bn + 1).

现在你有这样的定理,可以稍微修改埃拉托色尼的筛子把所有整数高达100 000规范的形式。

Now that you have this theorem, you can modify slightly Eratosthenes's sieve to get all integers up to 100 000 in canonical form.

下面是一些code,做什么我的意思修改erathosthenes的筛子。

Here is some code that does what I mean by modified erathosthenes's sieve.

const int size = 100000;
int devs[size + 1];
void compute_devs() {
  for (int i = 0; i < size + 1; ++i) {
    devs[i] = (i%2 == 0) ? 2 : 1;
  }

  int o = sqrt(size);    
  for (int i = 3; i <= size; i += 2) {
    if (devs[i] != 1) {
      continue;
    }
    devs[i] = i;
    if (i <= o) {
      for (int j = i * i; j < size; j += 2 * i) {
        devs[j] = i;
      }
    }
  }
}

调用后 compute_devs 开发者将存储的每个号码的最大素因子的价值高达大小的值。我将在任务的其余部分留给你,但有这个数组成为pretty的直线前进。

After calling compute_devs the value of devs will store the value of the greatest prime divisor of each number up to size. I will leave the rest of the task to you, but having this array it becomes pretty straight forward.