Ejemplo n.º 1
0
void *find_primes(threadinfo_t *ti) {
  char buffer[100];

  int *primes = ti->logarray;
  int size = ti->arraysize;

  int file = ti->logfile;

  int start = 3 + 2*ti->thread_id;
  int delta = 2*ti->num_threads; 
  int upto = ti->max;

  
  int count = 0;
  int check = start;
  int index = 0;
  while (check < upto) {
    if (index >= size) {
       write(file,primes,index*sizeof(int));
       index = 0;
    } 
    primes[index] = check;
    index++;
    if (isprime(check)) {
      count++;
    }
    check += delta;
  }
  write(file,primes,index*sizeof(int));
  close(file);
   
  ti->result = count;

  return NULL;
}
Ejemplo n.º 2
0
PyObject *wrap_isprime(PyObject *self, PyObject *args) {
  long num;
  if (!PyArg_ParseTuple(args,"l",&num))
	 return NULL;
  return isprime(num) ? Py_True : Py_False;

}
Ejemplo n.º 3
0
/* libc_hidden_proto(hcreate_r) */
int hcreate_r (size_t nel, struct hsearch_data *htab)
{
  /* Test for correct arguments.  */
  if (htab == NULL)
    {
      __set_errno (EINVAL);
      return 0;
    }

  /* There is still another table active. Return with error. */
  if (htab->table != NULL)
    return 0;

  /* Change nel to the first prime number not smaller as nel. */
  nel |= 1;      /* make odd */
  while (!isprime (nel))
    nel += 2;

  htab->size = nel;
  htab->filled = 0;

  /* allocate memory and zero out */
  htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY));
  if (htab->table == NULL)
    return 0;

  /* everything went alright */
  return 1;
}
Ejemplo n.º 4
0
/* Get 32 bits of entropy and convert the entropy in to a 31-bit-long
 * prime number */
int main() {
        FILE *rand = 0;
        uint32_t candidate = 0;
        uint8_t get = 0;
        int a = 0;

        rand = fopen("/dev/urandom","rb");
        if(rand == 0) {
                fatal("Could not open /dev/urandom");
        }

        for(a = 0; a < 4; a++) {
                get = getc(rand);
                candidate <<= 8;
                candidate |= get;
        }

        fclose(rand);

        candidate &= 0x3fffffff;
        candidate |= 0x40000001;

        while(isprime(candidate) == 0 || num_1bits(candidate) < 16) {
                candidate += 2;
                candidate &= 0x3fffffff;
                candidate |= 0x40000001;
        }

        printf(HEADER,candidate);
        printf("/* %d has %d bits set to 1 */\n",candidate,
                num_1bits(candidate));

        return 0;
}
Ejemplo n.º 5
0
int main(){
	s[0] = s[1] = 1;
	for(int i = 2; i < MAX; i++)
		if(!s[i]){
			prime[pct++] = i;
			for(int j = i + i; j < MAX; j += i)
				s[j] = 1;
		}
	int a, b;
	while(scanf("%d%d", &a, &b) > 0){
		int dis, last = 0, max, min, maxd = 0, mind = 2147483647;
		memset(test, 0, sizeof(test));
		for(unsigned int i = a; i <= b; i++)
			if(!test[i-a] && isprime(i)){
				if(last){
					dis = i - last;
					if(dis > maxd)
						maxd = dis, max = last;
					if(dis < mind)
						mind = dis, min = last;
				}
				last = i;
				for(unsigned int j = i+i; j <= b; j += i)
					test[j-a] = 1;
		}
		if(maxd)
			printf("%d,%d are closest, %d,%d are most distant.\n", min, min + mind, max, max + maxd);
		else
			puts("There are no adjacent primes.");
	}
	return 0;
}
Ejemplo n.º 6
0
int nearest_prime(int n, float rerror)
     /* relative error; new prime will be in range
      * [n-n*rerror, n+n*rerror];
      */
{
  int bound,k;

  if (isprime(n)) return(n);
  /* assume n is large enough and n*rerror enough smaller than n */
  bound = n*rerror;
  for(k = 1; k <= bound; k++) {
    if (isprime(n+k)) return(n+k);
    if (isprime(n-k)) return(n-k);
  }
  return(-1);
}
int main() {
    unsigned long long N;
    unsigned n;
    int t;
            sieve();
            scanf("%d", &t);
            while( t-- ) {
                scanf("%llu", &N);
                if( N < 5 ) {
                    printf("2\n");
                    continue;
                }
                if( N > 4294967291LL ) {
                    printf("4294967291\n");
                    continue;
                }
                if( N & 1 ) N -= 2;
                else N -= 1;
                n = (unsigned)N;
                if( n < MAX ) {
                    for( ; ; n -= 2) if( !ifC(n) ) {
                        printf("%u\n", n);
                        break;
                    }
                }
                else {
                    for( ; ; n -= 2) if( isprime(n) ) {
                        printf("%u\n", n);
                        break;
                    }
                }
            }
            return 0;
}
Ejemplo n.º 8
0
main()
{
	long int primes[300000];
	long int i,j;
	long long int sum;
	
	primes[0]=2;
	primes[1]=3;

	i=4;j=2;sum=5LL;

	while (i<2000000)
	{
		if (isprime(i,primes,j))
		{
			primes[j]=i;
			sum += i;
			if( j%10000 == 0 )
			{
				printf("primes[%ld] = %ld\n",j,primes[j]);
			}
			j++;
/*			printf("primes[%d] = %d\n",j-1,primes[j-1]); */
		}
		i++;
	}
	printf("The sum of primes below two million is %lld\n",sum);
}	
Ejemplo n.º 9
0
int main(void)
{
    int count, i, j, k, m, n;
    int ri, looptimes;

    looptimes = GetInteger();
    for (ri = 1; ri <= looptimes; ++ri) {
        m = GetInteger();
        n = GetInteger();
        printf("primes:\n");
        count = 0;
        for (j = m; j <= n; ++j) {
            if (isprime(j)) {
                if (count == 6) {
                    printf("\n");
                    count = 0;
                }
                if (count != 0) {
                    printf(" ");
                }
                ++count;
                printf("%d", j);
            }
        }
        printf("\n");
    }
    return 0;
}
Ejemplo n.º 10
0
int main(void)
{
   int     pp;
   long    k;
   clock_t tt;

   k = 3;

   for (;;) {
      /* start time */
      tt = clock();

      /* test if 2^k - 1 is prime */
      if (is_mersenne(k, &pp) != MP_OKAY) {
         printf("Whoa error\n");
         return -1;
      }

      if (pp == 1) {
         /* count time */
         tt = clock() - tt;

         /* display if prime */
         printf("2^%-5ld - 1 is prime, test took %ld ticks\n", k, tt);
      }

      /* goto next odd exponent */
      k += 2;

      /* but make sure its prime */
      while (isprime(k) == 0) {
         k += 2;
      }
   }
}
Ejemplo n.º 11
0
int main(){
    /* 老樣子的篩法 */
    int i = 2, j;
    while(i < TOP){
        primes[primen++] = i;
        j = i * 2;
        while(j < TOP){
            notprime[j] = 1;
            j += i;
        }
        i++;
        while(i < TOP && notprime[i]) i++;
    }
    
    int a, bp, founda, foundb, maxn = 0;
    for(bp = 0; primes[bp] < 1000; bp++){    /* b 列舉小於 1000 的質數即可 */
        int b = primes[bp];
        for(a = -999; a < 1000; a++){    /* a 則只好全都列舉了 */
            long long n;
            for(n = 1; isprime(n*n+a*n+b); n++);
            n--;
            if(n > maxn){   /* 找到更高的 K 就更新 */
                founda = a;
                foundb = b;
                maxn = n;
            }
        }
    }
    
    printf("%d\n", founda*foundb);
    return 0;
}
int main()
{
    int k,i,max,a;
    scanf("%d",&k);
    int *arr=new int [k];
    max=0;
    for(i=0;i<k;i++)
    {
        scanf("%d",&arr[i]);
        if(arr[i]>max)
            max=arr[i];
    }
    int *p=new int[max];
    a=2;
    i=0;
    while(i<max)
    {
        if(isprime(a)==true)
            p[i++]=a;
        a++;
    }
    for(i=0;i<k;i++)
        printf("%d\n",p[arr[i]-1]);
        return EXIT_SUCCESS;
}//you can check only 6*i-1 or 6*i+1 integers;prime number can't be any other type:))
Ejemplo n.º 13
0
Archivo: parse.c Proyecto: ah42/cuda-p1
int valid_assignment(int exp, int fftlen)
/*
 returns 1 if the assignment is within the supported bounds of CUDALucas,
 0 otherwise.
 */
{
	int ret = 1;
	
	// Perhaps add a largest exponent?
	if (exp < 8000) {
		ret = 0;
		fprintf(stderr, "Warning: exponents < 8000 are not supported!\n");
	}
	if (!isprime(exp)) {
		ret = 0;
		fprintf(stderr, "Warning: exponent is not prime!\n");
	}
	if (fftlen % (1024)) {
		ret = 0;
		fprintf(stderr,
				"Warning: FFT length %d is invalid, it must be a multiple of 1024. See CUDALucas.ini for more details about good lengths.\n",
				fftlen);
	}
	// This doesn't guarantee that it *is* valid, but it will catch horribly bad lengths.
	// (To do more checking, we'd need access the "threads" variable from CUDALucas.cu.)
	
	return ret;
}
Ejemplo n.º 14
0
int main(void) {
	
	int answer;
	int multipleofprimes = 1;
	int i;
	
	for(i = 20; i >= 2; i--)
	{
		int temp = 1;
		int h = isprime(i); 
		if(h)
		{
			printf("%d is a prime.\n", h);
				while(temp<20)
				{
					temp*=h;
				}
				multipleofprimes*=temp/h;
		}
	}
	
	printf("%d", multipleofprimes);
	
	return 0;
}
Ejemplo n.º 15
0
long PerlinNoise::randomPrime(int v)
{
	std::vector<long> primeList;
	long i = 0;
	int i1 = 0;
	int i2 = 0;
	if(v==1){
		i1 = 15000000;
		i2 = 20000000;
	}
	else if(v==2){
		i1 = 40000;
		i2 = 90000;
	}
	if(primeList.size() == 0)
	{
		primeList.clear();
		primeList.cbegin();
		for (i=i1; i<i2; i++)
		{
			if (isprime(i))
			{
				primeList.push_back(i);
			}
		}
	}
	//int listLength = primeList.size();
	//std:: cout << listLength << std::endl << std::endl;
	int blah = rand()%primeList.size();
	//std::cout << blah << std::endl;
	long prime = primeList[blah];
	std::cout << prime << std::endl;
	return prime;
}
Ejemplo n.º 16
0
int main()
{
	unsigned int n=1;
	unsigned int i;
	while(n){
		scanf("%u", &n);
		if( isprime(n) ){
				printf(" %u is normal \n", n);
				continue;
		}
		for(i = 2; i < n; i++) {
			int ret =  mod(i, n, n);
#if 0
			printf(" %u, %u - %d\n", i, n, ret);
#endif
			if( ret != i){
				printf(" %u is normal \n", i);
				break;
			}	
		}	
		if( i == n ){
			printf("The number %u is a Carmichael number \n",n);
		}
	}	
}
Ejemplo n.º 17
0
/* Before using the hash table we must allocate memory for it.
   We allocate one element more as the found prime number says.
   This is done for more effective indexing as explained in the
   comment for the hash function.  */
static bool htab_create(struct libusb_context *ctx, unsigned long nel)
{
	if (htab_table != NULL) {
		usbi_err(ctx, "hash table already allocated");
		return true;
	}

	// Create a mutex
	usbi_mutex_init(&htab_write_mutex);

	// Change nel to the first prime number not smaller as nel.
	nel |= 1;
	while (!isprime(nel))
		nel += 2;

	htab_size = nel;
	usbi_dbg("using %lu entries hash table", nel);
	htab_filled = 0;

	// allocate memory and zero out.
	htab_table = calloc(htab_size + 1, sizeof(htab_entry));
	if (htab_table == NULL) {
		usbi_err(ctx, "could not allocate space for hash table");
		return false;
	}

	return true;
}
Ejemplo n.º 18
0
int main(int argc, char *argv[])
{
int n,                           /* loop variables */
    pc,                          /* prime counter */
    foundone;                    /* most recent prime found */
  struct timeval t_begin, t_end, t_diff, t_total;
//printf("Starting. Numbers to be scanned= %d\n",LIMIT);
gettimeofday(&t_begin, NULL);
pc=4;     /* Assume the primes less than 10 (2,3,5,7) are counted here */

for (n=11; n<=LIMIT; n=n+2) {
   if (isprime(n)) {
      pc++;
      foundone = n;
      /***** Optional: print each prime as it is found 
      printf("%d\n",foundone);
      *****/
      }			
   //if ( (n-1)%PRINT == 0 ) 
    //  printf("Numbers scanned= %d   Primes found= %d\n",n-1,pc);
   }
gettimeofday(&t_end, NULL);  
timeval_subtract(&t_diff, &t_end, &t_begin);
//printf("Done. Largest prime is %d Total primes %d\n",foundone,pc);
printf("%d %ld.%06ld\n",LIMIT,t_diff.tv_sec, t_diff.tv_usec);
} 
int main()
{
int n,f,i,j,k,p,q;
while(1)
{
f=0;
scanf("%d",&n);
if(n==0)return 0;
for(i=0;i<25;i++)
{
p=n;
p-=c[i]*c[i]*c[i];
for(j=0;j<168;j++)
{
q=p;
q-=s[j]*s[j];
if(isprime(q))
{
f=1;
printf("%d %d %d\n",q,s[j],c[i]);
break;
}
}
if(f)break;
}
if(f==0)printf("0 0 0\n");
}
return 0;
}
Ejemplo n.º 20
0
Archivo: ptime.c Proyecto: nphuc/alg
int main(){
    int n;
    scanf("%d",&n);
    memset(res,0,sizeof(int)*(n+2));
    register int i,t,k,l=2;
    for(i=2;i<=n;++i){
        k=i;
        t=2;
        while(t*t<=k && k!=1){
            if(isprime(t) && (k%t==0)){
                k=k/t;
                res[t]++;
                if(t>l)l=t;
            }else{
                t++;
            }
        }
        if(k!=1){
            res[k]++;
            if(k>l)l=k;
        }

    }
    for(i=2;i<=n;++i){
        if(res[i]==0)continue;
        if(i==l){
            printf("%d^%d",i,res[i]);
        }else{
            printf("%d^%d * ",i,res[i]);
        }
    }
    return 0;
}
Ejemplo n.º 21
0
Archivo: eu058.c Proyecto: pbevin/euler
void
eu058(char *ans) {
  const int MAX = 700000000;
  int i, n = 1;
  //char *sieve = malloc(MAX);
  int primes = 0, nonprimes = 1;

  //gensieve(sieve, MAX);

  // Increment is 2,2,2,2, 4,4,4,4, 6,6,6,6, 8,8,8,8, ...
  for (i = 2; n < MAX; i += 2) {
    for (int j = 0; j < 4; j++) {
      if (isprime(n)) primes++;
      else nonprimes++;
      n += i;
    }
    // primes ratio is primes / (primes+nonprimes)
    // which is under 10% if 10 * primes / (primes+nonprimes) < 1
    if (primes * 10 < primes + nonprimes) {
      // Side length is increment plus one
      sprintf(ans, "%d", i+1);
      break;
    }
  }
  //free(sieve);
}
int main()
{
    int n;
    scanf("%d",&n);
    printf("%s\n",isprime(n)?"\\t":"\\n");
    return 0;
}
Ejemplo n.º 23
0
int main ( int argc, char** argv )
{
   int i;
   for (i = 79000; i < 81000; i++)
     if (isprime(i)) { printf ( "%d ", i ); fflush(stdout); }
   return 0;
}
Ejemplo n.º 24
0
int main()
{
	float t = clock();
	std::set <unsigned long long int> num;
	for (int a = 7654321 ; a > 6754321 ; a -= 2 ){
		if (check(a) == 0){
			num.insert(a);
		}
	}

	for (auto a = num.end() ; a != num.begin() ; --a){
		if (ispan(*a) == 1){
			num.erase(*a);
		}
	}

	for (auto a = num.end() ; a != num.begin() ; --a){
		if (isprime(*a) == 0){
			std::cout << *a << std::endl;
			goto po;
		}
	}
	po:
	std::cout << (clock() - t) / CLOCKS_PER_SEC << " s " << std::endl;
	return 0;
}
Ejemplo n.º 25
0
int main()
{
   int num;
   for (num = 3; num < N; num++)
      if (isprime(num))
         printf("%d\n", num);
}
Ejemplo n.º 26
0
int main()
{
        int m,n,count=1;
        scanf("%d%d",&m,&n);
        int i=2,cnt=0;
        if(m==1){
                printf("%d",i);
                cnt=1;
                if(m<n)
                        putchar(' ');
        }
        for(i=3;;i+=2){
                if(isprime(i)){
                        count++;
                        if(count>=m&&count<n){
                                if(cnt<9){
                                        printf("%d ",i);
                                        cnt++;
                                }
                                else{
                                        printf("%d\n",i);
                                        cnt = 0;
                                }
                        }
                        else if(count==n)
                                printf("%d",i);
                        else if(count>n)
                                break;
                }
        }
        return 0;
}
Ejemplo n.º 27
0
Archivo: stdfn.c Proyecto: ahe01/rufus
/*
 * Before using the hash table we must allocate memory for it.
 * We allocate one element more as the found prime number says.
 * This is done for more effective indexing as explained in the
 * comment for the hash function.
 */
BOOL htab_create(uint32_t nel, htab_table* htab)
{
	if (htab == NULL) {
		return FALSE;
	}
	if (htab->table != NULL) {
		uprintf("warning: htab_create() was called with a non empty table");
		return FALSE;
	}

	// Change nel to the first prime number not smaller as nel.
	nel |= 1;
	while(!isprime(nel))
		nel += 2;

	htab->size = nel;
	htab->filled = 0;

	// allocate memory and zero out.
	htab->table = (htab_entry*)calloc(htab->size + 1, sizeof(htab_entry));
	if (htab->table == NULL) {
		uprintf("could not allocate space for hash table\n");
		return FALSE;
	}

	return TRUE;
}
Ejemplo n.º 28
0
int main()
{
	int i;
	int j;
	long long int num = 1;
	const long long int change = 1234567890LL;
	const long long int mod = 1<<31;
	long long int out =0;
	init();
	for ( i = 0 ; i < LIMIT ; i ++ )
	{
		
		if ( num == 1 ) out += 1;
		else if ( num == 0 ) out += 1;
		else out = out + isprime(num)+1;
		out *= 10;
		num = (num + change) % mod;
		
		if(i % LONG_INT_LIM == LONG_INT_LIM-1) {
		
			printf("%018lld", out/10 - 111111111111111111LL);
			out = 0;
		}
	}
	output[i] = '\0';
	printf("%s", output);
	return 0;										// Successful termination
}
Ejemplo n.º 29
0
Archivo: p1-4.c Proyecto: kingfree/haut
int main(void) {
    int n;
    int isprime(int n);
    scanf("%d", &n);
    printf("%d %s素数。\n", n, isprime(n) ? "是" : "不是");
    return 0;
}
Ejemplo n.º 30
0
int main( int ac, char *av[] )
{
    mpz_t n, tmp, max_n;

    /*  コマンドラインから素数探索範囲を決定する    */
    if( ac < 2 )
        return( 1 );
    mpz_init_set_str( max_n, av[1], 10 );
    mpz_init( n );
    mpz_init( tmp );

    /*  探索範囲の数を調べる    */
    mpz_set_ui( n, 1 );
    while( mpz_cmp( n, max_n ) <= 0 ) {
        if( isprime( n ) )
            gmp_printf( "%Zd\n", n );
        mpz_add_ui( tmp, n, 1 );
        mpz_set( n, tmp );
    }

    mpz_clear( n );
    mpz_clear( tmp );
    mpz_clear( max_n );

    return( 0 );
}