Exemplo n.º 1
0
Arquivo: 027.c Projeto: fand/euler
int main(){
  test_prime();
  
  int maxa = 0;
  int maxb = 0;
  int maxn = 0;  
  
  for (int a = -1000; a < 1001; a++) {
    for (int b = -1000; b < 1001; b++) {

      int n = 0;
      while (true) {
        if (! is_prime(value(n, a, b))) break;
        n++;
      }
      if (n > maxn) {
        maxa = a;
        maxb = b;
        maxn = n;
        printf("a: %d, b: %d, n: %d\n", a, b, n);
      }
    }
  }

  printf("a: %d, b: %d\n", maxa, maxb);
  printf("product: %d\n", maxa * maxb);  
  return 0;
}
Exemplo n.º 2
0
Arquivo: p10.c Projeto: ipeet/euler
int main() {
	const unsigned int arr_sz = 250000; //gues 250k as an upper bound for primes below 2M
	unsigned int primes[arr_sz];
	primes[0] = 2;
	primes[1] = 3;
	primes[2] = 5;
	unsigned int next = 3;
	int i;
	for(i = 6; i<2000000; i++) {
		if(next>=arr_sz) {
			printf("Array too small!\n");
			return 1;
		}
		if(test_prime(i,primes,next)) {
			//printf("%d is prime\n",i);
			primes[next] = i;
			++next;
		}
	}
	printf("Found %d primes!\n",next);
	unsigned long sum = 0;
	for(i = 0; i<next; i++) {
		if(sum>(1UL<<63)) {
			printf("Overflow detected!\n");
			return 1;
		}
		sum+=primes[i];
	}
	printf("Sum is %ld.\n",sum);
	return 0;
}
Exemplo n.º 3
0
static int
calc_prime (int x)
{
	int i;
	
	for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
		if (test_prime (i))
			return i;
	}
	return x;
}
Exemplo n.º 4
0
/* main function */
int program12_4(void){
    unsigned long long trial = 5ULL;        /* Prime candidate  */
    unsigned long num_primes = 3UL;         /* Primer count     */
    unsigned long total = 0UL;              /* Total required   */

    printf("How many primes would you like? ");
    scandf("%lu", &total);          /* Total is how any we need to find */
    total = total<4UL ? 4UL:total;  /* Make sure it is at least 4       */

    /* Prime finding and storing loop */
    while(num_primes < total){          /* Loop until we get total required */
        trial += 2ULL;                  /* Next value for checking          */
        if(test_prime(trial)){          /* Check if trial is prime          */
            global.primes[global.index++] = trial;      /* so store it      */
            num_primes++;               /* Increment total number of primes */

            if(global.index == MEM_PRIMES){     /* Check if array is full   */
                /* File Opened OK ? */
                if(!(global.pfile = fopen(global.filename, "ab"))){
                    /* No, so explain and end the program */
                    printf("\nUnable to Open %s to append\n", global, filename);
                    exit(1);
                }

                /* Write the array */
                fwrite(global.primes, sizeof(unsigned long long),
                        MEM_PRIMES, global.pfile);

                fclose(global.pfile);       /* Close the file                   */
                global.index = 0U;          /* Reset count of primes in memoery */
                global.nrec++;              /* Increment file record count      */
            }
        }
    }

    if( total > MEM_PRIMES)         /* If we wrote some to file         */
        put_primes();               /* Display the contents of the file */
    if( global.index )              /* Display any left in memory       */
        for(size_t i=0; i<global.index; i++){
            if( i%5 == 0)
                printf("\n");                       /* Newline after five   */
            printf("%12llu", global.primes[i]);     /* Output a prime       */
        }

    if ( total>MEM_PRIMES )                                         /* Did we need a file?  */
        if( remove(global.filename))                                /* the delete it.       */
            printf("\nFailed to delete %s\n", global.filename);     /* Delete failed        */
        else
            printf("\nFile %s deleted.\n", global.filename);        /* Delete OK            */

    return 0;
}
Exemplo n.º 5
0
int nprime(int N)
{
	if( N == 1)
	{
		Array1[0] = 2;
		Array1[1] = 3;
		Array1[2] = 5;
		Array1[3] = 7;
		return 4;
	}
	int num = nprime(N - 1);
	int total=0;
	int i = 0;
	if(N % 2 == 0){
		for(; i < num ; i++){
			if(test_prime(Array1[i] * 10 + 1))
				Array2[total++] =Array1[i] * 10 + 1;
			if(test_prime(Array1[i] * 10 + 3))
				Array2[total++] =Array1[i] * 10 + 3;
			if(test_prime(Array1[i] * 10 + 7))
				Array2[total++] =Array1[i] * 10 + 7;
			if(test_prime(Array1[i] * 10 + 9))
				Array2[total++] =Array1[i] * 10 + 9;
		}
	}
	else{
		for(; i < num ; i++){
			if(test_prime(Array2[i] * 10 + 1))
				Array1[total++] =Array2[i] * 10 + 1;
			if(test_prime(Array2[i] * 10 + 3))
				Array1[total++] =Array2[i] * 10 + 3;
			if(test_prime(Array2[i] * 10 + 7))
				Array1[total++] =Array2[i] * 10 + 7;
			if(test_prime(Array2[i] * 10 + 9))
				Array1[total++] =Array2[i] * 10 + 9;
		}
	}
	return total;
}
Exemplo n.º 6
0
int main(int argc, char **argv)
{
	test_prime(&gnutls_srp_8192_group_prime);
	test_prime(&gnutls_srp_4096_group_prime);
	test_prime(&gnutls_srp_3072_group_prime);
	test_prime(&gnutls_srp_2048_group_prime);
	test_prime(&gnutls_srp_1536_group_prime);
	test_prime(&gnutls_srp_1024_group_prime);

	test_prime(&gnutls_ffdhe_8192_group_prime);
	test_prime(&gnutls_ffdhe_6144_group_prime);
	test_prime(&gnutls_ffdhe_4096_group_prime);
	test_prime(&gnutls_ffdhe_3072_group_prime);
	test_prime(&gnutls_ffdhe_2048_group_prime);

	return 0;
}