Question

a. Find an algorithm for solving the following problem: Given a positive integer n, find the...

a. Find an algorithm for solving the following problem: Given a positive

integer n, find the list of positive integers whose product is the largest

among all the lists of positive integers whose sum is n. For example, if

n is 4, the desired list is 2, 2 because 2 × 2 is larger than 1 × 1 × 1 × 1,

2 × 1 × 1, and 3 × 1. If n is 5, the desired list is 2, 3.

b. What is the desired list if n 2001?

Homework Answers

Answer #1

1. Accept the value for n, check if it is positive I.e. if n > 0

2. Now we need to store the elements into an array whose sum is n, so that is done using for loop as follows,

for(i=1;i<n;i++)

{

element[i]=n-i;

}

3. Now we need to store the products of these elements, so lets use the array product[], as follows

for(i=1;i<n;i++)

{

product[i] = element[i]*i;

}

4. Now we have the products of all the elements, we just need to find the maximum from this array

5. Lets assume first product is maximum, and proceed as follows

max=product[1];

for(i=2;i<n;i++)

{

if(product[i]>max)

max=product[i];

}

6. Print this value of max as the desired list.

display("The desired list is %d",max);

b. 1001as 1001*1000 is the biggest I.e.1,001,000

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Algorithm problem 2 [2.3-7] Describe a Θ(nlgn) algorithm that, given a set S of n integers...
Algorithm problem 2 [2.3-7] Describe a Θ(nlgn) algorithm that, given a set S of n integers and another integer ,determines whether or not there exist two elements in S whose sum is exactly x.
Describe an algorithm that, given a set S of n integers and another integer x, determines...
Describe an algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. Your algorithm must be nlogn. Evaluate how long each step in the algorithm takes to demonstrate that it meets this requirement.
Given a list of n integers, find the largest integer in the list. Use Java or...
Given a list of n integers, find the largest integer in the list. Use Java or C++ please
How many ways are there to represent a positive integer n as a sum of (a)...
How many ways are there to represent a positive integer n as a sum of (a) k non-negative integers? (b) k positive integers? Note: the order of summation matters. For example, take n = 3, k = 2. Then the possible sums in (a) are 3+0, 2+1, 1+2, 0+3
Given this algorithm written in pseudocode:   Algo(n) Input: A positive integer n Output: Answer, Algo(n) If...
Given this algorithm written in pseudocode:   Algo(n) Input: A positive integer n Output: Answer, Algo(n) If n = 1 Answer = 3 Else Answer = 3 × Algo(n-1) End if What is Algo(4)? Please provide a detailed answer! thank you!
1). Describe an algorithm that takes a list of n integers a1,a2,...,an and find the average...
1). Describe an algorithm that takes a list of n integers a1,a2,...,an and find the average of the largest and smallest integers in the list.
draw flowchart of: 1-algorithm that asks the user to enter three numbers and computes and prints...
draw flowchart of: 1-algorithm that asks the user to enter three numbers and computes and prints out the largest number. 2- an algorithm that asks the for an integer N from the user. The algorithm should find whether the number is positive, negative, or ZERO. 3-an algorithm that asks the user to enter a positive integer N. You then need to calculate the sum of all values from 1 to N.
Can you plzz do part D . Transform and Conquer Design a reasonably efficient algorithm for...
Can you plzz do part D . Transform and Conquer Design a reasonably efficient algorithm for solving each of the following problems and determine its efficiency class. a. You are given n telephone bills and m checks sent to pay the bills (n ≥ m). Assuming that telephone numbers are written on the checks, find out who failed to pay. (For simplicity, you may also assume that only one check is written for a particular bill and that it covers...
5.Write pseudocode for the algorithm using iteration (looping) and make a flow chart and python code...
5.Write pseudocode for the algorithm using iteration (looping) and make a flow chart and python code from your pseudocode. Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. For example given 3 the algorithm will print 14 (because 1 + 4 + 9 =14) You can use multiplication denoted as * in your solution and you do not have to define it (For example. 3*3=9) For...
2. Assume that n has been declared as an integer variable and received a positive value...
2. Assume that n has been declared as an integer variable and received a positive value which is unknown to us, you write statements to sum all values from 1 to n with a step size at 2 and print out the summation. For example, if n is 4, the summation is 4 because 1 + 3 equals 4; if n is 5 the summation is 9 because 1 + 3 + 5 equals 9.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT