I watched an interesting youtube video on the numberphile channel:

https://www.youtube.com/watch?v=ZfKTD5lvToE

about "perfect" numbers. I was thinking that nobody really sits around trying to calculate these things do they?

Anyway I thought it might be cool to practice my arduino programming skills to replicate the results. It took about 4 hours but I learned a lot about how to manipulate arrays. Here are the results:





















and here's the code I came up with:


unsigned int number = 2;         //the number being tested
unsigned int divisor = 1;        //divisor to find factors
int r = 0;                       //remainder
int f = 0;                       //test factor
int fCount = 0;                  //interger for storing array values
int factors[32];                 //array to store the factors
int sum = 0;                     //sum of factor interger
int lim = 10000;                 //highest number to check
int i = 0;


void setup() {
  Serial.begin(9600);
}

void loop() {
  while(number < lim){
    //Serial.print("number:");
    //Serial.print("\t");
    //Serial.println(number);
    //Serial.print("factors:");
    //Serial.print("\t");
    while(divisor <= number){
      r = number % divisor;
      f = number / divisor;
      if(r == 0){
        factors[fCount] = f;
        fCount++;
      }
      divisor++;
    }
    for(i = 0; i < fCount; i = i + 1){
      //Serial.print(factors[i]);
      //Serial.print("\t");
    }
    //Serial.println();
    for(i = 1; i <fCount; i = i + 1){
      sum = sum + factors[i];
    }
    //Serial.print("sum:");
    //Serial.print("\t");
    //Serial.println(sum);
    if(sum == number){
      Serial.print("Found one!");
      Serial.print("\t");
      Serial.println(number);
      Serial.print("The factors are:");
      Serial.print("\t");
      for(i = 1; i < fCount; i = i + 1){
        Serial.print(factors[i]);
        Serial.print("\t");
      }
      Serial.println();
      Serial.print("Found at:");
      Serial.print("\t");
      Serial.print(millis());
      Serial.println(" milliseconds");
      Serial.println();  
    }
    //Serial.println();
    sum = 0;
    fCount = 0;
    divisor = 1;
    number = number + 2;
  }
  if(number == lim){
    Serial.println("Done!");
    delay(60000);
  }
}



It could probably be optimized a lot more. I told the program to ignore odd numbers to speed it up but I'm sure it could use it's information to ignore more numbers by the looks of it. There's a bunch of commented out lines for debugging but it works. It was more of a learning tool for me than anything. Arrays are no-longer a mystery!