Related Articles

# Cunningham chain

• Last Updated : 26 May, 2021

A Cunningham chain is a sequence of prime numbers. It is of 2 types:

• Cunningham chain of the first kind: It is a sequence of prime numbers of length n described as below :

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)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

• Here p1, p2, p3, …., pn are all prime numbers. If any value of p comes out to be non-prime then chain ends at the number which came before it.
for p0 = 2, the sequence will be 2 5 11 23 47
Below is the implementation of the above:

## 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

 `

## Javascript

 ``
Output:
`2 5 11 23 47`

• Cunningham chain of the second kind: It is a sequence of prime numbers of length n described as below:

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)

• for p0 = 19, the sequence will be 19, 37, 73.
Below is the implementation of the above:

## 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

 `

## Javascript

 ``
Output:
`19 37 73`

My Personal Notes arrow_drop_up