Programming Articles - Page 1957 of 3366

Count common prime factors of two numbers in C++

Sunidhi Bansal
Updated on 15-May-2020 09:04:26

367 Views

We are given the two numbers let’s say x and y and the task is to find the common prime factors between two numbers. Common prime factors can be found by firstly calculating the common numbers between two numbers and after that checking from the list of common factors the one which are prime numbers.For ExampleInput − x = 10 y = 20 Output − Common prime factor of two numbers are: 2 5Explanation − common primes factors between 10 and 20 are 2 and 5 only.Input − x = 34 y = 12 Output − Common prime factor of ... Read More

Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++

Sunidhi Bansal
Updated on 15-May-2020 09:00:55

618 Views

We are given the range having start and end numbers and the task is to calculate the total count of Fibonacci numbers available between the given range in O(Log n) time and O(1) space.What are Fibonacci numbersFibonacci numbers are the sequence of numbers known as Fibonacci sequence where every new number is the sum of the last two preceding numbers.Where, f(0) = 0 and f(1) = 1 i.e. f(0) and f(1) have fixed positions in the sequence and the calculation will start from the third number.Formula used for calculating the sequence is −Fn = Fn-1 + Fn-2Where, F0 = 0, ... Read More

Count digits in a factorial in C++

Sunidhi Bansal
Updated on 15-May-2020 08:51:41

2K+ Views

We are given an integer value and the task is to first calculate the factorial of a number and then calculate the total number of digits in a result.What is a factorial numberFactorial of a number is calculated by multiplying the digits in a number while decrementing the value of digit by 1. It is denoted by the symbol ‘!’ i.e. 0!, 1!, 2!, 3!, 5!, ...., etc. Factorial of 0! and 1! is always 1.I.e. factorial of 2 = 2 * (2-1) = 2 * 1 = 2       factorial of 3 = 3 * (3-1) * ... Read More

Count factorial numbers in a given range in C++

Sunidhi Bansal
Updated on 15-May-2020 08:49:16

367 Views

We are given the range starting from an integer value holded by a variable let’s say start till the variable end and the task is to count the total number of factorial numbers available in the given range.What is a factorial numberFactorial of a number is calculated by multiplying the digits in a number while decrementing thevalue of digit by 1. It is denoted by the symbol ‘!’ i.e. 0!, 1!, 2!, 3!, 5!, ...., etc. Factorial of 0! and 1! is always 1.I.e. factorial of 2 = 2 * (2-1) = 2 * 1 = 2       ... Read More

Count even and odd digits in an Integer in C++

Sunidhi Bansal
Updated on 15-May-2020 08:45:56

3K+ Views

We are given with an integer number and the task is to count the even numbers and the odd numbers in a digit. Also, we will keep check on whether the even digits in an integer are occurring an even number of times and also the odd digits in an integer are occurring an odd number of times.For ExampleInput − digit = 12345 Output − count for even digits = 2       count for odd digits = 3Explanation − Yes, Also, even digits are occurring even number of times i.e. 2 and odd digits are occurring odd number ... Read More

Count common characters in two strings in C++

Sunidhi Bansal
Updated on 15-May-2020 08:41:27

2K+ Views

We are given with the two strings let’s say str1 and str2 and the task is to find the count of common characters in two strings i.e. if str1[i] = str[j], then they will be considered as a pair and count will increased to 1 and if str1[i]!=str2[j] then they willn’t be considered as a pair and count willn’t increase to 1.For ExampleInput − str1 = “hello”       str2 = “heoo” Output − count is: 3Explanation − str1[0] = str2[0] i.e. h ; str1[1] = str2[1] i.e. e ; str1[2]!=str2[2] i.e. l and o; str1[3]=str2[3] i.e. o. So, the ... Read More

Convert characters of a string to opposite case in C++

Sunidhi Bansal
Updated on 15-May-2020 08:30:17

592 Views

We are given a string of any length and the task is to convert the string having uppercase letters to lowercase letters and lowercase letters to uppercase letters.For ExampleInput − string str = ”Welcome To The Site!”Output − wELCOME tO tHE sITE!Explanation − converted the letters W, T, T, S to lowercase and letters e, l, c, o, m, e, o, , i, t, e to uppercase and it doesn’t perform any operations to the special characters.Input − string str = ”HELLO”Output − helloExplanation − converted the letters H, E, L, L, E to lowercase.This can be done using two different approachesUsing inbuilt functions provided by ... Read More

Find common elements in list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:54:18

2K+ Views

It is possible to have a list whose inner elements are also lists. In such cases we may come across a need when we have to find out the common elements among these inner lists. In this article we will find out the approaches to achieve this.With map and intersectionIntersection is a simple mathematical concept of finding the common elements between different sets. Python has the set method which returns a set that contains the similarity between two or more sets. So we first convert the elements of the list into set through a map function and then apply the ... Read More

Convert list into list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:52:01

3K+ Views

During data analysis we face scenarios to convert every element of a list into a sublist. So in this article we will need to take a normal list as an input and convert into a list of lists where each element becomes a sublist.Using for loopThis is a very straight forward approach in which we create for loop to read each element. We read it as a list and store the result in the new list.Example Live DemoAlist = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] #Given list print("Given list: ", Alist) # Each element as list NewList= [[x] for x ... Read More

Convert dictionary to list of tuples in Python

Pradeep Elance
Updated on 13-May-2020 14:49:30

876 Views

Converting from one collection type to another is very common in python. Depending the data processing needs we may have to convert the key value pairs present in a dictionary to pairs representing tuples in a list. In this article we will see the approaches to achieve this.With inThis is a straight forward approach where we just consider theExample Live DemoAdict = {30:'Mon', 11:'Tue', 19:'Fri'} # Given dictionary print("The given dictionary: ", Adict) # Using in Alist = [(key, val) for key, val in Adict.items()] # Result print("The list of tuples: ", Alist)OutputRunning the above code gives us ... Read More

Advertisements