Prime Number of Set Bits in Binary Representation in C++

sudhir sharma
Updated on 03-Feb-2020 10:57:50

271 Views

In this problem, we are given two integers L and R. Our task to print the total numbers that have set bits counting to a prime number that is in between L to R.Let’s take an example to understand the problemInput: L = 7, R = 12 Output: 6 Explanation: 7 -> 111 , set bits = 2, prime number. 8 -> 1000 , set bits = 1, not prime number. 9 -> 1001 , set bits = 2, prime number 10 -> 1010 , set bits = 2, prime number 11 -> 1011, set bits = 3, prime number ... Read More

Insertion Sort in C++

Arnab Chakraborty
Updated on 03-Feb-2020 10:56:31

499 Views

Suppose we have a linked list, we have to perform the insertion sort on this list. So if the list is like [9, 45, 23, 71, 80, 55], sorted list is [9, 23, 45, 55, 71, 80].To solve this, we will follow these steps −dummy := new Node with some random valuenode := given listwhile node is not null, newNode = next of node, dummyHead := next of dummy, prevDummyHead := dummywhile true −if dummyHead is not present, value of dummyHead > value of nodenext of node := dummyHeadnext of prevDummyHead := nodebreak the loopprevDummyHead := dymmyHead, and dummyHead = ... Read More

Prime Numbers After Prime P with Sum S in C++

sudhir sharma
Updated on 03-Feb-2020 10:53:23

371 Views

In this problem, we are given three numbers, sum S, prime P, and N. Our task is to find all N prime numbers greater than P whose sum is equal to S.Let’s take an example to understand our problemInput: N = 2, P = 5, S = 18 Output: 7 11 Explanation: Prime numbers greater than 5 : 7 11 13 Sum = 7 + 11 = 18To solve this problem, we have to find all prime numbers between P and S. And then find N prime numbers which sum up to S. For this we will use backtracking.Program to ... Read More

Prime Numbers and Fibonacci in C++

sudhir sharma
Updated on 03-Feb-2020 10:49:44

821 Views

In this problem, we are given a number n. Our task is to print all prime and Fibonacci numbers less than or equal to n.Let’s take an example to understand the problemInput: n = 30 Output: 2 3 5 13ExplanationFibonacci numbers less than 30 are : 1 1 2 3 5 8 13 21.Out of these numbers, prime numbers are 2 3 5 13.To solve this problem, we have to check if all numbers of the Fibonacci series less than n is a prime number.For this, we will find all prime numbers less than or equal to n. And check ... Read More

Prime Points That Split a Number into Two Primes in C++

sudhir sharma
Updated on 03-Feb-2020 10:39:59

195 Views

In this problem, we are given a number N. Our task is to print all prime points of the number otherwise print -1, if there is no prime point.Prime points are those index values which split the number into two prime numbers, one on the left and other on the right.Let’s take an example to understand the problemInput: 2359 Output: 1Explanation: on splitting the number at index 1. We will get 2 and 59 as two prime numbers.To solve this problem, we will check if there are left-right divisions possible for the number. If it’s valid, we will try all ... Read More

Prime String in C++

sudhir sharma
Updated on 03-Feb-2020 10:35:33

433 Views

In this problem, we are given a string. Our task is to print YES / NO based on is the sum of ASCII values of the characters of the string is prime or not.ASCII values are character encodingsPrime number is a number that is divisible only by the number itself and 1.Let’s take an example to understand the problem, Input: string = “Hello” Output:NoTo solve this problem, we will have to find the sum of ASCII values of all characters of the string. And store the sum in a variable and then check if the sum is a prime number ... Read More

Prime Triplet in C++

sudhir sharma
Updated on 03-Feb-2020 10:33:02

358 Views

In this problem, we are given a number N. Our task is to print all prime triplets less than N.Prime triplet is a set of three prime numbers. That are of the form (p, p+2, p+6) or (p, p+4, p+6). All prime numbers are grouped according to the above triplets as every third prime in the direct pattern is a multiple of 6.Let’s see an example to understand the problemInput: N = 13 Output: 5 7 11To solve this problem, we have to find all prime numbers less than equal to N. And the check for triplets.The code to show ... Read More

Primitive Root of a Prime Number n Modulo n in C++

sudhir sharma
Updated on 03-Feb-2020 10:29:48

1K+ Views

In this problem, we are given a prime number N. our task is to print the primitive root of prime number N modulo N.Primitive root of prime number N is an integer x lying between [1, n-1] such that all values of xk (mod n) where k lies in [0, n-2] are unique.Let’s take an example to understand the problem, Input: 13 Output: 2To solve this problem, we have to use mathematical function called Euler’s Totient Function.Euler’s Totient Function is the count of numbers from 1 to n which are relatively prime to the number n.A number i is relatively ... Read More

Primorial of a Number in C++

sudhir sharma
Updated on 03-Feb-2020 10:24:24

507 Views

In this problem, we are given a number n. Our task is to print its primorial number.Primorial number (Pn#) is a number that is the product of first n prime numbers.Primorial number is similar to factorial of a number n. The difference is that factorial can be any number but in case of primorial number, all prime numbers are used.Let’s take an example to understand the problem, Input: N = 4 Output 210 Explanation: Primorial number, Pn# = 2 * 3 * 5 * 7 = 210To solve this problem, we have to find the first n prime numbers. Print ... Read More

Print in Place of Characters for Reading Passwords in C

sudhir sharma
Updated on 03-Feb-2020 10:20:11

648 Views

In this problem, we are given a string password. Our task is to print * in place of characters of the password.Let’s take an example to understand the problem,Input: password Output ********To solve this problem, we will traverse the password we have entered and print * in place of characters of the password.ExampleThe below program will show the implementation of our solution Live Demo#include #include int main() {    char password[50] = "password";    int length = strlen(password);    printf("Password : ");    for(int i = 0; i

Advertisements