Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Cunningham chain

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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)

  • 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 <bits/stdc++.h>
 
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




<?php
// PHP program for cunningham chain
// Function to print
// Cunningham chain of the first kind
function print_C($p0)
{
    $p1 = 0; $i = 0; $x; $flag; $k;
 
    // Iterate till all elements
    // are printed
    while (1)
    {
        $flag = 1;
        $x = 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;
        echo $p1 . " ";
        $i++;
    }
}
 
// Driver Code
$p0 = 2;
print_C($p0);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Javascript




<script>
// Javascript program for cunningham chain
// Function to print
// Cunningham chain of the first kind
function print_C(p0)
{
    let p1 = 0;
    let i = 0;
    let x;
    let flag;
    let k;
 
    // Iterate till all elements
    // are printed
    while (1)
    {
        flag = 1;
        x = Math.pow(2, i);
        p1 = x * p0 + (x - 1);
 
        // check prime or not
        for (let k = 2; k < p1; k++)
        {
            if (p1 % k == 0) {
                flag = 0;
                break;
            }
        }
        if (flag == 0)
            break;
        document.write(p1 + " ");
        i++;
    }
}
 
// Driver Code
let p0 = 2;
print_C(p0);
 
// This code is contributed
// by gfgking
 
</script>


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 <bits/stdc++.h>
 
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




<?php
// PHP program for cunningham chain
 
 
// Function to print
// Cunningham chain of the second kind
function print_t($p0)
{
    $p1; $i = 0; $x; $flag; $k;
 
    // Iterate till all elements
    // are printed
    while (1)
    {
        $flag = 1;
        $x = 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;
        echo $p1 . " ";
        $i++;
    }
}
 
// Driver Code
$p0 = 19;
print_t($p0);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Javascript




<script>
 
// JavaScript program for cunningham chain
 
// Function to print
// Cunningham chain of the second kind
function print(p0)
{
    var p1, i = 0, x, flag = 1, k, m = 4;
  
    // Iterate till all elements
    // are printed
    while (flag) {
        flag = 1;
        x = 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;
        document.write(p1 + " ");
        i++;
    }
}
 
// Driver Code
var p0 = 19;
print(p0);
 
//This code is contributed by Shivani
 
</script>


Output: 

19 37 73

 

Time complexity : O(n^2), where n is the number of elements in the Cunningham chain of the first kind.

Space complexity: O(1), as it only uses a few variables and doesn’t increase with the size of the input.

 


My Personal Notes arrow_drop_up
Last Updated : 14 Feb, 2023
Like Article
Save Article
Similar Reads
Related Tutorials