Prime Number, affinity number, and end number

Source: Internet
Author: User

These problems can be solved using an auxiliary array, also known as an accompanying array.

1. Prime Number

Evaluate the prime number: sort the integers in a range starting from 1 in ascending order. 1 is not a prime number. First, sieve it out. In the remaining number, select the smallest number as the prime number, and then remove its multiples. And so on until the sieve is empty.

void prime(int n){vector<bool> isPrime(n+1,true);int i;for (i = 2;i*i <= n;i++){if(isPrime[i]){int j = i << 1;while (j <= n){isPrime[j] = false;j += i;}}}for (i = 1;i <= n;i++){if(isPrime[i])cout << i << " ";}cout << endl;}

2. affinity (reference)

If the sum of two integers, each of which is exactly the same as the other, then these two numbers constitute a pair of "affinity numbers. For example, the true factor of 220 is: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110; the true factors of 284 are: 1, 2, 4, 71, 142. These two numbers are exactly the sum of the true factors of each other, that is, 220 = 1 + 2 + 4 + 71 + 142 = sum [284].
284 = 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = sum [220], sum [220] = sum [sum [284] = 284.

Void friendnum (int n) {vector <int> sum (n + 1, 1); int I; for (I = 2; I <= (n> 1 ); I ++) {Int J = I <1; // does not contain its own while (j <= N) {sum [J] + = I; J + = I ;}for (I = 1; I <= N; I ++) {If (sum [I] <= N & sum [sum [I] = I & I <sum [I]) cout <I <"" <sum [I] <Endl; // prevents cross-border and deduplication }}

3. Quantity

If a number is equal to the sum of its factors, the number is called "full number. For example, the first complete number is 6, which has an approximate number of 1, 2, 3, and 6. Except for its own number of 6, the remaining three numbers are added, and 1 + 2 + 3 = 6.

Void perfectnum (int n) {vector <int> sum (n + 1, 1); int I; for (I = 2; I <= (n> 1 ); I ++) {Int J = I <1; // does not contain its own while (j <= N) {sum [J] + = I; J + = I ;}for (I = 2; I <= N; I ++) {If (sum [I] = I) cout <I <"" ;}cout <Endl ;}

In addition, there is an application of the adjoint array, that is, to calculate the number of K small intervals. For more information, see here.




Prime Number, affinity number, and end number

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.