如何让这个程序更快?

时间:2017-11-06 19:36:04

标签: c

分配是编写一个读入整数k的程序,并打印出正整数的数量 介于1和100000之间(含),具有正好k个除数。例如,数字24有8个除数: 1,2,3,4,6,8,12和24。

我有一个正在运行的程序,但无论如何我可以更快地进行搜索吗?

#include <stdio.h>
#include <math.h>


int main(void)
{   int a; //user input//
    int divisors; //running total of number of divisors//
    int sum; //running total of numbers with the required number of divisors//

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

    int i;
    for (i=1; i<=100000; i++)
    {
            divisors=2;
            int p;
            for(p=2; p<i; p++)
            {if (i%p==0)
            divisors++;}

        if (divisors==a)  
        sum++;}

    printf("There are %d numbers between 1 and 100000 inclusive which have exactly %d divisors.", sum, a);

return 0;
}

2 个答案:

答案 0 :(得分:-1)

我们可以通过检查p进行优化,而不是将i递增1,而不是检查sqrt(i) uptil divisors的值,我们将它增加2,一个数字表示k除以i,第二个表示数字i/k

n=1000000;

for (i=1; i<=10000; i++)
    {
            divisors=2;
            int p;
            for(p=2; p<=sqrt(i); p++)
            {
                if (i%p==0)
                {
                    if(p != (i/p)
                        divisors = divisors + 2;
                    else 
                      divisors++;
                }
            }

        if (divisors==a)  
        sum++;
}

答案 1 :(得分:-1)

示例代码。移动了i和p的声明以与旧的C类型编译器兼容(我使用Microsoft / Visual Studio)。使用ceil(sqrt(i))外循环。代码处理输入1(只有数字1有1个除数)。输入2将输出小于100,000的素数(有9592个素数小于100,000)。

此方法需要超过2100万次迭代。迭代次数〜= .67 n sqrt(n)。

#include <stdio.h>

int main(void)
{
    int a;          /* user input */
    int divisors;   /* total number of divisors */
    int sum;        /* count of numbers with required number of divisors */
    int i;          /* moved here for C compiler */
    int p;          /* moved here for C compiler */ 
    int sqrti;      /* ceil(sqrt(i)) */

    printf("Enter the target number of divisors: ");
    scanf("%d", &a);
    printf("\n");
    sum = 0;                            /* init sum */
    sqrti = 1;                          /* init sqrti */
    for (i = 1; i <= 100000; i++)
    {
        divisors = 0;                   /* init number of divisors */
        if(sqrti*sqrti < i)             /* update sqrti as needed */
            sqrti += 1;
        for(p = 1; p < sqrti; p++)
            if(i%p == 0)                /* if p is a divisor, count it and i/p */
                divisors += 2;
        if(p*p == i)                    /* if p*p == i, count it */
            divisors += 1;
        if (divisors == a)              /* if i has a divisors, increment sum */
            sum += 1;
    }
    printf("There are %d numbers from 1 to 100000 inclusive which have exactly %d divisors.\n", sum, a);
    return 0;
}

如果可以使用类似于素数的筛子方法的数组,这种方法需要超过100万次迭代。迭代次数〜= n ln(n)。

#include <stdio.h>
#define n 100000
int main(void)
{
int * cnt = (int *)calloc(n+1, sizeof(int));
int d;
    printf("Enter the target number of divisors: ");
    scanf("%d", &d);
    /* time complexity O(n log(n)) */
    {
    int i, j;
        for (i = 1; i <= n; i++) {
            for(j = i; j <= n; j += i) {
                cnt[j]++;
            }
        }
    }
    {
    int i;
    int sum = 0;
        for (i = 1; i <= n; i++)
            sum += (cnt[i] == d) ? 1 : 0;
        printf("excactly %d numbers have %d divisors\n", sum, d); 
    }
    free(cnt);
    return 0;
}