33,027
社区成员




package test;
import java.util.HashMap;
import java.util.Map;
public class DivisorTest {
static Map<Integer, Integer> map;
static void search(int from, int to) {
map = new HashMap<Integer, Integer>();
for (int i = from; i <= to; i++) {
map.put(i, countCommonDivisor(i));
}
}
static int countCommonDivisor(int number) {
int count = 2;
for (int i = 2; i <= (number / 2); i++) {
if (number % i == 0) {
count++;
}
}
return count;
}
public static void main(String[] args) {
search(10, 100);
System.out.println(map);
int key = 0, value = 0;
for (Map.Entry<Integer, Integer> iter : map.entrySet()) {
if (value < iter.getValue()) {
key = iter.getKey();
value = iter.getValue();
}
}
System.out.println("Number=" + key + ",the number of max divisor is " + value);
}
}
const int MAXP = 10000;//最多的质数个数
unsigned int primes[MAXP];//质数表
int countOfPrimes=0;
void fillPrime()
{//填满质数表:
primes[0] = 2;
primes[1] = 3;
for(countOfPrimes = 2 ; countOfPrimes< MAXP; countOfPrimes++)
{
primes[pos] = nextPrime(primes[countOfPrimes-1]);
}
}
bool isPrime(unsigned int prime)
{
int pos;
for(pos = 0 ; pos < countOfPrimes ; pos ++)
{
if(0 == prime%primes[pos]) return false;
}
return true;
}
unsigned int nextPrime(unsigned int prime)
{
do {
prime += 2;
} while(!isPrime(prime));
return prime;
};
int div(int x)
{
int divx = 1;
for(int pos = 0 ; pos < countOfPrimes && x > primes[pos] * primes[pos]; pos ++)
{
unsigned int & prime = primes[pos];
if(x%prime) continue;
int count = 0;
while(0==x%prime) {
x = x/ prime;
count ++;
}
divx *= (1+count);
}
return divx;
}
int search(int from, int to)
{
int max = div(from);
int maxn = from;
for(int i=from+1; i < to; i++)
{
int divx = div(i);
if(divx > max) {
max = divx;
maxn = i;
}
}
return maxn;
}