Calculate the sum of the numbers of 2 ^ 1000/2 ^ 10000

Source: Internet
Author: User
Tags binary to decimal

2 ^ 1000 in hexadecimal notation:

2 hexadecimal: 1000 ...... (1000 0 in total)

8 hexadecimal: 2000... (333 0 in total)

Hexadecimal: 10000... (a total of 250 0)

 

The binary to decimal calculation process is equal to: 2*2*2... (a total of 1000 2 multiplied)

Considering that the result of multiplication is relatively large = (2 ^ 10) ^ 100 = (1024) ^ 100> = (1000) ^ 100 = 1000 .. (the last 300 digits are 0)

If accurate calculation is performed, the existing types cannot be met.

 

Therefore, we generally use char * to store related values and perform related calculations:

2 ^ 2 = 2 + 2 = 4

2 ^ 3 = 4 + 4 = 8

2 ^ 4 = 8 + 8 = 16

2 ^ 5 = 16 + 16 = 32

...

2 ^ 1000 is equivalent to 999 addition calculations. We only need to develop a byte addition char * to simulate the 10-digit addition operation.

 

The following computation is a computation program compiled using G ++. The efficiency of computation using PC is as follows:

2 ^ 1000 computing completed 15 ms

2 ^ 10000 computing completed 2.5 s

 

# Include "stdio. H "# include <vector> # include" time. H "# define n_size 10000 bool plusself (STD: vector <char> & vecbytes) {int nplus = 0; For (INT I = 0; I <vecbytes. size (); I ++) {// use * 2 speed = <1 speed> A + A speed. The speed is about 30% faster, probably because of // 1. no need to find variables // 2. for * 2 optimization, use the <1-bit result int result = vecbytes [I] * 2 + nplus; // int result = vecbytes [I] <1 + nplus; // int result = vecbytes [I] + vecbytes [I] + nplus; // The efficiency of the two methods is very close., The second is a little faster, no more than 3% // 1. The second method adds a comparison judgment, but the nplus adopts a direct value assignment // 2. the first method uses remainder and division, because Char data is short and only 8 bytes are used. In this case, it is faster. // we recommend that you, either method works. The first method is short, and the second method is slightly faster. // vecbytes [I] = Result % 10; // nplus = Result/10; if (result <10) {vecbytes [I] = result; nplus = 0 ;}else {vecbytes [I] = Result-10; nplus = 1 ;}}} int main () {STD: vector <char> vecbytes; vecbytes. resize (n_size/3, 0); vecbytes [0] = 1; for (INT I = 0; I <n_size; I ++) {plusself (vecbytes );} for (INT I = 0; I <vecbytes. size (); I ++) {printf ("% C", vecbytes [I] + '0');} printf ("\ n"); int ntotal = 0; for (INT I = 0; I <vecbytes. size (); I ++) {ntotal + = vecbytes [I];} printf ("ntotal: % d clock: % d \ n", ntotal, clock ()); getchar (); Return 0 ;}

 

 

Calculate the sum of the numbers of 2 ^ 1000/2 ^ 10000

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.