We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<iostream>usingnamespacestd;intf(intnum){intfact=num;if(num==0){fact=1;}else{for(inti=num-1;i>0;--i){fact=fact*i;}}returnfact;}intfn(intn){intsum_f=0;stringtemp=to_string(n);for(inti=0;i<temp.length();++i){intconv_i=temp[i]-48;sum_f+=f(conv_i);}returnsum_f;}intsfn(intnum){intsum_fn=0;intsum_f=fn(num);stringtemp=to_string(sum_f);for(inti=0;i<temp.length();++i){intconv_i=temp[i]-48;sum_fn+=conv_i;}returnsum_fn;}intgi(intnum){intsum_fn;inti=0;while(num!=sum_fn){++i;sum_fn=sfn(i);}returni;}intsgi(intnum){intsum_gi=0;for(inti=1;i<=num;++i){inttemp=gi(i);if(temp<10){sum_gi+=temp;}else{strings_temp=to_string(temp);for(intj=0;j<s_temp.length();++j){intconv_i=s_temp[j]-48;sum_gi+=conv_i;}}}returnsum_gi;}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intq,n,m;cin>>q;for(inti=0;i<q;++i){cin>>n>>m;cout<<sgi(n)<<endl;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #254: Sums of Digit Factorials
You are viewing a single comment's thread. Return to all comments →