Question

In: Computer Science

Write an algorithm to input a number n, then calculate 13 +2 3 + 33 +...

  1. Write an algorithm to input a number n, then calculate 13 +2 3 + 33 + ... + n3, the sum

    of the first n cubic numbers, and output the result.

2-Construct a trace table of the algorithm in question 1 with input n=4.

Solutions

Expert Solution

Algorithm:-

cubeNNatural(n)

begin    sum := 0    for i in range 1 to n, do       sum := sum + i^3    done    return sum end

Example:-

 #include<stdio.h> long cube_sum_n_natural(int n) {    long sum = 0;    int i;    for (i = 1; i <= n; i++) {       sum += i * i * i; //cube i and add it with sum    }    return sum; } main() {    int n;    printf("Enter value of n: ");    scanf("%d", &n);    printf("Result is: %ld", cube_sum_n_natural(n)); }

Output

Enter value of n: 6 Result is: 441

SUM <--0

FOR i ← 1 TO 4

    SUM <---SUM+i^3

ENDFOR

trace table :-

                   i                    i^3               SUM
0
1 1 1
2 8 9
3 27 36
4 64 100

Related Solutions

Write a program to implement the Romberg Algorithm. Input: f(x), an interval [a,b], N (the number...
Write a program to implement the Romberg Algorithm. Input: f(x), an interval [a,b], N (the number of subintervals on the finest grid on level 0 is 2^N, therefore, N is usualy a small integer) Output: the triangle generated by Romberg Algorithm.
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2 P is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2 P ≤ n then put 1 into column P set n := n − 2 P Else put 0 into column...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a number output result: true if x in a false otherwise Sideeffect NA Plan if (top < bottom) result := false else // get the middle of the array (refining) middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) // example what is midpoint between 6 and 8 // (8-6)/2 = 1 we need to add 6 to...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2^p≤n then put 1 into column P set n := n - 2^p Else put 0 into column P End if Subtract 1...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal matrix given as two vectors: n×1 vector v representing the main diagonal and (n−1)×1 vector w representing the upper diagonal. Have this function output the Cholesky factor of the matrix as a vector for the main diagonal and a vector for the upper diagonal and output the number of flops and, separately, the number of square roots used as well. Use only basic programming....
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number of pairs of integers in A that are equal. Analyze your algorithm thoroughly. Your analysis should include a thorough examination of both the best and the worst-case scenarios. This includes a description of what the best and worst cases would look like. You should include both space and time in your analysis, but keep in mind that space refers to “extra space,” meaning in...
Use the sample information x¯ = 43, σ = 3, n = 13 to calculate the...
Use the sample information x¯ = 43, σ = 3, n = 13 to calculate the following confidence intervals for μ assuming the sample is from a normal population. (a) 90 percent confidence. (Round your answers to 4 decimal places.) The 90% confidence interval is from to (b) 95 percent confidence. (Round your answers to 4 decimal places.) The 95% confidence interval is from to (c) 99 percent confidence. (Round your answers to 4 decimal places.) The 99% confidence interval...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT