示例#1
0
文件: randprime.c 项目: gale320/imath
mp_result find_strong_prime(mp_int seed, FILE *fb)
{
  mp_result res;
  mpz_t     t;

  mp_int_init(&t);
  for(;;) {
    if ((res = find_prime(seed, fb)) != MP_TRUE)
      break;
    if ((res = mp_int_copy(seed, &t)) != MP_OK)
      break;

    if ((res = mp_int_mul_pow2(&t, 1, &t)) != MP_OK ||
	(res = mp_int_add_value(&t, 1, &t)) != MP_OK) 
      break;

    if ((res = mp_int_is_prime(&t)) == MP_TRUE) {
      if (fb != NULL)
	fputc('!', fb);

      res = mp_int_copy(&t, seed);
      break;
    }
    else if (res != MP_FALSE) 
      break;

    if (fb != NULL)
      fputc('x', fb);
    if ((res = mp_int_add_value(seed, 2, seed)) != MP_OK)
      break;
  }

  mp_int_clear(&t);
  return res;
}
示例#2
0
文件: prime.c 项目: Jason-Vic/Victor
/*************************************************
  Function:       main
  Description:    实现基本输入输出
  Calls:          find_prime
  Called By:      no
  Input:          no
  Output:         no
  Return:         0
  Others:         no
*************************************************/
int main()
{
	printf("\033[40;33mthe sushu of 1-100 have:\033[0m\n");
	find_prime();            /* 调用函数 */
	
	return 0;

}
示例#3
0
/**
 * Initialises a new htable.
 *
 * @param capacity the size of the hash-table.
 * @param hashing_type the type of hashing.
 *
 * @return htable the new hash-table.
 */
htable htable_new(int capacity, hashing_t hashing_type){
    int i;
    htable result = emalloc(sizeof(*result));

    capacity = find_prime(capacity);
    result->capacity = capacity;
    result->num_keys = 0;
    result->keys = emalloc(capacity * sizeof(result->keys[0]));
    result->count = calloc(capacity, sizeof(int));
    result->hashing_type = hashing_type;
    
    for (i = 0; i < capacity; i++){
        result->keys[i].key = NULL;
    }
    return result;
}
int find_prime(int n, int count) {
	if (n <= 1) {
		return 0;
	}

  	else if (count > n/2) {
  		return 1;
  	}

  	else if (n % count == 0) {
  		return 0;
  	}

  	else {
		return find_prime(n, count+1);
	}
}
int find_prime(int n, int i) {
  if(n < 2) {
    return 0;
  }
  if (n == 2) {
    return 1;
  }
  if (n % 2 == 0 || n % 3 == 0) {
    return 0;
  }
  if (n % 5 == 0 || n % 7 == 0) {
    return 0;
    }
  else {
    return 1;
  }
  return find_prime(n, i + 1);
}
int main()
{
    int n,no,max_prime_n,max_prime=1,min_no=20000,i,primes[20010];
    find_prime(primes);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&no);
        max_prime_n=find_max_prime(primes, no);
        if(max_prime_n>max_prime)
        {
            max_prime=max_prime_n;
            min_no=no;
        }
    }
    printf("%d",min_no);
    return 0;
}
示例#7
0
int main(){
    find_prime(MAXN-1);
    factoring();
    scanf("%d",&n);
    dfs(0,n,n);
    long long temp = result;

    if( std::abs(result- temp) < std::abs(result - (temp+1)) ){
        std::cout << temp <<std::endl;
    }
    else {
        std::cout << temp+1  <<std::endl;
    }

    

    return 0;
}
int main()
{
    int i,n,primes[32000],cnt;
    find_prime(primes,32000);
    while(1)
    {
        scanf("%d",&n);
        if(n==0)
            break;
        cnt=0;
        for(i=3;i<=n/2;i+=2)
        {
            if(primes[i]==1&&check_prime(primes,n-i))
                cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}
int main()
{
    int j,primes[10010],n,cnt,sum,k;
    find_prime(primes,10010);
    while(1)
    {
        scanf("%d",&n);
        if(!n)
            break;
        j=3;
        sum=2;
        cnt=0;
        while(sum<n)
        {
            if(primes[j])
                sum+=j;
            j+=2;
        }
        if(sum==n)
            cnt++;
        for(j=3;j<=n;j+=2)
        {
            if(primes[j])
            {
                sum=0;
                k=j;
                while(sum<n)
                {
                    if(primes[k])
                        sum+=k;
                    k+=2;
                }
                if(sum==n)
                    cnt++;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}
示例#10
0
文件: hash.c 项目: rcarbone/ark
void hash_table_init (struct hash_table *h)
{
  int i = 0;

  if (!h)
    return;

  if (h->size < 1)
    h->size = 100;
  if (!h->func)
    h->func = hash_pjw;

  h->size = (unsigned int) find_prime (h->size);
  h->tbl = (struct hlist *) malloc (h->size * (sizeof (struct hlist)));
  if (!h->tbl)
    return;
  memset (h->tbl, 0, (h->size * sizeof (struct hlist)));

  for (i = 0 ; i < h->size ; i++) {
    list_init (&(h->tbl[i]));
  }

  return;
}
int is_prime_number(int n)
{
  n = find_prime(n, 2);
  return (n);
}
示例#12
0
int main(void)
{
	std::cout<<"Prime number no. 10001 is "<<find_prime(10001, 1000000)<<std::endl;
	return 0;
}
示例#13
0
void
output_all_gnu_mo_files(void)
{
	struct catalog	*p, *op;
	struct messages	*m;
	size_t	id_len, str_len, id_off, str_off, ids_top, strs_top;
	unsigned int	*hash_tbl;
	unsigned int	hash_size;
	unsigned int	num = 0, fnum = 0, unum = 0;
	unsigned int	i, idx;
	char	*ids, *strs;
	struct msgtbl	*id_tbl, *str_tbl;
	struct gnu_msg_info	header;
	FILE	*out;

	p = catalog_head;

	while (p) {
		num += p->nmsg;
		fnum += p->fnum;
		unum += p->unum;


		free(p->thash);
		if (p->nmsg == 0) {
			/*
			 * no message in this file
			 * skip generating a mo
			 */
			goto skip;
		}

		if (p->header)
			num--;

		p->msg = (struct messages *)Xrealloc(p->msg,
			sizeof (struct messages) * p->nmsg);

		/*
		 * Sort the message array
		 */
		qsort(p->msg, p->nmsg, sizeof (struct messages),
			(int (*)(const void *, const void *))msg_cmp);


		hash_size = find_prime(p->nmsg);
		hash_tbl = (unsigned int *)Xcalloc(hash_size,
			sizeof (unsigned int));


		/* Setting Header info */
		header.magic = GNU_MAGIC;
		header.revision = GNU_REVISION;
		header.num_of_str = p->nmsg;
		header.off_msgid_tbl = sizeof (struct gnu_msg_info);
		header.off_msgstr_tbl = sizeof (struct gnu_msg_info) +
			p->nmsg * sizeof (struct msgtbl);
		header.sz_hashtbl = hash_size;
		header.off_hashtbl = header.off_msgstr_tbl +
			p->nmsg * sizeof (struct msgtbl);

		m = p->msg;

		id_len = 0;
		str_len = 0;
		for (i = 0; i < p->nmsg; i++) {
			id_len += m[i].id_len;
			str_len += m[i].str_len;
		}
		ids = (char *)Xmalloc(id_len);
		strs = (char *)Xmalloc(str_len);
		id_tbl = (struct msgtbl *)Xmalloc(sizeof (struct msgtbl) *
			p->nmsg);
		str_tbl = (struct msgtbl *)Xmalloc(sizeof (struct msgtbl) *
			p->nmsg);
		id_off = 0;
		str_off = 0;
		ids_top = header.off_hashtbl +
			sizeof (unsigned int) * hash_size;
		strs_top = ids_top + id_len;
		for (i = 0; i < p->nmsg; i++) {
			/*
			 * Set the hash table
			 */
			idx = get_hash_index(hash_tbl, m[i].hash, hash_size);
			hash_tbl[idx] = i + 1;

			/*
			 * rearrange msgid and msgstr
			 */
			id_tbl[i].len = m[i].id_len - 1;
			str_tbl[i].len = m[i].str_len - 1;
			id_tbl[i].offset = id_off + ids_top;
			str_tbl[i].offset = str_off + strs_top;
			(void) memcpy(ids + id_off, m[i].id, m[i].id_len);
			(void) memcpy(strs + str_off, m[i].str, m[i].str_len);
			id_off += m[i].id_len;
			str_off += m[i].str_len;
			free(m[i].id);
			free(m[i].str);
		}

		if ((out = fopen(p->fname, "w")) == NULL) {
			error(gettext(ERR_OPEN_FAILED), p->fname);
			/* NOTREACHED */
		}

		/* writing header */
		(void) fwrite(&header, sizeof (struct gnu_msg_info),
			1, out);

		/* writing msgid offset table */
		(void) fwrite(id_tbl, sizeof (struct msgtbl),
			p->nmsg, out);
		/* writing msgstr offset table */
		(void) fwrite(str_tbl, sizeof (struct msgtbl),
			p->nmsg, out);
		/* writing hash table */
		(void) fwrite(hash_tbl, sizeof (unsigned int),
			hash_size, out);
		/* writing msgid table */
		(void) fwrite(ids, id_len, 1, out);
		/* writing msgstr table */
		(void) fwrite(strs, str_len, 1, out);

		(void) fclose(out);
		free(id_tbl);
		free(str_tbl);
		free(hash_tbl);
		free(ids);
		free(strs);
skip:
		free(p->fname);
		free(p->msg);
		op = p->next;
		free(p);
		p = op;
	}
	if (verbose_flag) {
		diag(gettext(DIAG_RESULTS), num, fnum, unum);
	}
}
示例#14
0
long long main()
{
        //encryption
        FILE *fp;
        time_t t;
        long long k, b_size, i, j, pro = 0, max = 0, n1, n2, n, e, p, d, powe, m;
        char ch;
        struct p_text b_no[100], cip[100], dec[100];
        fp = fopen("plain.txt", "r");
        printf("Enter block size:");
        scanf("%lld", &b_size);
        i = 0;
        j = 0;
        while ((ch = fgetc(fp)) != EOF) {
                b_no[j].char_s[i] = ch;
                pro = pro + pow(26, (b_size - i - 1)) * (ch - 65);
                i++;
                if (i == b_size) {
                        b_no[j].char_s[i] = '\0';
                        b_no[j].value = pro;
                        i = 0;
                        j++;
                        pro = 0;

                }

        }
        while (i < b_size) {
                b_no[j].char_s[i] = 'X';
                pro = pro + pow(26, (b_size - i - 1)) * ('X' - 65);
                i++;
        }
        b_no[j].value = pro;
        b_no[j].char_s[i] = '\0';
        for (i = 0; i <= j; i++)
                printf("%s ", b_no[i].char_s);
        printf("\n");
        for (i = 0; i <= j; i++)
                printf("%lld ", b_no[i].value);
        printf("\n");
        fclose(fp);
        for (i = 0; i < b_size; i++)
                max = max + pow(26, i) * 25;
        printf("Maximum value can be: %lld", max);
        //random number generation
        srand((unsigned) time(&t));
        printf("\n");

        n1 = find_prime(sqrt(max));
        n2 = find_prime(n1);

        printf("two prime numbers:%lld %lld\n", n1, n2);
        n = n1 * n2;
        printf("Value of n: %lld\n", (n1 * n2));
        printf("Value of phi: %lld\n", ((n1 - 1) * (n2 - 1)));
        p = ((n1 - 1) * (n2 - 1));
        e = find_prime(sqrt(p));
        d = find_prime(e);

        for (i = 0; i < p; i++) {
                if ((i * e) % p == 1) {
                        printf("Value of D: %lld", i);
                        break;
                }
        }
        d = i;
        printf("\n");
        printf("Public key: {%lld ,%lld}\n", e, n);
        printf("Private key: {%lld ,%lld}\n", d, n);
        printf("ciphertext :");
        for (i = 0; i <= j; i++) {
                cip[i].value = mode(b_no[i].value, e, n);
                printf("%lld ", cip[i].value);
        }

        //Decryption
        printf("\n");
        printf("Decrypted: ");
        for (i = 0; i <= j; i++) {
                dec[i].value = mode(cip[i].value, d, n);
                printf("%lld ", dec[i].value);
        }
        for (i = 0; i <= j; i++) {
                k = 0;
                while (k < b_size) {
                        m = 0;
                        powe = pow(26, (b_size - 1 - k));
                        if (powe > dec[i].value)
                                dec[i].char_s[k] = 'A';
                        else {
                                while (powe * m < dec[i].value) {
                                        m++;
                                }
                                if (powe == 1)
                                        dec[i].char_s[k] = 65 + m;
                                else
                                        dec[i].char_s[k] = 65 + m - 1;
                                dec[i].value = dec[i].value - ((m - 1) * powe);
                        }
                        k++;
                }
                dec[i].char_s[k] = '\0';
        }
        printf("\n");
        for (i = 0; i <= j; i++) {
                dec[i].value = mode(cip[i].value, d, n);
                printf("%s ", dec[i].char_s);
        }
        return 0;

}
int is_prime_number(int n) {
  return find_prime(n, 2);
}
示例#16
0
int main()
{
	int i, j, k, n, a, b, index, num_inner, max_inner, value_decimal;
	char str_a[num_digits], str_b[num_digits], palindrome[num_digits], tmp[num_digits], fmt[8];
	int len_a, len_b;
	char digit_ends[] = { '1', '3', '7', '9'};
	num = (int *)malloc(num_primes * sizeof(int));
	num2 = (int *)malloc(num_primes * sizeof(int));
	n = find_prime();
	scanf("%d %d", &a, &b);
	sprintf(str_a, "%d", a);
	sprintf(str_b, "%d", b);
	len_a = strlen(str_a);
	len_b = strlen(str_b);
	for (i = len_a; i <= len_b; i++) {
		if (i == 1) {
			for (j = 5; j <= 9; j+=2) {
				if (j < a || j > b)
					continue;
				if (check_prime(j, n))
					printf("%d\n", j);
			}
			continue;
		}
		if (i == 2) {
			for (j = 11; j <= 99; j+=22) {
				if (j < a || j > b)
					continue;
				if (check_prime(j, n))
					printf("%d\n", j);
			}
			continue;
		}
		if(i % 2 == 1) {
			num_inner = i / 2;
			max_inner = 1;
			sprintf(fmt, "%%0%dd\n", num_inner);
			for (j = 0; j < num_inner; j++)
				max_inner *= 10;
			for (index = 0; index < 4; index++) {
				palindrome[0] = digit_ends[index];
				palindrome[i - 1] = digit_ends[index];
				palindrome[i] = '\0';
				for (j = 0; j < max_inner; j++) {
					sprintf(tmp, fmt, j);
					for (k = 1; k <= i / 2; k++) {
						palindrome[k] = tmp[k - 1];
						palindrome[i - 1 - k] = palindrome[k];
					}
					sscanf(palindrome, "%d", &value_decimal);
					if (value_decimal < a)
						continue;
					if (value_decimal > b)
						break;
					if (check_prime(value_decimal, n))
						printf("%d\n", value_decimal);
				}
			}
			continue;
		}
		if ( i % 2 == 0)
			continue;
	}
	free(num);
	free(num2);
	return 0;
}
示例#17
0
文件: rsakey.c 项目: chapuni/polly
int main(int argc, char *argv[])
{
  int       opt, modbits;
  FILE     *ofp = stdout;
  char     *expt = NULL;
  rsa_key   the_key;
  mp_result res;

  /* Process command-line arguments */
  while((opt = getopt(argc, argv, "e:")) != EOF) {
    switch(opt) {
    case 'e':
      expt = optarg;
      break;
    default:
      fprintf(stderr, "Usage: rsakey [-e <expt>] <modbits> [<outfile>]\n");
      return 1;
    }
  }
  
  if(optind >= argc) {
    fprintf(stderr, "Error:  You must specify the number of modulus bits.\n");
    fprintf(stderr, "Usage: rsakey [-e <expt>] <modbits> [<outfile>]\n");
    return 1;
  }
  modbits = (int) strtol(argv[optind++], NULL, 0);
  if(modbits < CHAR_BIT) {
    fprintf(stderr, "Error:  Invalid value for number of modulus bits.\n");
    return 1;
  }
  if(modbits % 2 == 1)
    ++modbits;

  /* Check if output file is specified */
  if(optind < argc) {
    if((ofp = fopen(argv[optind], "wt")) == NULL) {
      fprintf(stderr, "Error:  Unable to open output file for writing.\n"
	      " - Filename: %s\n"
	      " - Error:    %s\n", argv[optind], strerror(errno));
      return 1;
    }
  }
  
  if((res = rsa_key_init(&the_key)) != MP_OK) {
    fprintf(stderr, "Error initializing RSA key structure:\n"
	    " - %s (%d)\n", mp_error_string(res), res);
    return 1;
  }

  /* If specified, try to load the key exponent */
  if(expt != NULL) {
    if((res = mp_int_read_string(&(the_key.e), 10, expt)) != MP_OK) {
      fprintf(stderr, "Error:  Invalid value for encryption exponent.\n"
	      " - %s (%d)\n", mp_error_string(res), res);
      goto EXIT;
    }
  }

  if((res = mp_int_randomize(&(the_key.p), (modbits / 2))) != MP_OK) {
    fprintf(stderr, "Error:  Unable to randomize first prime.\n"
	    " - %s (%d)\n", mp_error_string(res), res);
    goto EXIT;
  }
  fprintf(stderr, "p: ");
  find_prime(&(the_key.p), stderr);

  if((res = mp_int_randomize(&(the_key.q), (modbits / 2))) != MP_OK) {
    fprintf(stderr, "Error:  Unable to randomize second prime.\n"
	    " - %s (%d)\n", mp_error_string(res), res);
    goto EXIT;
  }
  fprintf(stderr, "\nq: ");
  find_prime(&(the_key.q), stderr);
  fputc('\n', stderr);

  /* Temporarily, the key's "n" field will be (p - 1) * (q - 1) for
     purposes of computing the decryption exponent.
   */
  mp_int_mul(&(the_key.p), &(the_key.q), &(the_key.n));
  mp_int_sub(&(the_key.n), &(the_key.p), &(the_key.n));
  mp_int_sub(&(the_key.n), &(the_key.q), &(the_key.n));
  mp_int_add_value(&(the_key.n), 1, &(the_key.n));

  if(expt == NULL &&
     (res = mp_int_randomize(&(the_key.e), (modbits / 2))) != MP_OK) {
    fprintf(stderr, "Error:  Unable to randomize encryption exponent.\n"
	    " - %s (%d)\n", mp_error_string(res), res);
    goto EXIT;
  }
  while((res = mp_int_invmod(&(the_key.e), &(the_key.n), 
			     &(the_key.d))) != MP_OK) {
    if(expt != NULL) {
      fprintf(stderr, "Error:  Unable to compute decryption exponent.\n"
	      " - %s (%d)\n", mp_error_string(res), res);
      goto EXIT;
    }
    if((res = mp_int_randomize(&(the_key.e), (modbits / 2))) != MP_OK) {
      fprintf(stderr, "Error:  Unable to re-randomize encryption exponent.\n"
	      " - %s (%d)\n", mp_error_string(res), res);
      goto EXIT;
    }
  }

  /* Recompute the real modulus, now that exponents are done. */
  mp_int_mul(&(the_key.p), &(the_key.q), &(the_key.n));

  /* Write completed key to the specified output file */
  rsa_key_write(&the_key, ofp);

 EXIT:
  fclose(ofp);
  rsa_key_clear(&the_key);
  return 0;
}