Yesterday, I wrote two procedures for calculating large digital steps and multipliers. The main reason for writing this thing is because my first interview. This spring, I went to Beijing a very good company interview, one of which is a written test, and the proposition is a question: Try calculation 2 ^ 256, write programming ideas, not requiring the final result. As a student who has not yet left the school, there is a problem such as such a question. It is not too difficult. It is very simple. It is to use an array store result, each of which is a few digits of the results of the village, then according to the image The multiplier described in the Computer Multiplex is logically implemented. I was so simple to write a realistic thinking. In the 9 people who interviewed at the time, I only had me one for this question, so I took this question for myself a very high impression, although although Later, I didn't go to that company because of some reasons, but this is also my interview success, so I still remember. Below is the calculation program that I have written under UNIX, which is similar to the operation of the idea and the multiplication. Specific processes should be very clear in the annotation.
#include
/ *****************
This function fact () implements the calculation of large numbers
Parameter n int
The return result is 0 indicates an error, otherwise the result of printing results
***************** /
INT FACT (INT N)
{
INT A [900]; / * Record result * /
INT flag = 1; / * Result bits Sign * /
INT DATA = n; / * Multiply in the calculation process * /
INT i = 0; / * cycle sign * /
/ * Only the number between 0 - 1041 is calculated * /
IF ((n <0) || (n> 1042)))
{
Printf ("Digital Overflow! / N");
Return 0;
}
/ * 0 step by step processing * /
IF (n == 0)
{
Printf ("0! = 1 / n");
Return 0;
}
/ * If the input value is greater than 2 digits, the initialization head is two array values * /
IF (n> = 1000)
{
FLAG = 2;
}
a [0] = 1;
/ * Main operation * /
While ((Data> 0) && (Flag <900))
{
/ * Take the last result and the next multiplier. * /
For (i = 0; i { a [i] = a [i] * data; } / * Adjustment result * / For (i = 0; i { a [i 1] = a [i] / 1000; a [i] = a [i]% 1000; } / * Adjustment result * / IF (a [Flag-1]> 1000) { FLAG ; a [i 2] = a [i 1] / 1000; A [i 1] = a [i 1]% 1000; } / * Multiplier is reduced * / Data-- } / * Print of the result * / Printf ("% D! =", n); Printf ("% D", A [FLAG-1]); / * Is the two bits of the three numbers to process * / For (i = flag-2; i> = 0; I -) { IF (a [i] <100) Printf ("0"); IF (a [i] <10) Printf ("0"); Printf ("% d", a [i]); } Printf ("/ n"); Return 1; } Main () { FACT (1000); } Executive results: 1000! = 4023872600770937735437024337748398571937486479991042 993851239862902059204420848696940480047998861019779988631668729 94808558901323829669944590997424504087073759918823627727188732519 77950595099527612087497546249704360 ... (limited to space, there is no more numbers here) 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000 This is the code that calculates the big digital step, it is very simple! But it is simple, I also saw such a code that I didn't know where, the style is good or bad, even if someone calls it is a small pile of garbage code, but it can calculate the PI to 1000 in the decimal point, you Is it too powerful? #include #include Main () { Long a = 10000, b, c = 2800, d, e, f [2801], g; For (; b-c;) F [B ] = a / 5; For (; D = 0, g = c * 2; c- = 14, printf ("%. 4D", E D / A), E = D% a) For (b = C; D = f [b] * a, f [b] = D% - g, d / = g -, - b; d * = b); } Print Result: 31415926535897932384626433832795028841971693993751058209749445923 07816406286208998628034825342117067982148086513282306647093844609 5505822317253594081284811174502841027019385211055964462294895493 03819644288109756659334461284756482337867831652712019091456485669 23460348610454326648213393607260249141273724587006606315888174881 52092096282925409171536436789259036001133053054882046652138414695 19415116094330572703657595919530921861173819326117931051185480744 62379962749567351885752724891227938183011949129833673362440656643 08602139494639522473719070217986094370277053921717629317675238467 48184676694051320005681271452635608277857713427577896091736371787 2146844090122495343014654958537105079227968925892354201956112129 0219608640344181598136297747130996051870721134999999837297804995105973173281609633185 Of course, if you have a better classmate, you can use some formulas related to the level, it is easy to write up to 10,000 pieces of code after the PI decimal point, may not exceed dozens of lines, but want to streamline this degree. Not easy. What is introduced above is just a little fur, which is really involved in the theory. It is not so skinless. Unless you are specializing in this or a very new idea, you will use it if you don't expect so much strength to write it yourself. Some functions of large digital operations, if just for the application, you can choose Mathematics software like Maple, its large number operation is enough to meet your needs, it can print 10000! , Numbers can roll a few screens!