This article provides five simple integer questions. I have a poor foundation, so I can sort it out for future reference. It is not recommended that you read this article because it is really simple and will waste time. Article 2... Fight ~
Link 1: "interesting integer" exercises and interview questions (Article 1)
6 questions: number of replies
When a multi-digit reads by bit, the result is the same whether it is read from left to right or from right to left. For example, 11, 22, 101, etc. Write a program to obtain the prime number of the reply and the number of the reply within 1000.
(1) Prime number of input: both the number of input and the prime number. For example, 121,151.
(2) square return number: it is the square of another integer. For example, 121,484.
# Include
Int IsPrime (int m) {int I, result = 1; for (I = 2; I * I <m + 1; I ++) {if (m % I = 0) {result = 0; break ;}} return result ;}int IsSquare (int m) {int I, result = 0; for (I = 2; I * I <= m; I ++); if (m = (I-1) * (I-1) result = 1; return result;} // return Prime void HuiWPrime () {int I, j, k; int result; for (I = 0; I <9; I ++) {for (j = 0; j <9; j ++) {if (I = 0 & j = 0) continue; for (k = 1; k <9; k ++) {if (I = 0 & j! = K) | (I! = 0 & I! = K) continue; result = I * 100 + j * 10 + k; if (IsPrime (result) printf ("% d", result );}}} printf ("\ n"); return;} // void HuiWSquare () {int I, j, k; int result; for (I = 0; I <9; I ++) {for (j = 0; j <9; j ++) {if (I = 0 & j = 0) continue; for (k = 1; k <9; k ++) {if (I = 0 & j! = K) | (I! = 0 & I! = K) continue; result = I * 100 + j * 10 + k; if (IsSquare (result) printf ("% d", result );}}} printf ("\ n"); return ;}
Resolution: return Prime number: compare the highest and lowest bits, the second high and the second low ..., Then judge whether it is a prime number. Square retrieval number: compare the highest and lowest places, the second highest and the second lowest ..., Then judge whether it is the number of shards. In implementation, you can save each separation to an array and then compare it accordingly.
Question 7: godbach's conjecture
Each even number not less than 6 is the sum of two prime numbers. Write a program to verify the godebach conjecture. (To simplify the program, this example only processes the numbers in the integer range that can be expressed by a computer. if you want to process more integers, you need to write code to process the big integer operation)
# Include
Int main (void) {int n, m, I, flag; scanf ("% d", & n); for (m = 6; m <n; m + = 2) {flag = 1; for (I = 2; I <m/2 + 1; I ++) {if (IsPrime (I) & IsPrime (m-I) {printf ("% d = % d + % d \ n", m, I, m-I); flag = 0; break ;}} if (1 = flag) printf ("Find the even number that does not meet the requirements: % d \ n", m);} return 0 ;}
Resolution: improved algorithm: filters out prime numbers in a specified range, so you do not need to determine whether a number is a prime number every time.
Question 8: Maximum common approx. and minimum common multiples
Thought 1:Euclidean algorithm
Euclidean algorithm is used to calculate the maximum common approx.
int Gcd(int m, int n){int a, b, r;a = m >= n ? m : n;b = m + n - a;if (b == 0)return a;while ((r = (a % b)) != 0){a = b;b = r;}return b;}int Lcm(int m, int n){return (m * n) / Gcd(m, n);}
Idea 2:Stein algorithm
Euclidean algorithms are simple and efficient. the defects of this algorithm are only displayed when the prime number is large. these are mainly determined by the integer range that can be expressed by computers. Generally, integers are represented by a maximum of 64 bits. it is very easy to calculate the modulo between two integers. However, for larger prime numbers, you need to design a program to calculate the modulo of two numbers. For modern cryptographic algorithms, it is common to calculate a prime number of more than 128 bits. to improve efficiency, division and modulo operations are not recommended.
# Define ABS (x)> 0? (X):-(x) # define MIN (x, y) (x) <(y )? (X): (y) int Gcd (int m, int n) {int result, temp; if (m = 0) return n; if (n = 0) return m; if (m & 0x1) = 0) & (n & 0x1) = 0 )) // both m and n are even {m >>= 1; n >>= 1; result = Gcd (m, n) <1 ;} else if (m & 0x1) = 0) // m is an even number, and n is an odd number {m >>= 1; result = Gcd (m, n );} else if (n & 0x1) = 0) // n is an even number, and m is an odd number {n> = 1; result = Gcd (m, n );} else if (m & 0x1) & (n & 0x1) // both m and n are odd {temp = ABS (m-n ); n = MIN (m, n); result = Gcd (temp, n);} return result ;}
Question 9: Factorial
Thought 1:Recursion and iteration
// Recursion long factorial (int n) {if (n <= 1) return 1; elsereturn n * factorial (n-1 );} # endif // iterate long factorial (int n) {int result = 1; for (int I = 1; I <= n; I ++) result * = I; return result ;}
Idea 2:Big integer factorial
# Include
# Include
# Include
Void carry (int bit [], int n) // process carry {int carry = 0, I; for (I = 0; I <= n; I ++) {bit [I] + = carry; if (bit [I] <= 9) {carry = 0;} else if (bit [I]> 9 & I <n) // not the highest bit {carry = bit [I]/10; bit [I] = bit [I] % 10 ;} else if (bit [I]> 9 & I> = n) // It is the highest bit {while (bit [I]> 9) {carry = bit [I]/10; bit [I] = bit [I] % 10; bit [++ I] = carry ;}}return ;}int main (void) {int m, n, I, j, pos; int * fact; Double temp = 0; printf ("enter a big integer m:"); scanf ("% d", & m); for (I = 1; I <= m; I ++) {temp + = log10 (I);} n = (int) temp + 1; // find m! If (! (Fact = (int *) malloc (sizeof (int) * n) {return 0 ;}for (I = 0; I <n; I ++) {* (fact + I) = 0;} fact [0] = 1; // The start time is 1 for (I = 2; I <= m; I ++) {for (j = n-1; j> = 0; j --) {if (fact [j]! = 0) // find the highest position {pos = j; break ;}}for (j = 0; j <= pos; j ++) {fact [j] * = I;} carry (fact, pos) ;}for (I = n-1; I> = 0; I --) {if (fact [I]! = 0) {pos = I; break;} m = 0; printf ("the factorial of the big integer m is \ n"); for (I = pos; i> = 0; I --) {printf ("% d", fact [I]); m ++; if (m % 5 = 0) {printf ("") ;}if (40 = m) {printf ("\ n"); m = 0 ;}} printf ("\ n "); printf ("The total number of digits of the factorial of the big integer m is % d \ n", n); free (fact); fact = NULL; return 0 ;}
Resolution: use an array to save the result of each factorial. Even if you use the long type to save the result of multiplying each bit, the maximum number of factorial is still limited. If a factorial with no limit is required, more complex algorithms are required for data storage and processing.
Question 10: factor and factorial
Enter a positive integer n (2 <=n <= 100) and multiply the factorial n! = 1*2*3 *... * N is decomposed into prime numbers (2, 3, 5,…) in the form of prime factor multiplication ,...) Index. For example, 825 = 3*5 ^ 2*11 should be expressed as (, 1 ), 0, 1, 2, 0, 1, 2, 3, 5, 7, and 11 respectively. Your program should ignore prime numbers that are larger than the maximum prime factor (otherwise there will be an infinite number of zeros at the end ).
Sample input:
5
53
Sample output:
5! = 3 1 1
53! = 4923 12 8 4 4 3 2 2 1 1 1 1 1 1
# Include
# Include
# Define MAXN 100int PrimeTable [MAXN], count = 0; int IsPrime (int m) {for (int I = 2; I * I <= m; I ++) if (m % I = 0) return 0; return 1;} int main (void) {int I, j, m, p [MAXN], maxp, tmp; // p is an exponential for (I = 2; I <= MAXN; I ++) // constructs a prime number table {if (IsPrime (I )) primeTable [count ++] = I;} while (scanf ("% d", & m) = 1) {memset (p, 0, sizeof (p )); maxp = 0; printf ("% d! = ", M); for (I = 1; I <= m; I ++) {tmp = I; for (j = 0; j <count; j ++) {while (tmp % PrimeTable [j] = 0) {tmp/= PrimeTable [j]; p [j] ++; if (j> maxp) maxp = j ;}}for (I = 0; I <= maxp; I ++) printf ("% d", p [I]); printf ("\ n");} return 0 ;}