Topic: 请问N!的阶乘这个类怎么写的?

  Print this page

1.请问N!的阶乘这个类怎么写的? Copy to clipboard
Posted by: Moto
Posted on: 2004-10-25 12:54

请高手帮帮忙。。。

2.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: 齐彬
Posted on: 2004-10-25 14:43

.math中没有这个方法么?

3.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-10-25 15:07

没有吧,这个应该极其简单的就写出来了吧
int N=you give it to me;

for(int i=1;i<N;i++){
N=N*i;
}

return N;

不行吗?(JAVA 基础版)

4.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: qingbo777
Posted on: 2004-10-25 15:18

呵.楼上没有考虑N=0的情况...

5.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-10-25 15:51

0的阶乘就是0Smile

(以上假命题,应该是0的阶乘就是1)

6.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: qingbo777
Posted on: 2004-10-25 15:57

晕.楼上的把我弄糊涂了.特地查了一下书.
N的阶乘公式:
N!=1*2*3*....(N-2)*(N-1)*N 且规定0!=1 1!=1
Eg:8!=1*2*3*4*5*6*7*8

7.Re:请问N!的阶乘这个类怎么写的? [Re: qingbo777] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-10-25 16:14

qingbo777 wrote:
晕.楼上的把我弄糊涂了.特地查了一下书.
N的阶乘公式:
N!=1*2*3*....(N-2)*(N-1)*N 且规定0!=1 1!=1
Eg:8!=1*2*3*4*5*6*7*8


Shock,好象我弄错了,不好意思,规定0!=1 1!=1 是对的,我说的是错的Black Eye

int N=you give it to me;
if(N==0){
N=1;
}else{
for(int i=1;i<N;i++){
N=N*i;
}
}
return N;

这回怎么样呢

别看问题简单,就抓我这粗心的人Black Eye

8.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: qingbo777
Posted on: 2004-10-25 16:20

改的好快Smile.

9.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: 鸡肋男
Posted on: 2004-10-26 10:05

long factorial (int n) {
long result = 1;
if (n != 0) {
result = n * factorial (n - 1);
}
return result;
}

10.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: steven_652
Posted on: 2004-11-01 09:11

还是littledeer1974写的好,鸡肋男写的递归函数如果n很大的话,递归效率很低的,还可能导致JVM栈溢出。

11.Re:请问N!的阶乘这个类怎么写的? [Re: littledeer1974] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2004-11-01 13:18

littledeer1974 wrote:
Shock,好象我弄错了,不好意思,规定0!=1 1!=1 是对的,我说的是错的Black Eye

int N=you give it to me;
if(N==0){
N=1;
}else{
for(int i=1;i<N;i++){
N=N*i;
}
}
return N;

这回怎么样呢

别看问题简单,就抓我这粗心的人Black Eye


程序好像有问题吧???
for的判断应该是i<=N
存放结果的不该用N,应该换个变量单独存放,
我来写一个看看:

int result=1;
for (i=1; i<=n; i++) {
result = result * i;
}
return result;

12.Re:请问N!的阶乘这个类怎么写的? [Re: mitnickcbc] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-01 13:25

恩,for的判断应该是i<=N是对的,
关于存放的结果,应该可以的吧,当然你的写法比较简单明了,也很正统

13.Re:请问N!的阶乘这个类怎么写的? [Re: littledeer1974] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2004-11-01 13:37

littledeer1974 wrote:
恩,for的判断应该是i<=N是对的,
关于存放的结果,应该可以的吧,当然你的写法比较简单明了,也很正统


不可以吧,那样N会不断变大,for语句不会结束.最后溢出.

14.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: wtfg_N188
Posted on: 2004-11-02 10:08

搞错没有哦?
一个超级简单的问题就被你们这样说来说去,真是郁闷!!!
革命尚未成功,同志仍需努力!
public long getJieCheng(int n) {
long result = 1;
if (n == 0 || n == 1) {
result = 1;
}
else {
for (int i = 1; i <= n; i++) {
result = result * i;
}
}
return result;
}

15.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: qingbo777
Posted on: 2004-11-02 10:53

楼上的写法比较正统,但是小鹿的没有错.只是比较怪异.呵呵Smile

16.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: floater
Posted on: 2004-11-02 11:37

No, this is not a 超级简单的问题. Try n = 26.

17.Re:请问N!的阶乘这个类怎么写的? [Re: floater] Copy to clipboard
Posted by: leowu2000
Posted on: 2004-11-02 12:45

呵呵 何止呀 当超出范围的时候 就不能用这种办法了。

记得很早的时候,有个杂志上出过这么一道题,求出100的阶乘。

有兴趣的可以试一试:)

18.Re:请问N!的阶乘这个类怎么写的? [Re: leowu2000] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-02 12:55

恩,我上边的那些回答都是没有想很多的(觉得很简单),看来看似简单的问题里有大学问,看来我们可以试着把次贴置顶,把这个问题讨论的深入一点

19.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-11-02 13:33

给一个估计是正确的答案:

public static String getJieCheng(int n) throws Exception{
if(n<0)throw new Exception("Error input");
BigInteger result = new BigInteger("1");
if (n == 0 || n == 1) {
return result.toString();
}
for (int i = 1; i <= n; i++) {
BigInteger b = new BigInteger(Integer.toString( i ));
result = result.multiply( b );
}
return result.toString();
}


有点投机取巧的嫌疑!

20.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2004-11-02 16:39

判断0和1的情况有点多余.

21.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: leowu2000
Posted on: 2004-11-02 16:50

使用google搜索一下,你会看到有关大数阶乘的算法的,以上的很多算法并不适用。
例如:

好像一般都是用数组然后自己控制进位来实现的,具体也不太清楚了,有兴趣的朋友不妨深入研究一下。

jiechengjieguo.txt (15.98k)

22.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-11-02 19:04

我那段代码能够算出5000!的值的,不信你试试!

23.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-02 19:18

不错,厉害


import java.math.BigInteger;

public class kavinjiecheng {
  kavinjiecheng() {
    super();
  }

  public static String getJieCheng(int n) throws Exception {
    if (n < 0)
      throw new Exception("Error input");
    BigInteger result = new BigInteger("1");
    if (n == 0 || n == 1) {
      return result.toString();
    }
    for (int i = 1; i <= n; i++) {
      BigInteger b = new BigInteger(Integer.toString(i));
      result = result.multiply(b);
    }
    return result.toString();
  }

  public static void main(String[] args) {
    try {
      System.out.println(getJieCheng(5000));
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}


并且得到了,上边的结果

24.Re:请问N!的阶乘这个类怎么写的? [Re: kavinwang] Copy to clipboard
Posted by: leowu2000
Posted on: 2004-11-02 23:31

呵呵 我只是从网上粘贴过来而已 更有意思的是他们相关的讨论 比如 更多的位数、更有效的结果、更少的所需内存和更加快的运算速度:)

25.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: floater
Posted on: 2004-11-02 23:56

Here are some points I know about computing, please google and check other references.

1. Correctness, under what condition we can say the code will work. What if it's not working, throw exception, raise a flag, or something else.
2. We need to consider the overflow, underflow, and error accumulation. In case of overflow/underflow, do we get a flag raised saying so? What are the error bounds from hardware? The error accumulation could eat up the effective digits easily(That's why we complain the weather forecast sucks, Tounge).
3. Even if in theory, it's working, there is no 100% sure in practice. For example, even we can proof a series is convergent, if it converges too slow, then in order to compute a number, we need to add up 10,000 numbers, this is not acceptable either.
4. Even if we can compute a number in some way, we need to think about the cost to do it because there is a chance we might need to do it over and over again, say 1 million times.

These are the basic steps we need to think about when we crunch numbers.

In this case, factorial computing, there are many ways to simplify this
1. one way is to trade speed with space, save some of the results, say 5!, 10!, and others, so we don't need start all over again everytime.
2. Another way is to use floating(Double, etc) point operation(That's why I am floating ... Tounge).
3. Also, there is a mathematical special function called gamma function.
4. Use asympotic approximation.

If my memory serves right, when n is about 30, the asymptotic approximation is used because it's much faster.

n! is the simplest case of a very interesting mathematical subject call special functions, gamma function is just a generalization of n!, there is another version with complex variables. There are others too, I have particular interest in the so-called ecliptic functions. Hundreds of them, more or less like the trig functions, quadratic functions in the high school.

Another general approach is called asymptotics, namely, you give me the error bound and a number, I give you a way to compute, how many terms you need to add together. This is the foundation for the calculus we learned in the college, unfortunately, we learned only the tip of the pyramid in the college(partly because there is no enough time).

There are applications where we need to compute this kind of numbers for millions of times, weather forecast, stealth figher design, auto pilot of planes, space shuttle, as far as I know. A very fantastic subject, but could easily comsume our entire lives.

26.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: AnthonyPig
Posted on: 2004-11-06 23:24

这只是一个比较简单的源码,考虑的条件不多,是用递归的方式实现的,稍后提供一个改进版,将这个看似简单的问题说的更清楚一些。

import java.io.*;

public class FactorialDemo {
public static void main(String[] args) {
System.out.println(String.valueOf(factorial(4)));
}

static int factorial(int n) {
int tmp = 0;

if (n == 0 || n == 1) {
tmp = 1;
}else if (n > 1) {
tmp = n * factorial(n - 1);
}

return tmp;
}
}

27.Re:请问N!的阶乘这个类怎么写的? [Re: Moto] Copy to clipboard
Posted by: chengbd
Posted on: 2004-11-07 10:18

楼上的在开玩笑了,您的代码只是可以显示一下递归编程的思想,没什么实用价值,其运行效率非常低,怎么能说是“但是可以说明阶乘的计算方式”,只能说是计算阶乘的另一种方法而已。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923