gpt4 book ai didi

java - 支持大整数的单词列表生成器

转载 作者:行者123 更新时间:2023-12-01 11:36:59 25 4
gpt4 key购买 nike

我需要帮助来完成一个程序,该程序将从所选字符和长度生成单词列表(它需要支持大长度)。

首先,您需要通过添加所需的长度(字长)并制作指定字符(字母)的字符串来解决此问题。

所以完整的字数是:

long MAX_WORDS = (long) Math.pow(alphabet.length(), wordlength);

实际上,我做到了并且它有效(例如 2 或 66 个字符的短单词)。

import java.math.BigInteger;
public class wordlistgenenreg {

public static void main(String[] args) {
generate();
}

private static void generate(){
int wordlength =2;
String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_~";
final long MAX_WORDS = (long) Math.pow(alphabet.length(), wordlength);
final int RADIX = alphabet.length();

for (long i = 0; i < MAX_WORDS; i++) {
int[] indices = convertToRadix(RADIX, i, wordlength);
char[] word = new char[wordlength];
for (int k = 0; k < wordlength; k++) {word[k] = alphabet.charAt(indices[k]);}
String fullword=new String(word);
System.out.println(fullword);
}

System.out.println("completed!");
}

private static int[] convertToRadix(int radix, long number, int wordlength) {
int[] indices = new int[wordlength];
for (int i = wordlength - 1; i >= 0; i--) {
if (number > 0) {
int rest = (int) (number % radix);
number /= radix;
indices[i] = rest;
} else {
indices[i] = 0;
}

}
return indices;
}
}

但是当我想从 66 个字符生成一个非常大的 64 个字符的字符串时,就会出现问题。因为:

MAX_WORDS = 66^64 = 282365657377235405270307754780751252031361330095689004197961218014051357270480550051149871489969454245263206971867136

因此我尝试更改它以使其与 BigInteger 一起使用。但我们的结果是,我总是获得字符串:

"0000000000000000000000000000000000000000000000000000000000000000"

所以有一个问题我没有弄清楚。这是我改变它的工作:

import java.math.BigInteger;

public class wordlistgen {

public static void main(String[] args) {
generate();
}

private static void generate() {
int wordlength = 64;
String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_~";
BigInteger max_words=new BigInteger("282365657377235405270307754780751252031361330095689004197961218014051357270480550051149871489969454245263206971867136");
final int RADIX = alphabet.length();
BigInteger plus=BigInteger.valueOf(1);

for (BigInteger i = new BigInteger("0"); i.compareTo(max_words) <0; i.add(plus)) {
int[] indices = convertToRadix(RADIX, i, wordlength);
char[] word = new char[wordlength];
for (int k = 0; k < wordlength; k++) {word[k] = alphabet.charAt(indices[k]);}
String fullword=new String(word);
System.out.println(fullword);
}
}

private static int[] convertToRadix(int radix, BigInteger i2, int wordlength) {
BigInteger zero=BigInteger.valueOf(0);
BigInteger big_radix=BigInteger.valueOf(radix);
int[] indices = new int[wordlength];
for (int i = wordlength - 1; i >= 0; i--) {
if (i2.compareTo(zero)==0) {

BigInteger rest =i2.remainder(big_radix);
BigInteger ab=i2.divide(big_radix);
i2=ab;
indices[i] = rest.intValue();
} else {
indices[i] = 0;
}
}
return indices;
}
}

最佳答案

这是原始版本中的 if:

if (number > 0) {
int rest = (int) (number % radix);
number /= radix;
indices[i] = rest;
} else {
indices[i] = 0;
}

以及 BigInteger 版本中相同的 if:

if (i2.compareTo(zero)==0) {

BigInteger rest =i2.remainder(big_radix);
BigInteger ab=i2.divide(big_radix);
i2=ab;
indices[i] = rest.intValue();
} else {
indices[i] = 0;
}

如您所见,在新的 if 中,您询问是否 number == 0 而不是 number > 0。所以你总是会陷入else

附注:您正在运行一个从 0 到 max_words 的循环。如果每次迭代只需要一纳秒即可完成,则仍需要 3687886676721203490906725006126388162312177668963067239285600631885632818310441214790267460959878872632642 65 年。足够的时间让宇宙分解成完全熵。我建议重新考虑你的算法。

关于java - 支持大整数的单词列表生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com