Hexagonal numbers on a spiral matrix - MATLAB Cody - MATLAB Central

Problem 44341. Hexagonal numbers on a spiral matrix

Difficulty:Rate

Put hexagonal numbers in a ( m x m ) spiral matrix and return the sum of its diagonal elements.

Formula of hexagonal numbers h(n) = 2n^2 - n

If m = 5;

spiral(5) =   
  21    22    23    24    25
  20     7     8     9    10
  19     6     1     2    11
  18     5     4     3    12
  17    16    15    14    13

First 5x5=25 hexagonal numbers are;

h = [1 6 15 28 45 66 91 120 153 190 231 276 325 378 435 496 561 630 703 780 861 946 1035 1128 1225]

We put them in a spiral format;

   spiralHex = [
861	946	1035	1128	1225
780	91	120	153	190
703	66	1	6	231
630	45	28	15	276
561	496	435	378	325

And sum its diag = 861 + 91 + 1 + 15 + 325 = 1293.

Return the output as char.

Solution Stats

24.17% Correct | 75.83% Incorrect
Last Solution submitted on Feb 14, 2025

Problem Comments

Solution Comments

Show comments

Group

Number theory Image
Number theory
  • 44 Problems
  • 21 Finishers

Problem Recent Solvers142

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Go to top of page