Newman-Conway numbers is the one which generates the following integer sequence.
1 1 2 2 3 4 4 4 5 6 7 7….. and follows below recursive formula.
P(n) = P(P(n - 1)) + P(n - P(n - 1))
Given a number n then print n terms of Newman-Conway Sequence
Examples:
Input : 13 Output : 1 1 2 2 3 4 4 4 5 6 7 7 8 Input : 20 Output : 1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12
C++
// C++ Program to print n terms // of Newman-Conway Sequence #include <bits/stdc++.h> using namespace std; // Function to find // the n-th element void sequence(int n) { // Declare array to store sequence int f[n + 1]; f[0] = 0; f[1] = 1; f[2] = 1; cout << f[1] << " " << f[2] << " "; for (int i = 3; i <= n; i++) { f[i] = f[f[i - 1]] + f[i - f[i - 1]]; cout << f[i] << " "; } } // Driver Program int main() { int n = 13; sequence(n); return 0; } |
Java
// Java Program to print n terms // of Newman-Conway Sequence class GFG { // Function to find // the n-th element public static void sequence(int n) { // Declare array to store sequence int f[] = new int[n + 1]; f[0] = 0; f[1] = 1; f[2] = 1; System.out.print( f[1] + " " + f[2] + " "); for (int i = 3; i <= n; i++) { f[i] = f[f[i - 1]] + f[i - f[i - 1]]; System.out.print(f[i] + " "); } } //Driver code public static void main(String []args) { int n = 13 ; sequence(n); } } // This program is contributed // by upendra singh bartwal |
Python3
# Python Program to print n terms # of Newman-Conway Sequence def sequence(n): # Function to find # the n-th element # Declare array to store sequence f = [0, 1, 1] print(f[1], end=" "), print(f[2], end=" "), for i in range(3,n+1): f.append( f[f[i - 1]] + f[i - f[i - 1]]) print(f[i], end=" "), # driver code n = 13sequence(n) # This code is contributed # by upendra singh bartwal |
C#
// C# Program to print n terms // of Newman-Conway Sequence using System; class GFG { // Function to find // the n-th element public static void sequence(int n) { // Declare array to store sequence int []f = new int[n + 1]; f[0] = 0; f[1] = 1; f[2] = 1; Console.Write( f[1] + " " + f[2] + " "); for (int i = 3; i <= n; i++) { f[i] = f[f[i - 1]] + f[i - f[i - 1]]; Console.Write(f[i] + " "); } } // Driver code public static void Main() { int n = 13 ; sequence(n); } } // This program is contributed // by vt_m. |
PHP
<?php // PHP Program to print n terms // of Newman-Conway Sequence // Function to find // the n-th element function sequence($n) { // Declare array to // store sequence $f=array(0); $f[0] = 0; $f[1] = 1; $f[2] = 1; echo $f[1] , " " , $f[2] , " "; for ($i = 3; $i <= $n; $i++) { $f[$i] = $f[$f[$i - 1]] + $f[$i - $f[$i - 1]]; echo $f[$i], " "; } } // Driver Code { $n = 13; sequence($n); return 0; } // This code is contributed by nitin mittal. ?> |
Output :
1 1 2 2 3 4 4 4 5 6 7 7 8
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print first N terms of Lower Wythoff sequence
- Print N terms of Withoff Sequence
- Print the sequence of size N in which every term is sum of previous K terms
- Find n terms of Fibonacci type series with given first two terms
- Sum of first N terms of Quadratic Sequence 3 + 7 + 13 + ...
- Print the first N terms of the series 6, 28, 66, 120, 190, 276, ...
- Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Program to print the series 1, 9, 17, 33, 49, 73, 97... till N terms
- Program to print the series 2, 15, 41, 80, 132, 197… till N terms
- Program to print the series 1, 3, 4, 8, 15, 27, 50… till N terms
- Program to print Collatz Sequence
- Find the sum of all the terms in the n-th row of the given series
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Number of terms in Geometric Series with given conditions
- Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Sum of the series 5+55+555+.. up to n terms
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.
Improved By : nitin mittal

