Project Euler Homework
Classes | Functions | Variables

020.cpp File Reference

Find the sum of digits in 100! More...

#include <iostream>

Go to the source code of this file.

Classes

class  bigN

Functions

int main ()
 Compute factorial of a big number.

Variables

const unsigned long long digits = 1e17
const int gps = 10

Detailed Description

Find the sum of digits in 100!

n! means n = (n - 1) * ... * 3 * 2 * 1 Find the sum of the digits in the number 100!

Definition in file 020.cpp.


Function Documentation

int main ( )

Compute factorial of a big number.

C++ types: unsigned long: up to 2^32-1 ~ 10^9.63
unsigned long long: up to 2^64-1 ~ 10^19.27

Using estimation formula,

\[ n! \sim \sqrt{2n\pi} \left ( \frac{n}{e} \right )^n \left (1+\frac{1}{12n} \right ) \]

$ 100! \sim 10^{157.97} $ which has 158 digits, it is split to a number of segments in order to be processed by C++.

Let x be the number in one segment, $ 100x<2^{64} \Rightarrow x <= 10^{17} $

each segments should contains at most 17 digits.

So there are a total of $ 158/17<=10 $ segments.

Multiply each segments from 1 to 100, in each step raise the "carry" digits to next segments.

Definition at line 74 of file 020.cpp.

 All Classes Files Functions Variables