codeparrot/xlcost-text-to-code huggingface dataset:
Sum of the first N Prime numbers | C ++ implementation of above solution ; 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 . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; find the sum of 1 st N prime numbers ; count of prime numbers ; sum of prime numbers ; if the number is prime add it ; increase the count ; get to next number ; Driver code ; create the sieve ; find the value of 1 st n prime numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10000 NEW_LINE bool prime [ MAX + 1 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } int solve ( int n ) { int count = 0 , num = 1 ; long long int sum = 0 ; while ( count < n ) { if ( prime [ num ] ) { sum += num ; count ++ ; } num ++ ; } return sum ; } int main ( ) { SieveOfEratosthenes ( ) ; int n = 4 ; cout << " Sum ▁ of ▁ 1st ▁ N ▁ prime ▁ numbers ▁ are ▁ : " << solve ( n ) ; return 0 ; }
Implementation of Wilson Primality test | C ++ implementation to check if a number is prime or not using Wilson Primality Test ; Function to calculate the factorial ; Function to check if the number is prime or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long fact ( const int & p ) { if ( p <= 1 ) return 1 ; return p * fact ( p - 1 ) ; } bool isPrime ( const int & p ) { if ( p == 4 ) return false ; return bool ( fact ( p >> 1 ) % p ) ; } int main ( ) { cout << isPrime ( 127 ) ; return 0 ; }
Find the total Number of Digits in ( N ! ) N | C ++ program to find the total Number of Digits in ( N ! ) ^ N ; Function to find the total Number of Digits in ( N ! ) ^ N ; Finding X ; Calculating N * X ; Floor ( N * X ) + 1 return ceil ( sum ) ; equivalent to floor ( sum ) + 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountDigits ( int n ) { if ( n == 1 ) return 1 ; double sum = 0 ; for ( int i = 2 ; i <= n ; ++ i ) { sum += ( double ) log ( i ) / ( double ) log ( 10 ) ; } sum *= ( double ) n ; } int main ( ) { int N = 5 ; cout << CountDigits ( N ) ; return 0 ; }
Find the value of max ( f ( x ) ) | C ++ implementation of above approach ; Function to calculate the value ; forming the prefix sum arrays ; Taking the query ; finding the sum in the range l to r in array a ; finding the sum in the range l to r in array b ; Finding the max value of the function ; Finding the min value of the function ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define MAX  200006 NEW_LINE #define CONS  32766 NEW_LINE void calc ( ll a [ ] , ll b [ ] , ll lr [ ] , ll q , ll n ) { ll M , m , i , j , k , l , r , suma , sumb , cc ; cc = 0 ; for ( i = 0 ; i < n - 1 ; ++ i ) { a [ i + 1 ] += a [ i ] ; b [ i + 1 ] += b [ i ] ; } while ( q -- ) { l = lr [ cc ++ ] ; r = lr [ cc ++ ] ; l -= 2 ; r -= 1 ; suma = a [ r ] ; sumb = b [ r ] ; if ( l >= 0 ) { suma -= a [ l ] ; sumb -= b [ l ] ; } M = max ( CONS * suma + CONS * sumb , - CONS * suma - CONS * sumb ) ; M = max ( M , max ( CONS * suma - CONS * sumb , - CONS * suma + CONS * sumb ) ) ; m = min ( CONS * suma + CONS * sumb , - CONS * suma - CONS * sumb ) ; m = min ( m , min ( CONS * suma - CONS * sumb , - CONS * suma + CONS * sumb ) ) ; cout << ( M - m ) << " STRNEWLINE " ; } } int main ( ) { ll n = 5 , q = 2 ; ll a [ 5 ] = { 0 , 7 , 3 , 4 , 5 } ; ll b [ 5 ] = { 0 , 3 , 1 , 2 , 3 } ; ll lr [ q * 2 ] ; lr [ 0 ] = 1 ; lr [ 1 ] = 1 ; lr [ 2 ] = 1 ; lr [ 3 ] = 3 ; calc ( a , b , lr , q , n ) ; return 0 ; }
Program to find the Nth number of the series 2 , 10 , 24 , 44 , 70. ... . | CPP program to find the Nth term of the series 2 , 10 , 24 , 44 , 70. ... . ; function to return nth term of the series ; Driver code ; Get N ; Get Nth term
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  1000000009 NEW_LINE int NthTerm ( long long n ) { long long x = ( 3 * n * n ) % mod ; return ( x - n + mod ) % mod ; } int main ( ) { long long N = 4 ; cout << NthTerm ( N ) ; return 0 ; }
Sum of first N natural numbers by taking powers of 2 as negative number | C ++ implementation of above approach ; to store power of 2 ; to store presum of the power of 2 's ; function to find power of 2 ; to store power of 2 ; to store pre sum ; Function to find the sum ; first store sum of first n natural numbers . ; find the first greater number than given number then minus double of this from answer ; Driver code ; function call ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power [ 31 ] ; int pre [ 31 ] ; void PowerOfTwo ( ) { int x = 1 ; for ( int i = 0 ; i < 31 ; i ++ ) { power [ i ] = x ; x *= 2 ; } pre [ 0 ] = 1 ; for ( int i = 1 ; i < 31 ; i ++ ) pre [ i ] = pre [ i - 1 ] + power [ i ] ; } int Sum ( int n ) { int ans = n * ( n + 1 ) / 2 ; for ( int i = 0 ; i < 31 ; i ++ ) { if ( power [ i ] > n ) { ans -= 2 * pre [ i - 1 ] ; break ; } } return ans ; } int main ( ) { PowerOfTwo ( ) ; int n = 4 ; cout << Sum ( n ) ; return 0 ; }
Check if a number is Quartan Prime or not | CPP program to check if a number is Quartan Prime or not ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Program ; Check if number is prime and of the form 16 * n + 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } int main ( ) { int n = 17 ; if ( isPrime ( n ) && ( n % 16 == 1 ) ) { cout << " YES " ; } else { cout << " NO " ; } return 0 ; }
Print a number strictly less than a given number such that all its digits are distinct . | CPP program to find a number less than n such that all its digits are distinct ; Function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ; creating a copy of i ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int n ) { for ( int i = n - 1 ; > = 0 ; i -- ) { int count [ 10 ] = { 0 } ; int x = i ; int count1 = 0 , count2 = 0 ; while ( x ) { count [ x % 10 ] ++ ; x /= 10 ; count1 ++ ; } for ( int j = 0 ; j < 10 ; j ++ ) { if ( count [ j ] == 1 ) count2 ++ ; } if ( count1 == count2 ) return i ; } } int main ( ) { int n = 8490 ; cout << findNumber ( n ) ; return 0 ; }
Check if two Linked Lists are permutations of each other | C ++ program to check if linked lists are permutations of each other ; A linked list node ; Function to check if two linked lists * are permutations of each other * first : reference to head of first linked list * second : reference to head of second linked list ; Variables to keep track of sum and multiplication ; Traversing through linked list and calculating sum and multiply ; Traversing through linked list and calculating sum and multiply ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver program to test above function ; First constructed linked list is : 12 -> 35 -> 1 -> 10 -> 34 -> 1 ; Second constructed linked list is : 35 -> 1 -> 12 -> 1 -> 10 -> 34
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; bool isPermutation ( struct Node * first , struct Node * second ) { int sum1 = 0 , sum2 = 0 , mul1 = 1 , mul2 = 1 ; struct Node * temp1 = first ; while ( temp1 != NULL ) { sum1 += temp1 -> data ; mul1 *= temp1 -> data ; temp1 = temp1 -> next ; } struct Node * temp2 = second ; while ( temp2 != NULL ) { sum2 += temp2 -> data ; mul2 *= temp2 -> data ; temp2 = temp2 -> next ; } return ( ( sum1 == sum2 ) && ( mul1 == mul2 ) ) ; } void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int main ( ) { struct Node * first = NULL ; push ( & first , 1 ) ; push ( & first , 34 ) ; push ( & first , 10 ) ; push ( & first , 1 ) ; push ( & first , 35 ) ; push ( & first , 12 ) ; struct Node * second = NULL ; push ( & second , 35 ) ; push ( & second , 1 ) ; push ( & second , 12 ) ; push ( & second , 1 ) ; push ( & second , 10 ) ; push ( & second , 34 ) ; if ( isPermutation ( first , second ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } return 0 ; }
Find two distinct prime numbers with given product | C ++ program to find a distinct prime number pair whose product is equal to given number ; Function to generate all prime numbers less than n ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to print a prime pair with given product ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool SieveOfEratosthenes ( int n , bool isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } void findPrimePair ( int n ) { int flag = 0 ; bool isPrime [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 2 ; i < n ; i ++ ) { int x = n / i ; if ( isPrime [ i ] && isPrime [ x ] and x != i and x * i == n ) { cout << i << " ▁ " << x ; flag = 1 ; return ; } } if ( ! flag ) cout << " No ▁ such ▁ pair ▁ found " ; } int main ( ) { int n = 39 ; findPrimePair ( n ) ; return 0 ; }
Program to find the common ratio of three numbers | C ++ implementation of above approach ; Function to print a : b : c ; To print the given proportion in simplest form . ; Driver code ; Get the ratios ; Get ratio a : b1 ; Get ratio b2 : c ; Find the ratio a : b : c
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solveProportion ( int a , int b1 , int b2 , int c ) { int A = a * b2 ; int B = b1 * b2 ; int C = b1 * c ; int gcd = __gcd ( __gcd ( A , B ) , C ) ; cout << A / gcd << " : " << B / gcd << " : " << C / gcd ; } int main ( ) { int a , b1 , b2 , c ; a = 3 ; b1 = 4 ; b2 = 8 ; c = 9 ; solveProportion ( a , b1 , b2 , c ) ; return 0 ; }
Number of divisors of a given number N which are divisible by K | C ++ program to count number of divisors of N which are divisible by K ; Function to count number of divisors of N which are divisible by K ; Variable to store count of divisors ; Traverse from 1 to n ; increase the count if both the conditions are satisfied ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countDivisors ( int n , int k ) { int count = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { if ( n % i == 0 && i % k == 0 ) { count ++ ; } } return count ; } int main ( ) { int n = 12 , k = 3 ; cout << countDivisors ( n , k ) ; return 0 ; }
Calculate volume and surface area of a cone | CPP program to calculate Volume and Surface area of Cone ; Function to calculate Volume of cone ; Function to calculate Surface area of cone ; Driver Code ; Printing value of volume and surface area
#include <iostream> NEW_LINE using namespace std ; float pi = 3.14159 ; float volume ( float r , float h ) { return ( float ( 1 ) / float ( 3 ) ) * pi * r * r * h ; } float surface_area ( float r , float s ) { return pi * r * s + pi * r * r ; } int main ( ) { float radius = 5 ; float slant_height = 13 ; float height = 12 ; float vol , sur_area ; cout << " Volume ▁ Of ▁ Cone ▁ : ▁ " << volume ( radius , height ) << endl ; cout << " Surface ▁ Area ▁ Of ▁ Cone ▁ : ▁ " << surface_area ( radius , slant_height ) ; return 0 ; }
Program to find the Nth term of the series 0 , 14 , 40 , 78 , 124 , ... | CPP program to find the N - th term of the series 0 , 14 , 40 , 78 , 124 . . . ; calculate sum upto Nth term of series ; return the final sum ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int nthTerm ( int n ) { return 6 * pow ( n , 2 ) - 4 * n - 2 ; } int main ( ) { int N = 4 ; cout << nthTerm ( N ) ; return 0 ; }
Program to find the Nth term of series 5 , 10 , 17 , 26 , 37 , 50 , 65 , 82 , ... | CPP program to find the N - th term of the series : 5 , 10 , 17 , 26 , 37 , 50 , 65 , 82 , ... ; calculate Nth term of series ; return the final sum ; Driver Function
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int nthTerm ( int n ) { return pow ( n , 2 ) + 2 * n + 2 ; } int main ( ) { int N = 4 ; cout << nthTerm ( N ) ; return 0 ; }
Find nth term of a given recurrence relation | C ++ program to find nth term of a given recurrence relation ; function to return required value ; Get the answer ; Return the answer ; Driver program ; Get the value of n ; function call to print result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int n ) { int ans = ( n * ( n - 1 ) ) / 2 ; return ans ; } int main ( ) { int n = 5 ; cout << sum ( n ) ; return 0 ; }
Program to find Nth term of the series 3 , 12 , 29 , 54 , 87 , ... | CPP program to find N - th term of the series : 3 , 12 , 29 , 54 , 87 , ... ; calculate Nth term of series ; Return Nth term ; driver code ; declaration of number of terms ; Get the Nth term
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int getNthTerm ( long long int N ) { return 4 * pow ( N , 2 ) - 3 * N + 2 ; } int main ( ) { long long int N = 10 ; cout << getNthTerm ( N ) ; return 0 ; }
Find sum of product of number in given series | C ++ program to find sum of product of number in given series ; function to calculate ( a ^ b ) % p ; Initialize result ; 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 return required answer ; modulo inverse of denominator ; calculating commentator part ; calculating t ! ; accumulating the final answer ; Driver code ; function call to print required sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; const long long MOD = 1000000007 ; ll power ( ll x , unsigned long long y , ll p ) { ll res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } ll sumProd ( ll n , ll t ) { ll dino = power ( t + 1 , MOD - 2 , MOD ) ; unsigned long long ans = 1 ; for ( ll i = n + t + 1 ; i > n ; -- i ) ans = ( ans % MOD * i % MOD ) % MOD ; ll tfact = 1 ; for ( int i = 1 ; i <= t ; ++ i ) tfact = ( tfact * i ) % MOD ; ans = ans * dino - tfact + MOD ; return ans % MOD ; } int main ( ) { ll n = 3 , t = 2 ; cout << sumProd ( n , t ) ; return 0 ; }
Find the sum of series 3 , 7 , 13 , 21 , 31. ... | C ++ Program to find the sum of given series ; Function to calculate sum ; Return sum ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int findSum ( int n ) { return ( n * ( pow ( n , 2 ) + 3 * n + 5 ) ) / 3 ; } int main ( ) { int n = 25 ; cout << findSum ( n ) ; return 0 ; }
Minimum Players required to win the game | C ++ program to find minimum players required to win the game anyhow ; function to calculate ( a ^ b ) % ( 10 ^ 9 + 7 ) . ; function to find the minimum required player ; computing the nenomenator ; computing modulo inverse of denominator ; final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  1000000007 NEW_LINE long long int power ( long long int a , long long int b ) { long long int res = 1 ; while ( b ) { if ( b & 1 ) { res *= a ; res %= mod ; } b /= 2 ; a *= a ; a %= mod ; } return res ; } long long int minPlayer ( long long int n , long long int k ) { long long int num = ( ( power ( k , n ) - 1 ) + mod ) % mod ; long long int den = ( power ( k - 1 , mod - 2 ) + mod ) % mod ; long long int ans = ( ( ( num * den ) % mod ) * k ) % mod ; return ans ; } int main ( ) { long long int n = 3 , k = 3 ; cout << minPlayer ( n , k ) ; return 0 ; }
Sum of Factors of a Number using Prime Factorization | C ++ Program to find sum of all factors of a given number ; Using SieveOfEratosthenes to find smallest prime factor of all the numbers . For example , if N is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7 ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries in it as false . ; Initializing smallest factor equal to 2 for all the even numbers ; For odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " . ; Function to find sum of all prime factors ; Declaring array to store smallest prime factor of i at i - th index ; Filling values in s [ ] using sieve ; Current prime factor of N ; Power of current prime factor ; N is now N / s [ N ] . If new N als has smallest prime factor as currFactor , increment power ; Update current prime factor as s [ N ] and initializing power of factor as 1. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sieveOfEratosthenes ( int N , int s [ ] ) { vector < bool > prime ( N + 1 , false ) ; for ( int i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= N ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } int findSum ( int N ) { int s [ N + 1 ] ; int ans = 1 ; sieveOfEratosthenes ( N , s ) ; int currFactor = s [ N ] ; int power = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( currFactor == s [ N ] ) { power ++ ; continue ; } int sum = 0 ; for ( int i = 0 ; i <= power ; i ++ ) sum += pow ( currFactor , i ) ; ans *= sum ; currFactor = s [ N ] ; power = 1 ; } return ans ; } int main ( ) { int n = 12 ; cout << " Sum ▁ of ▁ the ▁ factors ▁ is ▁ : ▁ " ; cout << findSum ( n ) ; return 0 ; }
Find Multiples of 2 or 3 or 5 less than or equal to N | CPP program to count number of multiples of 2 or 3 or 5 less than or equal to N ; Function to count number of multiples of 2 or 3 or 5 less than or equal to N ; As we have to check divisibility by three numbers , So we can implement bit masking ; we check whether jth bit is set or not , if jth bit is set , simply multiply to prod ; check for set bit ; check multiple of product ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMultiples ( int n ) { int multiple [ ] = { 2 , 3 , 5 } ; int count = 0 , mask = pow ( 2 , 3 ) ; for ( int i = 1 ; i < mask ; i ++ ) { int prod = 1 ; for ( int j = 0 ; j < 3 ; j ++ ) { if ( i & 1 << j ) prod = prod * multiple [ j ] ; } if ( __builtin_popcount ( i ) % 2 == 1 ) count = count + n / prod ; else count = count - n / prod ; } return count ; } int main ( ) { int n = 10 ; cout << countMultiples ( n ) << endl ; return 0 ; }
Minimum value of N such that xor from 1 to N is equal to K | C ++ implementation of above approach ; Function to find the value of N ; variable to store the result ; handling case for '0' ; handling case for '1' ; when number is completely divided by 4 then minimum ' x ' will be ' k ' ; when number divided by 4 gives 3 as remainder then minimum ' x ' will be ' k - 1' ; else it is not possible to get k for any value of x ; Driver code ; let the given number be 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findN ( int k ) { int ans ; if ( k == 0 ) ans = 3 ; if ( k == 1 ) ans = 1 ; else if ( k % 4 == 0 ) ans = k ; else if ( k % 4 == 3 ) ans = k - 1 ; else ans = -1 ; return ans ; } int main ( ) { int k = 7 ; int res = findN ( k ) ; if ( res == -1 ) cout << " Not ▁ possible " ; else cout << res ; return 0 ; }
Permutations to arrange N persons around a circular table | C ++ code to demonstrate Circular Permutation ; Function to find no . of permutations ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Circular ( int n ) { int Result = 1 ; while ( n > 0 ) { Result = Result * n ; n -- ; } return Result ; } int main ( ) { int n = 4 ; cout << Circular ( n - 1 ) ; }
Minimum time required to complete a work by N persons together | C ++ implementation of above approach ; Function to calculate the time ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float calTime ( float arr [ ] , int n ) { float work = 0 ; for ( int i = 0 ; i < n ; i ++ ) work += 1 / arr [ i ] ; return 1 / work ; } int main ( ) { float arr [ ] = { 6.0 , 3.0 , 4.0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << calTime ( arr , n ) << " ▁ Hours " ; return 0 ; }
Find the largest twins in given range | C ++ program to find the largest twin in given range ; Function to find twins ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the largest twin in range ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printTwins ( int low , int high ) { bool prime [ high + 1 ] , twin = false ; memset ( prime , true , sizeof ( prime ) ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p <= floor ( sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= high ; i += p ) prime [ i ] = false ; } } for ( int i = high ; i >= low ; i -- ) { if ( prime [ i ] && ( i - 2 >= low && prime [ i - 2 ] == true ) ) { cout << " Largest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " << i - 2 << " , ▁ " << i << " ) " ; twin = true ; break ; } } if ( twin == false ) cout << " No ▁ such ▁ pair ▁ exists " << endl ; } int main ( ) { printTwins ( 10 , 100 ) ; return 0 ; }
Complement of a number with any base b | CPP program to find complement of a number with any base b ; Function to find ( b - 1 ) 's complement ; Calculate number of digits in the given number ; Largest digit in the number system with base b ; Largest number in the number system with base b ; return Complement ; Function to find b 's complement ; b ' s ▁ complement ▁ = ▁ ( b - 1 ) ' s complement + 1 ; Driver code
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; int prevComplement ( int n , int b ) { int maxDigit , maxNum = 0 , digits = 0 , num = n ; while ( n != 0 ) { digits ++ ; n = n / 10 ; } maxDigit = b - 1 ; while ( digits -- ) { maxNum = maxNum * 10 + maxDigit ; } return maxNum - num ; } int complement ( int n , int b ) { return prevComplement ( n , b ) + 1 ; } int main ( ) { cout << prevComplement ( 25 , 7 ) << endl ; cout << complement ( 25 , 7 ) ; return 0 ; }
Minimum positive integer value possible of X for given A and B in X = P * A + Q * B | CPP Program to find minimum value of X in equation X = P * A + Q * B ; Utility function to calculate GCD ; Driver Code
#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 main ( ) { int a = 2 ; int b = 4 ; cout << gcd ( a , b ) ; return 0 ; }