Newman-Conway Sequence - GeeksforGeeks

16 Apr, 2021
Newman-Conway Sequence is the one which generates the following integer sequence. 1 1 2 2 3 4 4 4 5 6 7 7...In mathematical terms, the sequence P(n) of Newman-Conway numbers is defined by recurrence relation 
P(n) = P(P(n - 1)) + P(n - P(n - 1)) 
with seed values P(1) = 1 and P(2) = 1Given a number n, print n-th number in Newman-Conway Sequence.
Examples :  
Input : n = 2
Output : 1

Input : n = 10
Output : 6
Method 1 (Use Recursion) : 
A simple approach is direct recursive implementation of above recurrence relation.  
C++
Java
Python
C#
PHP
Javascript
// C++ program for n-th// element of Newman-Conway Sequence#include <bits/stdc++.h>using namespace std; // Recursive Function to find the n-th elementint sequence(int n){    if (n == 1 || n == 2)        return 1;    else        return sequence(sequence(n - 1))                + sequence(n - sequence(n - 1));} // Driver Programint main(){    int n = 10;    cout << sequence(n);    return 0;}
// Java program to find nth// element of Newman-Conway Sequenceimport java.io.*; class GFG {         // Recursion to find    // n-th element    static int sequence(int n)    {        if (n == 1 || n == 2)            return 1;        else            return sequence(sequence(n - 1))                  + sequence(n - sequence(n - 1));    }          // Driver Program    public static void main(String args[])    {        int n = 10;        System.out.println(sequence(n));    }} /*This code is contributed by Nikita Tiwari.*/
# Recursive function to find the n-th# element of sequencedef sequence(n):    if n == 1 or n == 2:        return 1    else:        return sequence(sequence(n-1)) + sequence(n-sequence(n-1));         # Driver codedef main():    n = 10    print sequence(n)     if __name__ == '__main__':    main()
// C# program to find nth element// of Newman-Conway Sequenceusing System; class GFG {         // Recursion to find    // n-th element    static int sequence(int n)    {        if (n == 1 || n == 2)            return 1;        else            return sequence(sequence(n - 1)) + sequence                           (n - sequence(n - 1));    }         // Driver code    public static void Main()    {        int n = 10;        Console.Write(sequence(n));    }} // This code is contributed by Nitin Mittal.
<?php// PHP program for n-th element// of Newman-Conway Sequence // Recursive Function to// find the n-th elementfunction sequence($n){    if ($n == 1 || $n == 2)        return 1;    else        return sequence(sequence($n - 1))+               sequence($n - sequence($n - 1));} // Driver Code$n = 10;echo(sequence($n)); // This code is contributed by Ajit.?>
<script> // JavaScript program to find nth// element of Newman-Conway Sequence // Recursion to find// n-th elementfunction sequence(n){    if (n == 1 || n == 2)        return 1;    else        return sequence(sequence(n - 1)) +           sequence(n - sequence(n - 1));} // Driver codelet n = 10;document.write(sequence(n)); // This code is contributed by souravghosh0416   </script>
Output : 
6
Method 2 (Use Dynamic Programming) : We can avoid repeated work done in method 1 by storing the values in the sequence in an array. 
C++
Java
Python
C#
PHP
Javascript
// C++ program to find the n-th element of// Newman-Conway Sequence#include <bits/stdc++.h>using namespace std; // Function to find the n-th elementint sequence(int n){    // Declare array to store sequence    int f[n + 1];    int i;    f[0] = 0;    f[1] = 1;    f[2] = 1;     for (i = 3; i <= n; i++)        f[i] = f[f[i - 1]] + f[i - f[i - 1]];        return f[n];} // Driver Programint main(){    int n = 10;    cout << sequence(n);    return 0;}
// JAVA Code for Newman-Conway Sequenceimport java.util.*; class GFG {         // Function to find the n-th element    static int sequence(int n)    {        // Declare array to store sequence        int f[] = new int[n + 1];        f[0] = 0;        f[1] = 1;        f[2] = 1;         int i;              for (i = 3; i <= n; i++)            f[i] = f[f[i - 1]] +                        f[i - f[i - 1]];                 return f[n];    }         /* Driver program to test above function */    public static void main(String[] args)    {         int n = 10;         System.out.println(sequence(n));     }} // This code is contributed by Arnav Kr. Mandal.
''' Python program to find the n-th element of    Newman-Conway Sequence''' # To declare array import module arrayimport arraydef sequence(n):    f = array.array('i', [0, 1, 1])     # To store values of sequence in array    for i in range(3, n + 1):        r = f[f[i-1]]+f[i-f[i-1]]        f.append(r);     return r # Driver codedef main():    n = 10    print sequence(n)     if __name__ == '__main__':    main()
// C# Code for Newman-Conway Sequenceusing System; class GFG {         // Function to find the n-th element    static int sequence(int n)    {        // Declare array to store sequence        int []f = new int[n + 1];        f[0] = 0;        f[1] = 1;        f[2] = 1;         int i;             for (i = 3; i <= n; i++)            f[i] = f[f[i - 1]] +                   f[i - f[i - 1]];             return f[n];    }         // Driver Code    public static void Main()    {        int n = 10;        Console.Write(sequence(n));     }} // This code is contributed by Nitin Mittal.
<?php// PHP program to find the n-th element // of Newman-Conway Sequence // Function to find// the n-th elementfunction sequence($n){         // Declare array to    // store sequence    $i;    $f[0] = 0;    $f[1] = 1;    $f[2] = 1;     for ($i = 3; $i <= $n; $i++)        $f[$i] = $f[$f[$i - 1]] +                 $f[$i - $f[$i - 1]];     return $f[$n];} // Driver Code$n = 10;echo(sequence($n)); // This code is contributed by Ajit.?>
<script>// Javascript program to find the n-th element// of Newman-Conway Sequence // Function to find// the n-th elementfunction sequence(n){         // Declare array to    // store sequence    let i;    let f = [];    f[0] = 0;    f[1] = 1;    f[2] = 1;     for (let i = 3; i <= n; i++)        f[i] = f[f[i - 1]] +                f[i - f[i - 1]];     return f[n];} // Driver Codelet n = 10;document.write(sequence(n)); // This code is contributed by gfgking.</script>
Output : 
6
Time Complexity : O(n)References : https://archive.lib.msu.edu/crcmath/math/math/n/n078.htm 
nitin mittal
jit_t
souravghosh0416
gfgking
series
Dynamic Programming
Mathematical
Dynamic Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16
Subset Sum Problem | DP-25
Matrix Chain Multiplication | DP-8
Coin Change | DP-7
C++ Data Types
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
Coin Change | DP-7
Merge two sorted arrays