Some algorithm ideas of large number processing in. Net

Source: Internet
Author: User
Tags modulus net
Algorithm

In. NET development, sometimes because of the knowledge content of some marginal disciplines, such as statistics, finance, astronomy and other calculations, encryption and decryption algorithm will involve a large number of operations, that is,. NET, the largest number of numeric types stored will overflow, one of my ideas is to calculate the value of the type, storage (temporary) And the output is the string so stored when the need for Box[n] n arrays to temporarily store a calculation of small step results,

' As an example
==================== Algorithm Understanding Graph =======================
' 97*97*97*97*97 = 8587340257 box (1) = 587340257 box (2) =8
' 97*97*97*97*97*97 = 832972004929 box (1) = 972004929 box (2) =832
' 97*97*97*97*97*97*97 = 80798284478113 box (1) = 284478113 box (2) =80798
' 97*97*97*97*97*97*97*97 = 7837433594376961 box (1) = 594376961 box (2) =7837433
' 97^ 9 = 760231058654565217 box (1) = 654565217 box (2) =760231058
' 97^ = 73742412689492826049 box (1) = 492826049 box (2) =742412689 box (3) =73
...... ...... ...... ......

Note that the larger the corresponding number in the box below, the more high in the use of the above algorithm to remember

① first defines a box with a number of signs, such as 9 digits above (depending on the needs and actual circumstances),

② because of the calculation habit, a lot of people will calculate from the bottom when {box (0-> N)} to calculate the number of box (N+1) First, process the number of digits produced by Box (N) (as in the first calculation box (1) To Box (2) =0 to produce a rounding number 8 box (2) + Carry number = 8) such as the above time addition processing

③ it's best to count from high, you'll save a lot of trouble, box number unknown, it does not matter, with a dynamic array, full time (the highest subscript box produced by the number) and then add a modulo operation, if the module is not large, you can also use the above thought segmentation, and then link box have temporary results, redistribution box ( Must be truncated from high up) if the modulus 123456789123456789 is set to eight-bit box box (1) =89 box (2) =91234567 box (3) =12345678 each box to be combined ( The tradition is that the 123456789123456789÷333=370741108478849 module is 72 so the reallocation box should be box (1) =478849 box (2) =370741180 but not box (1) =370741180 bo X (2) = 478849 Why? ∵ from the high starting to take the mold, box (n) after being taken to the mold once, if the same, again take the model result is box (n) = Box (n) program will enter the dead loop

Another case involving a large number of operations is the N-second-order modulus of a (a ^n mod v) if the MoD number is not (a mod v) * a mod v) * A mod v ... This algorithm is not necessarily recursive implementation, simple loop can be, up to two layers of nested loops

Last advice: For a large number of subtraction, do not easily to be (add/subtract/multiply/divide) The number of factorization, this algorithm efficiency will be very bottom

(Writing a hasty article, there may be a typo, please forgive)



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.