Example 2-3 the sum of factorial

Source: Internet
Author: User

Topic Requirements: Enter n, calculate s = 1! +2! +3! +...n! The last 6 bits, n<=10^6

Example input: 10

Sample output: 37913

Source:

1#include <stdio.h>2#include <time.h>3 4 intMain ()5 {6     //const int MOD = 1000000;7     intI,j,n,s =0;8scanf"%d",&n);9      for(i =1; I <= n;i++)Ten     { One         intfactorial =1; A          for(j =1; J <= i;j++) -Factorial = (factorial *j); -s = (s +factorial); the     } -printf"%d\n", s%1000000); -printf"Time =%.21f\n",(Double) clock ()/clocks_per_sec); -     return 0; +}

This is the summation and then the modulo.

If the gradual modulo is summed again, the following:

1#include <stdio.h>2#include <time.h>3 4 intMain ()5 {6     Const intMOD =1000000;7     intI,j,n,s =0;8scanf"%d",&n);9      for(i =1; I <= n;i++)Ten     { One         intfactorial =1; A          for(j =1; J <= i;j++) -Factorial = (factorial * J%MOD); -s = (s + factorial)%MOD; the     } -printf"%d\n", s); -printf"Time =%.21f\n",(Double) clock ()/clocks_per_sec); -     return 0; +}

The first paragraph, the small number can be, input 100 multiplication overflow

The second big number can also be, that is, the number of large-size program to run too long time;

Example 2-3 the sum of factorial

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.