The Method of Finding Prime Number (Judgment Number n)
1. The integers from 2 to n-1, in turn, determine whether n can be divided by n, if not prime.
2. If a number has a factor, it will have within its square, otherwise there will be no factor (i.e. prime).
3. screening. First, store the number in a certain range, leaving 2 times to remove 2 times, 3 times to remove 3 times.
int sushu(int n)//Is a prime number returning 1 { int flag=1; for(int i=2;i<=n-1;i++) { if(n%i==0) { flag=0; } } return flag; }
int sushu(int n)//Is a prime number returning 1 { int flag=1; for(int i=2;i<sqrt(n);i++) { if(n%i==0) { flag=0; } } return flag; }
#include <iostream> #include <vector> #include <fstream> using namespace std; int main(){ vector<int> prime(10000, 1); for(int i=2; i<100; i++){ if(prime[i]){ for(int j=i; i*j<10000; j++) prime[i*j] = 0; } } for(int k; cin>>k && k>1 && k<10000; ) cout << k << " is " << (prime[k] ? "":"not ") << "a prime." << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define maxn 1000000 bool valid[maxn]; void getPrime(int n,int &tot,int ans[maxn]) { //N looks for the range of prime numbers. //Total number of tot primes; //An prime table; tot=0; int i,j; for(i=2; i<=n; i++) //All valid s in the prime fanwei are assigned to true. valid[i]=true; for(i=2; i<=n; i++) { if(valid[i]) { if(n/i<i) break; for(j=i*i; j<=n; j+=i) //valid[i] corresponding to numbers that are not prime numbers is assigned false. valid[j]=false; } for(i=2; i<=n; i++) //All prime numbers in the range of [2,n] are stored in an ans array. if(valid[i]) ans[tot++]=i; } } int main() { int n,tot; int ans[100000]; tot=0; cin>>n; getPrime(n,tot,ans); cout<<tot<<endl; for(int i=0;i<tot;i++) cout<<ans[i]<<" "; return 0; }
Method of Exchange without Temporary Variables
1. XOR Operator^
int &swap(int &a, int &b) { return (b ^= a ^= b ^= a); }
void swap(int *p, int *q) { *p = *p + *q - (*q = *p); }
HDU3823
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <memory.h> /*20,000,000 was chosen because the test data is roughly within this range.*/ const int MAX_VALUE = 2e7; static int primes[MAX_VALUE]; static char is_prime[MAX_VALUE+1]; /*Initialize a prime table for query Return quantity Prime Definition: In natural numbers larger than 1, there are no other factors except 1 and itself. */ int createPrimeTable() { int size = 0; /*Initialized to 1*/ memset(is_prime, 1, sizeof(is_prime)); /*Calculate the square value*/ /* Open Root Number: Find the square root of the number N greater than 2 to get S. If N can be divided by the number between 2 and S, then N is not a prime number. */ int s = sqrt((double)MAX_VALUE) + 1; /*Computation of Prime Number Dual cycle: 2,3,4,5...s 2,3,4,5...M/i Multiply up and down, and calculate all combinations. */ for (int i = 2; i <= s; i++) { if (is_prime[i]) { /*Find the maximum*/ for (int j = 2; j <= MAX_VALUE / i; j++) { is_prime[i * j] = 0; } } } /*All that's left is the prime number.*/ for (int i = 2 ; i <= MAX_VALUE; i++) if (is_prime[i]) primes[size++] = i; /*0 1 is not prime*/ is_prime[0] = is_prime[1] = 0; return size; } int main() { int count; scanf("%d", &count); /*Initialize prime table*/ int size = createPrimeTable(); int case_number = 1; while (count--) { int a, b; /*Input a and b*/ scanf("%d%d", &a, &b); /*Adjustment order*/ if (a > b) { /*Cooler way, no temporary variables*/ a = a ^ b; b = a ^ b; a = a ^ b; }; int prime = -1; for (int i = 0; i < size - 1; i++) { /*Guarantee that there is only one prime between a and b*/ if (primes[i] >= a && primes[i + 1] >= b) { /*If it happens to be equal*/ if ((primes[i] - a) == (primes[i + 1] - b)) { /*output*/ prime = primes[i] - a; break; } } } printf("Case %d: %d\n", case_number++, prime); } return 0; }