3

ulong値を大きくする必要がありますが、正しい出力が得Math.Powられません。

私のC#コード:

 ulong a = 9123456789;
 ulong b = (ulong)Math.Pow(a, 9999);
 Console.WriteLine(b);

画面への出力は 0 です。この計算を実行して正しい結果を得るにはどうすればよいですか?

4

2 に答える 2

9

はい、特殊なBigIntegerクラスを使用してその数を計算することは可能です。ushort幅が 16 ビットの でこれを行うのは絶望的です。

using System;
using System.Collections;
using System.Numerics;

public class Test 
{
    public static void Main(string[] args)
    {
        BigInteger a = 9123456789;
        BigInteger b = BigInteger.Pow(a, 9999);

        //Output this number if you're feeling lucky.
        Console.WriteLine(b);
    }
}

出力

43056151396124937171542222696555900626431494491043369510338912076154406943108
..
8614367506747618876018379290109

ちなみに、結果を格納するには 330,837 ビットが必要です。

于 2016-12-08T19:11:53.037 に答える
0

非常に大きな数を扱う場合は、BigIntegerを調べてください。

于 2016-12-08T19:10:03.710 に答える