I am trying to sum up the digits in a very large number. I have gotten the length of the number withl = answer.bitLength()
but I can't figure out how to increament through each digit using a For loop. Any ideas?
我试图总结一个非常大的数字。我已经得到了l = answer.bitLength()的数字长度,但我无法弄清楚如何使用For循环增加每个数字。有任何想法吗?
I'm using the java.math.biginteger
.
我正在使用java.math.biginteger。
Visual Studio 2005 Version 2.0
Visual Studio 2005版本2.0
I should also add that I can't seem to use <> or any of the simple math options with the biginteger I'm using. If anyone could tell me how to use a different biginteger I would be more than willing to swap.
我还应该补充一点,我似乎无法使用<>或任何简单的数学选项与我正在使用的biginteger。如果有人能告诉我如何使用不同的大整数,我会更愿意交换。
Dim answer As java.math.BigInteger
Dim sum As Integer = 0
Dim x As Integer
Dim i As Integer
'Sets value of answer equal to 1
answer = java.math.BigInteger.valueOf(1)
'gets 100!
For i = 1 To 100
answer = answer.multiply(java.math.BigInteger.valueOf(i))
Next
'gets length of answer
Dim l As Integer
l = answer.bitLength()
'Sums up digits in 100!
For x = 0 To l - 1
'Need to pull each character here to add them all up
Next
Final Solution for summing up the digits. Thanks to wageoghe.
总结数字的最终解决方案。感谢payoghe。
Dim r As Integer
Dim s As Integer
s = 0
While (answer.compareTo(java.math.BigInteger.valueOf(0)) > 0)
r = answer.mod(java.math.BigInteger.valueOf(10)).ToString()
s = s + r
answer = answer.divide(java.math.BigInteger.valueOf(10))
End While
3 个解决方案
#1
0
Something like this should work:
像这样的东西应该工作:
Dim bi As New System.Numerics.BigInteger(12345)
Dim c As Char
Dim s As Long
s = 0
For Each c In bi.ToString()
s = s + Integer.Parse(c.ToString())
Next
Or this more conventional way using Mod and / (integer division)
或者这种更传统的方式使用Mod和/(整数除法)
Dim bi As New System.Numerics.BigInteger(12345)
Dim s As Long
Dim r As Integer
s = 0
While bi <> 0
r = bi Mod 10
s = s + r
bi = bi / 10
End While
#2
0
If you think of the number as a list of binary characters, then you could get the least significant hex digit by AND
ing the number with 0xF
. If you then shifted the number right by 4 bits (>> 4
), then you could get the next hex digit.
如果您将数字视为二进制字符列表,那么您可以通过将数字与0xF进行AND运算来获得最低有效十六进制数字。如果你然后将数字右移4位(>> 4),那么你可以获得下一个十六进制数字。
After you get all of the hex digits, you could sum them up and then convert them to decimal.
获得所有十六进制数字后,您可以将它们相加,然后将它们转换为十进制数字。
#3
0
Another approach, is to do the following (this assumes that answer
is positive):
另一种方法是执行以下操作(假设答案是肯定的):
int sum = 0;
while(answer > 0){
sum += answer % 10;
answer /= 10;
}
#1
0
Something like this should work:
像这样的东西应该工作:
Dim bi As New System.Numerics.BigInteger(12345)
Dim c As Char
Dim s As Long
s = 0
For Each c In bi.ToString()
s = s + Integer.Parse(c.ToString())
Next
Or this more conventional way using Mod and / (integer division)
或者这种更传统的方式使用Mod和/(整数除法)
Dim bi As New System.Numerics.BigInteger(12345)
Dim s As Long
Dim r As Integer
s = 0
While bi <> 0
r = bi Mod 10
s = s + r
bi = bi / 10
End While
#2
0
If you think of the number as a list of binary characters, then you could get the least significant hex digit by AND
ing the number with 0xF
. If you then shifted the number right by 4 bits (>> 4
), then you could get the next hex digit.
如果您将数字视为二进制字符列表,那么您可以通过将数字与0xF进行AND运算来获得最低有效十六进制数字。如果你然后将数字右移4位(>> 4),那么你可以获得下一个十六进制数字。
After you get all of the hex digits, you could sum them up and then convert them to decimal.
获得所有十六进制数字后,您可以将它们相加,然后将它们转换为十进制数字。
#3
0
Another approach, is to do the following (this assumes that answer
is positive):
另一种方法是执行以下操作(假设答案是肯定的):
int sum = 0;
while(answer > 0){
sum += answer % 10;
answer /= 10;
}