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

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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