Cunningham chain

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 than 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++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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)

    chevron_right

    
    

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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)

    chevron_right

    
    

    Output:

    19 37 73
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.