Page 1 of 3

10539 - Almost Prime Numbers

Posted: Mon Aug 25, 2003 4:59 am
by sharklu2000
I first calculate all the prime numbers between 1 and 10<sup>6</sup>
then for every prime numbers below low and high, I add all the log below and high to the base of the prime number as a and b. Then b-a is the answer.
But it is really inefficient, can anyone tell me some efficient algorithm.
I will be very grateful.

Posted: Mon Aug 25, 2003 5:03 am
by Larry
I did more or less the same thing, though if you do that, you would probably get something like:

Code: Select all

9 9
wrong, because you would subtract the same number, while 9 would still be almost-perfect.

Posted: Mon Aug 25, 2003 5:25 am
by Whinii F.
Our team had the same problem during the contest. :-?

But ten minutes before the finish, we realized there are only about 80000~ almost primes in the range, so easily generatable & sortable within the time limit. And we could do a binary search on that.

Wondering if this could be better than our last approach we coded that but time was up.. :wink:

Now I coded that again got AC on 0.2 sec. :wink:
Good luck!

Posted: Mon Aug 25, 2003 6:19 am
by Larry
Ya, with my old algo, I had it AC in 6.3 seconds, but I didn't realize I could binary search, and after my friend (UFP), told me, I did it and got it AC in 0.2 secs too..

And then I kicked myself for not remembering.. heh

Posted: Mon Aug 25, 2003 7:39 am
by UFP2161
Yeah.. that would be me.. the logs were getting too messy and slow and my computer kept producing slight floating point errors on them, so I began thinking about the problem in reverse and it worked out much better =)

Still need a faster sieve though =P

Posted: Mon Aug 25, 2003 8:56 am
by sharklu2000
Hi Whinii , I use the method you told me. But the program still runs more than 1 second.
Could you tell me how I can accelerate it?
Many thanks.

Code: Select all

using namespace std;
long long alpri[80070];
unsigned int pri[80000];
inline bool isp(int m)
	for(int i=3;i*i<=m;i+=2)
		if (m%i==0) return false;
	return true;
int cmp1(const void *a, const void *b)
	long long *aa, *bb;
	aa = (long long *)a;
	bb = (long long *)b;
	if(*aa > *bb)
		return 1;
	else if(*aa == *bb)
		return 0;
		return -1;

int binsearch(long long x)
	int low = 0;
	int high = 80069;
	int k;
		k = (int)(low+high)/2;
		if(x < alpri[k] && x < alpri[k - 1])
			high = k - 1;
		if(x > alpri[k] && x > alpri[k + 1])
			low = k + 1;
		if(x >= alpri[k - 1] && x < alpri[k])
			return k;
		if(x >= alpri[k] && x < alpri[k + 1])
			return k + 1;
		if(x == alpri[k + 1])
			return k + 2;

int main()
	int i, c=0, n;
	long long a,b;
		if(isp(i)) pri[++c]=i;
	int d = -1;
	for(i = 0; i<=c; i++)
		long long tmp = pri[i];
		while((tmp*=pri[i]) < 1e12)
			alpri[++d] = tmp;

	qsort(alpri, d, sizeof(long long), cmp1);
	for(int j = 0; j<n; j++)
		cout<<binsearch(b) - binsearch(a-1)<<endl;


Posted: Mon Aug 25, 2003 9:54 am
by hujialie

I used an unefficient method:
First search all the prime numbers from 1 to 1000000;
Then write count(x) to caculate number of almost prime numbers
through 1..x;At last using count(high)-count(low-1) to get the answer.

Although it had been accepted,it run during 4+ seconds,that exceeded
the time limit (1 sec)during contest.Can anyone tell me a better method?

I also wrote a binary search version (almost the same as sharklu2000's),
but it also run longer than 2 second,and I got wrong answer.

Larry or Whinii,could you give some detail on programming?

Thank you.

Posted: Mon Aug 25, 2003 12:42 pm
by Larry
Use sieve first to generate prime table, then for each prime, generate all the almost primes.

Sort and binary search.. that's all I did.

(I binary searched to get the count to low, than the count to high, then subtract them..)

Ya, so sharklu, run a sieve first, and tell me if it helps that much..

Posted: Mon Aug 25, 2003 1:49 pm
by sharklu2000
Hi Larry, what do you mean by using sieve first. :oops:
Is it precomputing all the primes in the range?
Could you tell more in detail about sieve? Thanks.

Posted: Mon Aug 25, 2003 4:25 pm
by UFP2161
Just lookup "Sieve of Eratosthenes" on google.

Posted: Mon Aug 25, 2003 4:38 pm
by hujialie

I search all the prime numbers first,but the time is still very long. :(
Here is my c++ code:
Is there anything wrong?


Posted: Mon Aug 25, 2003 4:58 pm
by titid_gede
you dont need to store prime number. while doing sieve, generate almost primes number. :) :) :)
i did not use binary search (just sequential search), but got AC in 0.7 sec :) :)
hope it helps :)

titid gede

Posted: Mon Aug 25, 2003 5:59 pm
by hujialie
Thank you titd_gede,UFP2161 and Larry.
Now I got accepted within 0.5 seconds.

Posted: Mon Aug 25, 2003 6:51 pm
by sharklu2000
Thanks UFP2161, Larry, Whinii F.
I got acc with 0.4se now.

Posted: Thu Aug 28, 2003 4:11 pm
by Dmytro Chernysh
How many almost prime numbers are? I got 573 for 1..10^12.
And it's wrong...