Welcome to blogs

don't get bored and read those blogs :D
Unsplashed background img

java implementation of conway algorithm

Here I present my implementation of conway algorithm in java. For that i developped a recursive algorithm with two functions :
getOccurrenceOf(String number,int pos, String num) : this function takes 3 arguments : a number, a position and a number of one digit and it counts number of successive occurrence of the number of one digit from the given position :
public static int getOccurrenceOf(String number,int pos, String num){
    int occu = 1;
    for(int n = pos; n < number.length()-1 ; n++){
      if(  num.equals(""+number.charAt(n+1)) )
      	return occu;
    return occu;
The next function does the most of work but it goes to one next level, it takes a number and return the next levle in conway
public static String conwayOneLevel(String number){
    String coded = "";
    for(int i = 0; i < number.length() ;){
      	int occurrence  = getOccurrenceOf(number,i,""+number.charAt(i));
    	String res = ""+ occurrence + number.charAt(i);
      	coded += res;
      	i += occurrence;
  	return coded;
The last function is a recursive one, it takes two arguments the level you want to achieve and a number, it uses conwayOneLevel function and stops when level is 0
public static String conway(int lvl, String number){
    if(lvl != 0)
      return conway(lvl-1,conwayOneLevel(number));
    return number;
download the source project here
May. 21 2017

Leave a comment


Be the first one to leave a comment