因子挑战(初学者的挑战)

时间:2012-04-04 18:36:43

标签: java math factorial

我尝试了以下挑战:

鉴于前几个因素:

1! = 1

2! = 2 x 1 = 2

3! = 3 x 2 x 1 = 6

4! = 4×3×2×1 = 24

前15个阶乘的总和是多少,不包括0!?

我在Java中的解决方案如下:

public class Factorial
{
   public static void main(String[] args)
   {
     int sum = 0;    
     int multi = 1;
        for (int i=1;i<=15;i++)         
        {
        multi = multi*i;
        sum = multi+sum;     
        }     
     System.out.print(sum);
   }
}

我验证了前7个阶乘的解决方案,但它是否适用于前15个?

3 个答案:

答案 0 :(得分:6)

由于integer overflow,它不适用于前15个因子。正确的答案是1401602636313,它超过了Java的int界限2147483647.您可以使用范围为9223372036854775807或BigIntegerlong

答案 1 :(得分:4)

不,它不适用于15.使用时间长。此外,您可以在循环内移动print语句,以检查它开始失败的位置。我想在这种情况下它是13。

答案 2 :(得分:1)

public static void printFactorials (int max) {
    long fac = 1;
    long sum = 0;
    for (int i = 1; i <= max; fac *= ++i) {
        System.out.println(String.format("Factorial(%2d)=%d", i, fac));
        sum += fac;
    }
    System.out.println(String.format("Sum of Factorials(1 to %2d)=%d", max, sum));
}

给你

Factorial( 1)=1
Factorial( 2)=2
Factorial( 3)=6
Factorial( 4)=24
Factorial( 5)=120
Factorial( 6)=720
Factorial( 7)=5040
Factorial( 8)=40320
Factorial( 9)=362880
Factorial(10)=3628800
Factorial(11)=39916800
Factorial(12)=479001600
Factorial(13)=6227020800
Factorial(14)=87178291200
Factorial(15)=1307674368000
Sum of Factorials(1 to 15)=1401602636313

long在21开始失败,下一步是BigInteger

public static void printRlyBigFactorials (int max) {
    BigInteger fac = BigInteger.ONE;
    BigInteger sum = BigInteger.ZERO;
    for (int i = 1; i <= max; ++i) {
        fac = fac.multiply(BigInteger.valueOf(i));
        sum = sum.add(fac);
        System.out.println(String.format("Factorial(%2d)=%d", i, fac));
    }
    System.out.println(String.format("Sum of Factorials(1 to %2d)=%d", max, sum));
}

这几乎可以无限期地运作,并且可以给你带来花哨的结果,如:

Sum of Factorials(1 to 500)=
1222581999810786173488382263893486121736784649845260488587055662127413631697
9142090995417259894466676137016242713788312106218384177808117660024733369428
7060019503701220190523381023699528466605036804597249531428694859689049295904
5138704466475196055082304091214424335155644013903958356823605973150159110295
5787828433482529258832635575855564789877227459384652114477297831606218655683
9245588828671235437927278554210732477499719243692398907465554636521289870187
5799458234466791378320221140358905721655475503366304295011345436395868843079
5463780536087239619245051615759218253091986494512882003123090598805090122753
7135918455294416676103707115038417384516670399033063650562275830354903359872
0775172343137459008549361297203752431405977559950082400276439557196120290170
5516606073135650288107937474531851451830365876392678959480905477335825506233
3795849463603798966643420966668878072957663827751761832039623225350606860709
6479320263132522604054741925038640750661849690108363701190203548476572823422
7743271977187818002695582046473911765828511673121820261887951566200568565033
40092247479478684738621107994804323593105039052556442336528920420940313