Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print n terms of Newman-Conway Sequence
The Newman-Conway Sequence is a fascinating mathematical sequence that generates integers using a recursive formula. It starts with 1, 1 and each subsequent term is calculated based on previous values in the sequence.
Syntax
P(n) = P(P(n - 1)) + P(n - P(n - 1)) where P(1) = P(2) = 1
Algorithm
The algorithm to generate n terms of Newman-Conway sequence is −
START Step 1 ? Input variable n (e.g. 20) Step 2 ? Initialize variables as i, p[n+1], p[1]=1, p[2]=1 Step 3 ? Loop For i=3 and i<=n and i++ Set p[i] = p[p[i - 1]] + p[i - p[i - 1]] Print p[i] Step 4 ? End Loop For STOP
Example
Here's a complete C program to print the first n terms of Newman-Conway sequence −
#include <stdio.h>
int main() {
int n = 20, i;
int p[n + 1];
p[1] = 1;
p[2] = 1;
printf("Newman-Conway Sequence is: ");
printf("%d %d ", p[1], p[2]);
for (i = 3; i <= n; i++) {
p[i] = p[p[i - 1]] + p[i - p[i - 1]];
printf("%d ", p[i]);
}
printf("<br>");
return 0;
}
Newman-Conway Sequence is: 1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12
How It Works
The sequence works by using the recursive formula where each term depends on two previous terms. For example:
- P(3) = P(P(2)) + P(3-P(2)) = P(1) + P(2) = 1 + 1 = 2
- P(4) = P(P(3)) + P(4-P(3)) = P(2) + P(2) = 1 + 1 = 2
- P(5) = P(P(4)) + P(5-P(4)) = P(2) + P(3) = 1 + 2 = 3
Conclusion
The Newman-Conway sequence demonstrates how complex patterns can emerge from simple recursive formulas. This implementation efficiently computes the sequence using dynamic programming with O(n) time and space complexity.
Advertisements
