Help me in solving COUNTN problem

My issue

include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t–)
{
long long a,b=0;
cin>>a;

         if(a%2==0)
        cout<<a*2<<endl;
        else
        {
            for(int i=2;i<=a;i++)
            b=b+a*i;
            cout<<b<<endl;
        }
}

}

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        long long a,b=0;
        cin>>a;
            
             if(a%2==0)
            cout<<a*2<<endl;
            else
            {
                for(int i=2;i<=a;i++)
                b=b+a*i;
                cout<<b<<endl;
            }
    }

}

Problem Link: Sum of N Practice Coding Problem

Hello @alisvirani ,
This code is correct only for even numbers by chance.
But for odd it will be incorrect [ONLY correct for k = 3 (again by chance)]

Eg. - for k=5 → correct answer=50 but your answer=70

It is not as simple as it seems.

Pre-requisites : Sieve of Eratosthenes
HINT : Lookout for smallest prime factor

As for even numbers smallest prime factor is 2 → a*2 is working

Hope it helps !!