I'm getting tle in this code any suggestions.I'm calculating the sum n/i where n is the input and i goes from 2 to n. for example for 5 pairs will be (2,2),(3,3),(4,4),(5,5),(2,4)
#include<stdio.h>
int main()
{
int i,j,t,n,m;
long long k;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&n);
m=n/2;
k=0;
for(j=2;j<=m;j++)
{
k+=(n/j);
}
k+=(n-m);
printf("Case %d: %lld\n",i+1,k);
}
return 0;
}`
There are a lot of problems with your code.
Broadly classifying -
Optimization can be talked about when the issue no.2 gets resolved.
Code gives wrong answer. Your algorithm is,summarily, incorrect. Dump this approach and think in terms of "Euler Totient" and elementary dynamic programming.
For example,
Now, N would contain all the pairs that N-1 had. In addition to that it would also contain some newer ones( for which you can take help of Euler Totient function).
Sample Test cases for your practice -