# data structure in Java

Not what you're looking for?

Question:

You have a computer where multiplication is an enormously expensive operation compared to addition. Consider an algorithm for computing cubes using only addition (FYI: the first 5 cubes are 13 = 1, 23 = 8, 33 = 27, 43 = 64 and 53 = 125). This computation can be made using a two-pass (i.e. two loop) algorithm:

public int cube(int n) {

int result = 0;

int square = 0;

for (int i = 0; i < n; i++) {

square += n;

}

for (int j = 0; j < n; j++) {

result += square;

}

return result;

}

Write (in Java) an elegant single-pass (one loop) algorithm for computing cubes using only addition. (Hint: there is a pattern that you must find.) Note that the loop cannot be nested.

The original course website where the problem comes from is here, I think it will be helpful if you take a look at it first: www.student.math.uwaterloo.ca/~cs134

##### Purchase this Solution

##### Solution Summary

A data structure in Java problem is solved. The multiplication of enormously expensive operations for compared to addition are determined.

##### Solution Preview

public static int cube(int n)

{

int square = 0;

int result = ...

##### Purchase this Solution

##### Free BrainMass Quizzes

##### Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

##### Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

##### Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

##### Javscript Basics

Quiz on basics of javascript programming language.

##### C# variables and classes

This quiz contains questions about C# classes and variables.