Algorithm Analysis of the maximum number of approx.

Source: Internet
Author: User

Algorithm Analysis of the maximum number of approx.

Lab Problem Description

The approximate number of positive integers x is a positive integer that can divide X. The approximate number of positive integer x is counted as Div (x). For example, and 10 are the approximate numbers of positive integers 10, and Div (10) = 4. set a and B to two positive integers, A <= B, find the number X with the largest number of approx. A and B, and output its approx. Number.

Lab Purpose

In this experiment, we use number theory knowledge to create a prime number table to make up for the time complexity defect of the Division. In addition, combined with the division, we can calculate the most accurate approximate number in a small range,

The greatest optimization is achieved in terms of time complexity.

Experiment knowledge preparation process

The basic idea of division: when calculating the approximate number of positive integer a, first set a statistical variable count to calculate the approximate number of A, from 1 to calculate whether a can be divisible. If yes, count

Add 1. Otherwise, 1 is auto-incrementing and division is performed sequentially until division is reached, and the loop is exited.

The flowchart for solving the approximate number of each positive integer is as follows:

Disadvantages of division: When the range of two numbers is between 0.1 million and 1 billion, it takes a very large amount of time to count the approximate number of all numbers. (Tests show that a positive integer over 0.1 million must be calculated more than 1 second after division ).

Improvement Method: based on the knowledge of number theory, each positive integer can be divided into the product of several prime numbers. For example, 12 = 2*2*3. The method for calculating the approximate number is as follows: A (12) = (1 + 1) * (1 + 1) * (1 + 1) = 6, which means the product of the prime index plus one. Therefore, avoiding the use of the tired Division to obtain the approximate number will shorten the calculation time. Using the knowledge of number theory, we need to construct a prime number table to store the prime number, and then divide the prime number table with a positive integer to obtain the number of prime numbers in the end, and then store them in an array.

The flowchart is as follows:

Disadvantages of a prime number table: When the range of two numbers is small but the values of the two numbers are large, a large error may occur, for the following reasons: an error occurs when the approximate number of a number exceeds the range of the prime number table. Therefore, the advantages of Division must be combined to reduce errors.

Lab Process

The specific implementation process is as follows:

Determine the range of two positive integers: When the range of the two is greater than 100, it indicates that the data to be tested between the two is relatively small, and the error can be reduced by division; when the range of the two is over 100, select the prime number table for calculation.

The flowchart is as follows:

Time Complexity Analysis

The time complexity analysis is based on the following assumptions: The two positive integers are a and B (A <= B), and the time spent for each division is 1, set the time complexity to 0 (n)

Division: time complexity 0 (n) = (B-A) (B + a)/2

Prime Number Method: time complexity 0 (n) = (B-a) * 1000

From the two expressions above, we can see that when a> 1000, the use of the prime number method can shorten the computing time, especially when the number of A and B is more, the computing time is more obvious.

6. The core code is as follows:

The core code of the called function is as follows:

1) create a prime number table function numlist ()

Int numlist (int A []) // creates a prime number table.

{

Int flag = 0; // determines the position of the prime number currently stored in the array

Bool repeat = true; // set the mark of the loop

Int J = 0;

For (INT I = 2; I <1000; I ++) // the upper limit of I is set to expand the search range.

{

While (Repeat)

{

If (j = Flag) & (I % A [flag]! = 0) // determines whether the last element of the current prime number table has been reached. If not, push the current number into the prime number table.

{

Repeat = false;

Flag ++;

A [flag] = I;

}

Else

{

If (I % A [J] = 0) // If the while statement can be exceeded

{

Repeat = false;

}

Else // otherwise, continue to judge

{

J ++;

}

}

}

Repeat = true; // before each test, the value must be set to true.

J = 0; // The divisor of the next number should start from a [0], so J should be set to zero

}

Return flag; // return the number of computations.

// In fact, there may be an error. If there is a large prime factor in the number of the maximum approx.

// Or there may be an error because the prime number has too many common factors, so it is rarely calculated.

// But on the other hand, if the number really contains the most divisor, its prime number must be very small.

// Therefore, the probability of an error must be related to the number of errors between A and B.

}

2). Calculate the number of approx.

Int test (int A [], int start, int finish, int flag)

{

Bool repeat = true;

Int testnumber = start; // store the maximum number of approx)

Int numberflag = 0; // sets the comparison bit.

Int statist = 1; // The number of statistical values to be analyzed.

Int B [10000] = {0}; // initialize the number of such identical prime numbers recorded by the current flag, and initialize

Int J = 0; // Save the current cursor pointing to the flag Array

Int temp = 0; // carrier of cyclic Division

For (INT I = start; I <= finish; I ++)

{

// Can J not surpass the existence of flag?

For (j = 0; I> = A [J]; j ++) // The limit of the prime number table cannot be exceeded, but this number must be excluded from the limit of less than 10000, there is no need to compare this for hours.

{

If (j> flag)

{

Break;

}

Else

{

Temp = I;

While (TEMP % A [J] = 0) // if the flag can be divisible, add 1 to the flag and remember to remove the number of times that have been added.

{

B [J] ++;

// Remember that the subscript of A and B corresponds

Temp = temp/A [J];

}

}

}

// Calculate the number of common factors contained in this number

For (int K = 0; k <= J; k ++)

{

If (B [k]! = 0)

{

Statist = statist * (B [k] + 1 );

}

}

// Cout <I <"approx.:" <statist <Endl;

If (numberflag <statist) // obtain the largest number

{

Numberflag = statist;

Testnumber = I;

}

Statist = 1; // Reset

For (k = 0; k <= J; k ++)

{

B [k] = 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.