Twin prime number,

Source: Internet
Author: User

Twin prime number,

Description
Write a program to find the number of groups of all the twins in the prime number range. 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.
Input
N (0 <N <100) indicates 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
The output of each group of test data occupies one row, and the number of all twin prime groups in the m range of this behavior.


#include<iostream>
Using namespace std; int main () {int n; cin> n; int a [n]; // n groups int I; for (I = 0; I <n; I ++) cin> a [I]; // enter the numbers int k = 0, flag = 1; int sum = 0; int q = 0; while (k <n) // returns the number of twin prime numbers of each group in a loop {int j = a [k]; // 1 ~ A [k] int B [j]; int m; if (j = 2) // 2 is the prime number B [q ++] = j; else if (j> 2) // calculate the prime number of each group and store it in array B {for (m = 2; m <= j; m ++) {I = 2; while (I <= (m + 1)/2) {if (m % I = 0) {flag = 0; break;} I ++;} if (flag) B [q ++] = m; flag = 1 ;}for (I = 0; I <q-1; I ++) // calculate the number of twin prime groups of each prime number if (B [I + 1]-B [I] = 2 | B [I + 1]-B [I] = 1) sum ++; cout <sum <endl; // number of output Prime Number Groups sum = 0; // reset q = 0; k ++ ;}}


 

Related Article

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.