Find Primes

Finds all prime numbers between 2 and 255 and stores them in a global array.

Processor: ATtiny 2313

This example was compiled for the 2313 but will run on all processors.*
The calculation takes just over 8 million cycles to find all the primes and uses the following algorithm to find them:

char primes[55];

int main(void){
  int i,j,k;
  int count = 0;

  for (i = 2; i <= 255; ++i){
    for(k = 0,j = 1; j <= i; ++j){
      if(i % j == 0)
        k++;
    }

    if(k == 2)
      primes[count++] = i;
  }

  return 0;
}

Found primes are stored in ram starting at location 0x60. To best see the primes configure the memory panel to show bytes rather than words and switch to decimal representation.

The variable 'i' is stored at location 0x0D2
The variable 'j' is stored at location 0x0D4
The variable 'k' is stored at location 0x0D6
The variable 'count' is stored at location 0x0D8

*If run on the ATmega238 this location is in the I/O memory rather than sram.