Successively adding the squares of three integers using nested for loops? Help!

1 view (last 30 days)
I need to compute the numbers of ways in which you can add the squares of three integers (positive, negative, or zero) such that their sum successively assumes values no greater that 0, 1, 2 ... 100. My thought was that I could define 3 vectors
i = [-100:100];
j = [-100:100];
k = [-100:100];
And then use nested for loops to iterate through each possibility. Once I've created an array with all the possibilities I could just find the number of times 0, 1, 2, etc. appears in the array. Unfortunately it doesn't work and I don't really know why. I'm not even able to get to the point where I have an array with all the possibilities. I know there must be a way to do this, but my matlab experience is extremely limited. Help?
  1 Comment
Jan
Jan on 11 Oct 2011
Please post your code. It is easier to insert modifications than writing the program for you from the scratch. Especially if this is a homework question, it would be counter-productive, if we post a solution.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 11 Oct 2011
It could be done in two statements with ndgrid and histc.
No point in going down as low as -100 or as high as +100: no matter what j and k are, the square of anything beyond +/- 10 is going to be beyond 100.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!