Advertisement
cd62131

Sum of square

Jan 20th, 2014
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.41 KB | None | 0 0
  1. public class SumOfSquare {
  2.   private static int sumOfSquare(int n, int acc) {
  3.     if (n == 0) return acc;
  4.     return sumOfSquare(n - 1, acc + n * n);
  5.   }
  6.  
  7.   private static int sumOfSquare(int n) {
  8.     return n * (n + 1) * (2 * n + 1) / 6;
  9.   }
  10.  
  11.   public static void main(String[] args) {
  12.     System.out.println("sum = " + sumOfSquare(50, 0));
  13.     System.out.println("sum = " + sumOfSquare(50));
  14.   }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement