Problem
Link : UVA
Problem # 686 ( Goldbach's Conjecture (II))
প্রবলেমটা খুব সহজ কিন্তু আমার বার বার runtime error দেখাইতেছিল মাথা নষ্ট হওয়ার মতো অবস্থা পরে বুঝতে পারলাম আমার সিভের অ্যালগরিদমে prime Array ঠিক ভাবে declare করা হয় নাই কিন্তু আমি প্রথমেই ওইটাকে ঠিক ধরে নিয়ে বাকি গুলা চেক করছিলাম।
যাই হোক প্রবলেমটা সল্ভ করে মজা পাইছি,
Solution টা ঠিক আছে নাকি -ইনপুট আউট চেক করার জন্য নিচের input-output গুলো ব্যাবহার করা যেতে পারে ।
input ---> output
22292 ---> 177
30000 ---> 602
32000 ---> 312
100 ---> 6
20 ---> 2
598 ---> 15
2222 ----> 35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*------------------------------------------------*/ | |
//Problem Setter : Miguel A. Revilla | |
//Uva Problem No : 686 | |
//Problem Name : Goldbach's Conjecture (II) | |
//Author : shohanur Rahaman | |
//University : City University | |
//E-mail : shohan4556@gmail.com | |
/*-----------------------------------------------*/ | |
#include<stdio.h> | |
#include<math.h> | |
#define N 100000 | |
#define M 50000 | |
char P[N]; | |
int prime[M]; | |
void sieve( ); | |
int main() | |
{ | |
sieve(); | |
int i,j,count=0,n,temp; | |
while(scanf("%d",&n)==1){ | |
if(n==0) | |
break; | |
temp=n; | |
for(i=0;i<=temp/2;i++) | |
for(j=0;j<=i;j++){ | |
if(prime[i]+prime[j]==n){ | |
count++; | |
} | |
} | |
printf("%d\n",count); | |
count=0; | |
} | |
return 0; | |
} | |
void sieve( ) | |
{ | |
int i,j,root; | |
for(i=0;i<N;i++) | |
P[i]=1; | |
P[0]=P[1]=0; | |
root=sqrt(N); | |
for(i=2;i<=root;i++) | |
if(P[i]==1){ | |
for(j=2;i*j<=N;j++) | |
P[i*j]=0; | |
} | |
j=0; | |
for(i=2;i<N && j<M;i++){ | |
if(P[i]==1) | |
prime[j++]=i; | |
} | |
} | |