Factorial using Addition

2020-05-07 06:27发布

I am attempting to create a C code that finds the factorial of a integer so that I may convert my code to assembly language. My code seems to 'multiply' the second integer twice. i.e. 5*4*4*3... I cannot seem to find out why. Help please!

#define N 5

int main() {

    int j = 0;
    int i = 0;

    int num1 = N;
    int num2 = N - 1;
    int sum = 0;

    while (num2 != 0) {
        while (j < num2) {
            sum += num1;
            j++;
        }
        j = 0;
        printf("%d\n", sum);
        printf("--------------\n");
        --num2;
        num1 = sum;
    }
    printf("--->%d", sum);
}

Erroneous Output:

20
--------------
80
--------------
240
--------------
480
--------------
480

4条回答
太酷不给撩
2楼-- · 2020-05-07 06:37

Here's the machine state, from which you should be able to see why your algorithm isn't right:

enter image description here

PS Another, perhaps better, way to think about this is that your mathematics is wrong. You're doing three multiplications (repetitions of the inner loop--multiplying by an integer using repeated addition). But you also do three additions of the products. Those sums tell you that you're not computing a factorial.

查看更多
相关推荐>>
3楼-- · 2020-05-07 06:42

The steps for the computation are incorrect: it is simpler to start from the low factors to the larger ones.

Here is a corrected version:

#include <stdio.h>

#define N 10

int main() {
    int i, j, num1, sum;

    num1 = 1;
    sum = 1;
    for (i = 1; i <= N; i++) {
        sum = 0;
        for (j = 0; j < i; j++) {
            sum += num1;
        }
        printf("%d! -> %d\n", i, sum);
        printf("--------------\n");
        num1 = sum;
    }
    return 0;
}

Output:

1! -> 1
--------------
2! -> 2
--------------
3! -> 6
--------------
4! -> 24
--------------
5! -> 120
--------------
6! -> 720
--------------
7! -> 5040
--------------
8! -> 40320
--------------
9! -> 362880
--------------
10! -> 3628800
--------------
查看更多
家丑人穷心不美
4楼-- · 2020-05-07 06:53

If you add some intermediate debugging output you'll find where you went wrong:

#include <stdio.h>

#define N 5

int main()
    {
    int j = 0;
    int i = 0;

    int num1 = N;
    int num2 = N - 1;
    int sum = 0;

    while (num2 != 0)
        {
        printf("1 -> num1=%d  num2=%d  sum=%d  j=%d\n", num1, num2, sum, j);

        while (j < num2)
            {
            sum += num1;
            j++;
            }

        printf("2 -> num1=%d  num2=%d  sum=%d  j=%d\n", num1, num2, sum, j);

        j = 0;
        printf("%d\n", sum);
        printf("--------------\n");
        --num2;
        num1 = sum;
        }

    printf("--->%d", sum);
    }

This produces:

1 -> num1=5  num2=4  sum=0  j=0
2 -> num1=5  num2=4  sum=20  j=4
20
--------------
1 -> num1=20  num2=3  sum=20  j=0
2 -> num1=20  num2=3  sum=80  j=3
80
--------------
1 -> num1=80  num2=2  sum=80  j=0
2 -> num1=80  num2=2  sum=240  j=2
240
--------------
1 -> num1=240  num2=1  sum=240  j=0
2 -> num1=240  num2=1  sum=480  j=1
480
--------------
--->480

You can see here that the problem is that the sum value is carried forward from each pass through the loop, when it should really be reset to 0 each time. So add

sum = 0;

at the top of the while loop.

So your final code becomes:

#include <stdio.h>

#define N 5

int main()
    {
    int j = 0;
    int i = 0;

    int num1 = N;
    int num2 = N - 1;
    int sum = 0;

    while (num2 != 0)
        {
        sum = 0;

        while (j < num2)
            {
            sum += num1;
            j++;
            }

        j = 0;
        printf("%d\n", sum);
        printf("--------------\n");
        --num2;
        num1 = sum;
        }

    printf("--->%d", sum);
    }

Best of luck.

查看更多
萌系小妹纸
5楼-- · 2020-05-07 06:56

Check this...

https://code.sololearn.com/cKWo4Cc0GKd1

I've created it using JAVA

class Main {
public static void main(String[] args) {
String str="123456";
int sum=1,t=1;
for(int i=2;i<=str.length();i++){      
   for(int j=0;j<i-1;j++){
     sum=sum+t;
     System.out.println("i: "+i+" t: "+t+" sum: "+sum);
   }
   if(i<str.length()){
     t=sum;
   }
}
}
}
查看更多
登录 后发表回答