Answer:
The formula for calculating the sum of squares of the first N numbers can be described as below:
( n * ( n + 1 ) * ( 2n + 1 ) ) / 6
Where n represents the number of digits to calculate.
Explanation:
The sum of the squares of numbers is referred to as the sum of squared values of the numbers. It’s basically the addition of squared numbers.
Here 2 terms, 3 terms, or ‘n’ number of terms, first n odd terms or even terms, set of natural numbers or consecutive numbers, etc. could be squared terms
Algorithm
The algorithm as:
Step 1: Read N.
Step 2: Let ctr = 0, sum = 0.
Step 3: Read Num.
Step 4: ctr = ctr + 1.
Step 5: Compute the square of the number i.e., = sqr (Num * Num).
Step 6: sum = sum * sqr.
Step 7: If ctr is less than N then repeat steps 3 to 6.
Step 8: Print sum.
Step 9: End.
Program
package com.demo;
import java.util.*;
public class Main {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int n = sc.nextInt();
getvalues(n);
}
public static void getvalues(int n) {
int a = n;
int rem = 0;
int sum = 0;
while (a != 0) {
rem = a % 10;
sum = sum + (rem * rem);
a = a / 10;
}
System.out.println(sum);
}
}
def sum_of_squares_formula(end_number):
return ( end_number * ( end_number + 1 ) * ( 2 * end_number + 1 ) ) // 6
print(sum_of_squares_formula(5))