问题
A non-prime pair which forms N is 2 different non-prime numbers where the product of the numbers is N.
1<=N<=10^6
For example For N = 24 there are 2 good pairs (non-prime pairs that form N) (4,6), (1,24), but (2,12), (3,8) are not good.
Note: for any 2 numbers a and b pair(a,b) = pair(b,a).
There is another condition which states that if the number is a special number, so output = -1 otherwise count the number of non-primes.
Number is called special number if it can be represented as a product of three prime numbers. Example: 12 is a special number because 12=2*2*3;
I tried brute-force approach using https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes , which takes O(N*log(log(N)).
"Is there any more optimized way to solve it except brute-force?"
Any idea will be appreciated.
Thanks in advance.
回答1:
First of all, Eratosthenes' sieve is O(N*log(log(N)) to list all primes below or equal N (when well implemented).
Second: suppose you factored your number in Q
primes with multiplicity which, without sieving, is a process of O(sqrt(N)) at worst (worst: your number is prime). So you have a map of:
- p0 -> multiplicity m0
- p1 -> multiplicity m1
- ...
- pQ -> multiplicity mQ
How many divisors made from multiplying at least 2 prime factors?
Well, there will be m0*m1*...mq
of them [correction here]. Why? Well, prepare a list of all the divisors generated wit the powers of each factor (including pi0==1), but cross out the ones with a power of 1
.
- {1,
p0, p02, ...p0m0} arem0
ways of generating divisors with the powers ofp0
except p0 - {1,
p1, p12, ...p1m1} arem1
ways of generating divisors with the powers ofp1
except p1 - ...
- {1,
pQ, p1Q, ...p1mQ} aremQ
ways of generating divisors with the powers ofpQ
The number of all combinations with non-prime divisors (as 1
is already included in each set and each prime factors by itself is excluded ) will be the cardinality of the cartesian product of all the above subsets - thus the product of the individual cardinalities, therefore m0*m1*...mq
Code - Java
import java.util.HashMap;
import java.util.Map;
class Example {
static void factor(long N, Map<Long, Short> primesWithMultiplicity) {
// some arg checking and trivial cases
if(N<0) N=-N;
if(0==N) {
throw new IllegalArgumentException(
"Are you kidding me? Every number divides 0, "+
"you really want them all listed?"
);
}
if(1==N) {
primesWithMultiplicity.put(1L,(short)1);
return;
}
// don't try divisors higher than sqrt(N),
// if they would have been detected by their composite-complement
for(long div=2; div*div < N; ) {
short multiplicity=0;
while((N % div)==0) {
multiplicity++;
N /= div; // reduce N
}
if(multiplicity>0) {
primesWithMultiplicity.put(div, multiplicity);
}
div+= (div == 2 ? 1 : 2); // from 2 to 3, but then going only on odd numbers
}
// done.. well almost, if N is prime, then
// trying to divide up to sqrt(N) will lead an empty result. But,
// in this case, N will still be at original value (as opposed
// to being 1 if complete factored)
if(N>1) {
primesWithMultiplicity.put(N, (short)1);
}
}
static int countDistinctCompositePairs(long N) {
HashMap<Long, Short> factoringResults=new HashMap<>();
factor(N, factoringResults);
int ret=1;
for(short multiplicity : factoringResults.values()) {
ret*=multiplicity;
}
return ret/2;
}
static public void main(String[] args) {
System.out.println(countDistinctCompositePairs(24));
}
}
回答2:
Since you haven't mentioned the constraints I assume 1<=N<=10^6 Implement the sieve and simultaneously store the greatest prime factor of the numbers up to 10^6.
Here's the code for same.
int a[1000001];
a[1]=1;
for(int i=2;i*i<1000001;i++)
{
if(a[i]==0)
{
for(int j=2*i;j<1000001;j+=i)
a[j]=i;
}
}
Now if the number is prime your answer is 0.
if(a[n]==0)
cout<<'0';
If it is semi prime(product of two primes) your answer will be 1.
if(a[n/a[n]]==0)
cout<<"1";
If it is special then
int x=n/a[n];
if(a[x/a[x]]==0)
cout<<"-1";
If it doesn't satisfy any of the aforementioned conditions then calculate all non prime divisors.
int c=0;
for(int i=1;i*i<n;i++)
{
if(n%i==0)
{
if(a[i]!=0&&a[n/i]!=0)
c++;
}
}
Hope this helps!
来源:https://stackoverflow.com/questions/40692681/count-number-of-non-prime-pairs-that-when-multiplied-form-a-given-number-n