Dirichlet's Theorem on Arithmetic Progressions
http://poj.org/problem?id=3006
1 #include<stdio.h> 2 #include<math.h> 3 int is_prime(int n) 4 { 5 int i,m; 6 if (n==1) 7 return 0; 8 m = sqrt(n); 9 for (i = 2; i <= m; i ++) 10 { 11 if (n%i==0) 12 return 0; 13 } 14 return 1; 15 } 16 int main() 17 { 18 int a,d,n,i; 19 while(~scanf("%d%d%d",&a,&d,&n)) 20 { 21 int cnt = 0; 22 if (a==0&&d==0&&n==0) 23 break; 24 for (i = a;; i =i+d) 25 { 26 if(is_prime(i)) 27 cnt++; 28 if(cnt==n) 29 break; 30 } 31 printf("%d\n",i); 32 } 33 return 0; 34 }