java阶乘问题,用java求阶乘

发布时间:2022-11-24

本文目录一览:

1、java怎么写求阶乘?
2、[java 的阶乘问题](#java 的阶乘问题)
3、java输入一个数n,计算n的阶乘(5的阶乘=1*2*3*4*5)
4、[Java 求阶乘问题](#Java 求阶乘问题)
5、java阶乘的算法是什么?
6、java阶乘问题?

java怎么写求阶乘?

亲测可用

long jiecheng(int x) {
    long int i, k = 1;
    for (i = 1; i = x; i++) {
        k = k * i;
    }
    return k;
}
int main() {
    long int j, k = 0;
    int i;
    for (i = 1; i = 20; i++) {
        j = jiecheng(i);
        k += j;
    }
    printf("%ld\n", k);
}

输出的结果是 2561327494111820313

扩展资料:

一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。1808年,基斯顿·卡曼引进这个表示法。 亦即 n! = 1 × 2 × 3 × ... × n。阶乘亦可以递归方式定义:0! = 1n! = (n-1)! × n

计算方法:

  • 大于等于1:任何大于等于1 的自然数n 阶乘表示方法:n! = 1 × 2 × 3 × ... × (n-1) × nn! = n × (n-1)!
  • 0的阶乘:0! = 1 参考资料:百度百科——阶乘

java 的阶乘问题

public static void main(String[] args) {
    pintResult();
}
public static void pintResult() {
    System.out.println("请输入要求阶乘的数: ");
    Scanner str = new Scanner(System.in);
    long a = str.nextLong();
    long b = factorial(a);
    System.out.println(a + "! = " + b);
    pintResult();
}
public static long factorial(long n) {
    if ((n == 1) || (n == 0)) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

java输入一个数n,计算n的阶乘(5的阶乘=1*2*3*4*5)

1、首先要理解一下阶乘的公式:

n! = n × (n-1) × (n-2) × ... × 2 × 1,
例如:5! = 5 × 4 × 3 × 2 × 1

#include <stdio.h>
int main() {
    int t = 5, i = 4; // 要是求其他的数的阶乘的话,把t的值改为其他数,
                      // 再把i改为(t-1)就行了
    while (i >= 1) {
        t = t * i;
        i--;
    }
    printf("5的阶乘结果是:%d\n", t);
    return 0;
}

2、运行结果如下:

(略)

3、上面这种方法虽然能求出结果,但是不能求任意的数,也没有考虑到 0! = 1,这种情况,我们来改进一下;

#include <stdio.h>
int main() {
    int n, jc;
    int jiecheng(int j);
    printf("请输入任意一个整数\n");
    scanf("%d", &n);
    jc = jiecheng(n);
    printf("该数的阶乘结果是:%d\n", jc);
    return 0;
}
int jiecheng(int j) {
    int i = j - 1;
    if (j == 0 || j == 1) // 因为0的阶乘是1,1的阶乘也是1
        j = 1;
    while (i > 1) {
        j = j * i;
        i--;
    }
    return (j);
}

4、运行结果如下:

(略)

Java 求阶乘问题

  • 12! = 479001600 (4亿多)
  • 13! = 6227020800(62亿多) 而 Java 中 int 一般是32位的,表示的值的范围是 -21亿多+21亿多,因此从 13 的阶乘开始,int 型就表示不了了。long 的表示范围也有限。

java阶乘的算法是什么?

利用递归算法

public class Factorial {
    public static int factorial(int x) {
        if (x < 0) {
            throw new IllegalArgumentException("x must be >= 0");
        }
        int fact = 1;
        for (int i = 2; i <= x; i++) {
            fact *= i;
        }
        return fact;
    }
    public static void main(String args[]) {
        System.out.print(factorial(10));
    }
}

利用递归函数

public class factorial2 {
    public static int factorial2(int x) {
        if (x < 0) {
            throw new IllegalArgumentException("x must be >= 0");
        }
        if (x <= 1) {
            return 1;
        } else {
            return x * factorial2(x - 1);
        }
    }
    public static void main(String args[]) {
        System.out.print(factorial2(17));
    }
}

利用数组存储

public class Factorial3 {
    static long[] table = new long[21];
    static {
        table[0] = 1;
    }
    static int last = 0;
    public static long factorial(int x) throws IllegalArgumentException {
        if (x >= table.length) {
            throw new IllegalArgumentException("Overflow; x is too large.");
        }
        if (x < 0) {
            throw new IllegalArgumentException("x must be non-negative.");
        }
        while (last < x) {
            table[last + 1] = table[last] * (last + 1);
            last++;
        }
        return table[x];
    }
    public static void main(String[] args) {
        System.out.print(factorial(4));
    }
}

利用 BigInteger 类

import java.math.BigInteger;
import java.util.*;
public class Factorial4 {
    protected static ArrayList table = new ArrayList();
    static {
        table.add(BigInteger.valueOf(1));
    }
    public static synchronized BigInteger factorial(int x) {
        for (int size = table.size(); size <= x; size++) {
            BigInteger lastfact = (BigInteger) table.get(size - 1);
            BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
            table.add(nextfact);
        }
        return (BigInteger) table.get(x);
    }
    public static void main(String[] args) {
        System.out.print(factorial(4));
    }
}

其实方法还有很多,这里提供的也算是个框架形式。分享之。

java阶乘问题?

像你这个要设置一个最小值。用 if, 当它阶乘到了零的时候都应该停止运算。