[英]Java: How can I Sum Digits of a huge number n that is 10^19 < n <=10^20?
[英]In Java how can I get a subsequence of number string whose indices can be as 10^5 digits long
在Java中,如何獲取BigInteger的子序列,其索引長度可以為10 ^ 5位。
示例:BigInteger的長度是10 ^ 5。 我必須找到索引10 ^ 3和10 ^ 4之間的子序列
您可以使用:
String yourSubstring = Str.substring(9999, 10000);
這將為您提供從10 ^ 3到10 ^ 4的字符串。
要將BigInt轉換為String,可以使用:
String str = yourBigInt.toString();
學習這段代碼,看看它是否可以解決您的問題。 如果沒有,也許您會在其中找到一些有用的信息。
public class TestBigInteger
{
public static void main( String[] args )
{
String makeNumber = "";
int numberOfDigits = 10000;
String newNumberString = "";
Random random = new Random();
BigInteger result;
for ( int i = 0; i < numberOfDigits; i++ ) {
makeNumber += random.nextInt( 9 );
}
newNumberString = makeNumber.substring( 100, 999);
result = new BigInteger( newNumberString );
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.