Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Magic Square  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone! This is my first time posting on this website. I'm a beginner in java. I am facing a problem solving the following:

"A magic square is an arrangement of the numbers from 1 to n^2 (n-squared) in an nxn matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. It is not hard to show that this sum must be n(n^2+1)/2.  Magic squares have been around since 650 BC (China) ! For those who are interested, this Wikipedia article provides more background.

You should add code to Magic.java to check that each column equals the n(n^2+1)/2. Currently, the Magic.java program does an incomplete job of determining all the necessary checks for satisfying the conditions of a Magic Square. In particular, it does not check that the sum of each column equals n(n^2+1)/2    You may find it useful to look at how the row sums were computed."

Here's the code for the magic square:


I would really appreciate any help! Thank you in advance!
 
Milan Skriniar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.s.: ignore all the TODO's comments. I only need what is mentioned in the quotations at the beginning.
 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Milan!

Milan Skriniar wrote:p.s.: ignore all the TODO's comments. I only need what is mentioned in the quotations at the beginning.



What have you got so far, then? Post your attempt and we can discuss that.
 
Paul Clapham
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit surprised by this code:



I would have thought that if you have an array in which the rows, columns, and diagonals all summed to n(n^2+1)/2 then it would automatically have n instances of the numbers from 1 to n. But I don't have a proof of that, nor do I have a counterexample. Any takers?

Milan, I'm sure that's part of your requirements to write that code. So I'm not saying you shouldn't write it. You definitely should. Don't pay any attention to my math diversion.

And by the way there's an error in the code following that comment. But if I'm right then it won't actually make any difference.

There's also an error in the comment.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!