codeparrot/xlcost-text-to-code huggingface dataset:
Sphenic Number | C ++ program to check whether a number is a Sphenic number or not ; create a global array of size 10001 ; ; This functions finds all primes smaller than ' limit ' using simple sieve of eratosthenes . ; initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; One by one traverse all numbers so that their multiples can be marked as composite . ; If p is not changed , then it is a prime ; Update all multiples of p ; to store the 8 divisors ; to count the number of divisor ; finally check if there re 8 divisor and all the numbers are distinct prime no return 1 else return 0 ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool arr [ 1001 ] ; void simpleSieve ( ) { memset ( arr , true , sizeof ( arr ) ) ; for ( int p = 2 ; p * p < 1001 ; p ++ ) { if ( arr [ p ] ) { for ( int i = p * 2 ; i < 1001 ; i = i + p ) arr [ i ] = false ; } } } int find_sphene ( int N ) { int arr1 [ 8 ] = { 0 } ; int count = 0 ; int j = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( N % i == 0 && count < 9 ) { count ++ ; arr1 [ j ++ ] = i ; } } if ( count == 8 && ( arr [ arr1 [ 1 ] ] && arr [ arr1 [ 2 ] ] && arr [ arr1 [ 3 ] ] ) ) return 1 ; return 0 ; } int main ( ) { int n = 60 ; simpleSieve ( ) ; int ans = find_sphene ( n ) ; if ( ans ) cout << " Yes " ; else cout << " NO " ; }
Common Divisors of Two Numbers | C ++ implementation of program ; Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Count divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int commDiv ( int a , int b ) { int n = gcd ( a , b ) ; int result = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) result += 1 ; else result += 2 ; } } return result ; } int main ( ) { int a = 12 , b = 24 ; cout << commDiv ( a , b ) ; return 0 ; }
Count ways to spell a number with repeated digits | C ++ program to count number of ways we can spell a number ; Function to calculate all possible spells of a number with repeated digits num -- > string which is favourite number ; final count of total possible spells ; iterate through complete number ; count contiguous frequency of particular digit num [ i ] ; Compute 2 ^ ( count - 1 ) and multiply with result ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; ll spellsCount ( string num ) { int n = num . length ( ) ; ll result = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; while ( i < n - 1 && num [ i + 1 ] == num [ i ] ) { count ++ ; i ++ ; } result = result * pow ( 2 , count - 1 ) ; } return result ; } int main ( ) { string num = "11112" ; cout << spellsCount ( num ) ; return 0 ; }
Happy Number | C / C ++ program to check a number is a Happy number or not ; Utility method to return sum of square of digit of n ; method return true if n is Happy number ; initialize slow and fast by n ; move slow number by one iteration ; move fast number by two iteration ; if both number meet at 1 , then return true ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numSquareSum ( int n ) { int squareSum = 0 ; while ( n ) { squareSum += ( n % 10 ) * ( n % 10 ) ; n /= 10 ; } return squareSum ; } bool isHappynumber ( int n ) { int slow , fast ; slow = fast = n ; do { slow = numSquareSum ( slow ) ; fast = numSquareSum ( numSquareSum ( fast ) ) ; } while ( slow != fast ) ; return ( slow == 1 ) ; } int main ( ) { int n = 13 ; if ( isHappynumber ( n ) ) cout << n << " ▁ is ▁ a ▁ Happy ▁ number STRNEWLINE " ; else cout << n << " ▁ is ▁ not ▁ a ▁ Happy ▁ number STRNEWLINE " ; }
Count Divisors of Factorial | C ++ program to find count of divisors in n ! ; allPrimes [ ] stores all prime numbers less than or equal to n . ; Fills above vector allPrimes [ ] for a given n ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; Loop to update prime [ ] ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store primes in the vector allPrimes ; Function to find all result of factorial number ; Initialize result ; find exponents of all primes which divides n and less than n ; Current divisor ; Find the highest power ( stored in exp ) ' ▁ ▁ of ▁ allPrimes [ i ] ▁ that ▁ divides ▁ n ▁ using ▁ ▁ Legendre ' s formula . ; Multiply exponents of all primes less than n ; return total divisors ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef unsigned long long int ull ; vector < ull > allPrimes ; void sieve ( int n ) { vector < bool > prime ( n + 1 , true ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) allPrimes . push_back ( p ) ; } ull factorialDivisors ( ull n ) { ull result = 1 ; for ( int i = 0 ; i < allPrimes . size ( ) ; i ++ ) { ull p = allPrimes [ i ] ; ull exp = 0 ; while ( p <= n ) { exp = exp + ( n / p ) ; p = p * allPrimes [ i ] ; } result = result * ( exp + 1 ) ; } return result ; } int main ( ) { cout << factorialDivisors ( 6 ) ; return 0 ; }
Non Fibonacci Numbers | C ++ program to find n 'th Fibonacci number ; Returns n 'th Non-Fibonacci number ; curr is to keep track of current fibonacci number , prev is previous , prevPrev is previous of previous . ; While count of non - fibonacci numbers doesn 't become negative or zero ; Simple Fibonacci number logic ; ( curr - prev - 1 ) is count of non - Fibonacci numbers between curr and prev . ; n might be negative now . Make sure it becomes positive by removing last added gap . ; Now add the positive n to previous Fibonacci number to find the n 'th non-fibonacci. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nonFibonacci ( int n ) { int prevPrev = 1 , prev = 2 , curr = 3 ; while ( n > 0 ) { prevPrev = prev ; prev = curr ; curr = prevPrev + prev ; n = n - ( curr - prev - 1 ) ; } n = n + ( curr - prev - 1 ) ; return prev + n ; } int main ( ) { cout << nonFibonacci ( 5 ) ; return 0 ; }
Stein 's Algorithm for finding GCD | Iterative C ++ program to implement Stein 's Algorithm ; Function to implement Stein 's Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; Finding K , where K is the greatest power of 2 that divides both a and b . ; Dividing a by 2 until a becomes odd ; From here on , ' a ' is always odd . ; If b is even , remove all factor of 2 in b ; Now a and b are both odd . Swap if necessary so a <= b , then set b = b - a ( which is even ) . ; restore common factors of 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; int k ; for ( k = 0 ; ( ( a b ) & 1 ) == 0 ; ++ k ) { a >>= 1 ; b >>= 1 ; } while ( ( a & 1 ) == 0 ) a >>= 1 ; do { while ( ( b & 1 ) == 0 ) b >>= 1 ; if ( a > b ) swap ( a , b ) ; b = ( b - a ) ; } while ( b != 0 ) ; return a << k ; } int main ( ) { int a = 34 , b = 17 ; printf ( " Gcd ▁ of ▁ given ▁ numbers ▁ is ▁ % d STRNEWLINE " , gcd ( a , b ) ) ; return 0 ; }
Print all n | A C ++ recursive program to print all N - digit numbers with absolute difference between sum of even and odd digits is 1 ; n -- > value of input out -- > output array index -- > index of next digit to be filled in output array evenSum , oddSum -- > sum of even and odd digits so far ; Base case ; If number becomes n - digit ; if absolute difference between sum of even and odd digits is 1 , print the number ; If current index is odd , then add it to odd sum and recurse ; else else add to even sum and recurse ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit and calls findNDigitNumsUtil ( ) for remaining indexes . ; output array to store n - digit numbers ; Initialize number index considered so far ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function findNDigitNumsUtil for remaining indexes . Note that the first digit is considered to be present in even position . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNDigitNumsUtil ( int n , char * out , int index , int evenSum , int oddSum ) { if ( index > n ) return ; if ( index == n ) { if ( abs ( evenSum - oddSum ) == 1 ) { out [ index ] = ' ▁ ' ; cout << out ; } return ; } if ( index & 1 ) { for ( int i = 0 ; i <= 9 ; i ++ ) { out [ index ] = i + '0' ; findNDigitNumsUtil ( n , out , index + 1 , evenSum , oddSum + i ) ; } } { for ( int i = 0 ; i <= 9 ; i ++ ) { out [ index ] = i + '0' ; findNDigitNumsUtil ( n , out , index + 1 , evenSum + i , oddSum ) ; } } } int findNDigitNums ( int n ) { char out [ n + 1 ] ; int index = 0 ; int evenSum = 0 , oddSum = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) { out [ index ] = i + '0' ; findNDigitNumsUtil ( n , out , index + 1 , evenSum + i , oddSum ) ; } } int main ( ) { int n = 3 ; findNDigitNums ( n ) ; return 0 ; }
Program to convert a given number to words | Set 2 | C ++ program to print a given number in words . The program handles till 9 digits numbers and can be easily extended to 20 digit number ; strings at index 0 is not used , it is to make array indexing simple ; strings at index 0 and 1 are not used , they is to make array indexing simple ; n is 1 - or 2 - digit number ; if n is more than 19 , divide it ; if n is non - zero ; Function to print a given number in words ; stores word representation of given number n ; handles digits at ten millions and hundred millions places ( if any ) ; handles digits at hundred thousands and one millions places ( if any ) ; handles digits at thousands and tens thousands places ( if any ) ; handles digit at hundreds places ( if any ) ; handles digits at ones and tens places ( if any ) ; Driver code ; long handles upto 9 digit no change to unsigned long long int to handle more digit number ; convert given number in words
#include <iostream> NEW_LINE using namespace std ; string one [ ] = { " " , " one ▁ " , " two ▁ " , " three ▁ " , " four ▁ " , " five ▁ " , " six ▁ " , " seven ▁ " , " eight ▁ " , " nine ▁ " , " ten ▁ " , " eleven ▁ " , " twelve ▁ " , " thirteen ▁ " , " fourteen ▁ " , " fifteen ▁ " , " sixteen ▁ " , " seventeen ▁ " , " eighteen ▁ " , " nineteen ▁ " } ; string ten [ ] = { " " , " " , " twenty ▁ " , " thirty ▁ " , " forty ▁ " , " fifty ▁ " , " sixty ▁ " , " seventy ▁ " , " eighty ▁ " , " ninety ▁ " } ; string numToWords ( int n , string s ) { string str = " " ; if ( n > 19 ) str += ten [ n / 10 ] + one [ n % 10 ] ; else str += one [ n ] ; if ( n ) str += s ; return str ; } string convertToWords ( long n ) { string out ; out += numToWords ( ( n / 10000000 ) , " crore ▁ " ) ; out += numToWords ( ( ( n / 100000 ) % 100 ) , " lakh ▁ " ) ; out += numToWords ( ( ( n / 1000 ) % 100 ) , " thousand ▁ " ) ; out += numToWords ( ( ( n / 100 ) % 10 ) , " hundred ▁ " ) ; if ( n > 100 && n % 100 ) out += " and ▁ " ; out += numToWords ( ( n % 100 ) , " " ) ; return out ; } int main ( ) { long n = 438237764 ; cout << convertToWords ( n ) << endl ; return 0 ; }
Find number of subarrays with even sum | C ++ program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; Result may be large enough not to fit in int ; ; To keep track of subarrays with even sum starting from index i ; ; If a [ i ] is odd then all subarrays starting from index i + 1 which was odd becomes even when a [ i ] gets added to it . ; If a [ i ] is even then all subarrays starting from index i + 1 which was even remains even and one extra a [ i ] even subarray gets added to it . ; Driver Code
#include <iostream> NEW_LINE using namespace std ; long long countEvenSum ( int a [ ] , int n ) { long long res = 0 ; int s = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] % 2 == 1 ) { s = n - i - 1 - s ; } else { s = s + 1 ; } res = res + s ; } return res ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ even " " ▁ sum ▁ is ▁ " << countEvenSum ( arr , n ) ; return 0 ; }
Linear Diophantine Equations | C ++ program to check for solutions of diophantine equations ; utility function to find the GCD of two numbers ; This function checks if integral solutions are possible ; driver function ; First example ; Second example ; Third example
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { return ( a % b == 0 ) ? abs ( b ) : gcd ( b , a % b ) ; } bool isPossible ( int a , int b , int c ) { return ( c % gcd ( a , b ) == 0 ) ; } int main ( ) { int a = 3 , b = 6 , c = 9 ; isPossible ( a , b , c ) ? cout << " Possible STRNEWLINE " : cout << " Not ▁ Possible STRNEWLINE " ; a = 3 , b = 6 , c = 8 ; isPossible ( a , b , c ) ? cout << " Possible STRNEWLINE " : cout << " Not ▁ Possible STRNEWLINE " ; a = 2 , b = 5 , c = 1 ; isPossible ( a , b , c ) ? cout << " Possible STRNEWLINE " : cout << " Not ▁ Possible STRNEWLINE " ; return 0 ; }
Farey Sequence | Efficient C ++ program to print Farey Sequence of order n ; Optimized function to print Farey sequence of order n ; We know first two terms are 0 / 1 and 1 / n ; double x , y = 0 ; For next terms to be evaluated ; Using recurrence relation to find the next term ; Print next term ; Update x1 , y1 , x2 and y2 for next iteration ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void farey ( int n ) { double x1 = 0 , y1 = 1 , x2 = 1 , y2 = n ; printf ( " % .0f / % . 0f ▁ % .0f / % . 0f " , x1 , y1 , x2 , y2 ) ; while ( y != 1.0 ) { x = floor ( ( y1 + n ) / y2 ) * x2 - x1 ; y = floor ( ( y1 + n ) / y2 ) * y2 - y1 ; printf ( " ▁ % .0f / % . 0f " , x , y ) ; x1 = x2 , x2 = x , y1 = y2 , y2 = y ; } } int main ( ) { int n = 7 ; cout << " Farey ▁ Sequence ▁ of ▁ order ▁ " << n << " ▁ is STRNEWLINE " ; farey ( n ) ; return 0 ; }
Find smallest values of x and y such that ax | C ++ program to find the smallest values of x and y that satisfy " ax ▁ - ▁ by ▁ = ▁ 0" ; To find GCD using Eculcid 's algorithm ; Prints smallest values of x and y that satisfy " ax ▁ - ▁ by ▁ = ▁ 0" ; Find LCM ; Driver program
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return ( gcd ( b , a % b ) ) ; } void findSmallest ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; cout << " x ▁ = ▁ " << lcm / a << " y = " } int main ( ) { int a = 25 , b = 35 ; findSmallest ( a , b ) ; return 0 ; }
Compute n ! under modulo p | C ++ program to comput n ! % p using Wilson 's Theorem ; Utility function to do modular exponentiation . It returns ( x ^ y ) % p ; int res = 1 ; Initialize result x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to find modular inverse of a under modulo p using Fermat 's method. Assumption: p is prime ; Returns n ! % p using Wilson 's Theorem ; n ! % p is 0 if n >= p ; Initialize result as ( p - 1 ) ! which is - 1 or ( p - 1 ) ; Multiply modulo inverse of all numbers from ( n + 1 ) to p ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , unsigned int y , int p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int modInverse ( int a , int p ) { return power ( a , p - 2 , p ) ; } int modFact ( int n , int p ) { if ( p <= n ) return 0 ; int res = ( p - 1 ) ; for ( int i = n + 1 ; i < p ; i ++ ) res = ( res * modInverse ( i , p ) ) % p ; return res ; } int main ( ) { int n = 25 , p = 29 ; cout << modFact ( n , p ) ; return 0 ; }
Count number of ways to divide a number in 4 parts | A Simple C ++ program to count number of ways to represent a number n as sum of four . ; Returns count of ways ; Generate all possible quadruplet and increment counter when sum of a quadruplet is equal to n ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( int n ) { for ( int i = 1 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) for ( int k = j ; k < n ; k ++ ) for ( int l = k ; l < n ; l ++ ) if ( i + j + k + l == n ) counter ++ ; return counter ; } int main ( ) { int n = 8 ; cout << countWays ( n ) ; return 0 ; }
Sum of Bitwise And of all pairs in a given array | A Simple C ++ program to compute sum of bitwise AND of all pairs ; Returns value of " arr [ 0 ] ▁ & ▁ arr [ 1 ] ▁ + ▁ arr [ 0 ] ▁ & ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ & ▁ arr [ j ] ▁ + ▁ . . . . . ▁ arr [ n - 2 ] ▁ & ▁ arr [ n - 1 ] " ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pairAndSum ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) ans += arr [ i ] & arr [ j ] ; return ans ; } int main ( ) { int arr [ ] = { 5 , 10 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << pairAndSum ( arr , n ) << endl ; return 0 ; }
Primality Test | Set 1 ( Introduction and School Method ) | A school method based C ++ program to check if a number is prime ; Corner case ; Check from 2 to n - 1 ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } int main ( ) { isPrime ( 11 ) ? cout << " ▁ true STRNEWLINE " : cout << " ▁ false STRNEWLINE " ; isPrime ( 15 ) ? cout << " ▁ true STRNEWLINE " : cout << " ▁ false STRNEWLINE " ; return 0 ; }
Euler 's Totient function for all numbers smaller than or equal to n | C ++ program to compute Totient function for all numbers smaller than or equal to n . ; Computes and prints totient of all numbers smaller than or equal to n . ; Create and initialize an array to store phi or totient values ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Print precomputed phi values ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void computeTotient ( int n ) { long long phi [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) for ( int p = 2 ; p <= n ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= n ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } for ( int i = 1 ; i <= n ; i ++ ) cout << " Totient ▁ of ▁ " << i << " ▁ is ▁ " << phi [ i ] << endl ; } int main ( ) { int n = 12 ; computeTotient ( n ) ; return 0 ; }
Euler 's Totient function for all numbers smaller than or equal to n | C ++ program for the above approach ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll Euler_totient_function ( ll n ) { ll result = 1 ; for ( ll i = 2 ; i * i <= n ; i ++ ) { ll c = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { c ++ ; n /= i ; } } if ( c > 0 ) { ll power = ( ll ) pow ( i , c - 1 ) ; ll sm = ( ll ) pow ( i , c - 1 ) * ( i - 1 ) ; result *= sm ; } } if ( n > 1 ) { result *= ( n - 1 ) ; } return result ; } int main ( ) { for ( ll i = 1 ; i < 13 ; i ++ ) { cout << " Euler _ totient _ function ( " << i << " ) : ▁ " ; cout << Euler_totient_function ( i ) << endl ; } } #praveeny182
Check if a number can be expressed as x ^ y ( x raised to power y ) | C ++ program to check if a given number can be expressed as power ; Returns true if n can be written as x ^ y ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( unsigned n ) { float p ; if ( n <= 1 ) return 1 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { p = log2 ( n ) / log2 ( i ) ; if ( ( ceil ( p ) == floor ( p ) ) && p > 1 ) return true ; } return false ; } int main ( ) { for ( int i = 2 ; i < 100 ; i ++ ) if ( isPower ( i ) ) cout << i << " ▁ " ; return 0 ; }
Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | C ++ program to find sum of all element between to K1 ' th ▁ and ▁ k2' th smallest elements in array ; Returns sum between two kth smallest elements of the array ; Sort the given array ; Below code is equivalent to ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumBetweenTwoKth ( int arr [ ] , int n , int k1 , int k2 ) { sort ( arr , arr + n ) ; int result = 0 ; for ( int i = k1 ; i < k2 - 1 ; i ++ ) result += arr [ i ] ; return result ; } int main ( ) { int arr [ ] = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; int k1 = 3 , k2 = 6 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumBetweenTwoKth ( arr , n , k1 , k2 ) ; return 0 ; }
Find nth Magic Number | C ++ program to find nth magic number ; Function to find nth magic number ; Go through every bit of n ; If last bit of n is set ; proceed to next bit n >>= 1 ; or n = n / 2 ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthMagicNo ( int n ) { int pow = 1 , answer = 0 ; while ( n ) { pow = pow * 5 ; if ( n & 1 ) answer += pow ; } return answer ; } int main ( ) { int n = 5 ; cout << " nth ▁ magic ▁ number ▁ is ▁ " << nthMagicNo ( n ) << endl ; return 0 ; }
Given a number n , count all multiples of 3 and / or 5 in set { 1 , 2 , 3 , ... n } | C ++ program to find count of multiples of 3 and 5 in { 1 , 2 , 3 , . . n } ; Add multiples of 3 and 5. Since common multiples are counted twice in n / 3 + n / 15 , subtract common multiples ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; unsigned countOfMultiples ( unsigned n ) { return ( n / 3 + n / 5 - n / 15 ) ; } int main ( ) { cout << countOfMultiples ( 6 ) << endl ; cout << countOfMultiples ( 16 ) << endl ; return 0 ; }
Program to find GCD or HCF of two numbers | C ++ program to find GCD of two numbers ; Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } int main ( ) { int a = 98 , b = 56 ; cout << " GCD ▁ of ▁ " << a << " ▁ and ▁ " << b << " ▁ is ▁ " << gcd ( a , b ) ; return 0 ; }
Find XOR sum of Bitwise AND of all pairs from given two Arrays | C ++ algorithm for the above approach ; Function to calculate the XOR sum of all ANDS of all pairs on A [ ] and B [ ] ; variable to store xor sums of first array and second array respectively . ; Xor sum of first array ; Xor sum of second array ; required answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int XorSum ( int A [ ] , int B [ ] , int N , int M ) { int ans1 = 0 , ans2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) ans1 = ans1 ^ A [ i ] ; for ( int i = 0 ; i < M ; i ++ ) ans2 = ans2 ^ B [ i ] ; return ( ans1 & ans2 ) ; } int main ( ) { int A [ ] = { 3 , 5 } ; int B [ ] = { 2 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( B ) / sizeof ( B [ 0 ] ) ; cout << XorSum ( A , B , N , M ) << endl ; }