我需要素数的原始根。我写了这段代码,但它有堆大小错误,它不适用于像101这样的大数。我没有任何其他想法来计算原始根。如果你有请帮帮我。我需要这么多。你还有其他算法来计算素数的原始根吗?
static ArrayList<ArrayList<Integer>> list1=new ArrayList<ArrayList<Integer>>();
private static int primitiveRoot(int prim){
ArrayList<ArrayList<Integer>> number=new ArrayList<ArrayList<Integer>>();// this has all sequence numbers of x power of 0 to prime-1
ArrayList<Integer> sublist=new ArrayList<Integer>();
for (int x=2;x<prim;x++ ){
sublist = new ArrayList<Integer>();
for (int power=0;power<prim-1;power++){
int i=(int)((Math.pow(x, power))%prim);
sublist.add(i);
}
number.add(sublist);
}
for (int j=0;j<number.size();j++){
for (int m=0;m<list1.size();m++){
if(number.get(j).equals(list1.get(m)) ){// element of number arraylist compare to list1,equality means that we find one of primitive root
a=j+2;
break;
}
}
}
return a;// this is primitive root
}
list1是一个arraylists的arraylist,它包含1到prime-1之间的所有数字排列。它只适用于7或11等小素数。我增加了堆大小,但它没有效果。
答案 0 :(得分:0)
Try wikipedia找到算法。
在Java中,计算a^b mod n
的快速方法是使用BigInteger
,因为它安全且快速。
BigInteger a = new BigInteger("101");
BigInteger res = a.modPow(b, n);
答案 1 :(得分:0)
import java.math.BigInteger;
import java.util.*;
public class PrimitiveRoot {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int p = in.nextInt();
int m = p;
if (isPrime(p)) {
m = p - 1;
}
int primeRoot = 0;
Map<Integer, Integer> primeFactor = getPrimeFactor(m);
for (Map.Entry<Integer, Integer> map : primeFactor.entrySet()) {
primeFactor.put(map.getKey(), m / map.getKey());
}
for (int i = 2; i <= m; i++) {
boolean notPrimeRoot = false;
Set<Integer> reminder = new HashSet<>();
for (Map.Entry<Integer, Integer> map : primeFactor.entrySet()) {
if(BigInteger.valueOf(i).modPow(BigInteger.valueOf(map.getValue()), BigInteger.valueOf(p)).equals(BigInteger.ONE))
notPrimeRoot = true;
}
if (!notPrimeRoot) {
primeRoot = i;
break;
}
}
System.out.println("Prime Root is: " + primeRoot);
}
private static boolean isPrime(int p) {
for (int i = 2; i <= Math.sqrt(p); i++) {
if (p % i == 0) {
return false;
}
}
return true;
}
private static Map<Integer, Integer> getPrimeFactor(int p) {
Map<Integer, Integer> map = new HashMap<>();
while (p % 2 == 0) {
insertToMap(2, map);
p /= 2;
}
for (int i = 3; i <= Math.sqrt(p); i += 2) {
while (p % i == 0) {
insertToMap(i, map);
p /= i;
}
}
if (p > 2)
insertToMap(p, map);
return map;
}
private static void insertToMap(int i, Map<Integer, Integer> map) {
if (map.get(i) != null) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
}
答案 2 :(得分:-2)
这是一个python版本,用于计算给定数字的所有原始根:D
#David Isaac Pinos
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
N=int(input("Insert number: "))
phi_N=N-1
factor_aux=prime_factors(phi_N)
print(factor_aux)
factor = []
i=0
while(i<len(factor_aux)-1):
if (factor_aux[i]!=factor_aux[i+1]):
factor.append(factor_aux[i])
if (i==(len(factor_aux)-2)):
factor.append(factor_aux[i+1])
i=i+1
N_factor=len(factor)
print("")
print("")
i=2
a=0
while (i<10):
print ("*****Evaluating for " , i , ":")
j=0
while (j<N_factor):
tester=int(phi_N/factor[j])
test= pow(int(i),tester)%int(N)
print (i, "^" , tester, " mod ", N, "=" , test )
if (test!=1):
j=j+1
if (j==N_factor and a==0):
print ("********************* ",i, " IS THE PRIMITIVE ROOT OF ",N)
primitive_root=i
i=100
else:
j=N_factor
i=i+1
print("All primitive numbers are: ")
i=1
while(i<N):
print(primitive_root, "^", i, "mod",N,"=",pow(primitive_root,i)%N)
i=i+1