Sum of squares of first N natural numbers

You are given an integer 'N'. You need to find the sum of squares of the first 'N' natural numbers.

For example:
If 'N' = 4. You need to return 1^2 + 2^2 + 3^2 + 4^2 = 30.
Input Format:
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run. 
The first and the only line of each test case contains an integer 'N'.
Output Format:
For each test case, return the sum of squares of the first 'N' natural numbers in a single line.
Constraints:
1 ≤ T ≤ 10^4
1 ≤ N ≤ 5*10^5

where 'T' is the number of test cases and 'N' is the given number.
Time Limit: 1 sec.

Note:

You are not required to print the expected output, it has already been taken care of. Just implement the function.
girish jagtap
5mo
int in=4; int op=IntStream.rangeClosed(1, in).map(x->x*x).sum(); System.out.println(op);
Help your peers!
Add answer anonymously...
LTIMindtree Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter