如何算出1000的阶乘

解决方案 »

  1.   

    for(int i=1000;i>0;i--)
    {
    int j=1;
    j*=i;
    }// 不要忘记给分 嘿嘿~
      

  2.   

    for(int i=1000;i>0;i--)
    {
    long j=1;
    j*=i;
    }// 不要忘记给分 嘿嘿~
      

  3.   

    是句玩笑话吧?
    这个数字实在太大了,都不知道用什么java类型表示了,如果是比较小的数如:1-170的话,用double可以计算出来170的阶乘:7.257415615307994E306,都不知道是不是正确的,再大就变成:Infinity了。3楼、4楼代码肯定写错了,按照其代码,运行结果必然为1,
    还循环了半天,其效果直接相当于:j=1*1=1;
    ,按其思路,应该这么写:long j=1;
    for(int i=1000;i>0;i--) 

      j*=i; 
    } 不过程序还是算不出来的,最终会看到j=0了。
    这个数实在是太大了。
      

  4.   


    上边的有点问题哟,long也放不下那个数     BigDecimal bd = new BigDecimal(1);
         for(int i=1000;i>0;i--) 
         { 
         bd = bd.multiply(new BigDecimal(i));
         }
         System.out.println(bd);
      

  5.   

    恩,值太大,无法表示。package src;public class JieC {
    public static void main(String[] args) {
    System.out.println(getValue(1000));
    }
    public static long getValue(long var){
    long t;
    if (var == 1)
    t = 1;
    else
    t = var * getValue(var - 1);
    System.out.println("var=>[" +var + "] t =>[" + t +  "]");
    return t;
    }
    }
      

  6.   

    LS的算法是对的 但是Long根本装不下1000的阶乘,这个数实在太大了 以至于自然界里都找不到与之配对的相关事物  这个数字对人类来说完全没有意义的。如果计算小数量级的阶乘还是用递归吧。
    public class FabPlus {
     public static long fabPlus(long index) {
    if(index == 1)
    return 1;
    else {
    return index * fabPlus(index - 1);
    }
     }
    public static void main(String args[]) {
    System.out.println(FabPlus.fabPlus(n));  //n必须小于或者等于25,否则溢出。
    }
    }
      

  7.   

    用斯特林公式计算的话,最大的 int 数的阶乘是2147483647! = 1.128460650183488 * 10^19107526488
      

  8.   

    提供两种算法吧,都不是精确的1,斯特林公式,算法复杂度 O(1):n! ≈ sqrt(2 * pi * n) * pow(n / e, n)pi 是圆周率;e 是自然对数的底
    2,将乘法化为加法的公式,算法复杂度 O(N):     n! = 1 * 2 * 3 * ... * (n - 1) * n
    log(n!) = log(1) + log(2) + log(3) + ... + log(n - 1) + log(n)
         n! = 10^(log(1) + log(2) + log(3) + ... + log(n - 1) + log(n))
      

  9.   

    这个问题的难点在于如何存放大数。用long型肯定是存放不了1000!的结果的,Java提供了BigInteger类用于实现超大数的存放,不过效率很低。BigInteger result = BigInteger.ONE;
    for (int i = 1; i <= 1000; i++)
    result = result.multiply(new BigInteger("" + i));
    System.out.println("result = " + result.toString());  
    如果要求高效,需要自定义数据结构,比如链表。
      

  10.   

    这个求出来是大数,就算是double类型也放不下
    好像 求出来 放到数组里面,具体怎么实现……观望中…………
      

  11.   

    用的上这么复杂吗?一个数组一个循环足以.
    比如我计算5的阶乘,先求5!的位数为3
    数组初始值为{0,0,1}
    x2: {0,0,2}
    x3: {0,0,6}
    x4: {0,0,24} 进位=> {0,2,4}
    x5: {0,10,20} 进位=> {0,12,0} 进位=> {1,2,0}
    最后输出120
      

  12.   

    不好意思 忘记考虑数位大小了 应该用大整数来做而不是long
      

  13.   

    本想简单胡乱的测试一下,但花了很长时间public class Test {
    static int[] v = new int[500];
    static int ten = 0;
    static int length = 1; public static void m(int t) {
    while (t%10==0) {
    t /= 10;
    ten++;
    }
    for (int i=0; i<length; i++) v[i]*=t;
    int value;
    for (int i=0; i<length; i++) {
    value = v[i];
    v[i] = 0;
    t = i;
    while (value>0) {
    if (length==t) length++;
    v[t] = v[t]+value%1000000;
    value /= 1000000;
    t++;
    }
    }
    }

    public static void n(int t) { for (int i=1; i<=t; i++) m(i); } public static void main(String[] args) {
    v[0] = 1;
    long time = System.currentTimeMillis();
    n(1000);
    System.out.println(System.currentTimeMillis()-time); int i=v.length-1;
    while (v[i]==0) i--;
    for (; i>-1; i--) {
    System.out.print(v[i]/100000);
    System.out.print(v[i]/10000%10);
    System.out.print(v[i]/1000%10);
    System.out.print(v[i]/100%10);
    System.out.print(v[i]/10%10);
    System.out.print(v[i]%10);
    }
    for (i=0; i<ten; i++) {
    System.out.print('0');
    }
    }
    }
      

  14.   

    Ok,一个循环不够, 两个足够了,时间复杂度O(n^2)
    矩阵?不知道你哪个大学的,被乘数1000,用不着矩阵.
      

  15.   

    给出程序代码和输出结果,来这里是研究程序的,不是来吵架的.
    class PowerOf1000
    {  public static void main(String[] args)
      {
        int[] digits = new int[2568];
        int max_digit = 2567;
        digits[max_digit] = 1;
        for (int d=2;d<=1000;d++)
        {
          for (int k=max_digit; k<digits.length; k++)
            digits[k] *= d;      int k = digits.length-1;
          while (k>=max_digit)
          {
            if (digits[k]>10)
            {
              digits[k-1] += digits[k] / 10;
              digits[k] = digits[k] % 10;
              if (k-1<max_digit) max_digit = k-1;
            }
            k--;
          }
        }
        for (int i=max_digit; i<digits.length; i++)
        {
          System.out.print(digits[i]);
        }
        System.out.println();
      }
    }输出:
    4023872600770937735437024339230039857193748642107146325437999
    10429938512398629020592044208486969404800479988610197196058631
    666872994808558901323829669944590997424504087073759918823627727
    1887325197795059509952761208749754624970436014182780946464962910
    5639388743788648733711918104582578364784997701247663288983595573
    5432513185323958463075557409114262417474349347553428646576611667
    7973966688202912073791438537195882498081268678383745597317461360
    8537953452422158659320192809087829730843139284440328123155861103
    69768013573042161687476096758713483120254785893207671691324484262
    36131412508780208000261683151027341827977704784635868170164365024
    15369139828126481021309276124489635992870511496497541990934222156
    68325720808213331861168115536158365469840467089756029009505376164
    75847728421889679646244945160765353408198901385442487984959953319
    10172335555660213945039973628075013783761530712776192684903435262
    52000158885351473316117021039681759215109077880193931781141945452
    57223865541461062892187960223838971476088506276862967146674697562
    91123408243920816015378088989396451826324367161676217916890977991
    19037540312746222899880051954444142820121873617459926429565817466
    283029555702990243241531816172104658320367869061172601587835207515
    1628422554026517048330422614397428693306169089796848259012545832716
    82264580665267699586526822728070757813918581788896522081643483448259
    932660433676601769996128318607883861
    5027946595513115655203609398818061213
    85586003014356945272242063446317974605
    9468257310379008402443243846565724501
    440282188525247093519062092902313649327349
    75655139587205596542287497740114133
    4696271542284586237738753823048386568897646192
    73838149001407673104466402598994
    90222221765904339901886018566526485061799702356193897017
    860040811889729918311021
    17122984590164192106888438712185564612496079872290851
    9296819372388642614839657382291
    123125024186649353143970137428531926649875337218940694
    281434118520158014123344828015
    0513996942901534830776445690990731524332782882698646027
    89864321139083506217095002597389
    8635542771967428222487575867657523442202075736305694988
    250879689281627538488633969099598
    26280956121450994871701244516461260379029309120889086
    942028510640182154399457156805941
    87274899809425474217358240106367740459574178516082
    923013535808184009699637252423056
    085590370062427124341690900415369010593398383577
    7939410970027753472
    0000000000000000000000000000000000000000000000000
    000000000000000000000000000000
    0000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000
    0000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000
      

  16.   

    以前也有人提过这样的问题,但终无程序结果。此贴收藏,并推荐,感谢 runffer_yang  给出的程序。
      

  17.   

    上面的算法可以优化,我是10进1,也可以256,65536进位,这样循环长度减少.
    To kokobox:我说话可能尖刻点,别介意.
      

  18.   

    不是有BigInteger吗,只要内存够用,再大的数也能算
    import java.math.BigInteger;public class Jc1000 {
        public static void main(String[] args) {
            BigInteger x = BigInteger.valueOf(1);
            for(int i=2; i<=1000; i++) {
                x = x.multiply(BigInteger.valueOf(i));
            }
            System.out.println(x);
        }
    }
    结果:
    40238726007709377354370243392300398571937486421071
    46325437999104299385123986290205920442084869694048
    00479988610197196058631666872994808558901323829669
    94459099742450408707375991882362772718873251977950
    59509952761208749754624970436014182780946464962910
    56393887437886487337119181045825783647849977012476
    63288983595573543251318532395846307555740911426241
    74743493475534286465766116677973966688202912073791
    43853719588249808126867838374559731746136085379534
    52422158659320192809087829730843139284440328123155
    86110369768013573042161687476096758713483120254785
    89320767169132448426236131412508780208000261683151
    02734182797770478463586817016436502415369139828126
    48102130927612448963599287051149649754199093422215
    66832572080821333186116811553615836546984046708975
    60290095053761647584772842188967964624494516076535
    34081989013854424879849599533191017233555566021394
    50399736280750137837615307127761926849034352625200
    01588853514733161170210396817592151090778801939317
    81141945452572238655414610628921879602238389714760
    88506276862967146674697562911234082439208160153780
    88989396451826324367161676217916890977991190375403
    12746222899880051954444142820121873617459926429565
    81746628302955570299024324153181617210465832036786
    90611726015878352075151628422554026517048330422614
    39742869330616908979684825901254583271682264580665
    26769958652682272807075781391858178889652208164348
    34482599326604336766017699961283186078838615027946
    59551311565520360939881806121385586003014356945272
    24206344631797460594682573103790084024432438465657
    24501440282188525247093519062092902313649327349756
    55139587205596542287497740114133469627154228458623
    77387538230483865688976461927383814900140767310446
    64025989949022222176590433990188601856652648506179
    97023561938970178600408118897299183110211712298459
    01641921068884387121855646124960798722908519296819
    37238864261483965738229112312502418664935314397013
    74285319266498753372189406942814341185201580141233
    44828015051399694290153483077644569099073152433278
    28826986460278986432113908350621709500259738986355
    42771967428222487575867657523442202075736305694988
    25087968928162753848863396909959826280956121450994
    87170124451646126037902930912088908694202851064018
    21543994571568059418727489980942547421735824010636
    77404595741785160829230135358081840096996372524230
    56085590370062427124341690900415369010593398383577
    79394109700277534720000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    000000000000000000
      

  19.   

    http://topic.csdn.net/u/20081107/12/bbe2be15-2a95-48ef-80db-21cd756ee7c9.html?2095168368这个里面有代码,是别人写的,C++的,不过转成JAVA也比较简单.
      

  20.   

    如果你去微软或者谷歌面试,那么很遗憾,面试官不会让你用BigInteger或者BitDecimal,
    事实上这个题目不是考用什么类型,而是考高精度计算的算法+数据结构.
      

  21.   

    来学习了!
    的确不是那么简单的for语言或着long就可以了。
      

  22.   

    def fact(n)
        if n == 1
    1
        else
    n * fact(n-1)
        end
    endprint fact(1000)
      

  23.   

    Windows用户下载Python,Python是一种脚本语言,可进行任意精度的数学计算 
    http://www.activestate.com/Products/ActivePython/ 
    以下为Python源码,用TAB或空格替换~ 
    #!/usr/bin/python 
    def foo(a) 
    ~ans = 1 
    ~for i in range(1, a+1): 
    ~~ans*=i 
    ~return ans print foo(1000) 
      

  24.   

    数值计算基础问题。给个计算1! + 2! + ... + n!的,O(n*n)//calculate 1! + 2! + ... + n! where n is a positive integer#include <iostream>
    using namespace std;
    const long maxlen = 100000;int main()
    {
        cout << "Input n: ";
        int n = 0;
        cin >> n;
        cout << "1! + 2! + ... + " << n << "! = ";
        
        int result[maxlen] = {0}, len_r = 0;
        int temp[maxlen] = {0}, len_t = 0;
        
        temp[0] = 1;
        len_t = 1;
        
        for (int x = 1; x <= n; x++)
        {
            temp[0] = temp[0] * x;
            for (int j = 1; j < len_t; j++)
            {
                temp[j] = temp[j] * x + temp[j-1] / 10;
                temp[j-1] = temp[j-1] % 10;
            }
                
            while (temp[len_t-1] > 10)
            {
                temp[len_t] = temp[len_t-1] / 10;
                temp[len_t-1] = temp[len_t-1] % 10;
                len_t++;
            }
            
            for (int i = 0; i < len_t; i++)
            {
                result[i] = result[i] + temp[i];
                result[i+1] = result[i+1] + result[i] / 10;
                result[i] = result[i] % 10;
            }
            
            len_r = len_t;
            while (result[len_r] > 0)
            {
                result[len_r+1] = result[len_r] / 10;
                result[len_r] = result[len_r] % 10;
                len_r++;
            }
            
        }
        
        int k = len_r - 1;
        while (result[k] == 0 && k > 0) k--;
        for (; k >= 0; k--) cout << result[k];
        cout << endl << endl << "Total Digits: " << len_r << endl;
        return 0;
    }
      

  25.   

    先定义一个数组来存放结果,使它就像是一个数字一样,每次满足10就向前进一位。
    然后用for循环来执行,存放结果到那个数组中去
      

  26.   

    java 代码
    long j=1;
    for(int i=1000;i>0;i--) 

      j*=i; 
    } c语言 代码long j=1;
    for(int i=1000;i>0;i--) 

      j*=i; 
    } web代码<script language="javascripte">
    var i,j=1;
     function a(i,j)
    {
    while(i<1000)
    j*=i;}
    </script>在<body>调用 a()
    </body>
      

  27.   

    看清楼主的题目
    “发表于:2009-04-11 19:50:20 楼主
    如何算出1000的阶乘”
    你那只眼睛看到说不准用BigInteger了?
    别以为只有你自己会用数组实现。
    有现成的解决方案你不用,自找麻烦
      

  28.   

    由于 int 类型数据长度的限制,采用每位数字与乘数相乘的做法我认为并不好,
    也就是说使用该方法做出的阶乘计算受到 int 类型最大数字的限制,并不能称
    为大数计算。所谓的大数计算不管是 200 亿的阶乘还是 2 的阶乘在计算上除了内存空间的限
    制之外,不应受到编程语言数值类型最大数的限制。
      

  29.   

    #include<stdio.h> 
    #define N 1000 //要算的阶乘数 N!
    long s[N]={1,1},n=N,t=2,a=1,b=0; 
    int main()

        for(; a <= *s || (++t <= n ? (b = 0, a = 1) : 0); (*s == a++ && b) ? (*s)++ : 0) 
            s[a]=(b+=s[a]*t)%10000,b/=10000; 
        for(printf("%d",s[*s]);--*s>0;)
    printf("%04d",s[*s]); 
    printf("\n");
        return 0; 

    这是一个北大大二的女生写的,我看不懂。可以很快求出N的阶乘!
      

  30.   

    给你一个关于求PI的任意高阶精度值得算法,你看看里面的方法!
    #include <iostream>
    #include <fstream>
    #include  <ctime>
    #include "windows.h"
    using namespace std;void arctg(int * arctgx, int x, int len)
    {
    int n = 1, s = 1,c = 4,d = 0;
    int i, *t, *a, eps = 0;
    t = new int[len+1];
    a = new int[len+1];
    for(i=0;i<len;i++)
    {
    t[i] = c / x;
    d = c % x;
    c = d*10;
    }
    while(eps<len-1)
    {
    d = 0;
    for(i=0;i<len;i++)
    {
    c = t[i]+d*10;
    a[i] = c / n;
    d = c % n;
    }
    for(i=1;i<len;i++)
    {
    if (a[i])
    {
    eps = i;
    break;
    }
    else eps++;
    }
    if(s==1)
    {
    for(i=len-1;i>0;i--)
    {
    c = arctgx[i] + a[i];
    arctgx[i] = c%10;
    arctgx[i-1]+= c/10;
    }
    }
    else
    {
    for(i=len-1;i>0;i--)
    { arctgx[i]-=a[i];
    if (arctgx[i]<0)
    {
    arctgx[i] += 10;
    arctgx[i-1]-=1;
    }
    }
    }
    n = n+2;
    s = -s;
    d = 0;
    for(i=0;i<len;i++)
    {
    c = t[i]+d*10;
    t[i] = c / x;
    d = c % x;
    }
    d = 0;
    for(i=0;i<len;i++)
    {
    c = t[i]+d*10;
    t[i] = c / x;
    d = c % x;
    }
    }
    delete []t;
    delete []a;
    }
    int main()
    {
    DWORD dwStart,dwStop; //调用系统功能来计时
    dwStart=GetTickCount(); //开始计时
    int c, d, len;
    int decimal, *arctg5, *arctg239, *pi;
    cout << "请输入所要计算的π的精度值:";
    cin >> decimal;
    len = decimal+10;
    arctg5 = new int[len+1], arctg239 = new int[len+1], pi = new int[len+1];
    cout << "计算进行中,请稍等……" << endl;
    for(int i=0;i<len;i++)
    {
    arctg5[i] = 0;
    arctg239[i] = 0;
    }
    arctg(arctg5, 5, len);
    arctg(arctg239, 239, len);
    d = 0;
    for(i=len-1;i>=0;i--)
    {
    c = arctg5[i]*4 + d;
    pi[i] = c % 10;
    d = c / 10;
    }
    delete []arctg5;
    for(i=len-1;i>0;i--)
    {
    pi[i]-=arctg239[i];
    if (pi[i]<0)
    {
    pi[i] += 10;
    pi[i-1]-=1;
    }
    }
    delete []arctg239;
    cout<< "π= " << pi[0] << '.';
        for(i=1;i<=decimal;i++)
    {
    cout<<pi[i];
    }
    delete []pi;
    dwStop=GetTickCount(); //计时结束
    cout<<endl;
    cout<<"计算用时:"<<(double)(dwStop-dwStart)/1000<<"秒"<<endl;
    return 0;
    }
      

  31.   

    for循环啊!
    不过要注意数据的类型!
    double int 可能可以
      

  32.   

    一条print语句搞定,还用BigInterger?
    我没说就我会用,我觉得这是小学或者初中奥赛级别.
      

  33.   

    自定义数据类型&运算符重载
    以下是C#的实现,转成java就成,运算符重载那块可用一个方法代替!struct SuperNumber
    {
        //底数
        public double Num;
        //指数
        public int Index;    public static SuperNumber operator *(SuperNumber sn1,SuperNumber sn2)
        {
            SuperNumber sn=new SuperNumber();
            sn.Num=sn1.Num*sn2.Num;
            sn.Index=sn1.Index+sn2.Index;        where(sn.Num>10)
            {
                sn.Num/=10;
                sn.Index++;
            }        return sn;
        }
        
        public override string ToString()
        {
            return Num.ToString()+" X 10 的"+Index+"次方";
        }
    }class Program
    {
        SuperNumber tmp=new SuperNumber();
        SuperNumber sn=new SuperNumber();
        sn.Num=1;
        sn.Index=0;
        
        for(int i=2;i<=800;i++)
        {
            tmp.Number=i;
            sn=sn*tmp;
        }    System.Console.WriteLine(sn.ToString());
        
        System.Console.ReadKey();
    }
      

  34.   

    都有没有数学头脑,这个结果恐怕就其位数都要超出 long 吧,讨论这么大的数字有意义吗?
      

  35.   

    8楼的方法真不错,学习了。以前虽然见过,但是并没有留意,BigDecimal类的功能真是太强大了。
      

  36.   

    一个内存单元存放8个字节。我们把想要存储的内容按字节存放在内存中。输出时输出连续的内容。
    同样道理,我们也可以把一个long型规定的长度作为一个假定的内存单元,将结果放在一个数组中。
    然后按顺序输出数组。
      

  37.   

    网上还有 4 行的 C 代码。那个叫牛。
    下面是我弄的。import java.util.Arrays;
    import java.text.DecimalFormat;/**
     * 求 1000 阶乘(用自定义的 BigInteger)
     */
    public class TestBigInteger {    public static void main(String[] args) {
            BigInteger i = new BigInteger(1);        for (int n = 2; n <= 1000; n++) {
                i.multiply(n);
            }        System.out.println(i);
            System.out.println(i.toString().length());
        }
    }// 自定义的 BigInteger
    class BigInteger {    // 初始化数组。每个元素可以包含 9 位数字,所以初始化有 90 位
        private int ints[] = new int[10];    // 达到或超过这个值就进位
        private static final int UPPER_BOUND = 1000000000;    public BigInteger(int value) {
            ints[0] = value;
        }    public BigInteger(BigInteger value) {
            ints = Arrays.copyOf(value.ints, value.ints.length);
        }    public void multiply(int n) {
            BigInteger that = new BigInteger(this);
            for (int i = 0; i < n - 1; i++) {
                add(that);
            }
        }    private void add(BigInteger bigInteger) {
            int[] ints2 = bigInteger.ints;        for (int i = 0; i < ints2.length; i++) {
                ints[i] += ints2[i];
                checkAndCarry(i);
            }        checkAndIncrease();
        }    // 检查指定位置的值。有必要的话就进位。
        private void checkAndCarry(int i) {
            if (ints[i] >= UPPER_BOUND && i < ints.length - 1) {
                ints[i] -= UPPER_BOUND;
                ints[i + 1]++;
            }
        }    // 检查最后一个元素的值。达到或超过进位大小则增加数组长度。
        private void checkAndIncrease() {
            if (ints[ints.length - 1] > UPPER_BOUND) {
                int[] newints = Arrays.copyOf(ints, ints.length + 1);
                newints[ints.length - 1] -= UPPER_BOUND;
                newints[ints.length]++;
                ints = newints;
            }
        }    @Override
        public String toString() {
            DecimalFormat f = new DecimalFormat("000000000");
            StringBuffer sb = new StringBuffer();        for (int each : ints) {
                sb.insert(0, f.format(each));
            }        while (sb.charAt(0) == '0') {
                sb.delete(0, 1);
            }        return sb.toString();
        }
    }
      

  38.   

    for(int i=0;i<=1000;i++)
    {
    int sum=i++;
    i=sum*i;
    }
    System.out.print(i);