His program generates the primes fast enough (I thought!) that we didn't need to save the primes directly, inside the program..

@CSE do they test on YOUR PC, or test on THEIR PC?

Well, that's another fine mess!;)

I've got to go grab some lunch and do some errands, but I'll see what we can do to optimize the program.

Right now, I think it looks possible and necessary to add some primes right into the program's array, OR add some 0's to the sieve array directly, instead of by calculating them all.

To answer your question Smeagel - I'm pondering the matter, but using a gapped table of primes, appears unnecessary. Size is now OK.

Edited by Adak: n/a


Good that they do the testing - that way it's fair for everyone.

OK, to the speed up's:

Idea #1:
Write out sieve 1's and 0's to a file. Take the LAST part of that file, say 4,000 digits, and copy them into an aux1 array, in main(). Now lower the top of the for loop for calculating the sieve, by the number of digits you put into aux1[].
(and just let the system calculate how large aux1[] is)

After that loop is done, memcpy into the top of sieve, the aux1 data (see below). Since memcpy is very fast, and the looping applies to every number less than or equal to the root of size (86M+), it makes a nice speed up.

Here's an example, run it on your system, and compare it's time with the time from the previous CSE program, just above here. With just under 2k digits, I saved almost a second and a half, which is about 25% off the prior run time.

I have not checked it for accuracy, probably there's something one off, etc. But I believe the idea is sound.

Code source file size shows as 3K now (up from 1K), but those numbers are always rounded up in Explorer. We have a limit of < 10K, for the contest.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 86028122

int arr[5000000];
//int size=86028122; //30;
//char sieve[size]= {'\0'};
int main() {
   double start,end;
   int q;  //delete k
   char aux1[]={"111111111111111111111111111111111111111110111111111011111111111111111111111111111110111011111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111011111111111111111111111011111011111110111110111110111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111110111111111111111011111111111111111111111011111011111111111110111111111111111111111110111111111011111111111110111111111111111110111111111111111110111111111010111111111011111111111111111011111111111110111111111110111111111111111110111110111111111111111111111110111111111011111111111011111011111110111111111111111111111111111111111011111111111111111111111110111111111111111011111111111111111010111110111111111110111111111111111111111011111111111110111111111111111111111110111111111011111111111111111011111111111110111111111011111111111111111111111111111011111111111111111110111111111111111011111111111111111111111111111110111111111111111111111111111111111111111011111111111111111111111110111111111110111011111111111111111111111111111011111111111111111111111111111011111111111111111111111010111011111111111110111110111111111111111110111111111110111111111111111110111111111111111111111011111110111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111011111111111111111111111111111010111111111111111111111111111110111111111110111011111111111111111111111111111111111111111111111111111111111011111111111111111110111011111111111111111011111110111110111111111111111111111111111111111110111111111111111111111111111111111011111111111111111111111111111111111011111110111111111111111111111111111111111110111011111111111111111111111111111011111111111111111111111111111111111111111111111110111111111111111111111110111111111110111011111111111011111111111011111111111111111111111110111111111110111011111111111111111111111111111111111111111110111011111111111011111110"};
   //FILE *out;
   //out=fopen("aux1.txt", "w");

   //printf("%c%c\n", sieve[0], sieve[1]);
   //return 0;

   int i,j,m=0;
   char *sieve = calloc(size, 1);
   m=size-1956; //I took in 1956 digits for aux1[]
   for (i=2; i*i <= m; i++) {  //was i<=size
       if (!sieve[i]) {
           for(j = i+i; j < size; j+=i) { sieve[j] = 1; }
   //printf("%d", strlen(aux1));  //I believe! ;)
   putchar('!');        //success so far!
   for (i=2,m=0; i<size; i++) {
       if (!sieve[i])  arr[m++]=i;
       //fprintf(out, "%d", sieve[i]); //gave me raw data for aux1[]
   //fclose(out);  //file for raw data going to aux1[]
   return 0;

Let me know what you think and how it tests for you.

Edited by Adak: n/a


Looking at the code for the challenge so far, I don't see much else that can be optimized, unless we print directly to the video memory. That is something I haven't done in a decade, at least.

So I believe this is where the speed up's will be found - in creating the prime numbers. It's already fast, but we have to go to extremes to meet this challenge.

I'm try it with 5,000 char's next (same as above, but with more chars in the aux1[] array). Then I'll ensure it's accurate if it looks promising, which I believe it will.

If you have more idea's on optimizing this, by all means post up, and give it a try.

Ya know, using some magic pixie dust on the program, would be a whole lot easier! ;)

This may not be accurate yet, but it will work.


My IDE (editor) went bonkers on me when I tried to use 5,000 chars - I mean it refuses to print the 1's and 0's that I have pasted into the array!

Trick or Treat!

So the next try is to put in the N highest primes, directly into the prime numbers array. Hopefully, the IDE editor will handle separate numbers better than it handles long strings of digits as chars.


The numbers are working out OK, so far. CSE, your program looks a lot different, but you'll still recognize it -- I'm giving it it's own drivers license. ;)

I'm trying two idea's:
1) having a big[] array with the top prime numbers, and
2) having a tiny[] array with the smallest prime numbers.

Together, they decrease the time needed to produce all the primes for the arr[] array of all the primes. Not by much, but it all helps, and adds up, of course.

After adding this stuff, I'm putting it back together again.

I'll post again when I have something for ya. ;)


yep a good idea..that i was taking about to make a segment of array to be displayed.
16.5 sec is reduced to 13.4 sec.


what is 1956 ? why this number is chosen for the size of aux1[].?
didn't understand...:-/


I wanted to get about 2K char's, and that's what I wound up with. ;)

Right now I'm trying an approach using numbers, instead of sieve char's. Both are able to speed up the program in tests so far. The numbers are easier to work with, and check for accuracy with the program, but the char's allow more items to be used, since they're smaller.

I'll be touching up the numbers version today. Late today or tomorrow, I'll post it up so you can try it. I had to remove a lot of the numbers unfortunately, because the program was too big (over 10KB).

After that, I'm going to work again on the sieve data char's, and see what can be done to improve that version, some more. My C editor does NOT like a lot of char's though - not sure why. Maybe I'm pasting in too many chars at one time? Don't know. PITA though when it corrupts the program so the file can't be saved or compiled, either one. <mad!>

If you are looking for a new hobby that takes a lot of time, I can recommend trying to optimize programs that are already fast. ;)


I haven't check this for accuracy, but CSE, Smeagel and Diwakar, give this version a try, and compare it with the version just above here, using char's. Which is better, and by how much?

I know I can get a bit more optimizing done to the char version. This number version, is about as optimized as I know how, right now. For the size limit, it just will fit in at under 10KB (with a bit of unsightly stuffing).

I wouldn't bother submitting it because I don't believe it's accurate yet, but that is the easy part, I've found.

Do you submit executable programs, or source code and they compile it, for the tests?

Let me know how it does.

#define Size 86028122
#define BigQty 1011
unsigned int arr[5000001];
int main() {double start,end; unsigned int big[BigQty]={ 
86028121 }; start=clock(); unsigned int i,j,small=Size-BigQty; char *sieve = calloc(small,1);  

for (i=2; i*i <= small; i++) 
   {if (!sieve[i]) 
      {for(j = i+i; j < Size; j+=i) { sieve[j] = 1; }}}


for (i=0,j=0; i<small; i++) 
   {if (!sieve[i])  

   arr[small++] = big[i]; 

for(i=2475000;i<2525000;i++) { //centered around mid range 2,500,00
printf("%d\n",arr[i]);} end=clock(); printf("\n\n%f\n",(end-start)/CLOCKS_PER_SEC); return 0;}

@Adak :- What time is giving this program in your system. You have applied your brain, but doesn't seems to affect much. Showing 15.6s.

I have told I tried this approach with 5000000 prime numbers but after output redirection to file that file doesn't open showing not responding.

Its very strange to me the program which takes 13.5s to compile in windows takes 3.4s in in linux, is linux doesn't run much processes as compared to windows .?:-/

Edited by cse.avinash: n/a


It runs in just under five seconds on my system. The char version above does about the same, but I can optimize it still more.

If you want to save data to a file, add an end of file char to the stream at the end: ctrl+z on Windows and DOS, and ctrl+d on Linux (not sure for linux though). fclose(filePointerName), should do that for you, on your system.

Linux has a lot of processes, but the question is, how much cpu time are they taking (how active are they)? Some versions of Linux are pretty active - others are very responsive.

Just for an idea for you, the time on my system, breaks down to:

0.95 seconds to get all the prime numbers found, and loaded
3.82 seconds to print the 50,000 primes in the mid range of the contest (2,475,000 to 2,525,000).

I'm trying to reduce the 0.95 seconds part, and that's quite difficult, because there isn't much THERE to start with.

I've looked into printing the primes faster, but I'm unsure how to do that - especially when it's a strangers computer you know nothing about.

The easiest way may be to just reset his computer clock while the program is running. Put it back about 2 seconds or so! ;)

I have some other idea's, but I have a bug in the character based program that I have to fix first. Then I'll get to work on some other ideas to cut down the time. Those involve:

1) The logic we use in creating the sieve array data, and

2) The way the aux1 array is stored.

Both can be improved, imo.

Edited by Adak: n/a


This is the big idea for an improvement:

In this code

for (i=2; i*i <= Size; i++) {  
      if (sieve[i]==0) {
           for(j = i+i; j < Size; j+=i) { sieve[j] = 1; } 

i is simply incremented. So after testing for 2, and 3, i will be testing for 4 and 6, and 8, and 10, etc.

Next version will eliminate that redundancy and definitely speed up this portion of the program.

Edited by Adak: n/a


One more thing I want to add in the problem which I forgot is that You can't exceed 10000 characters in a program i.e., source limit is 10000 bytes


Forget my last post - you can do that with trial by division for a nice speed up, but it doesn't work with Sieves.

I've got it down now to 3.56 seconds (including printing 50K primes), but it's too long to be accepted. I'll shorten it, clean it up, and post it after I get it checked for accuracy.

I went with the numbers version, but streamlined how it works.

CSE, do you submit code or is it an executable file that gets tested?

I can't begin to tell you how many times I crashed this program, while working with it. Amazing how well the newer operating systems handle it.


CSE, do you submit code or is it an executable file that gets tested?

No,you just have to submit your code.
I compiled and ran your both programs and it took about 16 seconds for the version with direct numbers and about 13 seconds for the version with char array( intel core 2 duo, 2.22 GHz).The chars version was not accepted by my ide, so I had to build it in the command line.
Then I submitted the program I was working on( after editing a little bit),

#include <stdio.h>
#include <stdlib.h>

#define SIZE 86028122
int arr[5000000]={0};

void sieve(int size) {
       int i,j,k;
       char *sieve = calloc(size, 1);
       for (i=2,k=1; i*i <= size; i=k*2+1,k++) {
               if (!sieve[i]) {
                       for(j = i*i; j < size; j=j+i) {sieve[j]=1; }
       for (i=2,j=0; i<size; i++) {
           if (!sieve[i]) { arr[j++]=i; }

int main(void) {

	char number[15],*file=NULL;
	int n,flag=0;
	while((file = fgets(number,sizeof(number),stdin)) !=NULL && flag!=1){



       return 0;

I submitted it because, it was taking about 4.79 seconds on my PC and only 2.26 seconds on ideone which the site recommends to test your code.But it shows time limit exceeded whenever I submit it to the site itself.Frustrating, isn't it?

2)Take 1 as test case it will not take any input.
that's why its giving wrong answer.

But I was reading the input from a file, otherwise how else would you give this kind of input:


.Frustrating, isn't it?

Yep its really very frustrating.
I checked , 3 users have got accepted this question and I am wondering how any one can get accept this problem in just 0.64s..:-/
@Adak, @Diwakar @smeagle
I thaught a plan , can parallel processing of numbers to be found and stored in different arrays be possible ?
I want to say let there be five different arrays say arr1[1000000], arr2[1000000],
arr3[1000000],arr4[1000000],arr5[1000000] for 5000000 prime numbers and there will be pipelining in storing prime numbers in all arrays, can this be possible, if it is , I don't know how to do it..?


It's all possible, but not with the size limit they have. Good thinking, though! ;)

Plus, you don't know if the PC it's being judged on, will allow the program to use multiple cores or not. 0.64 seconds and such, seems like the program would have to load the primes up into an section of memory on the testing PC, then have stdin processed, and instantly directed to the right answer, via pointers. I could be wrong, but I can't print out 50,000 prime numbers that fast!

Don't sweat it, we're just getting to the heart of the matter. ;)


I asked there in the forum, they told me to solve this question first before solving that question, there is a lots of concept here in this question to solve that question..


If your time limit is exceeded, you aren't told by how much correct?

I see a *possible* part that could be improved, in Diwakar's code. I'll have to check on that before I know for sure though.

That second problem is this problem "light", in my opinion. I like this one! It's got some heft to it. ;)


I changed a small part of your code here, Diwakar. It's now a bit faster.

int main(void) {
	//char number[15],*file=NULL;
   unsigned int n,flag=0,query;
   fscanf(stdin, "%d", &query);
   while((fscanf(stdin, "%u", &n)) > 0)

   return 0;
   /*while((file = fgets(number,sizeof(number),stdin)) !=NULL && flag!=1){

I made no changes to any other function beside main(), except to make all int's into unsigned int's, instead of int's. The improvement was using fscanf(), instead of fgets and atoi.

Edited by Adak: n/a


Three ideas come to mind:

1) There are quick tests to find "probable" primes. If you used two of these tests, you could get a very high percentage of the primes correct. If you used enough of them to find ALL the correct primes however, it takes longer than using other algorithms.

2) Someone has learned how to hack the test rig, resetting it's clock so it matches, or almost matches, the time the test began, or telling it to pass this test, instantly.

3) If you could create a "virtual console" in memory, and send your answers there, and have the answers checked by the test rig, from there, then you would remove a large bottleneck. Printing out the data is the biggest bottleneck to our problem, by far. Moving it up to RAM speed would be a big time saver.

0.0 seconds though? No, not legit.

Edited by Adak: n/a


Skip all even numbers. That will cut your loop in half: for(i=3;i<=root;i+=2)

And skip any number higher than the square root of the number being tested as well.
Can't remember what the proof of that is, but it works :)

Of course for small values of n, calculating n/y for all y<n might be faster than calculating sqrt(n).



We are skipping every even number and we stop at the root as well, although we don't always use the sqrt() function.

Well guys, I hate to say it but:

"I found the answer!"

It was staring me right in the face, from day 1 of this thread. Yeah, it's embarrassing, and you will be embarrassed as well, when I tell you."

I'm tweaking it around a bit right now, but it's cut the time in half, already.

Let the anticipation begin!!



We are skipping every even number and we stop at the root as well, although we don't always use the sqrt() function.

Well guys, I hate to say it but:

"I found the answer!"

It was staring me right in the face, from day 1 of this thread. Yeah, it's embarrassing, and you will be embarrassed as well, when I tell you."

I'm tweaking it around a bit right now, but it's cut the time in half, already.

Let the anticipation begin!!

Come on man!!! I can't wait!


We've noted several times that the bottleneck for the program, is printing. Every system will be different, but on my system the printing part takes 4 X's as long, as the calculation of the primes part of the program.

And look at how many times we call printf: 50,000 times.

Every call to printf() incurs a small expense, because printf() can decode a boatload of formats, etc. So use those format features, and call printf() a LOT less. How about 125 X less? ;)

125 is good because it is a factor of 50,000, and it closely matches a multiple of two and 8 (128), which is an eight of a Megabyte. Computers tend to be most efficient at those multiples of 2 and 8 sizes.

This was the print statement that knocked about 30% off my previous best time:

   p[i],   p[i+1], p[i+2], p[i+3], p[i+4], p[i+5], p[i+6], p[i+7], p[i+8], p[i+9], p[i+10],p[i+11],
   p[i+122],p[i+123],p[i+124]); }

Note that i+124 numbers is 125 numbers being printed out. You may need to lower your i<5000000 part of our printf() statement, by one (I had to, but I used a slightly different index system than most).

I initially had 45% improvement with much more than 125 numbers being printed per call to printf, and using a much larger aux1[] size, but I had to cut them both down in size, to make the program under 10KB.

Second topic:

Just an idea, completely untested.

What if we took in ALL the queries from the tester, into an array, and quickly sorted them (time: maybe 0.2-1.0 seconds). Then we start the prime finding loop, and as soon as we find one, (in Sieve loop), that matches the next request, we print it solo, just that one number and newline.***

Now look at what's changed:

* We don't need to generate all the primes, we only will generate until the highest prime number that is requested.

* No primes[] array is not needed anymore. We get the answer right from the sieve loop.

* The printing might not be such a bottleneck, since there are gaps between the queries, so the sieve loop could work on (although it would be slowed down).

*** perhaps a faster idea would be as above, but keep the printing outside the sieve loop. Print with a big number per print call, (an obvious speed up), only after the sieve loop has found all the prime numbers for us.

Maybe print out 512 numbers with each call to printf()?

We keep the primes loop, and fill it like now, but we fill it ONLY with the matching numbers from the queries. We don't need a primes array of 5 million anymore. Just 50,000 primes stored would be fine, because we'd have the right 50,000 primes.;)

Summing up, I believe the new printing idea will allow your programs to beat the test, if it's otherwise well designed, and accurate.

I am now running my test program version with this printing format string, and getting times of 2.62 seconds. That's about 1 1/2 seconds faster than before. Try it on your programs and see how it works for you.

@Diwakar, I inserted part of your sieve loop (the nested for loop at the bottom of it), and found I wasn't getting all the prime numbers - lots of them, but not all. You may want to check it (but it could be just the interaction between my loop logic, and yours, in the top half of the larger loop).


I'm putting together a version of the primes test program, using the sorting approach mentioned in the previous post. Run this program on your system, and see what times you get - I'm getting 0.000 seconds (no measurable time). Quicksort does me proud on randomized numbers! <3 Quicksort!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void quickSort(unsigned *, unsigned, unsigned);

int main(void) {
unsigned int k,maxq=0,queNum=50000, *que;
   clock_t start,stop;  start=clock();    
   //input for the test
   //fscanf(stdin, "%u",&queNum);
   que = malloc(queNum * sizeof(unsigned int));
   //for(i=0;i<queNum;i++) {fscanf(stdin,"%u",&que[i]); if(que[i] > maxq) maxq=que[i];}
   for(i=0;i<queNum;i++) {que[i] = rand() % 50000+1; if(que[i] > maxq) maxq=que[i];}
   printf("maxq: %u",maxq); //getchar();

   //sort que
   quickSort(que, 0, queNum-1);
   //Check results >> for(i=0;i<queNum;i++) {printf("i: %u  que[i]: %u\n",i, que[i]); if(i % 20 == 0) getchar();}
   stop = clock() - start; printf("\n\nTime was %f seconds\n", (double) stop/CLOCKS_PER_SEC); 

   return 0;
void quickSort(unsigned *a, unsigned lo, unsigned hi) {
   unsigned i, j, pivot,temp;
   if(lo == hi) return; 
   pivot= a[(lo+hi)/2]; 

   /* Split the array into two parts */
   do {    
      while (a[i] < pivot) i++; 
      while (a[j] > pivot) j--;
      if (i<=j) { //without the = in there, it's an endless loop
         temp= a[i];
         a[i]= a[j];
   }while (i<=j);
   if (lo < j) quickSort(a, lo, j);
   if (i < hi) quickSort(a, i, hi);

Two things of note:

1) I have a much shorter version of Quicksort - called Quickshort - if size is critical for your program (it's only 4 or 5 lines). This version is the speed demon, however.

2) The "hi" parameter to this version of Quicksort is the highest VALID index you want sorted - NOT the size of the array. (for array[500], you send it to quicksort with a hi of 500-1, not 500.).

I'll report back as soon as I get some testing done on the program, using this sorting idea, within the whole program.

Funny of the Day: Run your program for time, with the console window at it's full size. Note the time. Now shrink the window down to a tiny little 2 rows of text showing, and run it again. <crack up>

Edited by Adak: n/a


Well, well, well, dept:

Just learned that my Quicksort version does NOT work with unsigned int's in my compiler - use signed (regular) integers. I haven't tried long's or long long's yet.


Every call to printf() incurs a small expense, because printf() can decode a boatload of formats, etc.

Yeah,I couldn't agree with you more.

Second topic:

Just an idea, completely untested.

What if we took in ALL the queries from the tester, into an array, and quickly sorted them (time: maybe 0.2-1.0 seconds). Then we start the prime finding loop, and as soon as we find one, (in Sieve loop), that matches the next request, we print it solo, just that one number and newline.***

Now we're talking! That's a brilliant idea. I wondered, why would you need the number of queries in the input at first, since you're given all the numbers for which you have to find the primes for.Now I know, you're supposed to use that in your code, it's like a hint right?

Maybe print out 512 numbers with each call to printf()?

Hmm, interesting!

@Diwakar, I inserted part of your sieve loop (the nested for loop at the bottom of it), and found I wasn't getting all the prime numbers - lots of them, but not all. You may want to check it (but it could be just the interaction between my loop logic, and yours, in the top half of the larger loop).

It may be possible since i haven't tested it for all five million! But until now it has been giving correct answers.I'll check that and inform you later.
By the way, your quicksort looks fast, I'll give that a try too.


It IS fast! (Although it can be optimized to be about 15% faster, but then the code gets longer). Notice that the pivot is never moved? This ain't your grandma's Quicksort! I haven't found a faster and clearer example for Quicksort.

Your code snippet was giving me about 4.8 Million primes, out of the 5 Million. It wasn't clear why that was happening.

I got seriously sidetracked yesterday when I found out my favorite Quicksort was not working - I had never tried it with unsigned int's before. Had no idea unsigned int's would cause it to go off the rails. Took me a WHILE to nail that one down.

I'll have some timing data for my implementation of idea #2, later today. Should be interesting.

Edited by Adak: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.