我想分割一个大整数的数字。让我更具体一点。我正在使用Fibonacci序列生成一个大整数,现在使用这个算法我需要循环,直到找到一个BigInteger,其中前9位和后9位是pandigital。唯一的问题是我必须循环的量是300K(现在BigInteger会如此巨大)。
我尝试将BigInteger转换为字符串,然后使用“substring(begin,end)”。但是,这太慢了,只花了将近28分钟来完成100K指数。
有一个数学解决方案,但我不完全确定它是什么,如果有人能够引导我在正确的方向,很多人会感激。注意:我不是直接要求答案,只是迈向找到正确答案的一步。
这是我的代码,以防万一你想知道:
import java.math.BigInteger;
public class Main {
public static void main(String...strings)
{
long timeStart = System.currentTimeMillis();
fib(300_000);
long timeEnd = System.currentTimeMillis();
System.out.println("Finished processing, time: " + (timeEnd - timeStart) + " milliseconds.");
}
public static BigInteger fib(int n)
{
BigInteger prev1 = BigInteger.valueOf(0), prev2 = BigInteger.valueOf(1);
for (int i = 0; i < n; i++)
{
BigInteger savePrev1 = prev1;
prev1 = prev2;
prev2 = savePrev1.add(prev2);
}
return prev1;
}
static BigInteger[] pows = new BigInteger[16];
static {
for (int i = 0; i < 16; i++) {
pows[i] = BigInteger.TEN.pow(i);
}
}
static boolean isPanDigital(BigInteger n) {
if (!n.remainder(BigInteger.valueOf(9)).equals(BigInteger.ZERO)) {
return false;
}
boolean[] foundDigits = new boolean[9];
boolean isPanDigital = true;
for (int i = 1; i <= 9; i++) {
BigInteger digit = n.remainder(pows[i]).divide(pows[i - 1]);
for (int j = 0; j < foundDigits.length; j++) {
if (digit.equals(BigInteger.valueOf(j + 1)) && !foundDigits[j]) {
foundDigits[j] = true;
}
}
}
for (int i = 0; i < 9; i++) {
isPanDigital = isPanDigital && foundDigits[i];
}
return isPanDigital;
}
}
更新,设法弄清楚如何生成前9位数(并且它似乎不太慢)。但是,我遇到了产生最后9位数的问题。
import java.math.BigInteger;
public class Main {
public static void main(String...strings)
{
long timeStart = System.currentTimeMillis();
fib(300_000);
long timeEnd = System.currentTimeMillis();
System.out.println("Finished processing, time: " + (timeEnd - timeStart) + " milliseconds.");
}
public static BigInteger fib(int n)
{
BigInteger prev1 = BigInteger.valueOf(0), prev2 = BigInteger.valueOf(1);
for (int i = 0; i < n; i++)
{
if (prev1.toString().length() > 19)
{
String leading9Digits = leading9Digits(prev1);
if (isPanDigital(BigInteger.valueOf(Integer.valueOf(leading9Digits))))
{
System.out.println("Solved at index: " + i);
break;
}
}
BigInteger savePrev1 = prev1;
prev1 = prev2;
prev2 = savePrev1.add(prev2);
}
return prev1;
}
public static String leading9Digits(BigInteger x) {
int log10 = (x.bitLength() - 1) * 3 / 10;
x = x.divide(BigInteger.TEN.pow(Math.max(log10 + 1 - 9, 0)));
return x.toString().substring(0, 9);
}
static BigInteger[] pows = new BigInteger[16];
static {
for (int i = 0; i < 16; i++) {
pows[i] = BigInteger.TEN.pow(i);
}
}
static boolean isPanDigital(BigInteger n) {
if (!n.remainder(BigInteger.valueOf(9)).equals(BigInteger.ZERO)) {
return false;
}
boolean[] foundDigits = new boolean[9];
boolean isPanDigital = true;
for (int i = 1; i <= 9; i++) {
BigInteger digit = n.remainder(pows[i]).divide(pows[i - 1]);
for (int j = 0; j < foundDigits.length; j++) {
if (digit.equals(BigInteger.valueOf(j + 1)) && !foundDigits[j]) {
foundDigits[j] = true;
}
}
}
for (int i = 0; i < 9; i++) {
isPanDigital = isPanDigital && foundDigits[i];
}
return isPanDigital;
}
}
答案 0 :(得分:1)
好的,我对你的代码进行了一些优化。
leading9Digits
方法不应返回String
,它应返回一个数字。如果他们愿意,允许代码的其他部分将其转换为字符串。BigInteger.pow()
都是浪费。您可以预先计算所有这些10的幂并将它们存储在一个数组中。boolean
数组中增加一个单元格两次,那么它不是pandigital。此外,您可以提前退出0
。 trailing9digits
非常简单
注意,我尝试使用String
而不是余数,他们的结果大致相同。我在你的代码中都包含了这两个内容。
最后,正确答案不在前300,000;它大概是329,000。在我的机器上运行大约需要29秒。
import java.math.BigInteger;
public class Main {
private static final BigInteger NINE = BigInteger.valueOf(9);
private static final BigInteger BILLION = BigInteger.valueOf(1000000000);
private static final double log10_2 = Math.log10(2);
private static final double CORRECTION = 9d;
private static final int ARRAY_SIZE = 131072;
static BigInteger[] pows = new BigInteger[ARRAY_SIZE];
static {
pows[0] = BigInteger.ONE;
for (int i = 1; i < ARRAY_SIZE; i++) {
pows[i] = pows[i - 1].multiply(BigInteger.TEN);
}
}
public static void main(String... strings) {
long timeStart = System.currentTimeMillis();
fib(500_000);
long timeEnd = System.currentTimeMillis();
System.out.println("Finished processing, time: "
+ (timeEnd - timeStart) + " milliseconds.");
}
public static BigInteger fib(int n) {
BigInteger prev1 = BigInteger.valueOf(0), prev2 = BigInteger.valueOf(1);
for (int i = 0; i < n; i++) {
if (prev1.bitLength() > 30) {
if (isDualPanDigital(prev1)) {
System.out.println("Solved at index: " + i);
break;
}
}
BigInteger savePrev1 = prev1;
prev1 = prev2;
prev2 = savePrev1.add(prev2);
}
return prev1;
}
public static BigInteger leading9Digits(BigInteger x) {
int dividePower = (int) (x.bitLength() * log10_2 - CORRECTION);
BigInteger y = x.divide(pows[dividePower]);
if (y.compareTo(BILLION) != -1) y = y.divide(BigInteger.TEN);
return y;
}
public static BigInteger trailing9Digits(BigInteger x) {
return x.mod(BILLION);
}
static boolean isDualPanDigital(BigInteger n) {
boolean leading = isPanDigital(leading9Digits(n));
boolean trailing = isPanDigital(trailing9Digits(n));
if (leading && trailing) {
System.out.format("leadingDigits: %s, trailingDigits: %s%n",
leading9Digits(n), trailing9Digits(n));
}
return leading && trailing;
}
static boolean isPanDigital(BigInteger n) {
if (!n.remainder(NINE).equals(BigInteger.ZERO)) {
return false;
}
return isPanDigitalString(n);
}
private static boolean isPanDigitalMath(BigInteger n) {
boolean[] foundDigits = new boolean[10];
for (int i = 1; i <= 9; i++) {
int digit = n.remainder(pows[i]).divide(pows[i - 1]).intValue();
if (digit == 0)
return false;
if (foundDigits[digit - 1])
return false;
else
foundDigits[digit - 1] = true;
}
return true;
}
private static boolean isPanDigitalString(BigInteger n) {
boolean[] foundDigits = new boolean[9];
String s = n.toString();
if (s.length() < 9) {
return false;
}
for (int i = 0; i < 9; i++) {
int digit = s.charAt(i) - '1';
if (digit == -1 || foundDigits[digit])
return false;
foundDigits[digit] = true;
}
return true;
}
}