- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7346 Articles for C++
5K+ Views
In 0-1 knapsack problem, a set of items are given, each with a weight and a value. We need to determine the number of each item to include in a collection so that the total weight is less than or equal to the given limit and the total value is large as possible.InputValue = [10, 20, 30, 40, 60, 70] Weight=[1, 2, 3, 6, 7, 4] int w=7Outputknapsack value is: 100AlgorithmBegin Input: set of items each with a weight and a value Set knapsack capacity Number of items=sizeof(values) / sizeof(values[0]) ... Read More
598 Views
Fermat's little theorem is one of the fundamental results of elementary number theory and is the basis for the Fermat primality test. The theorem is named after Pierre de Fermat, who stated it in 1640. The Theorem states that if p is a prime number, then for any integer a, the number a p–a is an integer multiple of p.AlgorithmBegin Function power() is used to compute a raised to power b under modulo M function modInverse() to find modular inverse of a under modulo m : Let m is prime If a and m are relatively ... Read More
1K+ Views
The Extended Euclidean Algorithm is just a another way of calculating GCD of two numbers. It has extra variables to compute ax + by = gcd(a, b). It's more efficient to use in a computer programAlgorithmBegin Declare variable a, b, x and y gcdExtended(int a, int b, int *x, int *y) if (a == 0) *x = 0; *y = 1; return b; Take two variables to store the result ... Read More
377 Views
This is a C++ Program which demonstrates the implementation of Euler Theorem. The number and modular must be coprime for the modular multiplicative inverse to exist.AlgorithmBegin Take input to find modular multiplicative inverse Take input as modular value Perform inverse array function: modInverse(x + 1, 0); modInverse[1] = 1; for i = 2 to x modInverse[i] = (-(y / i) * modInverse[y mod i]) mod y + y return modInverse EndExample Code#include #include using namespace std; vector inverseArray(int x, int y) { vector modInverse(x + 1, 0); modInverse[1] = 1; for (int i = 2; i
404 Views
Russian Peasant algorithm to multiply two numbers. It is a quick algorithm to calculate multiplication of two long numbers.AlgorithmBegin Russianpeasant(num1, num2) Int result=0 while (num2 > 0) if (num2 and 1) result = result + n; num1= num1 left shift 1; num2= num2left shift 1; return result EndExample Code#include using namespace std; unsigned int russianPeasant(unsigned int n, unsigned int m) { int result = 0; while (m > 0) { if (m & 1) result = result + n; n = n > 1; } return result; } int main() { cout
996 Views
Schonhage-Strassen Algorithm is used to multiply two numbers. The SchonhageStrassen algorithm is an asymptotically fast multiplication algorithm for large integers. In practice the Schonhage-Strassen algorithm starts to outperform older methods like karatsuba and Toom-CooK for numbers beyond 2215 to 2217 (10, 000 to 40, 000 decimal) digits.AlgorithmBegin function noOfDigit( x) Declare a variable n and assign n = 0; while (x > 0) x = x /10 Increment n return n End Begin Algorithm for schonhageStrassenMultiplication: schonhageStrassenMultiplication(a, b, n, m) define an array linearConvolution[n + m ... Read More
2K+ Views
Booth’s algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2’s compliment notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed.AlgorithmBegin Put multiplicand in BR and multiplier in QR and then the algorithm works as per the following conditions: 1. If Qn and Qn+1 are same i.e. 00 or 11 perform arithmetic shift by 1 bit. 2. If Qn Qn+1 = 10 do A= A + BR and perform arithmetic shift by 1 bit. 3. If Qn Qn+1 = ... Read More
699 Views
Fermat Primality test performs to check a given number is prime or not. Here is a C++ code of this algorithm.AlgorithmBegin modulo(base, e, mod) a = 1 b = base while (e > 0) if (e mod 2 == 1) a = (a * b) % mod b = (b * b) % mod e = e / 2 return a % mod End Begin Fermat(ll m, int iterations) if (m == 1) ... Read More
382 Views
This is C++ program to implement Sieve of Sundaram to Generate Prime Numbers Between Given Range. This algorithm was discovered in 1934 by Sundaram.AlgorithmBegin printPrimes(n) Here we find out primes smaller than n, we reduce n-2 to half. We call it New. New = (n-2)/2; Create an array marked[n] that is going to be used to separate numbers of the form i+j+2ij from others where 1
3K+ Views
This is the code to find out the GCD and LCM of n numbers. The GCD or Greatest Common Divisor of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. GCD is also known as Greatest Common Factor.The least common multiple (LCM) of two numbers is the smallest number (not zero) that is a multiple of both numbers.AlgorithmBegin Take two numbers as input Call the function gcd() two find out gcd of n numbers Call the function lcm() two find out lcm of n numbers ... Read More