Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Magic Square

By emrul_mu · Limits 1s, 512 MB

A little boy Towhid is good at mathematics. He solves a lot of mathematical equation regularly.

He is studying magic square attentively more than one week. But he is facing some difficulties with magic square. He didn’t find yet any equation to find sum of any row or any column or any diagonal in a magic square. But he knows that you are a good programmer in Metropolitan University.

Now, he needs your help to find the sum of any row or column or main diagonal in any magic square.

Note: A magic square is N*N grid where is the number of cells on each Side filled with distinct positive integers in the range of 1 to N*N such that each cell contains a different integer and the sum of the integers in each row, column and main diagonal is equal. Normal magic squares of all sizes can be constructed except 2×2 grid.


The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.

Each of the next t lines contains a single integer N (3 ≤ N ≤ 1000), the column or row size of the magic square.


For every test case, output one integer in a separate line in the format Case X: Y where X is the number of the test case starting from 1 and Y is the sum of the any row or any column or any main diagonal in N * N magic square.


Case 1: 15
Case 2: 34
Case 3: 500000500

Problem Setter: Foysol Ahmed Shuvo



98% Solution Ratio

RabelAhmedEarliest, Jul '17

Mili290Fastest, 0.0s

AlWarisLightest, 0 B

mashfiqur404Shortest, 68B


Login to submit