简体   繁体   中英

Calculate factorial of 50 using array only in java

I'm a total beginner of java. I have a homework to write a complete program that calculates the factorial of 50 using array. I can't use any method like biginteger. I can only use array because my professor wants us to understand the logic behind, I guess... However, he didn't really teach us the detail of array, so I'm really confused here.

Basically, I'm trying to divide the big number and put it into array slot. So if the first array gets 235, I can divide it and extract the number and put it into one array slot. Then, put the remain next array slot. And repeat the process until I get the result (which is factorial of 50, and it's a huge number..)

I tried to understand what's the logic behind, but I really can't figure it out.. So far I have this on my mind.

import java.util.Scanner;
class Factorial
{
    public static void main(String[] args)
    {
        int n;
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter n");
        n = kb.nextInt();
        System.out.println(n +"! = " + fact(n));
    }

    public static int fact(int n)
    {
        int product = 1;
        int[] a = new int[100];
        a[0] = 1;



        for (int j = 2; j < a.length; j++)
        {
            for(; n >= 1; n--)
            {
                product = product * n;

                a[j-1] = n;
                a[j] = a[j]/10;
                a[j+1] = a[j]%10;

            }

        }
        return product;
    }
}

But it doesn't show me the factorial of 50. it shows me 0 as the result, so apparently, it's not working.

I'm trying to use one method (fact()), but I'm not sure that's the right way to do. My professor mentioned about using operator / and % to assign the number to the next slot of array repeatedly. So I'm trying to use that for this homework.

Does anyone have an idea for this homework? Please help me!

And sorry for the confusing instruction... I'm confused also, so please forgive me.

FYI: factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000

Try this.

static int[] fact(int n) {
    int[] r = new int[100];
    r[0] = 1;
    for (int i = 1; i <= n; ++i) {
        int carry = 0;
        for (int j = 0; j < r.length; ++j) {
            int x = r[j] * i + carry;
            r[j] = x % 10;
            carry = x / 10;
        }
    }
    return r;
}

and

int[] result = fact(50);
int i = result.length - 1;
while (i > 0 && result[i] == 0)
    --i;
while (i >= 0)
    System.out.print(result[i--]);
System.out.println();
// -> 30414093201713378043612608166064768844377641568960512000000000000

Her's my result:

50 factorial - 30414093201713378043612608166064768844377641568960512000000000000

And here's the code. I hard coded an array of 100 digits. When printing, I skip the leading zeroes.

public class FactorialArray {

    public static void main(String[] args) {
        int n = 50;
        System.out.print(n + " factorial - ");

        int[] result = factorial(n);

        boolean firstDigit = false;
        for (int digit : result) {
            if (digit > 0) {
                firstDigit = true;
            }

            if (firstDigit) {
                System.out.print(digit);
            }
        }

        System.out.println();
    }

    private static int[] factorial(int n) {
        int[] r = new int[100];
        r[r.length - 1] = 1;
        for (int i = 1; i <= n; i++) {
            int carry = 0;
            for (int j = r.length - 1; j >= 0; j--) {
                int x = r[j] * i + carry;
                r[j] = x % 10;
                carry = x / 10;
            }
        }
        return r;
    }

}

how about:

int[] arrayOfFifty = new int[50];
//populate the array with 1 to 50
for(int i = 1; i < 51; i++){
    arrayOfFifty[i-1] = i;
}

//perform the factorial
long result = 1;
for(int i = 0; i < arrayOfFifty.length; i++){
   result = arrayOfFifty[i] * result;
}

Did not test this. No idea how big the number is and if it would cause error due to the size of the number.

Updated. arrays use ".length" to measure the size.

I now updated result to long data type and it returns the following - which is obviously incorrect. This is a massive number and I'm not sure what your professor is trying to get at. -3258495067890909184

How about:

public static BigInteger p(int numOfAllPerson) {

    if (numOfAllPerson < 0) {

        throw new IllegalArgumentException();

    }

    if (numOfAllPerson == 0) {

        return BigInteger.ONE;

    }

    BigInteger retBigInt = BigInteger.ONE;

    for (; numOfAllPerson > 0; numOfAllPerson--) {

        retBigInt = retBigInt.multiply(BigInteger.valueOf(numOfAllPerson));

    }

    return retBigInt;

}

Please recall basic level of math how multiplication works?

2344
X 34

= (2344*4)*10^0 + (2344*3)*10^1 = ans


2344
X334

= (2344*4)*10^0 + (2344*3)*10^1  + (2344*3)*10^2= ans

So for m digits X n digits you need n list of string array.

Each time you multiply each digits with m. and store it.

After each step you will append 0,1,2,n-1 trailing zero(s) to that string.

Finally, sum all of n listed string. You know how to do that.

So up to this you know m*n

now it is very easy to compute 1*..........*49*50.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM