/** * skienna-2-4.c * A simple implementation of the code from problem 2-4 in Skiena. * * Samuel A. Rebelsky * rebelsky@grinnell.edu * * Copyright (c) 2015 Samuel A. Rebelsky. All rights reserved. * * This file is part of "CSC 301 Examples" * * CSC 301 Examples is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * CSC 301 Examples is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with CSC 301 Examples. If not, see . */ // +---------+------------------------------------------------------- // | Headers | // +---------+ #include #include // +-----------+----------------------------------------------------- // | Constants | // +-----------+ /** The maximum value of N. */ #define MAX_N 64 // +---------+------------------------------------------------------- // | Helpers | // +---------+ /** * Problem 2-4 from Skiena, translated to C. */ int conundrum(int n) { int r = 0; int i, j, k; for (i = 0; i <= n; i++) { for (j = i+1; j <= n; j++) { for (k = i + j - 1; k <= n; k++) { r += 1; } // for k } // for j } // for i return r; } // conundrum int hypothesis(int n) { return n*n*n; } // hypothesis // +------+---------------------------------------------------------- // | Main | // +------+ int main(int argc, char *argv[]) { printf ("\tn\tconundrum\thypthesis\n"); int n = 1; while (n <= 64) { printf ("%8d\t%8d\t%8d\n", n, conundrum (n), hypothesis (n)); n *= 2; } return 0; } // main