-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQ20factorial_sum.cpp
59 lines (44 loc) · 1.11 KB
/
Q20factorial_sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*Project Euler Problem 20*/
/*
* author @fifinonz
* https://github.com/fifinonz
*
#NOTE: Logic feels correct,
#however the output produced is 784
# correct output is 648
*/
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(int argc, char * argv[] ) {
clock_t t;
t = clock();
string fvalue;
char x;
double factorial = 1;
int z=0;
int i=1;
while ( i<100)
{
factorial=factorial*i;
i++;
}
fvalue = to_string(factorial); // to compile in linux use-: g++ **-std=c++0x** filename.cpp -o filename
for(int j=0; j<(int)fvalue.length(); j++){
x=fvalue[j];
int y=atoi(&x);
z+=y; // ASCII arithmetic
cout<<z<<endl;
}
cout<<"\nFactorial: "<<setprecision(fvalue.length())<<factorial<<endl;
cout<<"\nTotal Number of Digits: "<<fvalue.length()<<endl;
cout<<"\nSum: "<<z<<endl;
t = clock() - t;
cout<<"\n\nProgram Runtime-:\n"<<"Clock ticks:"<<t<<" \nSeconds: "<<((float)t)/CLOCKS_PER_SEC<< endl;
return 0;
}