Task: Evolutionary algorithm

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char target[] = "METHINKS IT IS LIKE A WEASEL";
const char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";

#define CHOICE (sizeof(tbl) - 1)
#define MUTATE 15
#define COPIES 30


int irand(int n)
{
	int r, rand_max = RAND_MAX - (RAND_MAX % n);
	while((r = rand()) >= rand_max);
	return r / (rand_max / n);
}


int unfitness(const char *a, const char *b)
{
	int i, sum = 0;
	for (i = 0; a[i]; i++)
		sum += (a[i] != b[i]);
	return sum;
}


void mutate(const char *a, char *b)
{
	int i;
	for (i = 0; a[i]; i++)
		b[i] = irand(MUTATE) ? a[i] : tbl[irand(CHOICE)];

	b[i] = '\0';
}

int main()
{
	int i, best_i, unfit, best, iters = 0;
	char specimen[COPIES][sizeof(target) / sizeof(char)];

	
	for (i = 0; target[i]; i++)
		specimen[0][i] = tbl[irand(CHOICE)];
	specimen[0][i] = 0;

	do {
		for (i = 1; i < COPIES; i++)
			mutate(specimen[0], specimen[i]);

		
		for (best_i = i = 0; i < COPIES; i++) {
			unfit = unfitness(target, specimen[i]);
			if(unfit < best || !i) {
				best = unfit;
				best_i = i;
			}
		}

		if (best_i) strcpy(specimen[0], specimen[best_i]);
		printf("iter %d, score %d: %s\n", iters++, best, specimen[0]);
	} while (best);

	return 0;
}










Fortran:  
 	module evolve_routines
 
 	implicit none
 	
 	
 	character(len=*),parameter :: targ = 'METHINKS IT IS LIKE A WEASEL'
 	
 	contains
 
 	
 
 	pure elemental function fitness(member) result(n)
 
 
 
 
 	
 	implicit none
 	integer :: n
 	character(len=*),intent(in) :: member
 	
 	integer :: i
 	
 	n=0
 	do i=1,len(targ)
 		n = n + abs( ichar(targ(i:i)) - ichar(member(i:i))  )
 	end do
 	
 
 	end function fitness
 
 	
 
 	pure elemental subroutine mutate(member,factor)
 
 
 
 	
 	implicit none
 	character(len=*),intent(inout) :: member   
 	real,intent(in) :: factor                  
 	
 	integer,parameter :: n_chars = 27	
 	character(len=n_chars),parameter :: chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
 	
 	real    :: rnd_val
 	integer :: i,j,n
 	
 	n = len(member)
 		
 	do i=1,n
 		rnd_val = rand()
 		if (rnd_val<=factor) then   
 			rnd_val = rand()
 			j = int(rnd_val*n_chars)+1   
 			member(i:i) = chars(j:j)
 		end if
 	end do
 	
 
	end subroutine mutate
 

 
 	end module evolve_routines
 
 
 
 	program evolve
 
 
 
 	use evolve_routines
 	
 	implicit none
 	
 	
 	integer,parameter :: seed = 12345             
 	integer,parameter :: max_iter = 10000         
 	integer,parameter :: population_size = 200    
 	real,parameter    :: factor = 0.04            
 	integer,parameter :: iprint = 5               
 	
 	
 	integer :: i,iter
 	integer,dimension(1) :: i_best
 	character(len=len(targ)),dimension(population_size) :: population
 	
 	
 	call srand(seed)
 	
 	
 	
 	population(1) = 'PACQXJB CQPWEYKSVDCIOUPKUOJY'  
 	iter=0
 		
 	write(*,'(A10,A30,A10)') 'iter','best','fitness'
 	write(*,'(I10,A30,I10)') iter,population(1),fitness(population(1))
 		
 	do 
 	
 		iter = iter + 1 
 		
  		
 		if (mod(iter,iprint)==0) write(*,'(I10,A30,I10)') iter,population(1),fitness(population(1))
		
 		
 		if ( iter>max_iter .or. fitness(population(1))==0 ) exit
 	
 		
 		population = population(1)	
 		do i=2,population_size
 			call mutate(population(i),factor)	
 		end do
 	
 		
 		
 		i_best = minloc(fitness(population))
 		population(1) = population(i_best(1))
 		 	
 	end do
 	
 	
 	if (mod(iter,iprint)/=0) write(*,'(I10,A30,I10)') iter,population(1),fitness(population(1))
 	 	
 	if (iter>max_iter) then
 		write(*,*) 'No solution found.'
 	else
 		write(*,*) 'Solution found.'
 	end if
 	
 
 	end program evolve