Uva 1185 - Big Number

/*
 Problem:: Find the number of digits of N! till 10^7.

 Verdict :: Accepted
 Formula:- D = floor[log10(1)+log10(2)+log10(3)...+log10(N)];

*/

#include<bits/stdc++.h>

#define sf scanf
#define pf printf
#define pi acos(-1.0)
#define mx 10001000
using namespace std;

double ans[mx];

void facto_digits()
{
    ans[1] = log10(1);

    for(int i=2; i<=mx; i++)
    {
        ans[i] = ans[i-1] + log10(i);
    }
}

int main()
{
    facto_digits();
    int n,t;
    sf("%d",&t);
    while(t--)
    {
        sf("%d",&n);
        pf("%d\n",(int)(floor(ans[n]) + 1) );
    }

    return 0;
}


//=====================================================================================================



// Second Solution

#include<bits/stdc++.h>
#define pi acos(-1.0)

using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;

        if(n == 1)
        {
            cout << "1\n";
            continue;
        }

        int res = floor( ((n+0.5)*log(n) - n + 0.5*log(2*pi))/log(10) ) + 1;
        cout << res << "\n";
    }

    return 0;
}
 

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number