Find the total number of Squares in a N*N chessboard.
 
Example 1:
Input:
N = 1
Output:
1
Explanation:
A 1*1 chessboard has only 1 square.
Example 2:
Input:
N = 2
Output:
5
Explanation:
A 2*2 chessboard has 5 squares.
4 1*1 squares and a 2*2 square.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function squaresInChessBoard() which takes an Integer N as input and returns the number of squares in a N*N chessboard.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Combinatorics

Mathematics

Starter code: #User function Template for python3

class Solution:
    def squaresInChessBoard(self, N):
         # code here

Solutions:
class Solution:

	def squaresInChessBoard(self, N):
		i = N * (N + 1) * (2 * N + 1) // 6
		return i


class Solution:

	def squaresInChessBoard(self, n):
		return int(n * (n + 1) / 2 * (2 * n + 1) / 3)


class Solution:

	def squaresInChessBoard(self, N):
		sum = 0
		for i in range(1, N + 1):
			a = i * i
			sum = sum + a
		return sum


class Solution:

	def squaresInChessBoard(self, N):
		if N == 1:
			return 1
		else:
			b = 0
			for i in range(1, N + 1):
				a = i ** 2 + b
				b = a
			return a


class Solution:

	def squaresInChessBoard(self, n):
		ans = n * (n + 1) * (2 * n + 1) / 6
		return int(ans)


class Solution:

	def squaresInChessBoard(self, N):
		count = 0
		for i in range(1, N + 1):
			count += (N + 1 - i) ** 2
		return count


class Solution:

	def squaresInChessBoard(self, N):
		sumx = 0
		for var in range(1, N + 1, 1):
			sumx = sumx + var * var
		return sumx


class Solution:

	def squaresInChessBoard(self, N):
		return int(N * (N + 1) * (2 * N + 1) / 6)


class Solution:

	def squaresInChessBoard(self, N):
		sum = 0
		m = N
		for m in range(1, N + 1):
			sum = sum + m ** 2
		return int(sum)


class Solution:

	def squaresInChessBoard(self, N):
		return N * (N + 1) // 2 * (2 * N + 1) // 3


class Solution:

	def squaresInChessBoard(self, N):
		a = N * N
		b = 1
		t = []
		while b <= N:
			l = b * b
			t.append(l)
			b = b + 1
		return sum(t)


class Solution:

	def squaresInChessBoard(self, N):
		num = 0
		while N > 0:
			num += N * N
			N = N - 1
		return num


class Solution:

	def squaresInChessBoard(self, N):
		x = 1
		if N > 1:
			x = N * (N + 1) * (2 * N + 1) / 6
		return int(x)


class Solution:

	def squaresInChessBoard(self, N):
		s = 0
		for i in range(1, N + 1):
			m = 1
			m *= i * i
			s += m
		return s


class Solution:

	def squaresInChessBoard(self, N):
		ans = 0
		while N > 0:
			ans += N * N
			N = N - 1
		return ans


class Solution:

	def squaresInChessBoard(self, n):
		x = n * (n + 1) * (2 * n + 1) // 6
		return x


class Solution:

	def squaresInChessBoard(self, n):
		count = n * (n + 1) * (2 * n + 1) // 6
		return count


class Solution:

	def squaresInChessBoard(self, N):
		total = 0
		for i in range(N):
			total += (N - i) * (N - i)
		return total


class Solution:

	def squaresInChessBoard(self, N):
		l = []
		for i in range(1, N + 1):
			l.append(i * i)
		return sum(l)


class Solution:

	def squaresInChessBoard(self, N):
		lst = []
		lst.append(1)
		lst.append(5)
		d = 2
		for i in range(2, N):
			c = lst[i - 1] + (i + 1) ** 2
			lst.append(c)
		return lst[N - 1]


class Solution:

	def squaresInChessBoard(self, N):
		a = N * (N + 1) * (2 * N + 1) // 6
		return a


class Solution:

	def squaresInChessBoard(self, N):
		(pre, cur) = (0, 1)
		for i in range(1, N + 1):
			cur = pre + i ** 2
			pre = cur
		return cur


class Solution:

	def squaresInChessBoard(self, N):
		if N == 1:
			return 1
		else:
			a = N * (N + 1) * (2 * N + 1) // 6
			return a


class Solution:

	def squaresInChessBoard(self, N):
		if N == 1:
			return 1
		return N * (N + 1) * (2 * N + 1) // 6


class Solution:

	def squaresInChessBoard(self, N):
		total_squares = 0
		for i in range(1, N + 1):
			num_squares = (N - i + 1) ** 2
			total_squares += num_squares
		return total_squares


class Solution:

	def squaresInChessBoard(self, N):
		ans = 0
		for i in range(1, N + 1):
			ans += i * i
		return ans


class Solution:

	def squaresInChessBoard(self, N):
		a = 1
		b = 1
		count = 0
		for a in range(1, N + 1):
			count += (N - a + 1) * (N - a + 1)
		return count


class Solution:

	def squaresInChessBoard(self, N):
		sum = N * (N + 1) * (2 * N + 1) // 6
		return sum


class Solution:

	def squaresInChessBoard(self, N):
		k = N * (N + 1) * (2 * N + 1) // 6
		return k


class Solution:

	def squaresInChessBoard(self, N):
		s = 0
		while N > 0:
			s = s + N * N
			N = N - 1
		return s


class Solution:

	def squaresInChessBoard(self, n):
		k = n * (n + 1) * (2 * n + 1) // 6
		return k


class Solution:

	def squaresInChessBoard(self, n):
		s = 0
		for i in range(1, n + 1):
			s += i * i
		return s


class Solution:

	def squaresInChessBoard(self, N):
		ans = 1
		if N == 1:
			return ans
		s = 0
		i = 1
		while i <= N:
			s += i * i
			i += 1
		return s


class Solution:

	def squaresInChessBoard(self, n):
		s = n * (n + 1) * (2 * n + 1) / 6
		return int(s)


class Solution:

	def squaresInChessBoard(self, N):

		def sq(n):
			return n * n
		res = 0
		for i in range(N + 1):
			res += sq(i)
		return res