Factorial using Addition

后端 未结 4 802
眼角桃花
眼角桃花 2021-01-28 14:19

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 twi

相关标签:
4条回答
  • 2021-01-28 14:23

    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.

    0 讨论(0)
  • 2021-01-28 14:28

    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;
       }
    }
    }
    }
    
    0 讨论(0)
  • 2021-01-28 14:32

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

    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.

    0 讨论(0)
  • 2021-01-28 14:43

    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
    --------------
    
    0 讨论(0)
提交回复
热议问题