POJ3006: Dirichlet's Theorem on Arithmetic Progressions

Source: Internet
Author: User

DescriptionIf a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, I. e ., a, a + d, a + 2d, a + 3d, a + 4d ,..., contains infinitely implements prime numbers. this fact is known as Dirichlet's Theorem on Arithmetic Progressions, which had been conjectured by Johann Carl Friedrich Gauss (1777-1855) and was proved by Johann Peter Gustav Lejeune Diri Chlet (1805-1859) in 1837.For example, the arithmetic sequence beginning with 2 and increasing by 3, I. e ., 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98 ,..., contains infinitely contains prime numbers2, 5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89 ,.... your mission, shocould you decide to accept it, is to write a progr Am to find the nth prime number in this arithmetic sequence for given positive integers a, d, and n. inputThe input is a sequence of datasets. A dataset is a line containing three positive integers a, d, and n separated by a space. a and d are relatively prime. you may assume a <= 9307, d <= 346, and n <= 210.The end of the input is indicated by a line containing three zeros separated by a space. I T is not a dataset. outputThe output shocould be composed of as many lines as the number of the input datasets. each line shoshould contain a single integer and shoshould never contain extra characters. the output integer corresponding to a dataset a, d, n shoshould be the nth prime number among those contained in the arithmetic sequence beginning with a and increasing by d. FYI, it is known that the result is Always less than 106 (one million) under this input condition. sample Input367 186 151179 10 203271 39103 230 127 104 185253 851 19075 337 210307 79331 221 177259 170 40269 1020 0 0 0 Sample examples // The question is very simple. Find out the question starting with, use the nth prime number that increments by B to create a [cpp] # include <iostream> # include <string. h> using namespace std; const int maxn = 1000005; int num [maxn], I, j; int main () {int a, d, n; memset (num, 1, sizeof (num); num [1] = num [0] = 0; for (I = 2; I <= maxn/2; I ++) {if (num [I]) {for (j = I + I; j <maxn; j + = I) {num [j] = 0 ;}} while (cin >>>a> d> n) {if (! A &&! D &&! N) {break;} for (I = a; n; I + = d) {if (num [I]) {n --;}} cout <I-d <endl;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.