UVA 10852 - Less Prime Solution

Solve in C++:

#include<bits/stdc++.h>
#define M 1000009
using namespace std;
bool marked[M];
vector<int>vec;
void sieve(int n)
{
    for(int i=4; i<=n; i+=2)
        marked[i]=true;
    for(int i=3; i*i<=n; i+=2)
    {
        if(marked[i]==false)
        {
            for(int j=i*i; j<=n; j+=i)
            {
                marked[j]=true;
            }
        }
    }
    vec.push_back(2);
    for(int i=3; i<=n; i+=2)
    {
        if(marked[i]==false)
        {
            vec.push_back(i);

        }
    }
}
main()
{
    int m,r=0,f,i,t;
    sieve(1000009);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&m);
        {
            r=(m/2)+1;
            while(1)
            {
                if(marked[r]==false)
                {
                    cout<<r<<endl;
                    break;
                }

                else
                    r++;
            }

            r=0;
        }
    }

}

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.