Optimization process of homogeneous search program

Source: Internet
Author: User

// Problem Description
// Use C to write a program to find 2 ~ The number of homogeneous elements between 999, which is the same as the number of homogeneous elements. He appears on the right side of his square, for example, 5's square = 25, and 25's right side is 5, so 5 is an homogeneous number.

// The cause of the vulnerability is that I found that the program has obvious optimization effect.
// The difference between the initial version and the final version is more than 100 times.

// When I use a multimedia clock for compilation, we need to link winmm. Lib
// My compilation options are:
// Cl/O2/MD/dwin32/d_windows winmm. Lib tonggou. cpp

//////////////////////////////////////// /////////////////////////////////////
// 1. The most direct version (see if I understand it wrong)
// Obtain the square m from 2 to 31 in sequence.
// Convert M and M into strings. The length is N, Mn, respectively.
// Start from the Mn-N position of M and compare it with M. If it is equal, M is the homogeneous number.
//////////////////////////////////////// /////////////////////////////////////
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <time. h>
# Include <windows. h>

Int check (int n)
{
Char buf_n [12] = {0 };
Char buf_m [12] = {0 };

ITOA (n, buf_n, 10 );
ITOA (N * n, buf_m, 10 );

Int len_n = strlen (buf_n );
Int len_m = strlen (buf_m );

If (len_m <len_n)
{
Return false;
}

Return 0 = strcmp (buf_n, & buf_m [len_m-len_n]);
}

Int main ()
{
DWORD begin = timegettime ();

For (INT I = 2; I <100000; I ++)
{
If (check (I ))
{
Printf ("% d is homogeneous, and its square is % d/N", I, I * I );
}
}
DWORD end = timegettime ();
Printf ("% d ms in total", end-begin );
Return 0;
}

//////////////////////////////////////// ////////////////////////////////////////
// 2. Optimized version
//////////////////////////////////////// ////////////////////////////////////////
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <time. h>
# Include <windows. h>

Int top (int n)
{
Int top_n = 10;
While (n> top_n-1)
{
Top_n * = 10;
}
Return top_n;
}

Int check (int n)
{
Return (N * n-n) % top (n) = 0;
}

Int main ()
{
DWORD begin = timegettime ();

For (INT I = 2; I <100000; I ++)
{
If (check (I ))
{
Printf ("% d is homogeneous, and its square is % d/N", I, I * I );
}
}
DWORD end = timegettime ();
Printf ("% d ms in total", end-begin );
Return 0;
}

//////////////////////////////////////// ////////////////////////////////////////
// 3. Final Version
// Analysis:
// N * n = x * 100 + n
// N * n-x = 0;
// Obtain
// N = 1/2 + SQRT (4 * x + 1)/2
// X = 10 * Y or 100 * Y or 1000 * Y (Y is a natural number) values are related to the n size.
// Only the number at the end of 1 and 9 is the number of records in XXX1.
// K = SQRT (4 * x + 1) = 2 * n-1 <= 999*2-1 = 1997
//
// Solution
// 2 ~ In the range of 1997, take the number ending with 1 or 9, K
// Obtain N from n = (k + 1)/2
// Obtain X from X = (k * k-1)/4
// Determine whether the relationship between N and X meets the requirements: X % top (n) = 0
//
//////////////////////////////////////// ////////////////////////////////////////
# Include <stdio. h>
# Include <time. h>
# Include <windows. h>

Int top (int n)
{
Int top_n = 10;
While (n> top_n-1)
{
Top_n * = 10;
}
Return top_n;
}

Int getn (int K)
{
Return (k + 1)/2;
}

Int getx (int K)
{
Int sqrt_4x = K * k-1;

If (sqrt_4x & 0x3)
{
// Cannot divide 4
Return 0;
}

Return sqrt_4x> 2;
}

Bool check (int K)
{
Int n = getn (k );
Int x = getx (k );

If (x> 0 & n> 1)
{
Return X % top (n) = 0;
}
Return false;
}

Int max_k (int n)
{
Return N * 2;
}

# Define n 100000

Int main ()
{
DWORD begin = timegettime ();

Int array_k [100];
Int count_k = 0;

For (INT I = 11; I <max_k (100000); I ++ = 10)
{
If (check (I ))
{
Array_k [count_k ++] = I;
}
}

For (INT I = 9; I <max_k (100000); I ++ = 10)
{
If (check (I ))
{
Array_k [count_k ++] = I;
}
}

For (INT I = 0; I <count_k; I ++)
{
Int n = getn (array_k [I]);
Printf ("% d is homogeneous, and its square is % d/N", N, N * n );
}

DWORD end = timegettime ();
Printf ("% d ms in total", end-begin );
Return 0;
}
/*
Both versions have overflow bugs, but the final version does not.

Execution result (piII 500 MHz)
1. Intuitive Edition
---------- Run application ----------
5 is homogeneous, and its square is 25
6 is homogeneous, and its square is 36
25 is homogeneous, and its square is 625
76 is homogeneous, and its square is 5776
376 is homogeneous, and its square is 141376
625 is homogeneous, and its square is 390625
9376 is homogeneous, and its square is 87909376
87231 is homogeneous, and its square is-980687231
183 MS in total

Optimized version
---------- Run application ----------
5 is homogeneous, and its square is 25
6 is homogeneous, and its square is 36
25 is homogeneous, and its square is 625
76 is homogeneous, and its square is 5776
376 is homogeneous, and its square is 141376
625 is homogeneous, and its square is 390625
9376 is homogeneous, and its square is 87909376
87232 is homogeneous, and its square is-980512768
Total time 9 MS

3. Final Version
---------- Run application ----------
6 is homogeneous, and its square is 36
76 is homogeneous, and its square is 5776
376 is homogeneous, and its square is 141376
9376 is homogeneous, and its square is 87909376
5 is homogeneous, and its square is 25
25 is homogeneous, and its square is 625
625 is homogeneous, and its square is 390625
3 MS in total

In fact, you can make the optimized version. it will take time to write a program. I spent 10 minutes in the intuitive version, 5 sub-categories in the optimized version, and an hour in the final version (mainly in the analysis process ).
In general, the final version is not cost-effective.
*/

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.