Factorial sum & program run time & algorithm analysis

Source: Internet
Author: User

Example: Enter n, calculate s = 1! + 2! + 3! + 4! + ... + n! of the last six bits (excluding the leading 0). Which n≤106.

Analysis: Consider the following data overflow program:

#include <stdio.h>intMainvoid){    intN, I; intsum =1; intfactorial =1; scanf ("%d", &N);  for(i =2; I <= N; i++) {factorial= (factorial * i)%1000000; Sum= (sum + factorial)%1000000; } printf ("%d\n", sum); return 0;}

The author source program is as follows:

#include <stdio.h>#include<time.h>intMainvoid){    Const intMOD =1000000; intI, J, n, S =0; scanf ("%d", &N);  for(i =1; i<= N; i++)    {        intfactorial =1;  for(j =1; J <= I; J + +) Factorial= (Factorial * J%MOD); S= (S + factorial)%MOD; } printf ("%d\n"S1000000); printf ("Time used =%.2lf\n", (Double) clock ()/clocks_per_sec); return 0;}

It is worth learning that the program can be timed, using the function clock () function in the Time.h library to return the run time of the program so far, divided by the constant clocks_per_sec the resulting value in "seconds" units. Where constant clocks_per_sec is associated with the operating system. Therefore, do not use the return value of clock () directly. As a result, we call this function before the end of the program, and we can get the whole program running time.

One problem is that when we run the program through the command line, the keyboard input time is also counted. We use pipelines to avoid the interference of the keyboard input time on the test results. In a Windows system, execute command-line commands in the same directory as the compiled program echo 20 | Factsum, where 20 is the data to be entered, and Factsum is the program name. So the operating system will automatically help us to put the 20 input program, so you can avoid keyboard input time.

Analysis of running time of algorithm I have done some detailed discussion here, so we can directly conclude that the algorithm runs at O (N2), and the first program I write is the linear time O (N).

Let's examine whether the analysis is correct, there are generally two common methods can be used:

One is to program and compare the actual observed run time to whether the elapsed time described by the analysis matches. When n is enlarged by one fold, the running time of the linear program is multiplied by the factor 2, the two times the running time of the program multiplied by factor 4, and so on.

Another common technique for verifying whether a program is O (f (n)) is a range of n (usually separated by multiples of 2), which calculates the ratio t (n)/f (n), where T (n) is the actual run time. If f (N) is an ideal approximation of the run time, the calculated value converges to a normal number. If f (N) is estimated to be too large, the calculated value converges to zero. If f (N) is estimated to be too low, then the calculated values diverge.

The final test, according to the running time table can get the program run time, and our analysis can be compared, here involved in the mapping, and the different configurations of the actual running time of the machine has a difference, so the running time table is omitted here. In addition, the average run time of the test 50000 on the local machine is run time is 17.60, so we can boldly predict that when the input is n = 10^6, the program needs to run 17.60* (10^6/50000) ^2 seconds, It takes about 1.96 hours for the program to run out (another: the first linear-time program almost instantly gives the answer). So how to solve this problem, one is directly with the first program, the other needs a little observation, we make a table of output results, found that starting from n=25, the results are unchanged, 940313. We can write a program to find out 25! It is found that there are 6 zeros at the end, so starting with item 25th, all subsequent entries do not affect the last 6 digits of the sum, so add a statement at the top of the program: if (n >) n = 25; So efficiency and overflow are no problem.

All rights Reserved.author: Haifeng:) Copyright©xp_jiang. Reprint please indicate source: http://www.cnblogs.com/xpjiang/p/4153672.html

Resources:
"Algorithmic Competition Primer"--Rujia
"Data structure and algorithm analysis: C Language Description _ Original Book Second Edition"--mark Allen Weiss

Factorial sum & program run time & algorithm analysis

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.