Found 7346 Articles for C++

C++ Program to Solve the 0-1 Knapsack Problem

Nancy Den
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Implement Fermat’s Little Theorem

Nancy Den
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Implement Extended Euclidean Algorithm

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Implement Euler Theorem

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Implement Russian Peasant Multiplication

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Implement Booth’s Multiplication Algorithm for Multiplication of 2 signed Numbers

Nancy Den
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Perform Fermat Primality Test

Nancy Den
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram

Nancy Den
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Find the GCD and LCM of n Numbers

Nancy Den
Updated on 30-Jul-2019 22:30:25

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

Advertisements