Python Program for Cube Sum of First N Natural Numbers

Pavitra
Updated on 25-Sep-2019 13:16:16

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an input n, we need to print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till n-th term.Here we will discuss two approach to reach the solution of the problem statement −Brute-force approach using loops.Mathematical solution of sum of n numbers.Approach 1 −Computing sum of each term by adding by iterating over the numbersExample Live Demodef sumOfSeries(n):    sum = 0    for i in range(1, n+1):       sum +=i*i*i    return sum # ... Read More

Find Substrings of Length K with ASCII Sum Divisible by K in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:14:18

359 Views

Here we will see another problem, where one string and another integer value say k is given. We have to find the number of substrings of length k, whose sum of ASCII values of characters is divisible by k.Suppose a string is “BCGABC”. And the value of k is 3. Here string BCG has ASCII sum 300, ABC has ASCII sum 294, both are divisible by k = 3.The approach is simple. At first we have to find the ASCII value of characters of first substring, whose length is k. We have to use the sliding window technique and subtract ... Read More

Python Program for Compound Interest

Pavitra
Updated on 25-Sep-2019 13:13:09

581 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −We are given with three input values i.e principle, rate & time and we need to compute compound interest.The code given below shows the process of computation of compound interest.The formula used here isCompound Interest = P(1 + R/100)rWhere, P is the principal amountR is the rate andT is the time spanThe implementation is given belowExample Live Demodef compound_interest(principle, rate, time):    CI = principle * (pow((1 + rate / 100), time))    print("Compound interest : ", CI) # main compound_interest(10000, 7.78, 2)OutputCompound ... Read More

Find Cube Pairs: A N^2/3 Solution in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:09:18

129 Views

A number is given. We have to find two pairs, that can represent the number as sum of two cubes. So we have to find two pairs (a, b) and (c, d) such that the given number n can be expressed as n = a3 + b3 = c3 + d3The idea is simple. Here every number a, b, c and d are all less than n1/3. For every distinct pair (x, y) formed by number less than n1/3, if their sum (x3 + y3) is equal to the given number, we store them into hash table with the sum ... Read More

Python Program for Bubble Sort

Pavitra
Updated on 25-Sep-2019 12:59:03

2K+ Views

In this article, we will learn about the implementation of bubble sort sorting technique.The figure shown below illustrates the working of this algorithm −ApproachStarting with the first element(index = 0), compare the current element with the next element of the array.If the current element is greater than the next element of the array, swap them.If the current element is less than the next element, move to the next element.Repeat Step 1.Now let’s see the implementation below −Exampledef bubbleSort(ar):    n = len(arr)    # Traverse through all array elements    for i in range(n):    # Last i elements are ... Read More

Find Any Pair with Given GCD and LCM in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:56:42

305 Views

In this section we will see how to get number of pairs using the given GCD and LCM values. Suppose the GCD and LCM values are 2 and 12. Now the possible pairs of numbers are (2, 12), (4, 6), (6, 4) and (12, 2). So our program will find the count of pairs. That is 4.Let us see the algorithm to understand what will be the technique to solve this problem.AlgorithmcountPairs(gcd, lcm): Begin    if lcm is nit divisible by gcd, then       return 0    temp := lcm/gcd    c := primeFactorCount(temp)    res := shift ... Read More

Minimum Insertions to Make a Co-Prime Array in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:47:05

194 Views

In this section we will see another interesting problem. Suppose we have an array of N elements. We have to find minimum number of intersection points to make this array as co-prime array. In the co-prime array gcd of every two consecutive elements is 1. We have to print the array also.Suppose we have elements like {5, 10, 20}. This is not co-prime array. Now by inserting 1 between 5, 10 and 10, 20, it will be co-prime array. So the array will be like {5, 1, 10, 1, 20}AlgorithmmakeCoPrime(arr, n): begin    count := 0    for i in ... Read More

Minimum Initial Energy Required to Cross Street in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:40:05

447 Views

Suppose we have an array where positive and negative numbers are stored. The array is representing the checkpoint from one end to another end of the streets. The positive and negative values are representing the energy at the checkpoints. The positive values can increase energy, and negative number decreases energy. We have to find the initial energy level to cross the street, such that energy level never becomes 0 or less than 0.Suppose we have an array A = {4, -6, 2, 3}. Let the initial energy is 0. So after reaching at first check point, the energy is 4. ... Read More

Calculate Area of a Tetrahedron in Python

Pavitra
Updated on 25-Sep-2019 12:37:59

401 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given the side of a tetrahedron, we need to find a tetrahedron.A Tetrahedron is a geometric figure which looks like a pyramid with a triangular base. It is a solid object with four triangular faces, three on the sides, one on the bottom of the base and four vertices or corners.Here we frame an area function as shown below −Exampleimport math def areatetrahedron(side):    return (math.sqrt(3) * (side * side)) # Driver Code side = 20 print("Area of Tetrahedron = ", area_of_tetrahedron(side))OutputArea ... Read More

Maximum Length Chain of Pairs in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:32:39

194 Views

There is a chain of pairs is given. In each pair, there are two integers and the first integer is always smaller, and second one is greater, the same rule can also be applied for the chain construction. A pair (x, y) can be added after a pair (p, q), only if q < x.To solve this problem, at first we have to sort given pairs in increasing order of first element. After that we will compare the second element of a pair, with the first element of next pair.Input − A chain of number pairs. {(5, 24), (15, 25), ... Read More

Advertisements