Consider an algorithm for computing cubes using addition

Assignment Help JAVA Programming
Reference no: EM13942147

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

Reference no: EM13942147

Questions Cloud

State the appropriate conclusion : The recommended number of hours of sleep per night is 8 hours, but everybody "knows" that the average college student sleeps less than 7 hours.
Are law enforcement cameras an invasion of privacy : Are law enforcement cameras an invasion of privacy?
Measures the performance of someone learning a skill : A learning curve is a graph of a function P(t) that measures the performance of someone learning a skill as a function of the training time t. At first, the rate of learning is rapid.
Submit your ethical theory selection for the project to your : Submit your ethical theory selection for the project to your instructor for approval. Though the general details of the project are in the project description, choose a particular set of issues or applications as the focus of your analysis. Choose fr..
Consider an algorithm for computing cubes using addition : 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 comp..
The probability of finishing on schedule : If there is a penalty of $5,000 a week for each week the project is late, what is the probability of incurring a penalty of at least $5,000?(Round "Probability" valuesto 4 decimal places, intermediate calculations and final answers to 2 decimal pl..
Message to prospective employers with confidence : When your brand is clear to you then you can market and communicate your message to prospective employers with confidence. So how do you go about building your brand?
Explain how the devices are or are not effective : Define the specific rhetorical device, explain how it is used, and analyze whether the persuasive technique is designed to be credible, misleading, descriptive or used for some other purpose. Explain how the devices are or are not effective.
Default constructor for the chatioexception class. : The second constructor takes in a string as a parameter and passes this string into the call to super();

Reviews

Write a Review

 

JAVA Programming Questions & Answers

  How to setup a program to respond to events

Discussing how to setup a program to respond to events from any one of these sources - How do you create the necessary listener class? How do you associate the listener object with the event source?

  Prepare a thread function to initialize int array

Prepare a thread function to initialize int array x so that each element receives its index in the array: x[i]=i, and each thread initializes its portion of the array.

  Create blackjack program in java

Create Blackjack program in Java following the instructions below must be in NetBEans

  Describe which base class members may not be inherited

Without considering the access attributes, list and describe which base class members may not be inherited by a derived class and provide a rationale for why this is.

  Program to computerize the billing system of a hospital

Design the class doctorType, inherited from the class personType, with an additional data member to store a doctor's specialty.

  Write a java method that repeatedly selects

Write a Java method that repeatedly selects and removes a random entry from an array until the array holds no more entries.

  Create a driver class in java

Your project is to create a driver class that uses SuperJavaIceCreamClass.

  Print the contents of the array

Prepare a second loop that prints the contents of the array

  Explain at least three scenarios where he is wrong

Explain at least three scenarios where he is wrong, that is, where users other than a file's owner need some kind of allowed access privileges

  Write a java program that declares an array alpha of 50

Write a Java program that declares an array alpha of 50 elements of type double. Initialize the array so that the first 25 elements are equal to the square of the index variable, and the last 25 elements are equal to three times the index variable. O..

  Write program that convert us dollar into canadian dollar

Design a new class named Polygon that extends GeometricObject and implements Comparable. The number of sides in polygons varies, you cannot use a certain number of data fields for sides of a polygon - Write a program that passes an unspecified numb..

  When an exception occurs in an ada procedure

When an exception occurs in an Ada procedure, either in the elaboration of its declaration or execution of its body, the exception

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd