主要因素的总和

时间:2018-06-30 21:42:27

标签: c

我试图显示给定数的素数因子的总和, 我很难在输出中显示主要因素。

样本输出:

Input number: 6
Factors are: 1 2 3
Sum of its factor: 1 +2 +3 =6

我能够显示总和,但我想显示1+2+3=6,就像上面示例中的因素为1 2 3一样。

您能帮我纠正我的语法来实现这一点吗?预先感谢。

这是我的代码:

#include <stdio.h>

int main() {
    int i, j, num, isPrime, sum;

    printf("Input number: ");
    scanf("%d", &num);

    printf("Factors are: ", num);

    for (i = 1; i <= num; i++) {
        if (num % i == 0) {
            isPrime = 1;
            for (j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = 0;
                    break;
                }
            }

            if (isPrime == 1) {
                printf("%d ", i);
                sum += i;
            }
        }
    }

    printf("\nSum of its factor : %d", sum);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

由于您希望两次打印所有质数因子,因此应该以某种方式这样做,以便避免重复的代码。这是一个主意:

#include <stdio.h>

/* Return the smallest prime that is smaller than or equal to n */
/* Assumes that the argument is greater than 1 */
int getFirst(int n)
{
    int i;
    for(i = 2; i <= n; i++)
        if(n % i == 0)
            return i;
}

int main()
{
    int num, x, tmp, sum=0;
    scanf("%d", &num);
    tmp = num;

    printf("Factors are: ");
    while(1) {
        x = getFirst(tmp);
        printf("%d ", x);
        if (x == tmp) /* If we are at the last prime */
            break;
        tmp /= x;
    }
    printf("\n");

    printf("Sum of factors is: ");
    tmp = num;
    while(1) {
        x = getFirst(tmp);
        printf("%d ", x);
        sum += x;
        if(x == tmp) /* If we are at the last prime */
            break;
        printf("+ ");
        tmp /= x;
    }
    printf("= %d\n", sum);
}

但是正如评论中指出的那样。 1不是素数,这就是为什么我将其排除在外。

答案 1 :(得分:1)

您的代码实际上具有未定义的行为,因为sum未初始化为0。它只是偶然产生正确的和。

您可以将因子存储在数组中,甚至可以像sprintf那样构造表达式。该表达式的最大长度不是很大,因为最多可以有9个不同的质因子(29 !!> 2 32

这是修改后的版本:

#include <stdio.h>

int main() {
    char expr[9 * 11 + 1];
    int i, j, pos, num, isPrime, sum;

    printf("Input number: ");
    if (scanf("%d", &num) != 1)
        return 1;

    printf("Factors are: 1");  // always include 1
    pos = 0;
    expr[pos] = '\0';
    sum = 1;
    for (i = 2; i <= num; i++) {
        if (num % i == 0) {
            isPrime = 1;
            for (j = 2; j * j <= i; j++) {
                if (i % j == 0) {
                    isPrime = 0;
                    break;
                }
            }
            if (isPrime == 1) {
                pos += sprintf(expr + pos, "+%d", i);
                printf(" %d", i);
                sum += i;
            }
        }
    }

    printf("\nSum of its factors: 1%s = %d\n", expr, sum);
    return 0;
}

输出:

Input number: 6
Factors are: 1 2 3
Sum of its factors: 1+2+3 = 6

这是一个更健壮且速度更快的版本,对于非常大的num而言,它没有未定义的行为:

#include <stdio.h>

int main() {
    char expr[9 * 11 + 1];
    int i, pos, num;
    unsigned sum;

    printf("Input number: ");
    if (scanf("%i", &num) != 1)
        return 1;

    printf("Factors are: 1");  // always include 1
    pos = 0;
    expr[pos] = '\0';
    sum = 1;
    for (i = 2; num / i >= i; i++) {
        if (num % i == 0) {
            pos += sprintf(expr + pos, "+%d", i);
            printf(" %d", i);
            sum += i;
            do { num /= i; } while (num % i == 0);
        }
    }
    if (num != 1) {
        pos += sprintf(expr + pos, "+%d", num);
        printf(" %d", num);
        sum += num;
    }
    printf("\nSum of its factors: 1%s = %u\n", expr, sum);
    return 0;
}

测试:

Input number: 0x7fffffff
Factors are: 1 2147483647
Sum of its factors: 1+2147483647 = 2147483648