High Precision computing 2

Source: Internet
Author: User

High Precision Division:

 

# Include <stdio. h> <br/> # include <string. h> </P> <p> # define max_len 200 </P> <p> char szline1 [max_len + 10]; <br/> char szline2 [max_len + 10]; <br/> int an1 [max_len + 10]; // divisor, an1 [0] corresponds to a single digit <br/> int an2 [max_len + 10]; // divisor, an2 [0] corresponds to a single digit <br/> int aresult [max_len + 10]; // storage provider, aresult [0] corresponds to a single digit </P> <p> // The Big integer P1 with the length of plen1 sends a big integer P2 with the length of nlen2 <br/> int substract (int * p1, int * P2, int nlen1, int nlen2) <br/>{< br/> int I; </ P> <p> // determine whether P1 is larger than P2. If not, return-1 <br/> If (nlen1 <nlen2) <br/> return-1; <br/> If (nlen1 = nlen2) <br/> for (I = nlen1-1; I> = 0; I --) <br/> If (P1 [I]> P2 [I]) <br/> break; <br/> else if (P1 [I] <P2 [I]) <br/> return-1; <br/> for (I = 0; I <nlen1; I ++) {<br/> // call this function to ensure that when I> = nlen2, P2 [I] = 0 <br/> P1 [I]-= P2 [I]; <br/> If (P1 [I] <0) {<br/> P1 [I] + = 10; <br/> P1 [I + 1] --; <br/>}< br/> for (I = nlen1- 1; I> = 0; I --) <br/> If (P1 [I]) // find the first of the highest bits, not 0 <br/> return I + 1; <br/> return 0; // All values are 0, indicating that the values are equal again <br/>}</P> <p> int main (void) <br/>{< br/> int t, n; </P> <p> scanf ("% d", & N ); <br/> for (t = 0; t <n; t ++) {<br/> scanf ("% s", szline1 ); <br/> scanf ("% s", szline2); <br/> int I, j; <br/> int nlen1 = strlen (szline1 ); <br/> memset (an1, 0, sizeof (an1); <br/> memset (an2, 0, sizeof (an2); <br/> memset (aresult, 0, sizeof (Aresult); <br/> for (j = 0, I = nlen1-1; I> = 0; I --) <br/> an1 [J ++] = szline1 [I]-'0'; <br/> int nlen2 = strlen (szline2 ); <br/> for (j = 0, I = nlen2-1; I> = 0; I --) <br/> an2 [J ++] = szline2 [I]-'0'; <br/> If (nlen1 <nlen1) {<br/> printf ("0/N"); <br/> continue; <br/>}</P> <p> int ntimes = nlen1-nlen2; <br/> If (ntimes> 0) {<br/> for (I = nlen1-1; I >= ntimes; I --) <br/> an2 [I] = an2 [I-ntimes ]; // Move toward the high position <br/> for (; I> = 0; I --) // fill 0 in the low position <br/> an2 [I] = 0; <br/> nlen2 = nlen1; <br/>}< br/> for (j = 0; j <= ntimes; j ++) {<br/> int ntmp; <br/> // until it is enough <br/> // first subtract several an2 * (10 to the power of ntimes), <br/> // not enough rebellion, then subtract several an2 * (10 to the power of the nTimes-1 ),... <br/> while (ntmp = substract (an1, an2 + J, nlen1, nlen2-j) >=0) {<br/> nlen1 = ntmp; <br/> aresult [ntimes-J] ++; // subtract each success, add the corresponding bid of the operator to 1 <br/>}< br/> // The output result below. Skip the high 0 <br/> for (I = max_len; I >=0 &&! Aresult [I]; I --) <br/>; <br/> if (I> = 0) <br/> for (; I> = 0; I --) <br/> printf ("% d", aresult [I]); <br/> else <br/> printf ("0 "); <br/> putchar ('/N'); </P> <p >}</P> <p> return 0; <br/>}

High-Precision multiplication:

 # Include <stdio. h> <br/> # include <string. h> </P> <p> # define max_len 200 </P> <p> int main (void) <br/>{< br/> int I, j, len1, len2; <br/> int A [max_len + 10], B [max_len + 10], C [max_len * 2 + 10]; <br/> char str1 [max_len + 10], str2 [max_len + 10]; </P> <p> // initialization <br/> for (I = 0; I <max_len + 10; I ++) <br/> A [I] = B [I] = 0; <br/> for (I = 0; I <max_len * 2 + 10; I ++) <br/> C [I] = 0; </P> <p> // enter two multiplier values. <br/> gets (St R1); <br/> gets (str2); </P> <p> // stores the number in an integer array in reverse order. <br/> len1 = strlen (str1 ); <br/> for (j = 0, I = len1-1; I> = 0; I --) <br/> A [J ++] = str1 [I]-'0'; <br/> len2 = strlen (str2 ); <br/> for (j = 0, I = len2-1; I> = 0; I --) <br/> B [J ++] = str2 [I]-'0'; </P> <p> for (I = 0; I <len2; I ++) // The second number multiplied by the first number. Each time one digit <br/> for (j = 0; j <len1; j ++) <br/> C [I + J] = B [I] * A [J]; // first multiply, followed by a unified carry <br/> for (I = 0; I <max_len * 2; I ++) <br/>{< br/> If (C [I]> = 10) {<br/> C [I + 1] + = C [I]/10; <br/> C [I] % = 10; <br/>}</P> <p> // output the multiplication result <br/> for (I = max_len * 2 ;! C [I] & I> = 0; I --) // crossing the high 0 <br/>; <br/> if (I> = 0) <br/> for (; I >= 0; I --) <br/> printf ("% d", C [I]); <br/> else <br/> printf ("0"); </P> <p> return 0; <br/>}

 

The last five hundred digits and total digits of msen

(You do not need to determine whether it is a prime number)

Formula: 2 ^ P-1

# Include <stdio. h> <br/> # include <memory. h> <br/> # include <math. h> </P> <p> # define Len 125 // each element contains four digits, the array requires a maximum of 125 elements </P> <p> // calculate the high-precision multiplication a * B, the result contains no 500 bits in a. <br/> void multiply (int * a, int * B) <br/>{< br/> int I, J; <br/> int ncarry; // memory carry <br/> int ntmp; <br/> int C [Len]; // store the last 500 bits of the result </P> <p> memset (C, 0, sizeof (INT) * Len); <br/> for (I = 0; I <Len; I ++) {<br/> ncarry = 0; <br/> for (j = 0; j <len-I; j ++) {<br/> Ntmp = C [I + J] + A [J] * B [I] + ncarry; <br/> C [I + J] = ntmp % 10000; <br/> ncarry = ntmp/10000; <br/>}< br/> memcpy (A, C, Len * sizeof (INT )); <br/>}</P> <p> int main (void) <br/>{< br/> int I, P; <br/> int anpow [Len]; // store the increasing power of 2 <br/> int aresult [Len]; // store the last 500 bits of the final result </P> <p> scanf ("% d", & P); <br/> printf ("% d/N ", (INT) (p * log10 (2.0) + 1); <br/> // initialize the power of 2 2 2 ^ (2 ^ 0) (A ^ B indicates the power B of A) <br/> // the final result is initialized to 1. <Br/> anpow [0] = 2; <br/> aresult [0] = 1; <br/> for (I = 1; I <Len; I ++) {<br/> anpow [I] = 0; <br/> aresult [I] = 0; <br/>}< br/> while (P> 0) {// calculate the power of 2 to the power of P <br/> // if p = 0, the valid bits in are used, if <br/> If (P & 1) // you do not need to calculate if the percentile bit in P is 1 <br/> multiply (aresult, anpow ); <br/> P> = 1; <br/> multiply (anpow, anpow); <br/>}< br/> aresult [0] --; // 2 ^ p-1 <br/> // output result <br/> for (I = len-1; I> = 0; I --) {<br/> if (I % 25 = 12) <Br/> printf ("% 02d/n % 02d", aresult [I]/100, aresult [I] % 100 ); <br/> else {<br/> printf ("% 04d", aresult [I]); <br/> If (! (I % 25) <br/> putchar ('/N'); <br/>}</P> <p> return 0; <br/>}

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.