Created
January 18, 2013 23:17
-
-
Save GaProgMan/4569509 to your computer and use it in GitHub Desktop.
One possible solution for problem 6 on the Project Euler website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Project Name: Problem Six Edit | |
* Solution Name: Problem Six Edit | |
* Original creation date: 11/07/2011 | |
* Edit date: 18/01/2013 | |
* Programmer name: Jamie Taylor (aka "GaProgMan") | |
* File name: ProblemSixEdit.c | |
* | |
* Purpose of the project: | |
* This code is my solution for "Problem Six" listed | |
* on Project Euler. | |
* The original problem is listed in the comment | |
* block below. | |
* Based on a comment made on my blog post conta- | |
* ining this code, I've decided to optimise it | |
* using the two forumlae below. | |
* Here is a link to the original post on my blog, | |
* with the comment trail at the bottom of the page: | |
* http://wp.me/p19MGD-jh | |
* I've also decided to re-write this code in c | |
* for the simple reason that it can be compiled on | |
* a terminal with MingW or gcc - the PC I'm using | |
* to writethis has no IDEs or compilers installed | |
* on it. | |
* Formulae used: | |
* Square of the sum: | |
* Sn = [n(n + 1)] / 2 | |
* Sum of the squares: | |
* S^2n = [n(n + 1)(2n + 1)]/6 | |
* Information on both of these formulae can be | |
* found in many textbooks on mathematics and on many | |
* mathematics websites. | |
* Here's an excellent website that provides a | |
* useful look at the forumla for the Square of the | |
* sum, and how to derive it: | |
* http://bit.ly/mD84iY | |
* Here is an excellent website that | |
* provides a useful look at the forumla for the Sum | |
* of the squares, and how to derive it: | |
* http://bit.ly/80s4Vg | |
* | |
* GNU Copyright information | |
* Copyright 2011 Jamie Taylor <jamie@taylorj.org.uk> | |
* | |
* This program 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 2 of the License, or (at | |
* your option) any later version. | |
* | |
* This program 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 this program; if not, write | |
* to the Free Software Foundation, Inc., 51 Franklin | |
* Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
*/ | |
#include <stdio.h> | |
int main () { | |
long sumOfSquares = 0; | |
long squareOfSums = 0; | |
long difference = 0; | |
const int N = 100; | |
sumOfSquares = (N * (N + 1)) / 2; | |
squareOfSums = (N * (N + 1) * (2 * N + 1)) / 6; | |
//by doing away with the calls to pow() in math.h, and just using x*x to | |
//calculate a square the code becomes even more lean. | |
difference = (sumOfSquares * sumOfSquares) - squareOfSums; | |
printf("The difference between the square of the sums and the\nsum of the squares for all real numbers between 1 and %d is: %ld", N, difference); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment