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.
First line is adopted from James's solution: https://www.mathworks.com/matlabcentral/cody/problems/2455-diagonal-of-a-spiral-matrix/solutions/1239477
I was just happy I could use "diag" and "spiral" in this problem, but it's always nice to see some of my solutions get used in other code.
Find the sum of the elements in the "second" diagonal
879 Solvers
357 Solvers
Who is the smartest MATLAB programmer?
469 Solvers
Given a 4x4 matrix, swap the two middle columns
298 Solvers
Pandigital Multiples of 11 (based on Project Euler 491)
39 Solvers
Solution 1330565
I am not able to figure out why the solutions of my code to test 8 and 9 are wrong.
My wrong results:
- test 8 -> '1250000041666667776'
- test 9 -> '13107200170666672128'
I get precisely the same results as Angelo...
Angelo and Heiko, your solutions are working to a degree. However to obtain exact results on big numbers you have to use different tools. You can search for uint64 and bigdecimal. Hope it helps.
Thanks for the tip with uint64.