A high-precision algorithm for Zhang Yifei seeking n!

Source: Internet
Author: User
Tags data structures
Abstract: Zhang Yifei is a member of the IOI national training Team of the 3 session (2000,2001,2002), 14th session (2002, Kyung Hee University, Yongin, Korea)The International information Science Olympic competition gold medal winner, this article is Zhang Yifei 2001 's thesis, the original question asks the n! the high precision algorithm, the original text may from Http://oibh.kuye.cn/download/thesis/thesis2001_zhangyifei.zip Download here   a high-precision algorithm for finding n!The algorithm in this paper focuses on the content of this article in the Pascal language, do you know the high precision? What data structures have you used before? Have you considered how to optimize the algorithm? Here, you will see how to multiply speed up to find n! high-precision algorithm
standard integer types in Pascal
Data type Range
Shortint -128 ~ 127
Byte 0 ~ 255
Integer -32768 ~ 32768
Word 0 ~ 65535
Longint -2147483648 ~ 2147483647
Comp -9.2e18 ~ 9.2e18
Comp, although a real type, is actually a 64-bit integer
The basic idea of high precision algorithmThe standard integer type in Pascal can only handle integers between 2 63 ~ 2 63. If you want to support larger integer operations, the basic idea of using high-precision and high-precision algorithms is to divide large integers that cannot be processed directly into a number of Small integer segment, converting the processing of large integers to these Small integer segmentThe processing
selection of data StructuresKeep as many bits as possible for each small integer segment use the Comp type in binary notation
  Keep as many bits as possible for each small integer segmentAn example: Calculates two 15-digit and Ø method one • Divides into 15 small integer segments, each of which is 1 digits, requires 15 times 1-digit addition O method Two • is divided into 5 small integer segments, each segment is 3 digits, 5 times 3-digit addition O method three comp types can directly handle 15-bit integers, so 1 times plus You can do it. Ø comparison • Calculates 1-digit addition with an integer and 3-digit addition is as fast as possible • The method of two methods is more efficient • Although the operation of comp is slower than integer, the number of additions is significantly reduced • Practice proves that method three is faster than method two
Use Comp typeIn high-precision operations, each small integer segment can be used as a comp type to indicate that the comp RMS number is 19 ~ 20 bits for two high-precision numbers, each integer segment can retain 17-bit high-precision number and not more than M-bit integer, each integer segment can retain the 18–M bit of two high-precision number of product, each integer Segment can be reserved 9 bits if each small integer segment retains the K-bit decimal number, it can actually be thought that it only holds 1-bit K-binary numbers, referred to as High binary number, which is called the 1-bit high binary number is single-precision number
using binary notationUsing binary representation, the time-space efficiency of the operation will be improved, but the problem generally needs to be output in decimal, so a time-consuming conversion process is also required. Therefore, this method is generally not used in the competition, nor is it discussed in this article.
optimization of the algorithmAnalysis of complexity of multiplication with high accuracy multiply complexity set cache decomposition factorization to find the factorial dichotomy method for the adjustment after factorization decomposition
complexity analysis of high precision multiplicationThe calculation of the n-bit high-and M-bit high-input number of the product Ø requires N*M multiplication O product may be n+m–1 or n+m bit high-binary number
analysis of the complexity of the multiplication (1)An example: Calculating the 5*6*7*8ø method one: Sequential multiplication 5*6=30, 1*1=1 multiplication 30*7=210, 2*1=2 multiplication 210*8=1680, 3*1=3 multiplication Total 6 times Ø method Two: non-sequential connecting multiplication 5*6=30, 1*1 = 1 times Multiplication 7*8=56, 1*1= 1 multiplication 30*56=1680, 2*2=4 multiply by 6 times
analysis of the complexity of the multiplication (2)If the "N-digit *m digit =n+m bit number", n single-precision number, no matter in what order, multiply the number of times must be N (n-1)/2 times O Proof: • Set f (n) to indicate multiplication times, f (1) = 0, to meet the problem set • Set K<n, f (k) =k (k-1)/2, now calculates F ( N) • The last multiplication is calculated as "K-number * (n-k) digits", then f (n) =f (k) +f (n-k) +k (n-k) =n (n-1)/2 (not related to K selection)
set Cache (1)An example: Calculating 9*8*3*2ø method One: Sequential 9*8=72, 1*1=1 multiplication 72*3=216, 2*1=2 times multiplication 216*2=432, 3*1=3 times multiplication O method two: non-sequential multiplication 9*8=72, 1*1=1 multiply • 3 *2=6, 1*1=1 multiplication 72*6=432, 2*1=2 times multiplication

Total 6 times multiplication
Features: n-digit *m number of digits may be n+m-1

Features: n-digit *m number of digits may be n+m-1
Set Cache (2)Consider k+t single-precision multiplication a1*a2* ... *ak*ak+1* ... *ak+tø a1*a2* ... *ak result is M-bit high-number (assuming already calculated) øak+1* ... *ak+t result is a 1-bit high-input number Ø If the order is multiplied, the T-time "m-digit * 1-digit number is required ", a total of Mt multiplication O can first calculate ak+1* ... *ak+t, then multiply again, only need m+t multiplication

When the cache is set, the product of m single-precision numbers is computed, and if the result is n-digits, the multiplication is approximately n (n–1)/2 times, and the M-relationship is not much


– Set S=a 1 a 2 ... a M, S is n-bit high-binary number – you can approximate the process of multiplication by dividing the m number into n groups, the product of each group is still a single-precision number, and finally the product of the number of the following n is calculated. Time is mainly concentrated in the final n number of the product, then basically meet the "n-digit *m =n+m bit number", so the multiplication can be approximated as n (n-1)/2 times
Set Cache (3)The size of the cache Ø Set the selected standard data type maximum can be directly processed T-bit decimal number O set the cache to K-bit decimal number, each small integer segment to save t–k bit decimal number O set the final result is n-bit decimal number, then multiply the number of øk/(n–k) ∑ (i=1..n/k) i= (n+k ) n/(2k (t–k)), where K is far less than nø to multiply the least, only K (t–k) is the largest, at this time k=t/2ø therefore, the size of the cache is the same size as each small integer segment, the most efficient o so in the general multiplication operation, you can use comp as the basic integer type, each small integer field 9-bit decimal number, cache is also a 9-bit decimal number
decomposition factorization to find factorialExample: 10!=2 8 * 4 2 *7øn! Decomposition of the complexity of factorization is far less than NLOGN, can be ignored Ø compared with the ordinary algorithm, decomposition factorization, although the number of factors m more, but the result of the number of n has not changed, as long as the use of the cache, multiply the number of times or about N (n-1)/2 o So, decomposition factorization not slow (this can also be Description

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.