Rule30: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 7: Line 7:


;Task:
;Task:
Write a program that prints out the evolution of the rule 30th 1-dimensional celular automata as consecutive rows of ASCII characters all forming the classic triangle:
Write a program that prints out the evolution of the rule 30 on 1D celular automata as consecutive rows of ASCII characters. Showing the classic triangle:

<center>
<center>
. . . . . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . .
Line 35: Line 36:
</center>
</center>


For standarization propuses, use rows of 50 cells, and just run the firts 25th iterations.
(For standardization proposes, use rows of 50 cells, and just run the first 25th iterations.)



=={{header|C}}==
=={{header|C}}==

Revision as of 13:39, 30 August 2020

Task
Rule30
You are encouraged to solve this task according to the task description, using any language you may know.

Rule 30 is a simple binary cellular automaton introduced by Stephen Wolfram on his book "A New Kind of Science" (1983).

This rule is of particular interest because it produces complex, seemingly random patterns from simple, well-defined rules. Because of this, Wolfram believes that Rule 30, and cellular automata in general, are the key to understanding how simple rules produce complex structures and behaviour in nature.

Rule 30 has also been used as a random number generator in Mathematica, and has also been proposed as a possible stream cipher for use in cryptography.

Task

Write a program that prints out the evolution of the rule 30 on 1D celular automata as consecutive rows of ASCII characters. Showing the classic triangle:

. . . . . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . # # # . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . # # . . # . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . # # . # # # # . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . # # . . # . . . # . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . # # . # # # # . # # # . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . # # . . # . . . . # . . # . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . # # . # # # # . . # # # # # # . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . # # . . # . . . # # # . . . . . # . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . # # . # # # # . # # . . # . . . # # # . . . . . . . . . . . . . . 
. . . . . . . . . . . . . # # . . # . . . . # . # # # # . # # . . # . . . . . . . . . . . . . 
. . . . . . . . . . . . # # . # # # # . . # # . # . . . . # . # # # # . . . . . . . . . . . . 
. . . . . . . . . . . # # . . # . . . # # # . . # # . . # # . # . . . # . . . . . . . . . . . 
. . . . . . . . . . # # . # # # # . # # . . # # # . # # # . . # # . # # # . . . . . . . . . . 
. . . . . . . . . # # . . # . . . . # . # # # . . . # . . # # # . . # . . # . . . . . . . . . 
. . . . . . . . # # . # # # # . . # # . # . . # . # # # # # . . # # # # # # # . . . . . . . . 
. . . . . . . # # . . # . . . # # # . . # # # # . # . . . . # # # . . . . . . # . . . . . . . 
. . . . . . # # . # # # # . # # . . # # # . . . . # # . . # # . . # . . . . # # # . . . . . . 
. . . . . # # . . # . . . . # . # # # . . # . . # # . # # # . # # # # . . # # . . # . . . . . 
. . . . # # . # # # # . . # # . # . . # # # # # # . . # . . . # . . . # # # . # # # # . . . . 
. . . # # . . # . . . # # # . . # # # # . . . . . # # # # . # # # . # # . . . # . . . # . . . 
. . # # . # # # # . # # . . # # # . . . # . . . # # . . . . # . . . # . # . # # # . # # # . . 
. # # . . # . . . . # . # # # . . # . # # # . # # . # . . # # # . # # . # . # . . . # . . # . 
# # . # # # # . . # # . # . . # # # . # . . . # . . # # # # . . . # . . # . # # . # # # # # # 

(For standardization proposes, use rows of 50 cells, and just run the first 25th iterations.)


C

Simple implementation using pointers.

<lang c>

  1. include <stdio.h>
  2. define N 50
  3. define N_ITER 25

int rule30(int x1, int x2, int x3){

       return !((x1 & (x2 | x3)) | (!x1 & !x2 & !x3));

}

int main(){ int i,k; int r1[N]={0}; int r2[N]={0}; int *p1,*p2,*temp;

p1=&r1[0]; //pointer a r1 p2=&r2[0]; //pointer a r2

r1[N/2]=1; //inicialización.

for (k=1;k<N_ITER;k++){ for (i=1;i<N-2;i++){ printf("%s ",*(p1+i+1)? "#":"."); *(p2+i+1)=rule30(*(p1+i),*(p1+i+1),*(p1+i+2)); } printf("\n"); temp=p1;p1=p2;p2=temp; //swap pointers del array }

return 0; }</lang>