2012-04-23 18 views
3

私はBigInteger形式で書く必要がある方程式を持っています。それはforループ内にある必要があります。これは私がこれまで行ってきたことですが、BigIntegerをどのように使用するのか分かりません。これは、forループであると記述された式である:私は(3 * iはI + 3 * I 1 *)/ 30独自の式のBigIntegerクラスをどのように実装しますか?

public static BigInteger[] nthtetranum(int n) //This is the method using the simple formula for tetra number. 
{ 

    BigInteger[] nth = new BigInteger[n]; 

    for(int i = 0; i <nth.length; i++) 
    { 
     //nth[i] = i*(i+1)*(2*i+1)*(3*i*i+3*i-1)/30; 
     nth[i] = 

    } 
    return nth; 
+0

質問がありますか? – ControlAltDel

+0

名前が 'nth'の2つの変数があります。あなたは何を達成しようとしていますか? –

+0

申し訳ございません。私はコードを修正した。私はBigInteger形式でi *(i + 1)*(2 * i + 1)*(3 * i * i + 3 * i-1)/ 30を必要とします – Mike

答えて

2
BigInteger two = new BigInteger("2"); 
BigInteger three = new BigInteger("3"); 
BigInteger I = new BigInteger(""+i); // "I" is a bigint version of "i" 
nth[i] = I 
    .multiply(I.add(BigInteger.ONE)) 
    .multiply(I.multiply(two).add(BigInteger.ONE)) 
    .multiply(I.multiply(I).multiply(three).add(I.multiply(three)).subtract(BigInteger.ONE)) 
    .divide(new BigInteger("30")); 

この表現は醜いですが、それもiの「ボーダーライン」の値のオーバーフローではないでしょう。

0

*(2 * I + 1)*(I + 1)* Um。通常の方法ですか?

nth[i] = BigInteger.valueOf(i) 
    .multiply(BigInteger.valueOf(i+1)) 
    .multiply(BigInteger.valueOf(2*i + 1)) 
    .multiply(BigInteger.valueOf(3L*i*i + 3*i - 1)) // should fit in a long 
    .divide(BigInteger.valueOf(30)); 
+0

私はそうしました。答えの最初のカップルは私の元の式とは異なります。最初の質問はbigintegerを使用していなかったからですか? – Mike

+0

はい、おそらく。これはあなたに正しい答えを与えるはずです。 –

関連する問題