Algorithm that finds the sum of numbers from 1 to N (including N) for the entered number N write step by step.
1st Method: In this method, we take a variable 'SUM' assigned to '0' initially and another variable 'i' that varies from 1 to N for each loop, we use variable 'SUM' to stores the sum of value i.e., at some number Nth loop SUM(N) = SUM(N-1)+i. So, present value of sum is previous value of SUM + i. By the end of the loop we have SUM = 1+2+3+....+(N-1)+N.
Algoritham:
1) Take integer variables i, N, SUM.
2) Read the value N, which is number of terms in the the summation series.
3) Assign value of SUM to '0' i.e., SUM = 0;
4) START loop with variable i that varies from 1 to N.
5) Replace the value of SUM with previous SUM + K i.e., SUM = SUM + K.
6) End the loop.
7) Print the value of the SUM.
8) END.
2nd Method: In this method we use formula, we know that sum of first N (including N) Natural numbers is N(N+1)/2. we can use this formula to find the sum of numbers directly, with out using any loop.
Algoritham:
1) Take integer variables N, SUM.
2) Read the value N from the user, which is number of terms in the summation series.
3) Comupte value of SUM = N*(N+1)/2.
4) Print the value of SUM.
5) End.
Algorithm that finds the sum of numbers from 1 to N (including N) for the entered...
Write a function that finds the sum of all numbers up to (and including) N that are multiples of either x or y. e.g. For N= 10 x=2 y=3 s=2+3 +4 +6+ 8 + 9 + 10 = 42 In [ ]: def multiplesum (N, x, y): returns In [ ]: # Your Provided Sample Test Cases print("#1", multipleSum (10, 2, 3) == 42) print("#2", multipleSum (25,7,8) == 90) print("#3", multipleSum (15,1, 12) == 120)
Question 4-6 Please. Python 3.6. def main(). entered by a user. Write a program that finds the sum and average of a series of numbers he program should first prompt the user to enter total numbers of numbers are to be summed and averaged. It should then as for input for each of the numbers, add them, and print the total of the numbers and their average 2. Write a progra m that finds the area of a circle. The...
We discuss the Euclidean algorithm that finds the greatest common divisor of 2 numbers u and v. We want to extend and compute the gcd of n integers gcd(u1,u2,….un). One way to do it is to assume all numbers are non-negative, so if only one of if uj≠0 it is the gcd. Otherwise replace uk by uk mod uj for all k≠j where uj is the minimum of the non-zero elements (u’s). The algorithm can be made significantly faster if one...
1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...
We discuss the Euclidean algorithm that finds the greatest common divisor of 2 numbers u and v. We want to extend and compute the gcd of n integers gcd(u_1,u_2,….u_n). One way to do it is to assume all numbers are non-negative, so if only one of if u_j≠0 it is the gcd. Otherwise replace u_k by u_k mod u_j for all k≠j where u_j is the minimum of the non-zero elements (u’s). The algorithm can be made significantly faster if...
3. Write a complete C++ program that finds and prints the sum of values from 1 to N where N is a positive value > 10 entered by the user. If the user inputs a value less the 10 ask him to re-enter the value again and again until the value is valid. (20 Points)
8. Consider the following algorithm, which finds the sum of all of the integers in a list procedure sum(n: positive integer, a1, a2,..., an : integers) for i: 1 to n return S (a) Suppose the value for n is 4 and the elements of the list are 3, 5,-2,4. List assigned to s as the procedure is executed. (You can list the the values that are values assigned to all variables if you wish) b) When a list of...
1. Design an algorithm to find all the non-common elements in two sorted lists of numbers. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, ?respectively 2. Estimate how many times faster it will be to find ged(98765, 56789) by Euclid's algorithm compared with the algorithm based on checking consecutive integers from min{m, n} down to gcd(m, n). 3. For each of the following functions, indicate how...
Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...
1. Parallelize the program of finding the sum of n numbers (a1+a2+...+an) using n/2 processors. (1) Draw the diagram; (2) Find the numbers of operations for the implementations of the sequential algorithm and the parallel program;