Newman-Conway序列用于生成以下整数序列。
1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12
用于生成n个数字的Newman-Conway序列的公式是-
P(n) = P(P(n - 1)) + P(n - P(n - 1)) Where, p(1) =p(2) =1
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
#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;
}输出结果
如果我们运行上面的程序,那么它将生成以下输出。
Newman-Conway Sequence is :1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12