Beispiel #1
0
int main() {
	int test, a, b, g;
	fread(buff, 1, 14000000, stdin);
	sieve();
	test = nextint();
	while(test--) {
		a = nextint(), b = nextint();
		g = gcd(a, b);
		printf("%d\n", cntdiv(g));
	}
	return 0;
}
Beispiel #2
0
int main(int argc, const char * argv[]) {
    reverse();
    
    sieve(5);
    
    printf("gcd: %d\n", gcd(gcd(72, 48), 54));
    printf("lcm: %d\n", lcm(lcm(6, 8), 15));
    
    printf("%d\n", fibonacciNumber3(5));
    
    return 0;
}
Beispiel #3
0
int solve(ull_t n)
{
	auto primes = sieve(n);
	ull_t primes_sum = 0;
	for(auto& p : primes) {
		primes_sum += p;
	}
	printf("Sum of primes up to %llu = %llu\n", n, primes_sum);
	printf("Largest found prime: %llu\n", primes.back());
	check_result(n == 10, primes_sum == 17);
	return 0;
}
unsigned count_circular() {
	char primes[LIMIT];
	unsigned counter = 0;
	int i;
	sieve(primes, LIMIT);
	for (i = 0; i < LIMIT; ++i) {
		if (primes[i] && is_circular(primes, i)) {
			++counter;
		}
	}
	return counter;
}
Beispiel #5
0
main()
{
	char bitarray[BITNSLOTS(47)];
	BITSET(bitarray, 23);
	BITCLEAR(bitarray, 14);
	if(BITTEST(bitarray, 35))
		printf("yep\n");
	else	printf("nope\n");

	sieve();
	return 0;
}
Beispiel #6
0
int main()
{
int t;
sieve();
scanf("%d",&t);
while(t--)
{
scanf("%ld",&j);
printf("%ld\n",pos[j]);
}
return 0;
}
Beispiel #7
0
TEST(primes, sieve) {
  int n = 100;

  bool under_test[n];

  sieve(under_test, n);

  int i;
  for (i = 2; i < n; i++) {
    EXPECT_EQ(is_prime(i), under_test[i]);
  }
}
int main()
{
    long kase,i,n;
    sieve(112000);
    scanf("%ld",&kase);
    while(kase--)
    {
        scanf("%ld",&n);
        printf("%ld\n",d[n]);
    }
    return 0;
}
Beispiel #9
0
int main(){
	long n;
	int ans[10] = {0, 1, 2, 6, 12, 60};
	
	sieve();
	while ( scanf("%ld", &n) != EOF ){
		if ( n < 6 ) printf("%d\n", ans[n]);
		else printf("%I64d\n", findLCM(n));
	}
	
	return 0;
}
/// Generate primes using the segmented sieve of Eratosthenes.
/// This algorithm uses O(n log log n) operations and O(sqrt(n)) space.
/// @param limit         Sieve primes <= limit.
/// @param segment_size  Size of the sieve array in bytes.
///
void segmented_sieve(int64_t limit, int segment_size = L1D_CACHE_SIZE)
{
    int sqrt = (int) std::sqrt((double) limit);
    int64_t count = (limit < 2) ? 0 : 1;
    int64_t s = 2;
    int64_t n = 3;

    // vector used for sieving
    std::vector<char> sieve(segment_size);

    // generate small primes <= sqrt
    std::vector<char> is_prime(sqrt + 1, 1);
    for (int i = 2; i * i <= sqrt; i++)
        if (is_prime[i])
            for (int j = i * i; j <= sqrt; j += i)
                is_prime[j] = 0;

    std::vector<int> primes;
    std::vector<int> next;

    for (int64_t low = 0; low <= limit; low += segment_size)
    {
        std::fill(sieve.begin(), sieve.end(), 1);

        // current segment = interval [low, high]
        int64_t high = std::min(low + segment_size - 1, limit);

        // store small primes needed to cross off multiples
        for (; s * s <= high; s++)
        {
            if (is_prime[s])
            {
                primes.push_back((int) s);
                next.push_back((int)(s * s - low));
            }
        }
        // sieve the current segment
        for (std::size_t i = 1; i < primes.size(); i++)
        {
            int j = next[i];
            for (int k = primes[i] * 2; j < segment_size; j += k)
                sieve[j] = 0;
            next[i] = j - segment_size;
        }

        for (; n <= high; n += 2)
            if (sieve[n - low]) // n is a prime
                count++;
    }

    std::cout << count << " primes found." << std::endl;
}
Beispiel #11
0
// if 3k+7 is not a prime, 3k+7 = a*b, then a, b <= 3k+6, 
// we get (3k+7) | (3k+6)!, so item = 0
// if 3k+7 is prime, (3k+7) | (3k+6)!+1 by Wilson's theorem
// so item = 1
int main() {
	sieve();
	for (int i = 1; i < 1048576; i++)
		S[i] = S[i-1] + (GET(3 * i + 7) == 0);
	
	int testcase, n; 
	scanf("%d", &testcase);
	while (testcase--) {
		scanf("%d", &n);
		printf("%d\n", S[n]);
	} 
	return 0;
}
int main() {
  constexpr long long SIZE = 5;
  std::array<long long, SIZE> curr;
  long long bound = 9999;
  long long result = bound * SIZE;

  while (result >= bound * SIZE) {
    sieve(std::max<long long>(bound * 10, 99999999));
    result = seek_seq<SIZE>(curr, 0, bound);
    bound *= 5;
  }
  std::cout << result << std::endl;
}
Beispiel #13
0
void top_main(int rank) {
	int primes[3];
	primes[0] = 2;
	primes[1] = 3;
	primes[2] = 5;
	printf("Hello from the top\n");
	fflush(stdout);

	int* composites = (int*)malloc(sizeof(int) * 100);
	initialize_range(composites, 100, 6);
	sieve(primes, 3, composites, 100);
	drop_ints(composites, 100);
}
int main()
{
    int t,n;
    int ans=0;
    sieve();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        ans=primeno[n]-primeno[n/2];
        printf("%d\n",ans);
    }
}
int main(int argc, char *argv[])
{
    int number = 0, i = 1;	//	number is triangle number, i is current term
    int sizeOfPrimes = 1000, sizeOfPrimesSqr = 1000000;
    int *primes;
    int numPrimes = sieve(sizeOfPrimes, &primes);

    while(numDivisors(number, primes, numPrimes) < 500) {
        if(sizeOfPrimesSqr < number) {	// Update if it becomes too small
            sizeOfPrimes *= 5;
            sizeOfPrimesSqr = sizeOfPrimes*sizeOfPrimes;
            numPrimes = sieve(sizeOfPrimes, &primes);
        }
        number += i++;					// Increment number
    }

    printf("Triangle number: %d\n", number);

    free(primes);

    return 0;
}
Beispiel #16
0
int main() {
    sieve();
    int l, r;
    int cases = 0;
    while (scanf("%d %d", &l, &r) == 2) {
    	int ret = 0;
    	assert(l >= 1 && l <= r);
    	for (long long i = l; i <= r; i ++)
    		ret += isPrime(i);
		printf("%d\n", ret);
	}
    return 0;
}
int main(int argc, char **argv) 
{
	pid_t pid;
	int i = 1;
	int top, floor;
	make_fifo();

	if (argc < 2)
	{
		fprintf(stderr, "usage: %s 44 100...\n", argv[0]);
		exit(1);
	}

	for(; argv[i] != '\0'; i++)
	{
		pid = fork();

		if (pid < 0)
		{
			exit(1);
		} 
		
		else if (pid == 0)
		{
			if(i == 1)
			{
				floor = 2;
			}
			else floor = atoi(argv[i-1]) + 1;
			top = atoi(argv[i]);

			printf("child %i: bottom=%i, top=%i\n", getpid(), floor, top);
			sieve(floor, top);
			exit(0x47);
		} 
		
		else
		{
			int status = 0;
			read_fifo();
			wait(&status);
			
			if(status == 18176) 
				printf("child %i exited cleanly\n", pid);
			
			else
				printf("unknown exit %i (0x%x)\n", status, status);
		}
	}
	return 0;
}
Beispiel #18
0
int main()
{
	int n;
	sieve(50000);
	
	while(scanf("%d",&n))
	{
		if(isprime(n))
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}
Beispiel #19
0
int main() {
	std::vector<int64_t> trunc_primes;
	
	// Generate a list of primes and check if each prime is truncatable
	euler::PrimeSieve sieve(kLimit);
	std::vector<int64_t> primes = sieve.getPrimes();
	
	for (int i = 5; trunc_primes.size() != 11 && i < primes.size(); i++)
		if (is_trunc_prime(primes[i], sieve)) trunc_primes.push_back(primes[i]);

	int64_t solution = std::accumulate(trunc_primes.begin(), trunc_primes.end(), 0);
	std::cout << solution << std::endl;
	
}
Beispiel #20
0
int main()
{
  sieve_of_eratosthenes<int> sieve(p_bound + 1);
  for (int i = 2; i < p_bound + 1; ++i)
    {
      if (sieve[i])
        {
          primes.push_back(i);
        }
    }
  iterate(1, 1, 0, max_first);
  std::cout << result << std::endl;
  return 0;
}
Beispiel #21
0
int main()
{
long long int t,i,n,j;
long long int res;
sieve();
scanf("%lld",&t);
for(i=0;i<t;i++)
	{
	scanf("%lld",&n);
	res = compute(n);
	printf("%lld\n\n",res);
	}
return 0;
}
Beispiel #22
0
int main(){
	sieve();
	int pos = 0;
	for(int i = 2;pos != M;i++){
		if(primes[i])
			data[pos++] = i;
	}
	int n;
	scanf("%d", &n);
	for(int temp;scanf("%d", &temp) != EOF;)
		printf("%d\n", data[temp - 1]);

	return 0;
}
Beispiel #23
0
int main() {
	sieve(primes, PRIMES);

	int i, sum = 0;

	for (i = 10; i < PRIMES; ++i) {
		if (is_truncatable_right(i) && is_truncatable_left(i)) {
			sum += i;
		}
	}

	printf("%d\n", sum);
	return 0;
}
int main(){
    int n,i,j;
    sieve();
    findalmostprimes();
    scanf("%d",&n);
    while(n--){
        scanf("%d %d",&i,&j);
        if(i==1 || !flag[i])
            printf("%d\n",almostprimes[j]-almostprimes[i]);
        else
            printf("%d\n",almostprimes[j]-almostprimes[i]+1);
    }
    return 0;
}
Beispiel #25
0
main(long argc, char *argv[])
{
  mpz_t n, buffer1, buffer2;
  mpz_init(n); mpz_init(buffer1); mpz_init(buffer2);
  if(argc == 2)
    mpz_set_str(n, argv[1], 10);
  if(argc == 3)
    mpz_ui_pow_ui(n, atol(argv[1]), atol(argv[2]));
  if (argc<2 || argc>3)
    {
      printf("Incorrect input format\n");
      exit(1);
    }
  printf("Using GMP...\n");
  uint64_t crn = lpow(n, 1, 3);
  size_t psize = (1.26*crn)/log(crn)*sizeof(uint64_t) + 1;                  //add 1 because primes array will run from 1 to pi(crN) with primes[0] not used
  uint64_t* primes = malloc(psize);
  assert(primes!=NULL);
  uint64_t i,j;

  uint64_t J = sieve(crn ,primes);

  int8_t* mu = malloc(sizeof(int8_t) * (crn+1));                        //mu will run from 1 to crN with s[0] not used
  assert(mu != NULL);
  memset(mu, 1, sizeof(int8_t) * (crn+1));

  uint64_t* lpf = malloc(sizeof(int64_t) * (crn+1));                        //lpf will run from 1 to crN with s[0] not used
  assert(lpf != NULL);
  memset(lpf, 0, sizeof(int64_t) * (crn+1));

  for(i=1; i<=J; i++)
    for(j=primes[i]; j<=crn; j+=primes[i])
      {
	mu[j] = -mu[j];
	if(lpf[j] == 0) lpf[j] = primes[i];
      }
  for(i=1; i<=J; i++)
    for(j=sqr(primes[i]); j<=crn; j+=sqr(primes[i])) mu[j]=0;           //remove numbers that are not squarefree

  mpz_t S1_result;
  mpz_init(S1_result);
  printf("Calculating S1..."); fflush(stdout);
  S1(n, crn, mu, S1_result);
  printf("\nS1 = ");
  mpz_out_str (stdout, 10, S1_result);
  printf("\n\n");

  free(primes); free(mu); free(lpf);
}
int main()
{
    long long int i,j,n;
    scanf("%lld",&n);
    long long int pm[n+1];
    sieve(&pm[0],n);
    for(i=2;i<=n;i++)
    {
        if(pm[i]==1)
        {
            printf("%lld ",i);
        }
    }
    return 0;
}
std::vector<int> generatePrimes(int maxPrime)
{
    std::vector<bool> sieve(maxPrime + 1, true);
    std::vector<int> primes = {2};
    for (int i = 3; i <= maxPrime; i += 2)
    {
        if (sieve[i])
        {
            primes.push_back(i);
            for (int j = i * 2; j <= maxPrime; j += i)
                sieve[j] = false;
        }
    }
    return primes;
}
int main()
{
    sieve();
    int T, cas = 0, a, b, c;
    scanf("%d", &T);
    while (T--) {
//        srand(time(NULL));
//        c = 1000000007;//465455467;//rand() % (pn-1) + 1;
//        a = rand() % 8 + 2, b = (LL)rand()*rand()*rand()%c;//
        scanf("%d%d%d", &c, &a, &b);
        printf("Case #%d:\n", ++cas);
        solve(a, b, c);
    }
    return 0;
}
Beispiel #29
0
void PrimeAndGenerator::Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned int qbits)
{
	// no prime exists for delta = -1, qbits = 4, and pbits = 5
	assert(qbits > 4);
	assert(pbits > qbits);

	if (qbits+1 == pbits)
	{
		Integer minP = Integer::Power2(pbits-1);
		Integer maxP = Integer::Power2(pbits) - 1;
		bool success = false;

		while (!success)
		{
			p.Randomize(rng, minP, maxP, Integer::ANY, 6+5*delta, 12);
			PrimeSieve sieve(p, STDMIN(p+PrimeSearchInterval(maxP)*12, maxP), 12, delta);

			while (sieve.NextCandidate(p))
			{
				assert(IsSmallPrime(p) || SmallDivisorsTest(p));
				q = (p-delta) >> 1;
				assert(IsSmallPrime(q) || SmallDivisorsTest(q));
				if (FastProbablePrimeTest(q) && FastProbablePrimeTest(p) && IsPrime(q) && IsPrime(p))
				{
					success = true;
					break;
				}
			}
		}

		if (delta == 1)
		{
			// find g such that g is a quadratic residue mod p, then g has order q
			// g=4 always works, but this way we get the smallest quadratic residue (other than 1)
			for (g=2; Jacobi(g, p) != 1; ++g) {}
			// contributed by Walt Tuvell: g should be the following according to the Law of Quadratic Reciprocity
			assert((p%8==1 || p%8==7) ? g==2 : (p%12==1 || p%12==11) ? g==3 : g==4);
		}
		else
		{
			assert(delta == -1);
			// find g such that g*g-4 is a quadratic non-residue, 
			// and such that g has order q
			for (g=3; ; ++g)
				if (Jacobi(g*g-4, p)==-1 && Lucas(q, g, p)==2)
					break;
		}
	}
Beispiel #30
0
int main (void) {
	long int *matrix;
	long int i;
	long int range;
	printf("Poszukiwanie liczb pierwszych w przedziale 0 - ");
	scanf("%d", &range);
	matrix=create_matrix(range); //alokacja pamieci
	if(matrix == NULL) {
		return 0;
	}
	fill_matrix(matrix, range); //wypelnienie tablicy kolejnymi liczbami
	sieve(matrix, range); 
	write_out(matrix, range);
	free(matrix);
	return 0;
}