The number of sieve method is not described in detail here, this paper focuses on the use of the prime number of sieve method to optimize the number of the table to reduce its space consumption. To compress the space consumption of prime tables, you can use bit manipulation. The following is a sample code of prime numbers within 100 using the Sieve prime method (Note 2):
#include <stdio.h>#include<memory.h>Const intMAXN = -;BOOLFLAG[MAXN];intPRIMES[MAXN/3+1], pi;//for each prime number, its multiple must not be a prime number. //There are many repetitions such as flag[10] will be visited once when accessing flag[2] and Flag[5]voidgetprime_1 () {intI, J; Pi=0; memset (Flag,false,sizeof(flag)); for(i =2; i < MAXN; i++) if(!Flag[i]) {Primes[pi++] =i; for(j = i; J < maxn; J + =i) flag[j]=true; }}voidPrintfarray () { for(inti =0; i < pi; i++) printf ("%d", Primes[i]); Putchar ('\ n');}intMain () {printf ("the number of prime \n--by Morewindows (http://blog.csdn.net/MoreWindows)--\n\n is obtained by using the Sieve prime method of 100."); Getprime_1 (); Printfarray (); return 0;}
The results of the operation are as follows:
In the above program is marked with a bool array, bool type data accounted for 1 bytes (8 bits), so the use of bit operations to compress the space occupied will reduce the space consumption by seven-eighths.
Consider how to set 1 for a given position in an array, and first consider how to place 1 on an integer at the specified position. For an integer, the code looks like this by shifting the 1 to the left and then to the effect of placing 1 on the specified bit, as follows:
// position 1 on a digital finger int 0 ; | = 1; printf ("%d\n", J);
Similarly, 1 can be shifted to the left and the original number to determine whether the positioning is 0 or 1 (you can also shift the original number to the right of a number of digits and 1 phase).
// determine if the position is 0 or 1. int 1 Ten ; if ((J & (10) printf (" refer to 1 on positioning" ); Else printf (" 0 on positioning");
Extended to the array, we can take this approach, because the array is in memory is also a contiguous allocation of space, can "think" is a very long integer. First write a test code to see how to use bit manipulation in an array:
#include <stdio.h>intMain () {printf ("positions the specified position in the array and determines the bit \ n"); printf ("---by morewindows (http://blog.csdn.net/MoreWindows)---\ n"); //writes 1 in the array at the specified position intb[5] = {0}; inti; //Write 1 in the first position for(i =0; I < +; i + =3) B[i/ +] |= (1<< (i% +)); //output the entire Bitset for(i =0; I < +; i++) { if((B[i/ +] >> (i% +)) &1) Putchar ('1'); ElsePutchar ('0'); } Putchar ('\ n'); return 0;}
The results of the operation are as follows:
It can be seen that the array is set to 1 per 3, proving that we have the correct method of bit manipulation on the array. Therefore, the above method can be changed to use a bit operation compressed sieve prime method:
#include <stdio.h>#include<memory.h>Const intMAXN = -;intFLAG[MAXN/ ++1];intPRIMES[MAXN/3+1], pi;voidgetprime_1 () {intI, J; Pi=0; memset (Flag,0,sizeof(flag)); for(i =2; i < MAXN; i++) if(! ((Flag[i/ +] >> (i% +)) &1) ) {Primes[pi++] =i; for(j = i; J < maxn; J + =i) flag[j/ +] |= (1<< (j% +)); }}voidPrintfarray () { for(inti =0; i < pi; i++) printf ("%d", Primes[i]); Putchar ('\ n');}intMain () {printf ("the number of prime \n--by Morewindows (http://blog.csdn.net/MoreWindows)--\n\n is obtained by using the bit operation to compress the number of sieve primes by 100."); Getprime_1 (); Printfarray (); return 0;}
The same operation results are:
In addition, you can use the Bitset class in C + + STL as the Prime number table. The number of screening methods in the test interview appear the odds are relatively large, can write a bit operation compression of the sieve number method will undoubtedly make your code stand out, so it is strongly recommended that the reader himself to implement it again, usually more efforts, the exam is not panic.
Bit manipulation of the compression space technique is also used to implement the Strtok function, please refer to the Strtok source profiling bit operation and space compression (http://blog.csdn.net/morewindows/article/details/8740315)
Bit operation and space compression