Checkout
checkout
view
Your Cart Your Cart: item(s)
Add to Shopping Cart
$2.19 Instant Download
Computer Science, Data Structures and Algorithms
Year 1

A problem about data structure in Java


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

By OTA:  Luigi Sorbara, MSc (IP)

OTA Rating:  4.6/5

Your Price:  $2.19  (original value ~$7.98)

What's included:

  • Plain text response
$2.19 Download Add to Cart

Add to Shopping Cart
$2.19 Instant Download

Page generated in 0.0136 seconds

About Us ·  Contact Us ·  Samples ·  Solutions ·  Legal Terms and Conditions ·  Privacy Policy

©2008 SolutionLibrary.com

Search for Solutions About Us Samples