坎宁安链是质数的序列。它有2种类型:
- 第一种坎宁安链:它是长度为n的质数序列,如下所述:
Let p1, p2, p3, …., pn be a cunningham chain of length n than
p2 = 2*p1 + 1
p3 = 4*p1 + 3
p4 = 8*p1 + 7
. . .
. . .
pn = 2n-1*p1 + (2n-1 – 1)这里的p1,p2,p3,….,pn都是质数。如果p的任何值不是非质数,则链终止于它之前的数字。
对于p0 = 2,序列将为2 5 11 23 47
下面是上述的实现:
C++
// C++ program for cunningham chain // Function to print the series // of first kind #include
using namespace std; // Function to print // Cunningham chain of the first kind void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all elements // are printed while (1) { flag = 1; x = (int)(pow(2, i)); p1 = x * p0 + (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; printf("%d ", p1); i++; } } // Driver Code int main() { int p0 = 2; print(p0); return 0; }
Java
// Java Program to print the // series of first kind class GFG { // Function to print // Cunningham chain // of the first kind static void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all // elements are printed while (true) { flag = 1; x = (int)(Math.pow(2, i)); p1 = x * p0 + (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; System.out.print(" " + p1); i++; } } // Driver Code public static void main(String args[]) { int p0 = 2; print(p0); } } // This code is contributed // by Kirti_Mangal
Python3
# Python3 program for cunningham chain # Function to print Cunningham chain # of the first kind def print_C(p0): i = 0; # Iterate till all elements # are printed while(True): flag = 1; x = pow(2, i); p1 = x * p0 + (x - 1); # check prime or not for k in range(2, p1): if (p1 % k == 0): flag = 0; break; if (flag == 0): break; print(p1, end = " "); i += 1; # Driver Code p0 = 2; print_C(p0); # This code is contributed by mits
C#
// C# Program to print the // series of first kind using System; class GFG { // Function to print // Cunningham chain // of the first kind static void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all // elements are printed while (true) { flag = 1; x = (int)(Math.Pow(2, i)); p1 = x * p0 + (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; Console.Write(" " + p1); i++; } } // Driver Code public static void Main() { int p0 = 2; print(p0); } } // This code is contributed // by Akanksha Rai(Abby_akku)
PHP
C++
// C++ program for cunningham chain // Function to print the series // of second kind #include
using namespace std; // Function to print // Cunningham chain of the second kind void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all elements // are printed while (1) { flag = 1; x = (int)(pow(2, i)); p1 = x * p0 - (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; printf("%d ", p1); i++; } } // Driver Code int main() { int p0 = 19; print(p0); return 0; }
Java
// Java program for cunningham chain // Function to print the series // of second kind class GFG{ // Function to print Cunningham chain // of the second kind static void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all elements // are printed while (true) { flag = 1; x = (int)(Math.pow(2, i)); p1 = x * p0 - (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; System.out.print(p1+" "); i++; } } // Driver Code public static void main(String[] args) { int p0 = 19; print(p0); } } // This code is contributed by mits
Python3
# Python3 program for cunningham chain # Function to print Cunningham chain # of the second kind def print_t(p0): i = 0; # Iterate till all elements # are printed while (True): flag = 1; x = pow(2, i); p1 = x * p0 - (x - 1); # check prime or not for k in range(2, p1): if (p1 % k == 0): flag = 0; break; if (flag == 0): break; print(p1,end=" "); i+=1; # Driver Code p0 = 19; print_t(p0); # This code is contributed by mits
C#
// C# program for cunningham chain // Function to print the series // of second kind using System; class GFG { // Function to print // Cunningham chain of the second kind static void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all elements // are printed while (true) { flag = 1; x = (int)(Math.Pow(2, i)); p1 = x * p0 - (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; Console.Write(p1 + " "); i++; } } // Driver Code static void Main() { int p0 = 19; print(p0); } } // This code is contributed by mits
PHP
输出:2 5 11 23 47
- 第二类坎宁安链:它是长度为n的质数序列,如下所述:
Let p1, p2, p3, …., pn be a cunningham chain of length n than
p2 = 2*p1 – 1
p3 = 4*p1 – 3
p4 = 8*p1 – 7
. . .
. . .
pn = 2n-1*p1 – (2n-1 – 1)对于p0 = 19,该序列将为19、37、73。
下面是上述的实现:
C++
// C++ program for cunningham chain // Function to print the series // of second kind #include
using namespace std; // Function to print // Cunningham chain of the second kind void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all elements // are printed while (1) { flag = 1; x = (int)(pow(2, i)); p1 = x * p0 - (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; printf("%d ", p1); i++; } } // Driver Code int main() { int p0 = 19; print(p0); return 0; } Java
// Java program for cunningham chain // Function to print the series // of second kind class GFG{ // Function to print Cunningham chain // of the second kind static void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all elements // are printed while (true) { flag = 1; x = (int)(Math.pow(2, i)); p1 = x * p0 - (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; System.out.print(p1+" "); i++; } } // Driver Code public static void main(String[] args) { int p0 = 19; print(p0); } } // This code is contributed by mits
Python3
# Python3 program for cunningham chain # Function to print Cunningham chain # of the second kind def print_t(p0): i = 0; # Iterate till all elements # are printed while (True): flag = 1; x = pow(2, i); p1 = x * p0 - (x - 1); # check prime or not for k in range(2, p1): if (p1 % k == 0): flag = 0; break; if (flag == 0): break; print(p1,end=" "); i+=1; # Driver Code p0 = 19; print_t(p0); # This code is contributed by mits
C#
// C# program for cunningham chain // Function to print the series // of second kind using System; class GFG { // Function to print // Cunningham chain of the second kind static void print(int p0) { int p1, i = 0, x, flag, k; // Iterate till all elements // are printed while (true) { flag = 1; x = (int)(Math.Pow(2, i)); p1 = x * p0 - (x - 1); // check prime or not for (k = 2; k < p1; k++) { if (p1 % k == 0) { flag = 0; break; } } if (flag == 0) break; Console.Write(p1 + " "); i++; } } // Driver Code static void Main() { int p0 = 19; print(p0); } } // This code is contributed by mits
的PHP
输出:19 37 73