Python Program for Fibonacci numbers

Pavitra
Updated on 25-Sep-2019 14:18:17

391 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Our task to compute the nth Fibonacci number.The sequence Fn of Fibonacci numbers is given by the recurrence relation given belowFn = Fn-1 + Fn-2with seed values (standard)F0 = 0 and F1 = 1.We have two possible solutions to the problemRecursive approachDynamic approachApproach 1 −Recursive ApproachExample Live Demo#recursive approach def Fibonacci(n):    if n

Check if a binary string has two consecutive occurrences of one everywhere in C++

Arnab Chakraborty
Updated on 25-Sep-2019 14:03:49

129 Views

Here we will see another interesting problem. We have to write a code that accepts a string, which has following criteria.Every group of consecutive 1s, must be of length 2every group of consecutive 1s must appear after 1 or more 0sSuppose there is a string like 0110, this is valid string, whether 001110, 010 are not validHere the approach is simple. we have to find the occurrences of 1, and check whether it is a part of sub-string 011 or not. If condition fails, for any substring then return false, otherwise true.Example Live Demo#include using namespace std; bool isValidStr(string str) ... Read More

Check if a binary string has a 0 between 1s or not in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:56:06

105 Views

Here we will see one interesting problem. We have to check whether a string has 0 in between a 1s or not. If not, then the string is valid, otherwise invalid.Suppose there are three strings −100011110100000111110001111101111From these three strings, only B is valid, because there is no 0 inside the stream of 1sTo solve this problem, we will find the index of first 1 present in the string, and also find the index of the last 1. Then we will check, is there any 0 from these two indices, if so, then return false, otherwise true (as valid)Example Live Demo#include ... Read More

Check for Integer Overflow in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:50:57

253 Views

The only safe way is to check for overflow before it occurs. There are some hacky ways of checking for integer overflow though. So if you're aiming for detecting overflow in unsigned int addition, you can check if the result is actually lesser than either values added. So for example, Exampleunsigned int x, y; unsigned int value = x + y; bool overflow = value < x; // Alternatively "value < y" should also workThis is because if x and y are both unsigned ints, if added and they overflow, their values can't be greater than either of them as ... Read More

Find One’s Complement of an Integer in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:34:21

2K+ Views

In this section, we will see how to find the 1’s complete of an integer. We can use the complement operator to do this task very fast, but it will make 32bit complemented value (4-bype integer). Here we want complement of n bit numbers.Suppose we have a number say 22. The binary equivalent is 10110. The complemented value is 01001 which is same as 9. Now the question comes, how to find this value? At first we have to find number of bits of the given number. Suppose the count is c (here c = 5 for 22). We have ... Read More

Python Program for Difference between sums of odd and even digits

Pavitra
Updated on 25-Sep-2019 13:20:27

398 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer, we need to calculate if the difference between the sum of odd digits and sum of even digits is 0 or not.The brute-force approach will be calculating the sum of all even and odd digits in the numbers and subtracting them to compute the answer.To reduce the computational time we use the concept of mental mathematics .The above constraints holds true only if the numbers are divisible by 11. So here in the implementation given below we check the ... Read More

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 number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++

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

251 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

398 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

74 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

Advertisements