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

  Descriptioncreate a web application for sheridan restaurant

descriptioncreate a web application for sheridan restaurant reviews that contains the following functionalitybull

  Prompts user to type a negative number

Displays Quotient and Remainder of above two inputs, Prompts user to type a negative number to exit and any other number to continue

  Write a java sorting application with two classes

Problem. Write a Java Sorting Application with two classes, JavaSort and JavaSortTest. Your JavaSort Class, as a minimum must contain sorting methods for BubbleSort, InsertionSort, ShellSort, MergeSort, and QuickSort.

  Write a program in java to perform lzw decoding

Write a program in java to perform LZW decoding. It should read and write textual lists of integers.

  Create listener objects for each jcombobox

Create listener objects for each JComboBox that set the background color of the panel when the user selects an integer. Use the current values supplied by the JComboBox objects to determine the background color.

  Write a program with an exception handler

Write a program with an exception handler that deals with nonnumeric operands; then write another program without using an exception handler to achieve the same objective.

  Write down a java gui application index2java based on the

write a java gui application index2.java based on the program in step 1 that inputs several lines of text and uses

  Tasks for an array of integers

Write array methods that carry out the subsequent tasks for an array of integers by completing the ArrayMethods class below. Test each method.

  2 before we developed a stringlog adt it represents a log

2. before we developed a stringlog adt. it represents a log that holds objects of class string. suppose that instead of

  Representing an investment value and interest rate

Write a Java test program, all your code should be in the main method that asks the user for two numbers representing an investment value and interest rate.

  Swing gui java application running with eclipse on windows x

To test the program just start MoneyMain to see if you click the icons for the detail screens and click Report to view the two reports.

  Write a program that shows the current time and date

Write a program that shows the current time and date

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