Prime number and composite number and their applications

Source: Internet
Author: User

Prime number and composite number

Excerpt from Wikipedia:

Prime number, also known as Prime , refers to the number of natural numbers greater than 1, except for 1 and the integer itself, which cannot be divisible by other natural numbers (which can also be defined as numbers of only 1 and two factors per se).

A number larger than 1 but not prime is called a composite. 1 and 0 are neither prime nor composite. Primes have a very important position in number theory.

The decomposition of mass factor is the decomposition of decomposition. Each composite can be written in the form of several prime numbers multiplied. Each of these prime numbers is the factor of this composite number, called the decomposition decomposition of this composite number. The number of decomposed mass is only for composite numbers. It is called Short division that decomposes the decomposition formula.

For more information, please click on the wiki link: http://zh.wikipedia.org/wiki/%E7%B4%A0%E6%95%B0

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The application of the factorial

Question 1

Please, n! The number of the end 0

Method 1. Using the mass factor decomposition method.

The total number of factors that are divisible by 5 in the statistical 1-n   
int findzeronum (int N)  
{  
    int ncount = 0;  
    for (int i = 5;i < N + 1;i++)  
    {  
        int ncur = i;  
        while (ncur% 5 = 0)//the number of factors divisible by 5 in the statistic Ncur, that is, several 5    
        {  
            ncount++;  
            Ncur/= 5;  
        }  
    return ncount;  
}

Method 2.

N! The number of the mass factor K contained in is: [n/k]+[n/k^2]+[n/k^3]+ ...

Where [n/k] represents a multiple contribution of (1--N) k in a number that is not greater than N (k)

[N/k^2] Represents a multiple contribution of k^2 in numbers that are not greater than N. A K

......

int findzeronum (int n,int k)//Find out how many factors k  
{  
    int ncount = 0 in n!;  
    while (n)  
    {  
        n = n/k;//1-n can dedicate how many k  
        ncount = n;  
    }  
    return ncount;  
}

Complexity: O (LOGKN).

Question 2

Find the position of the lowest bit 1 in the binary representation of the n!.

Idea: The position of the lowest bit 1 in the binary representation is equivalent to the number +1 of the end 0 in the binary representation, which is equivalent to n! The number of medium quality factor 2 is +1

Findzeronum (n,2) +1;

Author: csdn Blog Duplan

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.