Here's a fun exercise.
http://www.hughchou.org/hugh/grid_game.cgi?CLEAR
Who can write the most elegant yet performant code to win this game? My solution is in C/C++.
#include<vector>
#include<iostream>
#include<fstream>
#define N 64
std::vector<int> numbers(N+1);
void init()
{
for(int i = 0; i < N+1; ++i)
numbers[i] = 1;
}
int getbest()
{
int max_difference = -9999999;
int best_number = -1;
for(int i = 1; i <= N; ++i)
{
// if the number is available for choosing, check how many points it's worth
if(numbers[i])
{
int sum_factors = 0;
for(int j = 1; j < i; ++j)
{
if(numbers[j] && i%j == 0)
sum_factors += j;
}
// if we found any factors
if(sum_factors != 0 && (i - sum_factors) > max_difference)
{
max_difference = i - sum_factors;
best_number = i;
}
}
}
std::cout<<"max difference: "<<max_difference<<"\n";
std::cout<<"best choice: "<<best_number<<"\n";
for(int i = 1; i < best_number; ++i)
{
if(best_number % i == 0)
numbers[i] = 0;
}
return best_number;
}
int main()
{
init();
int n;
do
{
n = getbest();
} while(n != -1);
}