unsigned squares_above( unsigned n, unsigned m, unsigned min ) { if ( n == 0 ) return 1; if ( m == 0 ) return 0; unsigned c = 0; for ( int i = min; i * i <= n; ++i ) { c += squares_above( n - i * i, m - 1, i ); } return c; } unsigned squares( unsigned n, unsigned m ) { return squares_above( n, m, 1 ); }