Print n terms of Newman-Conway Sequence

CServer Side ProgrammingProgramming

Newman-Conway Sequence is used for generating following integer sequence.

1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12

Formula used for generating Newman-Conway sequence for n numbers is −

P(n) = P(P(n - 1)) + P(n - P(n - 1))
Where, p(1) =p(2) =1

Algorithm

START
Step 1 -> Input variable n(e.g. 20)
Step 2 -> start 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

#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]);
   }
   return 0;
}

Output

if we run above program then it will generate following output.

Newman-Conway Sequence is :1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12
raja
Published on 30-Jul-2019 08:40:43
Advertisements