# Total SUM(https://www.codechef.com/CBST2021/problems/TSUM)

Practice

Author: noob_tech
Tester: noob_tech
Editorialist: noob_tech

# DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

Math .

# PROBLEM:

You are given two integers N and M.
You have to find Sum of integers from N to M which are not divisible by 3 or 5.

# EXPLANATION:

Here,we are given two integer N and M .We have to find sum of interger Form N to M which are not divisible by 3 or 5.
Note- N and M must be included.
Consider N=1 and M=10 , we have to find sum of integer from 1 to 10.
Add all digit from 1 to 10 which is not divisible by 3 or 5. i.e (1+2+4+7+8)=22.
So output will be 22.

# SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t–)
{
int n, m;
cin >> n >> m;
long long total = ((m - n + 1) * (n + m)) / 2;
long long sum = 0, sum1 = 0;
for (int i = n; i <= m; i++)
{
if (i % 3 == 0)
{
sum = sum + i;
}
if (i % 5 == 0 && i % 3 != 0)
{
sum1 = sum1 + i;
}
}
long long final_ans = total - (sum + sum1);
cout << final_ans << endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t–)
{
int n, m;
cin >> n >> m;
long long total = ((m - n + 1) * (n + m)) / 2;
long long sum = 0, sum1 = 0;
for (int i = n; i <= m; i++)
{
if (i % 3 == 0)
{
sum = sum + i;
}
if (i % 5 == 0 && i % 3 != 0)
{
sum1 = sum1 + i;
}
}
long long final_ans = total - (sum + sum1);
cout << final_ans << endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t–)
{
int n, m;
cin >> n >> m;
long long total = ((m - n + 1) * (n + m)) / 2;
long long sum = 0, sum1 = 0;
for (int i = n; i <= m; i++)
{
if (i % 3 == 0)
{
sum = sum + i;
}
if (i % 5 == 0 && i % 3 != 0)
{
sum1 = sum1 + i;
}
}
long long final_ans = total - (sum + sum1);
cout << final_ans << endl;
}
return 0;
}