#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Print 10000000 primes


    Code:
    #include <stdio.h>
    #include <math.h>
    #include <mem.h>
    #define MAX 10000000
    
    int IsPrimes(char* NT, int x)
    {
    	return ((NT[x/8] >> (x%8))&1);
    }
    void Gan(char* NT, int x, int GiaTri)
    {
    	if (GiaTri)
    		NT[x/8] = NT[x/8] | (1<<(x%8));
    	else
    		NT[x/8] = NT[x/8] & (~(1<<(x%8)));		
    }
    int Primes(int n)
    {
    	char NT[MAX/8]; 
    	int i,j;
    	int Dem = 0;
    	for (i=0;i<MAX;i++) Gan(NT,i,1);
    	memset(NT,0xFF,MAX/8);
    	
    	for (i=2;i<MAX;i++)
    		if ( IsPrimes(NT,i) )  
    		{	
    			if (Dem==n) return i;
    			Dem++;
    			for (j=i<<1; j<MAX ;j+=i)
    				Gan(NT,j,0); 
    		}
    }
    main()
    {
    	FILE* f;
    	int n,m;
    	f = fopen("3.inp","rt");
    	fscanf(f,"%d",&n);
    	while (n--)
    	{	
    		fscanf(f,"%d",&m);
    		printf("%d\n",Primes(m));
    	}	
    	fclose(f);
    }
    I can't understand this code.Anyone can help me :chomp: :chomp:
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    It's probably the algorithm, not the code, giving you trouble. Maybe someone reading this forum will recognize and explain it. Maybe you'll need to research prime number algorithms until you understand. At a glance, I'm clueless. Oh wait, I'll guess better than "clueless". It's a sieve method packing the table onto bits.
    Last edited by b49P23TIvg; January 7th, 2014 at 10:49 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo