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 ++ ;}}