& Quot; interesting integer & quot; Class Exercises, Interview Questions (Article 2), and exercises (Article 2)

Source: Internet
Author: User

"Interesting integer" exercises, Interview Questions (Article 2), and exercises (Article 2)

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 <stdio. h> 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) contin Ue; 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 <stdio. h> 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 <stdio. h> # include <math. h> # include <stdlib. h> 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 ;}} re Turn;} int main (void) {int m, n, I, j, pos; int * fact; double temp = 0; printf ("enter a large 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 <stdio. h> # include <string. h> # 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.