Sum a*a + ... + b * b#9551

Sum a*a + ... + b * b (Editorial)

Compute the specified sum using a loop.

Algorithm implementation

Read the input data.

scanf("%d %d", &a, &b);

The variable is used to compute the sum a \cdot a + ... + b \cdot b with a for loop. Before starting the loop, the variable is initialized to .

s = 0;
for (i = a; i <= b; i++)
  s = s + i * i;

Print the answer.

printf("%lld\n", s);

Java implementation

import java.util.*;

public class Main
{
  public static void main(String[] args) 
  {
    Scanner con = new Scanner(System.in);
    int a = con.nextInt();
    int b = con.nextInt();

    int sum = 0;
    for (int i = a; i <= b; i++)
      sum = sum + i * i;
    
    System.out.println(sum);
    con.close();
  }
}

Python implementation

Read the input data.

a, b = map(int,input().split())

The variable is used to compute the sum a \cdot a + ... + b \cdot b with a for loop. Before starting the loop, the variable is initialized to .

s = 0
for i in range(a, b + 1):
  s = s + i * i

Print the answer.

print(s)