In this HackerEarth Anti-palindrome strings problem solution you are given a number N. you are required to determine the value of the following function:

long long int solve(N)
{
    ans=0;
    for(i=1;i<=N;i++)
    ans+=(N/i);
    return ans;
}

All divisions are integer divisions(i.e. N/i is actually floor(N/i)).


HackerEarth Summation program problem solution


HackerEarth Summation program problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll n,ans,t;
void solve()
{
    ll i,j,k;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ans=0;
        j=sqrt(n);
        for(i=1;i<=j;i++)
        {
            ans+=(n/i);
        }
        for(i=1;i<=j;i++)
        {
            ll lo,hi;
            hi=n/i;
            lo=n/(i+1);
            lo=max(lo,j);
            hi=max(hi,j);
            ans+=i*(hi-lo);
        }
        cout<<ans<<"\n";
    }
}
int main()
{
    
        solve();
    return 0;
}

second solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class FloorSum {
  static class FastReader {
    BufferedReader br;
    StringTokenizer st;
    
    public FastReader() {
      br = new BufferedReader(new
        InputStreamReader(System.in));
    }
    
    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }
    
    int nextInt() {
      return Integer.parseInt(next());
    }
    
    long nextLong() {
      return Long.parseLong(next());
    }
    
    double nextDouble() {
      return Double.parseDouble(next());
    }
    
    String nextLine() {
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }
  
  public static void main(String[] args) {
    
    FastReader fr = new FastReader();
    int t = fr.nextInt();
    assertion(t >= 1 && t <= 100);
    for (int i = 0; i < t; i++) {
      long n = fr.nextLong();
      assertion(n >= 1 && n <= 1e12);
      System.out.println(evaluate(n));
    }
    
  }
  
  private static long evaluate(long n) {
    long ans = 0;
    if (n == 1) {
      return 1;
    }
    ans += 1 + n;
    for (long i = 2; i * i <= n; i++) {
      if (i != n / i) {
        ans += (i + n / i);
      } else {
        ans += i;
      }
      long l = (n / i) + 1;
      long h = (n / (i - 1)) - 1;
      if (l <= h) {
        ans += (h-l+1)*(i-1);
      }
      
    }
    return ans;
  }
  
  private static long evaluate_brute(long n) {
    long ans = 0;
    for (int i = 1; i < n; i++) {
      ans += (n / i);
    }
    return ans;
  }
  
  private static void assertion(boolean condition) {
    if (!condition)
      throw new AssertionError();
  }
}