# HackerEarth Recursive Sums problem solution

In this HackerEarth Recursive Sums problem solution Little Bear has received a home assignment to find the sum of all digits in a number N. Following his affinity towards single-digit numbers, he intends to repeatedly compute the sum of all digits until the sum itself becomes a single-digit number.

Can you write a program to compute the final single-digit sum?

As the number N is very big, it is given in the following run-length encoded format - N is represented as a sequence of M blocks, where each block i (0 ≤ i < M) is represented by two integers - (len[i], d[i]). This implies that the digit d[i] occurs a len[i] number of times.

## HackerEarth Recursive Sums problem solution.

`#include <bits/stdc++.h>using namespace std;#define MOD                     1000000007#define pb(x)                   push_back(x)#define mp(x,y)                 make_pair(x,y)#define FF                      first#define SS                      second#define s(n)                    scanf("%d",&n)#define sl(n)                   scanf("%lld",&n)#define sf(n)                   scanf("%lf",&n)#define ss(n)                   scanf("%s",n)#define sc(n)                   {char temp; ss(temp); n=temp;}#define INF                     (int)1e9#define LINF                    (long long)1e18#define EPS                     1e-9#define maX(a,b)                ((a)>(b)?(a):(b))#define miN(a,b)                ((a)<(b)?(a):(b))#define abS(x)                  ((x)<0?-(x):(x))typedef long long ll;typedef unsigned long long LL;typedef pair<int,int> PII;typedef pair<LL,LL> PLL;typedef pair<int,PII> TRI;typedef vector<int> VI;typedef vector<LL> VL;typedef vector<ll> vl;typedef vector<PII> VII;typedef vector<TRI> VT;int n;int TEST_NO;void precompute() {  }void read() {  s(n);  ll ans = 0;  bool g0 = false;  for (int i = 0; i < n; ++i) {    ll len, d;    cin >> len >> d;    if(d > 0) g0 = true;    ans += (len * d) % 9;    if(ans >= 9) ans -= 9;  }  if(ans == 0 and g0) ans = 9;  cout << ans << endl;}void preprocess() {  }void solve() {  }int main() {  precompute();  int t;  s(t);  for(TEST_NO = 1; TEST_NO <= t; TEST_NO ++) {    read();    preprocess();    solve();  }  return 0;}`

### Second solution

`#include <iostream>using namespace std;#define ull unsigned long longint cal(ull d) {  if (d < 10) return d;  int res = 0;  while (d > 0) {    res += d % 10;    d /= 10;  }  return cal(res);}int main() {    int cases, n, d;    for (scanf("%d", &cases); cases--; ) {      scanf("%d", &n);      ull sum = 0, len;      for (int i = 0; i < n; i++) {        cin >> len >> d;        sum += d * len;        sum = cal(sum);      }      sum = cal(sum);      cout << sum << endl;    }    return 0;}`