Esempio n. 1
0
int main()
{
	int *a = createPrime(20000);
	int i;
	for(i = 0; i < 20000; i++)
		printf("%d\n",a[i]);

}
Esempio n. 2
0
int countPrimes(int n) 
{
	int *a = createPrime(n, n);
	int i; int count = 0;
	for(i = 1; i < n; i++)
	{
		if(a[i] < n) count++;
		else break;
	}
	return count;
}
Esempio n. 3
0
/*
 * 初始化素数信息, 其中bound表示大致估计的需要判断素数的一个范围
 */
void initPrime(long bound) {
  FILE *fin = fopen(FILENAME, "rb");
  FILE *fout;
  long i, j, k, length;
  long len, num;

  printf("Initializing New Prime_List...\n");

  if (fin == NULL) {    
    /* 若文件不存在, 则创建一个新的 */
    createPrime(bound);
  }
  else {
    /* 否则从文件中读取信息 */

    fscanf(fin, "%ld", &length);

    /* 开辟数组空间 */
    len = (bound % bits == 0? bound / bits : bound / bits + 1);
    NEWLIST(len);

    /*
     * 从文件中读取信息
     * 这里出现两种情况:
     * 1. 文件中的信息大于给定的上限, 只需要读取即可
     * 2. 文件给出的信息小于上线, 除了需要读取文件信息外, 还需要将对未给出的信息进行初始化判断
     */    
    for (i = 0; i < SMALL(length,len); i++) {
      fscanf(fin, "%ld", &primeList[i]);
    }
    fclose(fin);

    list_len = length;

    /* 若文件给出信息不足, 则继续进行初始化判断 */
    if (len > length) {

      for (;i < len; i++) {
	for (j = 0; j < bits; j++) {
	  num = i * bits + j;
	  if (!checkPrime(num))
	    SETLIST(num);
	}
      }

      /* 将新的信息写入文件 */
      fout = fopen("prime.dat", "wb");
      fprintf(fout, "%ld\n", len);
      for (i = 0; i < len; i++) {
	fprintf(fout, "%ld\n", primeList[i]);
      }			      
      fclose(fout);

      list_len = len;
    }

  }

  printf("Initializing DONE\n");
  return;

}