UVA 10311 Goldbach and Euler Solution

Solution in c++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include<set>
#include<queue>
#define efor(i,a,b) for(int i=a; i<=b; i++)
#define zfor(i,n) for (int i=0; i<n; i++)
#define mall(ar, val) memset(ar, val, sizeof(ar))
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
using namespace std;
const int M = 100000000 + 1;
bool isprime[M];
vector<int>vec;
void sieve()
{
    mall(isprime,true);
    isprime[0] = isprime[1] = false;

    for (int i=4; i<M; i+=2)
        isprime[i] = false;

    for (int i=3; i*i<M; i+=2)
        if (isprime[i]==true)
            for (int j=i*i; j<M; j+=i+i)
                isprime[j] = false;

    vec.pb(2);
    for (int i=3; i<M; i+=2)
        if (isprime[i]==true)
            vec.pb(i);
}
main()
{

    //freopen("in.txt","r",stdin);
    //freopen("ou.txt","w",stdout);
    int m,r,f,i,lower,v,l;
    sieve( );
    while(scanf("%d",&m)!=EOF)
    {
        int i,j,f=0;

        if(m<4)
            printf("%d is not the sum of two primes!\n",m);
         else  if(m%2==0)
        {
            f=0;
            for(i=(m/2)-1; i>=0; i--)
            {
                if((isprime[i]==true)&&(isprime[m-i]==true))
                {
                    printf("%d is the sum of %d and %d.\n",m,i,m-i);
                    f=1;
                    break;
                }
                if(f==1)
                    break;
            }
            if(f!=1)
                printf("%d is not the sum of two primes!\n",m);
        }
        else if (m%2==1)
        {
            if(isprime[m-2]==true)
                printf("%d is the sum of 2 and %d.\n",m,m-2);
            else
                printf("%d is not the sum of two primes!\n",m);
        }
    }
}

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

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

Powered by Blogger.