CRACK1- Editorial

CRACK1: Editorial

Problem-code: CRACK1

Contest-Code: CSMH2021

Author: RAPETI MAHENDRA

Editorialist: RAPETI MAHENDRA

DIFFICULTY:

Simple

PREREQUISITES:

Basic observations, Sieve of Eratosthenes

PROBLEM:

One day Chef teacher gives him a problem to solve.
The problem is as follows:
Given a number N , print the number corresponding to input by observing following series

0 2 5 5 10 10 ...

Chef took it as a challange but struggling a lot to find series help him carefully so that his teacher don't notice you!!

EXPLANATION

on observing the series it is easy to conclude that for given n the answer is just sum of all prime numbers ranging from 1 to n.
As n is very small we can simply check for primes (or) use sieve of eratosthenes.

SOLUTION:

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,i,j;
cin>>t;
bool prime[n+1];
memset(prime,true,sizeof(prime));
for(i=2;ii<=n;i++)
{
if(prime[i]==true)
{
for(j=i
i;j<=n;)
{
prime[j]=false;
j+=i;
}
}

}
while(t--)
{
    cin>>n;
     int sum=0;
for(i=2;i<=n;i++)
{
    if(prime[i])
    {
     sum+=i;
    }
}
cout<<sum<<endl;

}
return 0;

}

Feel free to Share your approach, if you want to. (even if its same :stuck_out_tongue: ) . Suggestions are welcomed as always had been. :slight_smile:

1 Like