public static boolean IsPrime(long number) {
int begin = 2;
int end = (int) Math.sqrt(number) + 1;
for (int i = begin; i < end; i++) {
if (number % i == 0)
return false;
for_count1++;
}
return true;
}
// 得到因子
public static Set<Long> getFactor(Long number) {
Set<Long> factors = new HashSet<Long>();
Long begin = 2L;
Long end = number;
for (Long i = begin; i < end&&number!=1; i++) {
if (number % i == 0) {
factors.add(i);
while ((number % i) == 0) {
number = number / i;
for_count1++;
}
}
for_count1++;
}
if (number != 1) {
factors.add(number);
}
return factors;
}
public static Set<Integer> getFactor1(Long number) {
Set<Integer> factors = new HashSet<Integer>();
if (number % 2 == 0) {
factors.add(2);
do {
number = number / 2;
for_count2++;
} while (number % 2 == 0);
}
int i = 3;
do {
if (number % i == 0) {
factors.add(i);
do {
number = number / i;
for_count2++;
}while (number % i == 0);
}
i = i + 2;
for_count2++;
} while (number != 1);
return factors;
}
public static Set<Long> getFactor2(Long number) {
Set<Long> factors = new HashSet<Long>();
if (number % 2 == 0) {
factors.add(2L);
do {
number = number / 2;
for_count3++;
} while (number % 2 == 0);
}
Long i = 3L;
long maxFactor = (long)Math.sqrt(number);
do {
while (number % i == 0) {
factors.add(i);
number = number / i;
for_count3++;
}
i = i + 2;
for_count3++;
} while (number != 1&&i<maxFactor);
if(number!=1){
factors.add(number);
}
return factors;
}
运算结果:
2018的因子
[2, 1009] for_count1:1010
[2, 1009] for_count2:506
[2, 1009] for_count3:15
600851475143的因子
[6857, 71, 839, 1471] for_count1:6860
[6857, 71, 839, 1471] for_count2:3432
[6857, 71, 839, 1471] for_count3:3432