codeparrot/xlcost-text-to-code huggingface dataset:
Find smallest number n such that n XOR n + 1 equals to given k . | CPP to find n such that XOR of n and n + 1 is equals to given n ; function to return the required n ; if k is of form 2 ^ i - 1 ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorCalc ( int k ) { if ( k == 1 ) return 2 ; if ( ( ( k + 1 ) & k ) == 0 ) return k / 2 ; return -1 ; } int main ( ) { int k = 31 ; cout << xorCalc ( k ) ; return 0 ; }
Find n | C ++ program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append 7 and move to parent ; Reverse res and return . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findNthNo ( int n ) { string res = " " ; while ( n >= 1 ) { if ( n & 1 ) { res = res + "4" ; n = ( n - 1 ) / 2 ; } else { res = res + "7" ; n = ( n - 2 ) / 2 ; } } reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } int main ( ) { int n = 13 ; cout << findNthNo ( n ) ; return 0 ; }
Total number of divisors for a given number | CPP program for finding number of divisor ; program for finding no . of divisors ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divCount ( int n ) { bool hash [ n + 1 ] ; memset ( hash , true , sizeof ( hash ) ) ; for ( int p = 2 ; p * p < n ; p ++ ) if ( hash [ p ] == true ) for ( int i = p * 2 ; i < n ; i += p ) hash [ i ] = false ; int total = 1 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( hash [ p ] ) { int count = 0 ; if ( n % p == 0 ) { while ( n % p == 0 ) { n = n / p ; count ++ ; } total = total * ( count + 1 ) ; } } } return total ; } int main ( ) { int n = 24 ; cout << divCount ( n ) ; return 0 ; }
Maximum number of unique prime factors | C ++ program to find maximum number of prime factors for a number in range [ 1 , N ] ; Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of unique prime factors . ; Return maximum element in arr [ ] ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPrimefactorNum ( int N ) { int arr [ N + 1 ] ; memset ( arr , 0 , sizeof ( arr ) ) ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( ! arr [ i ] ) for ( int j = 2 * i ; j <= N ; j += i ) arr [ j ] ++ ; arr [ i ] = 1 ; } return * max_element ( arr , arr + N ) ; } int main ( ) { int N = 40 ; cout << maxPrimefactorNum ( N ) << endl ; return 0 ; }
Decimal to binary conversion without using arithmetic operators | C ++ implementation of decimal to binary conversion without using arithmetic operators ; function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decToBin ( int n ) { if ( n == 0 ) return "0" ; string bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } int main ( ) { int n = 38 ; cout << decToBin ( n ) ; return 0 ; }
Difference between two given times | C ++ program to find difference between two given times . ; remove ' : ' and convert it into an integer ; Main function which finds difference ; change string ( eg . 2 : 21 -- > 221 , 00 : 23 -- > 23 ) ; difference between hours ; difference between minutes ; convert answer again in string with ' : ' ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int removeColon ( string s ) { if ( s . size ( ) == 4 ) s . replace ( 1 , 1 , " " ) ; if ( s . size ( ) == 5 ) s . replace ( 2 , 1 , " " ) ; return stoi ( s ) ; } string diff ( string s1 , string s2 ) { int time1 = removeColon ( s1 ) ; int time2 = removeColon ( s2 ) ; int hourDiff = time2 / 100 - time1 / 100 - 1 ; int minDiff = time2 % 100 + ( 60 - time1 % 100 ) ; if ( minDiff >= 60 ) { hourDiff ++ ; minDiff = minDiff - 60 ; } string res = to_string ( hourDiff ) + ' : ' + to_string ( minDiff ) ; return res ; } int main ( ) { string s1 = "14:00" ; string s2 = "16:45" ; cout << diff ( s1 , s2 ) << endl ; return 0 ; }
Sum of array elements that is first continuously increasing then decreasing | Efficient C ++ method to find sum of the elements of array that is halfway increasing and then halfway decreassing ; Driver code
#include <iostream> NEW_LINE using namespace std ; int arraySum ( int arr [ ] , int n ) { int x = ( n + 1 ) / 2 ; return ( arr [ 0 ] - 1 ) * n + x * x ; } int main ( ) { int arr [ ] = { 10 , 11 , 12 , 13 , 12 , 11 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << arraySum ( arr , n ) ; return 0 ; }
Balance pans using given weights that are powers of a number | C ++ code to check whether scale can be balanced or not ; method returns true if balancing of scale is possible ; baseForm vector will store T 's representation on  base a in reverse order ; convert T to representation on base a ; make first digit of representation as 0 ; loop over base representation of T ; if any digit is not 0 , 1 , ( a - 1 ) or a then balancing is not possible ; if digit is a or ( a - 1 ) then increase left index ' s ▁ count / ▁ ( case , ▁ when ▁ this ▁ weight ▁ is ▁ transferred ▁ to ▁ T ' s side ) ; if representation is processed then balancing is possible ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isBalancePossible ( int T , int a ) { vector < int > baseForm ; while ( T ) { baseForm . push_back ( T % a ) ; T /= a ; } baseForm . push_back ( 0 ) ; for ( int i = 0 ; i < baseForm . size ( ) ; i ++ ) { if ( baseForm [ i ] != 0 && baseForm [ i ] != 1 && baseForm [ i ] != ( a - 1 ) && baseForm [ i ] != a ) return false ; if ( baseForm [ i ] == a || baseForm [ i ] == ( a - 1 ) ) baseForm [ i + 1 ] += 1 ; } return true ; } int main ( ) { int T = 11 ; int a = 4 ; bool balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) cout << " Balance ▁ is ▁ possible " << endl ; else cout << " Balance ▁ is ▁ not ▁ possible " << endl ; return 0 ; }
Number of digits in the product of two numbers | C ++ implementation to count number of digits in the product of two numbers ; function to count number of digits in the product of two numbers ; if either of the number is 0 , then product will be 0 ; required count of digits ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int a , int b ) { if ( a == 0 b == 0 ) return 1 ; return floor ( log10 ( abs ( a ) ) + log10 ( abs ( b ) ) ) + 1 ; } int main ( ) { int a = 33 ; int b = -24 ; cout << countDigits ( a , b ) ; return 0 ; }
Distributing M items in a circle of size N starting from K | C ++ program to find the position where last item is delivered . ; n == > Size of circle m == > Number of items k == > Initial position ; n - k + 1 is number of positions before we reach beginning of circle If m is less than this value , then we can simply return ( m - 1 ) th position ; Let us compute remaining items before we reach beginning . ; We compute m % n to skip all complete rounds . If we reach end , we return n else we return m % n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; } int main ( ) { int n = 5 ; int m = 8 ; int k = 2 ; cout << lastPosition ( n , m , k ) ; return 0 ; }
An interesting solution to get all prime numbers smaller than n | C ++ program to Prints prime numbers smaller than n ; Compute factorials and apply Wilson 's  theorem. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void primesInRange ( int n ) { int fact = 1 ; for ( int k = 2 ; k < n ; k ++ ) { fact = fact * ( k - 1 ) ; if ( ( fact + 1 ) % k == 0 ) cout << k << endl ; } } int main ( ) { int n = 15 ; primesInRange ( n ) ; }
A product array puzzle | Set 2 ( O ( 1 ) Space ) | C ++ program for product array puzzle with O ( n ) time and O ( 1 ) space . ; epsilon value to maintain precision ; to hold sum of all values ; output product for each index antilog to find original product value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define EPS  1e-9 NEW_LINE void productPuzzle ( int a [ ] , int n ) { long double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( long double ) log10 ( a [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) cout << ( int ) ( EPS + pow ( ( long double ) 10.00 , sum - log10 ( a [ i ] ) ) ) << " ▁ " ; } int main ( ) { int a [ ] = { 10 , 3 , 5 , 6 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " The ▁ product ▁ array ▁ is : ▁ STRNEWLINE " ; productPuzzle ( a , n ) ; return 0 ; }
Change all even bits in a number to 0 | C ++ program to change even bits to 0. ; Returns modified number with all even bits 0. ; To store sum of bits at even positions . ; To store bits to shift ; One by one put all even bits to end ; If current last bit is set , add it to ans ; Next shift position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int changeEvenBits ( int n ) { int to_subtract = 0 ; int m = 0 ; for ( int x = n ; x ; x >>= 2 ) { if ( x & 1 ) to_subtract += ( 1 << m ) ; m += 2 ; } return n - to_subtract ; } int main ( ) { int n = 30 ; cout << changeEvenBits ( n ) << endl ; return 0 ; }
Find the number closest to n and divisible by m | C ++ implementation to find the number closest to n and divisible by m ; function to find the number closest to n and divisible by m ; find the quotient ; 1 st possible closest number ; 2 nd possible closest number ; if true , then n1 is the required closest number ; else n2 is the required closest number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int closestNumber ( int n , int m ) { int q = n / m ; int n1 = m * q ; int n2 = ( n * m ) > 0 ? ( m * ( q + 1 ) ) : ( m * ( q - 1 ) ) ; if ( abs ( n - n1 ) < abs ( n - n2 ) ) return n1 ; return n2 ; } int main ( ) { int n = 13 , m = 4 ; cout << closestNumber ( n , m ) << endl ; n = -15 ; m = 6 ; cout << closestNumber ( n , m ) << endl ; n = 0 ; m = 8 ; cout << closestNumber ( n , m ) << endl ; n = 18 ; m = -7 ; cout << closestNumber ( n , m ) << endl ; return 0 ; }
Check if a given number is Pronic | C / C ++ program to check if a number is pronic ; function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code ; Printing Pronic Numbers upto 200
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( sqrt ( x ) ) ; i ++ ) if ( x == i * ( i + 1 ) ) return true ; return false ; } int main ( void ) { for ( int i = 0 ; i <= 200 ; i ++ ) if ( checkPronic ( i ) ) cout << i << " ▁ " ; return 0 ; }
Find minimum sum of factors of number | CPP program to find minimum sum of product of number ; To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinSum ( int num ) { int sum = 0 ; for ( int i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { sum += i ; num /= i ; } } sum += num ; return sum ; } int main ( ) { int num = 12 ; cout << findMinSum ( num ) ; return 0 ; }
Minimum number with digits as 4 and 7 only and given sum | C ++ program to find smallest number with given sum of digits . ; Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMin ( int sum ) { int a = 0 , b = 0 ; while ( sum > 0 ) { if ( sum % 7 == 0 ) { b ++ ; sum -= 7 ; } else if ( sum % 4 == 0 ) { a ++ ; sum -= 4 ; } else { a ++ ; sum -= 4 ; } } if ( sum < 0 ) { printf ( " - 1n " ) ; return ; } for ( int i = 0 ; i < a ; i ++ ) printf ( "4" ) ; for ( int i = 0 ; i < b ; i ++ ) printf ( "7" ) ; printf ( " n " ) ; } int main ( ) { findMin ( 15 ) ; return 0 ; }
Add minimum number to an array so that the sum becomes even | CPP program to add minimum number so that the sum of array becomes even ; Function to find out minimum number ; Count odd number of terms in array ; Driver code
#include <iostream> NEW_LINE using namespace std ; int minNum ( int arr [ ] , int n ) { int odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 ) odd += 1 ; return ( odd % 2 ) ? 1 : 2 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minNum ( arr , n ) << " n " ; return 0 ; }
Find maximum number that can be formed using digits of a given number | CPP program to print the maximum number from the set of digits of a given number ; Function to print the maximum number ; hashed array to store count of digits ; Converting given number to string ; Updating the count array ; result is to store the final number ; Traversing the count array to calculate the maximum number ; return the result ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printMaxNum ( int num ) { int count [ 10 ] = { 0 } ; string str = to_string ( num ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ str [ i ] - '0' ] ++ ; int result = 0 , multiplier = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) { while ( count [ i ] > 0 ) { result = result + ( i * multiplier ) ; count [ i ] -- ; multiplier = multiplier * 10 ; } } return result ; } int main ( ) { int num = 38293367 ; cout << printMaxNum ( num ) ; return 0 ; }
Form the largest number using at most one swap operation | C ++ implementation to form the largest number by applying atmost one swap operation ; function to form the largest number by applying atmost one swap operation ; for the rightmost digit , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element up to the left element ; if ' num [ i ] ' is less than the greatest digit encountered so far ; else ; there is no greater right digit for ' num [ i ] ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and break ; performing the required swap operation ; required largest number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestNumber ( string num ) { int n = num . size ( ) ; int rightMax [ n ] , right ; rightMax [ n - 1 ] = -1 ; right = n - 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( num [ i ] < num [ right ] ) rightMax [ i ] = right ; else { rightMax [ i ] = -1 ; right = i ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( rightMax [ i ] != -1 ) { swap ( num [ i ] , num [ rightMax [ i ] ] ) ; break ; } } return num ; } int main ( ) { string num = "8725634" ; cout << " Largest ▁ number : " << largestNumber ( num ) ; return 0 ; }
Binomial Random Variables | C ++ program to compute Binomial Probability ; function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; Driver code
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; int nCr ( int n , int r ) { if ( r > n / 2 ) r = n - r ; int answer = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { answer *= ( n - r + i ) ; answer /= i ; } return answer ; } float binomialProbability ( int n , int k , float p ) { return nCr ( n , k ) * pow ( p , k ) * pow ( 1 - p , n - k ) ; } int main ( ) { int n = 10 ; int k = 5 ; float p = 1.0 / 3 ; float probability = binomialProbability ( n , k , p ) ; cout << " Probability ▁ of ▁ " << k ; cout << " ▁ heads ▁ when ▁ a ▁ coin ▁ is ▁ tossed ▁ " << n ; cout << " ▁ times ▁ where ▁ probability ▁ of ▁ each ▁ head ▁ is ▁ " << p << endl ; cout << " ▁ is ▁ = ▁ " << probability << endl ; }
Find pair with maximum GCD in an array | C ++ Code to find pair with maximum GCD in an array ; function to find GCD of pair with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least 2 numbers , it is a GCD of at least 1 pair ; Driver code ; Array in which pair with max GCD is to be found ; Size of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = max ( high , arr [ i ] ) ; int divisors [ high + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % j == 0 ) { divisors [ j ] ++ ; if ( j != arr [ i ] / j ) divisors [ arr [ i ] / j ] ++ ; } } } for ( int i = high ; i >= 1 ; i -- ) if ( divisors [ i ] > 1 ) return i ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxGCD ( arr , n ) ; return 0 ; }
Find pair with maximum GCD in an array | C ++ Code to Find pair with maximum GCD in an array ; function to find GCD of pair with max GCD in the array ; Calculating MAX in array ; Maintaining count array ; Variable to store the multiples of a number ; Iterating from MAX to 1 GCD is always between MAX and 1. The first GCD found will be the highest as we are decrementing the potential GCD ; Iterating from current potential GCD till it is less than MAX ; A multiple found ; Incrementing potential GCD by itself To check i , 2 i , 3 i ... . ; 2 multiples found , max GCD found ; Driver code ; Array in which pair with max GCD is to be found ; Size of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = max ( high , arr [ i ] ) ; int count [ high + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ) { int j = i ; counter = 0 ; while ( j <= high ) { if ( count [ j ] >= 2 ) return j ; else if ( count [ j ] == 1 ) counter ++ ; j += i ; if ( counter == 2 ) return i ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxGCD ( arr , n ) ; return 0 ; }
Evil Number | C / C ++ program to check if a number is Evil number or Odious Number ; returns number of 1 s from the binary number ; counting 1 s ; Check if number is evil or not ; converting n to binary form ; calculating remainder ; storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver Code
#include <iostream> NEW_LINE using namespace std ; #include <math.h> NEW_LINE int count_one ( int n ) { int c_one = 0 ; while ( n != 0 ) { int rem = n % 10 ; if ( rem == 1 ) c_one = c_one + 1 ; n = n / 10 ; } return c_one ; } int checkEvil ( int n ) { int i = 0 , bin = 0 , n_one = 0 ; while ( n != 0 ) { int r = n % 2 ; bin = bin + r * ( int ) ( pow ( 10 , i ) ) ; n = n / 2 ; } n_one = count_one ( bin ) ; if ( n_one % 2 == 0 ) return 1 ; else return 0 ; } int main ( void ) { int i , check , num ; num = 32 ; check = checkEvil ( num ) ; if ( check == 1 ) cout << num << " ▁ is ▁ Evil ▁ Number STRNEWLINE " ; else cout << num << " ▁ is ▁ Odious ▁ Number STRNEWLINE " ; return 0 ; }
Count number of pairs ( A <= N , B <= N ) such that gcd ( A , B ) is B | C ++ implementation of counting pairs such that gcd ( a , b ) = b ; returns number of valid pairs ; initialize k ; loop till imin <= n ; Initialize result ; max i with given k floor ( n / k ) ; adding k * ( number of i with floor ( n / i ) = k to ans ; set imin = imax + 1 and k = n / imin ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountPairs ( int n ) { int k = n ; int imin = 1 ; int ans = 0 ; while ( imin <= n ) { int imax = n / k ; ans += k * ( imax - imin + 1 ) ; imin = imax + 1 ; k = n / imin ; } return ans ; } int main ( ) { cout << CountPairs ( 1 ) << endl ; cout << CountPairs ( 2 ) << endl ; cout << CountPairs ( 3 ) << endl ; return 0 ; }
Find the last digit of given series | C ++ program to calculate to find last digit of above expression ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; 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 >> 1L L ; y = y / 2 ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Make x1 positive ; Function to calculate the above expression ; Initialize the result ; Compute first part of expression ; Compute second part of expression i . e . , ( ( 4 ^ ( n + 1 ) - 1 ) / 3 ) mod 10 Since division of 3 in modulo can ' t ▁ ▁ be ▁ performed ▁ directly ▁ therefore ▁ we ▁ ▁ need ▁ to ▁ find ▁ it ' s modulo Inverse ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long powermod ( long long x , long long y , long long p ) { while ( y > 0 ) { if ( y & 1LL ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } long long modInverse ( long long a , long long m ) { long long m0 = m , t , q ; long long x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m , a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } long long evaluteExpression ( long long & n ) { long long firstsum = 0 , mod = 10 ; for ( long long i = 2 , j = 0 ; ( 1LL << j ) <= n ; i *= i , ++ j ) firstsum = ( firstsum + i ) % mod ; long long secondsum = ( powermod ( 4LL , n + 1 , mod ) - 1 ) * modInverse ( 3LL , mod ) ; return ( firstsum * secondsum ) % mod ; } int main ( ) { long long n = 3 ; cout << evaluteExpression ( n ) << endl ; n = 10 ; cout << evaluteExpression ( n ) ; return 0 ; }
Sum of all proper divisors of natural numbers in an array | C ++ program to find sum of proper divisors for every element in an array . ; To store prime factors and their powers ; Fills factors such that factors [ i ] is a vector of pairs containing prime factors ( of i ) and their powers . Also sets values in isPrime [ ] ; To check if a number is prime ; If i is prime , then update its powers in all multiples of it . ; Returns sum of proper divisors of num using factors [ ] ; Applying above discussed formula for every array element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000001 NEW_LINE #define pii  pair<int, int> NEW_LINE #define F  first NEW_LINE #define S  second NEW_LINE vector < pii > factors [ MAX ] ; void sieveOfEratothenese ( ) { bool isPrime [ MAX ] ; memset ( isPrime , true , sizeof ( isPrime ) ) ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i ; j < MAX ; j += i ) { int k , l ; isPrime [ j ] = false ; for ( k = j , l = 0 ; k % i == 0 ; l ++ , k /= i ) ; factors [ j ] . push_back ( make_pair ( i , l ) ) ; } } } } int sumOfProperDivisors ( int num ) { int mul = 1 ; for ( int i = 0 ; i < factors [ num ] . size ( ) ; i ++ ) mul *= ( ( pow ( factors [ num ] [ i ] . F , factors [ num ] [ i ] . S + 1 ) - 1 ) / ( factors [ num ] [ i ] . F - 1 ) ) ; return mul - num ; } int main ( ) { sieveOfEratothenese ( ) ; int arr [ ] = { 8 , 13 , 24 , 36 , 59 , 75 , 91 } ; for ( int i = 0 ; i < sizeof ( arr ) / sizeof ( int ) ; i ++ ) cout << sumOfProperDivisors ( arr [ i ] ) << " ▁ " ; cout << endl ; return 0 ; }