LightOj 1234 Harmonic Number Solution



 Problem Link:  https://lightoj.com/problem/harmonic-number

Solution in C++: 

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include<bits/stdc++.h>
using namespace std;
#define R 0.57721566490 ///R is Euler's constant
double a[1000009];
int main()
{

    int t,x,ts,i;
    double ans,fans,s,n,k;
    ans = 0;
    for (i = 1; i <= 1000000; i++)
    {
        s+= 1.0 / i;
        a[i] =s;
    }
    cin>>t;
    for ( ts = 1; ts <= t; ts++)
    {
        cin>>x;
        if(x <= 1000000)
        {
            printf("Case %d: %.9lf\n", ts, a[x]);
        }
        else

        {
            /// finding harmonic sum formula
            n=x+.5;
            fans=log(n)+R;
            printf("Case %d: %.9lf\n", ts, fans);
        }

    }
}



No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.