Skip to content

Instantly share code, notes, and snippets.

@rodrigoalvesvieira
Created September 26, 2013 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigoalvesvieira/6708308 to your computer and use it in GitHub Desktop.
Save rodrigoalvesvieira/6708308 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
/**
* The contents of this file are not directly bound to a normal Algorithms class
* Here I plan to store some known Algorithms that represent proven Mathematical formulas
* for computing calculations that may appear in my programming assignments
*
* @author Rodrigo Alves
*
*/
public class Formulas {
/**
* Computes the sum of the squares of the first n positive integers
* http://www.texpaste.com/n/9a6oaoqf
* @param n
* @return the sum of the squares of the first n positive integers
*/
public static int integerSquareSum(int n) {
return ((n * (n + 1)) * ((2*n) + 1)) / 6;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/* System.out.println(integerSquareSum(3)); // 1^2 + 2^2 */
/* System.out.println(integerSquareSum(10)); // 1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 */
System.out.println("Calculate the sum of the squares of the first n integers. Enter n:");
int n = input.nextInt();
System.out.println(integerSquareSum(n));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment