Project Euler #6: Sum square difference
Question
The sum of the squares of the first ten natural numbers is,
The square of the sum of the first ten natural numbers is,
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is
.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Answer : 25164150
Hacker Rank Question
Solution
The sum of the squares of the first ten natural numbers is,
.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Answer : 25164150
Hacker Rank Question
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); long t = in.nextLong(); for(int a0 = 0; a0 < t; a0++){ int n = in.nextInt(); System.out.println(SumSquaredifference(n)); } } public static long SumSquaredifference(long n){ if(n == 1) return 0; return SumSquaredifference(n-1) + n*n*(n-1); } } |
Comments
Post a Comment