Twin Prime Number Problem

Source: Internet
Author: User

Twin Prime Number Problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 3 Description to write a program, find out all the twin prime number within the prime number range of the number of groups. In general, the twin prime number refers to an adjacent prime number that is near two prime numbers and cannot be near any more. Some children's shoes start to write programs as soon as they see the questions and do not read the questions carefully. In order to prevent the children's shoes that are not carefully read the questions, it is stipulated that the two prime numbers adjacent to 1 will also become the twin prime numbers. Enter N (0 <N <100) in the first line to indicate the number of test data groups. The next group of test data is given m, indicating to find all the twin prime numbers before m. (0 <m <1000000) output each group of test data output occupies one row, the number of all the twins in the m range. Sample input 114 sample output 4 [cpp]/**************************** * ***** Date: * Author: SJF0115 * question: Nanyang tech question 26: Twin Prime Number Problem * Source: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 26 * result: AC * Source: * conclusion: * *********************************/# include <stdio. h> # include <math. h> int prime [1000001]; // prime number table int Primes (int n) {int I, j; for (I = 1; I <= n; I ++) {// even if (I % 2 = 0) {prime [I] = 0 ;}// odd else {prime [I] = 1 ;}} // The multiples of odd numbers are certainly not prime numbers for (I = 3; I <= sqrt (n); I + = 2) {if (prime [I]) {for (j = I + I; j <= n; j + = I) {prime [j] = 0 ;}} return 0 ;}int main () {int N, M, count, I; while (scanf (" % D ", & N )! = EOF) {while (N --) {count = 0; scanf ("% d", & M); // evaluate the Susu Primes (M ); // calculate the twin prime number for (I = 2; I <= M-2; I ++) {if (prime [I] & prime [I + 2]) {count ++; // printf ("% d \ n", I, I + 2) ;}// the two prime numbers adjacent to 1 also become twin prime numbers. In this case, only 2, 3 if (M> = 3) {count ++;} printf ("% d \ n", count) ;}} return 0 ;} /********************************** Date: * Author: SJF0115 * question: Nanyang tech question 26: Twin Prime Number Problem * Source: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 26 * result: AC * Source: * conclusion: * *********************************/# include <stdio. h> # include <math. h> int prime [1000001]; // prime number table int Primes (int n) {int I, j; for (I = 1; I <= n; I ++) {// even if (I % 2 = 0) {prime [I] = 0 ;}// odd else {prime [I] = 1 ;}} // The multiples of odd numbers are certainly not prime numbers for (I = 3; I <= sqrt (n); I + = 2) {if (prime [I]) {for (j = I + I; j <= n; j + = I) {prime [j] = 0 ;}} return 0 ;}int main () {int N, M, count, I; while (scanf ("% d", & N )! = EOF) {while (N --) {count = 0; scanf ("% d", & M); // evaluate the Susu Primes (M ); // calculate the twin prime number for (I = 2; I <= M-2; I ++) {if (prime [I] & prime [I + 2]) {count ++; // printf ("% d \ n", I, I + 2) ;}// the two prime numbers adjacent to 1 also become twin prime numbers. There is only one such case 2, 3if (M> = 3) {count ++;} printf ("% d \ n", count) ;}} return 0 ;} note: The next group of test data is given m, indicating to find all the twin prime numbers before m. But it actually includes m. Test Case: m = 7 twin prime number: 2, 3, 5, 5, 7

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.