在 C# 中使用 BigInteger

发布于 2024-10-05 16:14:24 字数 548 浏览 2 评论 0原文

我正在测试 BigIntegers。

当我取一个大奇数并将其除以 2 时,我得到一个整数作为答案,没有任何迹象表明它无法精确地除以该数字。

所以第一个问题是我怎么知道两个数字可以整除。

然后我用一个小数字测试了它,这个代码:

        string myNumberAsString = "25";
        System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
        byte[] myNumberAsByteArray = encoding.GetBytes(myNumberAsString);
        BigInteger myNumber = new BigInteger(myNumberAsByteArray);
        Console.WriteLine(myNumber / 2);

给出结果 6809。有人知道为什么或者可以看到我的代码有什么问题吗?

我正在使用 BigInteger 的 .net 4.0 实现

I am testing out BigIntegers.

When I take a large odd number and divide it by 2, I get an integer as an anwser, without any indication that it could not divide the number exactly.

So first question is how do I know that two numbers divide exactly.

I then tested it with a small number, an this code:

        string myNumberAsString = "25";
        System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
        byte[] myNumberAsByteArray = encoding.GetBytes(myNumberAsString);
        BigInteger myNumber = new BigInteger(myNumberAsByteArray);
        Console.WriteLine(myNumber / 2);

Gives the result 6809. Anybody know why or can see what is wrong with my code?

I am using the .net 4.0 implementation of BigInteger

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无尽的现实 2024-10-12 16:14:25

如果必须从数字的字符串表示形式进行转换,请使用 BigInteger.TryParse 或 BigInteger.Parse。

但无论您如何实例化 BigInteger,您都可以使用模数学来确定一个数字是否可以被另一个数字整除。例如,如果您想查看 someNumber 是否能被 2 整除,那么只需验证 (someNumber % 2) == 0 (即 sumNumber / 2 有余数为零)。这适用于任何整数分母。只需将 2 替换为您想要测试的分母即可。不过,对于 BigInteger,您可能应该使用 DivRem 方法而不是 % 运算符。

If you must convert from a string representation of a number, use BigInteger.TryParse or BigInteger.Parse.

But regardless of how you instantiate your BigInteger, you can determine if a number is evenly divisible by another by using modular math. For example, if you want to see if someNumber is divisible by 2, then just verify that (someNumber % 2) == 0 (i.e., sumNumber / 2 has a remainder of zero). This works for any integer denominator. Just replace 2 with which ever denominator you want to test. With BigInteger, though, you should probably use the DivRem method instead of the % operator.

始终不够爱げ你 2024-10-12 16:14:25

您构建 BigInteger 的方式过于复杂 - 该框架提供了来自 byteInt16 等的隐式转换:

BigInteger myNumber = 25;
Console.WriteLine(myNumber / 2);

从字符串转换更大的数字表示,使用 BigInteger.Parse():

BigInteger myNumber = BigInteger.Parse("252525252525252525252525252525");
Console.WriteLine(myNumber / 2);

You're overcomplicating how you construct the BigInteger - the framework provides implicit casts from byte, Int16, etc:

BigInteger myNumber = 25;
Console.WriteLine(myNumber / 2);

To convert larger numbers from a string representation, use BigInteger.Parse():

BigInteger myNumber = BigInteger.Parse("252525252525252525252525252525");
Console.WriteLine(myNumber / 2);
路还长,别太狂 2024-10-12 16:14:24

除了其他人指出的字符串到 BigInteger 转换问题之外,除两个 BigInteger 总是会产生 BigInteger 结果(因为整数没有小数部分)。该结果将是浮点结果的整数部分。

要确定除法是否精确,请使用 DivRem ()方法:

var dividend = BigInteger.Parse("25");

BigInteger remainder;
var quotient = BigInteger.DivRem(dividend, 2, out remainder);
if (!remainder.IsZero) {
    throw new Exception("Division resulted in remainder of " + remainder + "!");
}

Aside from the string-to-BigInteger conversion problems that others have pointed out, dividing two BigIntegers always yields a BigInteger result (since integers don't have a fractional component). This result will be the integer part of whatever the floating-point result would have been.

To determine whether the division was exact or not, use the DivRem() method:

var dividend = BigInteger.Parse("25");

BigInteger remainder;
var quotient = BigInteger.DivRem(dividend, 2, out remainder);
if (!remainder.IsZero) {
    throw new Exception("Division resulted in remainder of " + remainder + "!");
}
2024-10-12 16:14:24

我不知道您使用的是什么 BigInteger 实现,但 myNumberAsByteArray 不会包含表示数字 25 的字节。您只需在这里将字符串转换为字节即可。您可以使用字符串 myNumberAsString = "abc"; 这会给您另一个结果。

您可能想改用 Parse 方法:

BigInteger myNumber = BigInteger.Parse("25");

I don't know what implementation of BigInteger you are using but myNumberAsByteArray will not contain the bytes representing the number 25. You are simply converting a string to bytes here. You could have used string myNumberAsString = "abc"; which would have given you another result.

You probably want to use the Parse method instead:

BigInteger myNumber = BigInteger.Parse("25");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文