codeparrot/xlcost-text-to-code huggingface dataset:
System of Linear Equations in three variables using Cramer 's Rule | CPP program to calculate solutions of linear equations using cramer 's rule ; This functions finds the determinant of Matrix ; This function finds the solution of system of linear equations using cramer 's rule ; Matrix d using coeff as given in cramer 's rule ; Matrix d1 using coeff as given in cramer 's rule ; Matrix d2 using coeff as given in cramer 's rule ; Matrix d3 using coeff as given in cramer 's rule ; Calculating Determinant of Matrices d , d1 , d2 , d3 ; Case 1 ; Coeff have a unique solution . Apply Cramer 's Rule ; double z = D3 / D ; calculating z using cramer 's rule ; Case 2 ; Driver Code ; storing coefficients of linear equations in coeff matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; double determinantOfMatrix ( double mat [ 3 ] [ 3 ] ) { double ans ; ans = mat [ 0 ] [ 0 ] * ( mat [ 1 ] [ 1 ] * mat [ 2 ] [ 2 ] - mat [ 2 ] [ 1 ] * mat [ 1 ] [ 2 ] ) - mat [ 0 ] [ 1 ] * ( mat [ 1 ] [ 0 ] * mat [ 2 ] [ 2 ] - mat [ 1 ] [ 2 ] * mat [ 2 ] [ 0 ] ) + mat [ 0 ] [ 2 ] * ( mat [ 1 ] [ 0 ] * mat [ 2 ] [ 1 ] - mat [ 1 ] [ 1 ] * mat [ 2 ] [ 0 ] ) ; return ans ; } void findSolution ( double coeff [ 3 ] [ 4 ] ) { double d [ 3 ] [ 3 ] = { { coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 2 ] } , { coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 2 ] } , { coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 2 ] } , } ; double d1 [ 3 ] [ 3 ] = { { coeff [ 0 ] [ 3 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 2 ] } , { coeff [ 1 ] [ 3 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 2 ] } , { coeff [ 2 ] [ 3 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 2 ] } , } ; double d2 [ 3 ] [ 3 ] = { { coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 3 ] , coeff [ 0 ] [ 2 ] } , { coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 3 ] , coeff [ 1 ] [ 2 ] } , { coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 3 ] , coeff [ 2 ] [ 2 ] } , } ; double d3 [ 3 ] [ 3 ] = { { coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 3 ] } , { coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 3 ] } , { coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 3 ] } , } ; double D = determinantOfMatrix ( d ) ; double D1 = determinantOfMatrix ( d1 ) ; double D2 = determinantOfMatrix ( d2 ) ; double D3 = determinantOfMatrix ( d3 ) ; printf ( " D ▁ is ▁ : ▁ % lf ▁ STRNEWLINE " , D ) ; printf ( " D1 ▁ is ▁ : ▁ % lf ▁ STRNEWLINE " , D1 ) ; printf ( " D2 ▁ is ▁ : ▁ % lf ▁ STRNEWLINE " , D2 ) ; printf ( " D3 ▁ is ▁ : ▁ % lf ▁ STRNEWLINE " , D3 ) ; if ( D != 0 ) { double x = D1 / D ; double y = D2 / D ; printf ( " Value ▁ of ▁ x ▁ is ▁ : ▁ % lf STRNEWLINE " , x ) ; printf ( " Value ▁ of ▁ y ▁ is ▁ : ▁ % lf STRNEWLINE " , y ) ; printf ( " Value ▁ of ▁ z ▁ is ▁ : ▁ % lf STRNEWLINE " , z ) ; } else { if ( D1 == 0 && D2 == 0 && D3 == 0 ) printf ( " Infinite ▁ solutions STRNEWLINE " ) ; else if ( D1 != 0 D2 != 0 D3 != 0 ) printf ( " No ▁ solutions STRNEWLINE " ) ; } } int main ( ) { double coeff [ 3 ] [ 4 ] = { { 2 , -1 , 3 , 9 } , { 1 , 1 , 1 , 6 } , { 1 , -1 , 1 , 2 } , } ; findSolution ( coeff ) ; return 0 ; }
Find larger of x ^ y and y ^ x | C ++ program to print greater of x ^ y and y ^ x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printGreater ( double x , double y ) { long double X = y * log ( x ) ; long double Y = x * log ( y ) ; if ( abs ( X - Y ) < 1e-9 ) { cout << " Equal " ; } else if ( X > Y ) { cout << x << " ^ " << y ; } else { cout << y << " ^ " << x ; } } int main ( ) { double x = 5 , y = 8 ; printGreater ( x , y ) ; return 0 ; }
n | CPP program to find the n - th term in series ; Function to find nth term ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { return n * ( n + 1 ) * ( 6 * n * n * n + 9 * n * n + n - 1 ) / 30 ; } int main ( ) { int n = 4 ; cout << sumOfSeries ( n ) ; return 0 ; }
Product of first N factorials | CPP Program to find the product of first N factorials ; To compute ( a * b ) % MOD ; long long int res = 0 ; Initialize result ; If b is odd , add ' a ' to result ; Multiply ' a ' with 2 ; Divide b by 2 ; Return result ; This function computes factorials and product by using above function i . e . modular multiplication ; Initialize product and fact with 1 ; ith factorial ; product of first i factorials ; If at any iteration , product becomes divisible by MOD , simply return 0 ; ; Driver Code to Test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int mulmod ( long long int a , long long int b , long long int mod ) { a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } long long int findProduct ( long long int N ) { long long int product = 1 , fact = 1 ; long long int MOD = 1e9 + 7 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = mulmod ( fact , i , MOD ) ; product = mulmod ( product , fact , MOD ) ; if ( product == 0 ) return 0 ; } return product ; } int main ( ) { long long int N = 3 ; cout << findProduct ( N ) << endl ; N = 5 ; cout << findProduct ( N ) << endl ; return 0 ; }
Check if sum of divisors of two numbers are same | C ++ program to find if two numbers are equivalent or not ; Function to calculate sum of all proper divisors num -- > given natural number ; To store sum of divisors ; Find all divisors and add them ; Function to check if both numbers are equivalent or not ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int n ) { long long int sum = 1 ; for ( long long int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) sum = sum + i + n / i ; return sum ; } bool areEquivalent ( int num1 , int num2 ) { return divSum ( num1 ) == divSum ( num2 ) ; } int main ( ) { int num1 = 559 , num2 = 703 ; areEquivalent ( num1 , num2 ) ? cout << " Equivalent " : cout << " Not ▁ Equivalent " ; return 0 ; }
Dodecahedral number | C ++ program to find nth dodecahedral number ; Function to find dodecahedral number ; Formula to calculate nth dodecahedral number and return it into main function . ; Driver Code ; print result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dodecahedral_num ( int n ) { return n * ( 3 * n - 1 ) * ( 3 * n - 2 ) / 2 ; } int main ( ) { int n = 5 ; cout << n << " th ▁ Dodecahedral ▁ number : ▁ " ; cout << dodecahedral_num ( n ) ; return 0 ; }
Count of divisors having more set bits than quotient on dividing N | C ++ Program to find number of Divisors which on integer division produce quotient having less set bit than divisor ; Return the count of set bit . ; check if q and d have same number of set bit . ; Binary Search to find the point at which number of set in q is less than or equal to d . ; while left index is less than right index ; finding the middle . ; check if q and d have same number of set it or not . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bit ( int x ) { int ans = 0 ; while ( x ) { x /= 2 ; ans ++ ; } return ans ; } bool check ( int d , int x ) { if ( bit ( x / d ) <= bit ( d ) ) return true ; return false ; } int bs ( int n ) { int l = 1 , r = sqrt ( n ) ; while ( l < r ) { int m = ( l + r ) / 2 ; if ( check ( m , n ) ) r = m ; else l = m + 1 ; } if ( ! check ( l , n ) ) return l + 1 ; else return l ; } int countDivisor ( int n ) { return n - bs ( n ) + 1 ; } int main ( ) { int n = 5 ; cout << countDivisor ( n ) << endl ; return 0 ; }
Check if two people starting from different points ever meet | C ++ program to find if two people starting from different positions ever meet or not . ; If speed of a person at a position before other person is smaller , then return false . ; Making sure that x1 is greater ; Until one person crosses other ; first person taking one jump in each iteration ; second person taking one jump in each iteration ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool everMeet ( int x1 , int x2 , int v1 , int v2 ) { if ( x1 < x2 && v1 <= v2 ) return false ; if ( x1 > x2 && v1 >= v2 ) return false ; if ( x1 < x2 ) { swap ( x1 , x2 ) ; swap ( v1 , v2 ) ; } while ( x1 >= x2 ) { if ( x1 == x2 ) return true ; x1 = x1 + v1 ; x2 = x2 + v2 ; } return false ; } int main ( ) { int x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ; if ( everMeet ( x1 , x2 , v1 , v2 ) ) printf ( " Yes " ) ; else printf ( " No " ) ; return 0 ; }
Divisibility by 3 where each digit is the sum of all prefix digits modulo 10 | CPP code to check divisibility by 3 ; Function to check the divisibility ; Cycle ; no of residual terms ; sum of residual terms ; if no of residue term = 0 ; if no of residue term = 1 ; if no of residue term = 2 ; if no of residue term = 3 ; sum of all digits ; divisibility check ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string check ( long int k , int d0 , int d1 ) { long int s = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 + ( 8 * ( d0 + d1 ) ) % 10 + ( 6 * ( d0 + d1 ) ) % 10 ; int a = ( k - 3 ) % 4 ; int x ; switch ( a ) { case 0 : x = 0 ; break ; case 1 : x = ( 2 * ( d0 + d1 ) ) % 10 ; break ; case 2 : x = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 ; break ; case 3 : x = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 + ( 8 * ( d0 + d1 ) ) % 10 ; break ; } long int sum = d0 + d1 + ( ( k - 3 ) / 4 ) * s + x ; if ( sum % 3 == 0 ) return " YES " ; return " NO " ; } int main ( ) { long int k , d0 , d1 ; k = 13 ; d0 = 8 ; d1 = 1 ; cout << check ( k , d0 , d1 ) << endl ; k = 5 ; d0 = 3 ; d1 = 4 ; cout << check ( k , d0 , d1 ) << endl ; return 0 ; }
Find ceil of a / b without using ceil ( ) function | C ++ program to find ceil ( a / b ) without using ceil ( ) function ; Driver function ; taking input 1 ; example of perfect division taking input 2
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int main ( ) { int a = 4 ; int b = 3 ; int val = ( a / b ) + ( ( a % b ) != 0 ) ; cout << " The ▁ ceiling ▁ value ▁ of ▁ 4/3 ▁ is ▁ " << val << endl ; a = 6 ; b = 3 ; val = ( a / b ) + ( ( a % b ) != 0 ) ; cout << " The ▁ ceiling ▁ value ▁ of ▁ 6/3 ▁ is ▁ " << val << endl ; return 0 ; }
Program to print Collatz Sequence | CPP program to print Collatz sequence ; We simply follow steps while we do not reach 1 ; If n is odd ; If even ; Print 1 at the end ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCollatz ( int n ) { while ( n != 1 ) { cout << n << " ▁ " ; if ( n & 1 ) n = 3 * n + 1 ; else n = n / 2 ; } cout << n ; } int main ( ) { printCollatz ( 6 ) ; return 0 ; }
Powers of 2 to required sum | CPP program to find the blocks for given number . ; Converting the decimal number into its binary equivalent . ; Displaying the output when the bit is '1' in binary equivalent of number . ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void block ( long int x ) { vector < long int > v ; cout << " Blocks ▁ for ▁ " << x << " ▁ : ▁ " ; while ( x > 0 ) { v . push_back ( x % 2 ) ; x = x / 2 ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] == 1 ) { cout << i ; if ( i != v . size ( ) - 1 ) cout << " , ▁ " ; } } cout << endl ; } int main ( ) { block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ; return 0 ; }
Given a number N in decimal base , find number of its digits in any base ( base b ) | C ++ program to Find Number of digits in base b . ; function to print number of digits ; Calculating log using base changing property and then taking it floor and then adding 1. ; printing output ; Driver method ; taking inputs ; calling the method
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void findNumberOfDigits ( long n , int base ) { int dig = ( int ) ( floor ( log ( n ) / log ( base ) ) + 1 ) ; cout << " The ▁ Number ▁ of ▁ digits ▁ of ▁ " << " Number ▁ " << n << " ▁ in ▁ base ▁ " << base << " ▁ is ▁ " << dig ; } int main ( ) { long n = 1446 ; int base = 7 ; findNumberOfDigits ( n , base ) ; return 0 ; }
Nesbitt 's Inequality | C ++ code to verify Nesbitt 's Inequality ; 3 parts of the inequality sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValidNesbitt ( double a , double b , double c ) { double A = a / ( b + c ) ; double B = b / ( a + c ) ; double C = c / ( a + b ) ; double inequality = A + B + C ; return ( inequality >= 1.5 ) ; } int main ( ) { double a = 1.0 , b = 2.0 , c = 3.0 ; if ( isValidNesbitt ( a , b , c ) ) cout << " Nesbitt ' s ▁ inequality ▁ satisfied . " << " for ▁ real ▁ numbers ▁ " << a << " , ▁ " << b << " , ▁ " << c << " STRNEWLINE " ; else cout << " Not ▁ satisfied " ; return 0 ; }
Ordered Prime Signature | CPP to find total number of divisors of a number , using ordered prime signature ; Finding primes upto entered number ; Finding primes by Sieve of Eratosthenes method ; If prime [ i ] is not changed , then it is prime ; Update all multiples of p ; Forming array of the prime numbers found ; Finding ordered prime signature of the number ; Map to store prime factors and the related exponents ; Declaring an iterator for map ; Finding prime factorization of the number ; Storing the prime factor and its exponent in map ; Storing the exponent in a vector ; Sorting the stored exponents ; Printing the prime signature ; Finding total number of divisors of the number ; Adding one to each element present ; in ordered prime signature ; Multiplying the elements ; Driver Method
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > primes ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } vector < int > arr ; for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] ) arr . push_back ( i ) ; } return arr ; } vector < int > signature ( int n ) { vector < int > r = primes ( n ) ; map < int , int > factor ; map < int , int > :: iterator it ; vector < int > sort_exp ; int k , t = n ; it = factor . begin ( ) ; for ( int i = 0 ; i < r . size ( ) ; i ++ ) { if ( n % r [ i ] == 0 ) { k = 0 ; while ( n % r [ i ] == 0 ) { n = n / r [ i ] ; k ++ ; } factor . insert ( it , pair < int , int > ( r [ i ] , k ) ) ; sort_exp . push_back ( k ) ; } } sort ( sort_exp . begin ( ) , sort_exp . end ( ) ) ; cout << " ▁ The ▁ Ordered ▁ Prime ▁ Signature ▁ of ▁ " << t << " ▁ is ▁ : ▁ STRNEWLINE { ▁ " ; for ( int i = 0 ; i < sort_exp . size ( ) ; i ++ ) { if ( i != sort_exp . size ( ) - 1 ) cout << sort_exp [ i ] << " , ▁ " ; else cout << sort_exp [ i ] << " ▁ } STRNEWLINE " ; } return sort_exp ; } void divisors ( int n ) { int f = 1 , l ; vector < int > div = signature ( n ) ; l = div . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { div [ i ] += 1 ; f *= div [ i ] ; } cout << " The ▁ total ▁ number ▁ of ▁ divisors ▁ of ▁ " << n << " ▁ is ▁ " << f << " STRNEWLINE " ; } int main ( ) { int n = 13 ; divisors ( n ) ; return 0 ; }
Cube Free Numbers smaller than n | Efficient C ++ Program to print all cube free numbers smaller than or equal to n . ; Initialize all numbers as not cube free ; Traverse through all possible cube roots ; If i itself is cube free ; Mark all multiples of i as not cube free ; Print all cube free numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCubeFree ( int n ) { bool cubFree [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) cubFree [ i ] = true ; for ( int i = 2 ; i * i * i <= n ; i ++ ) { if ( cubFree [ i ] ) { for ( int multiple = 1 ; i * i * i * multiple <= n ; multiple ++ ) { cubFree [ i * i * i * multiple ] = false ; } } } for ( int i = 2 ; i <= n ; i ++ ) { if ( cubFree [ i ] == true ) cout << i << " ▁ " ; } } int main ( ) { printCubeFree ( 20 ) ; return 0 ; }
Heap Sort for decreasing order using min heap | C ++ program for implementation of Heap Sort ; To heapify a subtree rooted with node i which is an index in arr [ ] . n is size of heap ; Initialize smalles as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; If left child is smaller than root ; If right child is smaller than smallest so far ; If smallest is not root ; Recursively heapify the affected sub - tree ; main function to do heap sort ; Build heap ( rearrange array ) ; One by one extract an element from heap ; Move current root to end ; call max heapify on the reduced heap ; A utility function to print array of size n ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void heapify ( int arr [ ] , int n , int i ) { int smallest = i ; int l = 2 * i + 1 ; int r = 2 * i + 2 ; if ( l < n && arr [ l ] < arr [ smallest ] ) smallest = l ; if ( r < n && arr [ r ] < arr [ smallest ] ) smallest = r ; if ( smallest != i ) { swap ( arr [ i ] , arr [ smallest ] ) ; heapify ( arr , n , smallest ) ; } } void heapSort ( int arr [ ] , int n ) { for ( int i = n / 2 - 1 ; i >= 0 ; i -- ) heapify ( arr , n , i ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { swap ( arr [ 0 ] , arr [ i ] ) ; heapify ( arr , i , 0 ) ; } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 4 , 6 , 3 , 2 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; heapSort ( arr , n ) ; cout << " Sorted ▁ array ▁ is ▁ STRNEWLINE " ; printArray ( arr , n ) ; }
Squared triangular number ( Sum of cubes ) | C ++ program to check if a given number is sum of cubes of natural numbers . ; Returns root of n ( n + 1 ) / 2 = num if num is triangular ( or integerroot exists ) . Else returns - 1. ; Considering the equation n * ( n + 1 ) / 2 = num . The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Returns square root of x if it is perfect square . Else returns - 1. ; Find floating point value of square root of x . ; If square root is an integer ; Function to find if the given number is sum of the cubes of first n natural numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isTriangular ( int num ) { if ( num < 0 ) return false ; int c = ( -2 * num ) ; int b = 1 , a = 1 ; int d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return -1 ; float root1 = ( - b + sqrt ( d ) ) / ( 2 * a ) ; float root2 = ( - b - sqrt ( d ) ) / ( 2 * a ) ; if ( root1 > 0 && floor ( root1 ) == root1 ) return root1 ; if ( root2 > 0 && floor ( root2 ) == root2 ) return root2 ; return -1 ; } int isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; if ( ( sr - floor ( sr ) ) == 0 ) return floor ( sr ) ; else return -1 ; } int findS ( int s ) { int sr = isPerfectSquare ( s ) ; if ( sr == -1 ) return -1 ; return isTriangular ( sr ) ; } int main ( ) { int s = 9 ; int n = findS ( s ) ; n == -1 ? cout << " - 1" : cout << n ; return 0 ; }
Smallest even digits number not less than N | CPP program to print the smallest integer not less than N with all even digits ; function to return the answer when the first odd digit is 9 ; traverse towwars the left to find the non - 8 digit ; index digit ; if digit is not 8 , then break ; if on the left side of the '9' , no 8 is found then we return by adding a 2 and 0 's ; till non - 8 digit add all numbers ; if non - 8 is even or odd than add the next even . ; add 0 to right of 9 ; function to return the smallest number with all digits even ; convert the number to string to perform operations ; find out the first odd number ; if no odd numbers are there , than n is the answer ; if the odd number is 9 , than tricky case handles it ; add all digits till first odd ; increase the odd digit by 1 ; add 0 to the right of the odd number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int trickyCase ( string s , int index ) { int index1 = -1 ; for ( int i = index - 1 ; i >= 0 ; i -- ) { int digit = s [ i ] - '0' ; if ( digit != 8 ) { index1 = i ; break ; } } if ( index1 == -1 ) return 2 * pow ( 10 , s . length ( ) ) ; int num = 0 ; for ( int i = 0 ; i < index1 ; i ++ ) num = num * 10 + ( s [ i ] - '0' ) ; if ( s [ index1 ] % 2 == 0 ) num = num * 10 + ( s [ index1 ] - '0' + 2 ) ; else num = num * 10 + ( s [ index1 ] - '0' + 1 ) ; for ( int i = index1 + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 ; return num ; } int smallestNumber ( int n ) { int num = 0 ; string s = " " ; int duplicate = n ; while ( n ) { s = char ( n % 10 + 48 ) + s ; n /= 10 ; } int index = -1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s [ i ] - '0' ; if ( digit & 1 ) { index = i ; break ; } } if ( index == -1 ) return duplicate ; if ( s [ index ] == '9' ) { num = trickyCase ( s , index ) ; return num ; } for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( s [ i ] - '0' ) ; num = num * 10 + ( s [ index ] - '0' + 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 ; return num ; } int main ( ) { int N = 2397 ; cout << smallestNumber ( N ) ; return 0 ; }
n | Simple CPP program to find n - th number with sum of digits as 10. ; Find sum of digits in current no . ; If sum is 10 , we increment count ; If count becomes n , we return current number . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNth ( int n ) { int count = 0 ; for ( int curr = 19 ; ; curr += 9 ) { int sum = 0 ; for ( int x = curr ; x > 0 ; x = x / 10 ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } return -1 ; } int main ( ) { printf ( " % d STRNEWLINE " , findNth ( 5 ) ) ; return 0 ; }
Sum of pairwise products | Simple CPP program to find sum of given series . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int findSum ( int n ) { long long int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) sum = sum + i * j ; return sum ; } int main ( ) { int n = 5 ; cout << findSum ( n ) ; return 0 ; }
Sum of pairwise products | Efficient CPP program to find sum of given series . ; Sum of multiples of 1 is 1 * ( 1 + 2 + . . ) ; Adding sum of multiples of numbers other than 1 , starting from 2. ; Subtract previous number from current multiple . ; For example , for 2 , we get sum as ( 2 + 3 + 4 + ... . ) * 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int findSum ( int n ) { long long int multiTerms = n * ( n + 1 ) / 2 ; long long int sum = multiTerms ; for ( int i = 2 ; i <= n ; i ++ ) { multiTerms = multiTerms - ( i - 1 ) ; sum = sum + multiTerms * i ; } return sum ; } int main ( ) { int n = 5 ; cout << findSum ( n ) ; return 0 ; }
Lemoine 's Conjecture | C ++ code to verify Lemoine 's Conjecture for any odd number >= 7 ; Function to check if a number is prime or not ; Representing n as p + ( 2 * q ) to satisfy lemoine 's conjecture ; Declaring a map to hold pairs ( p , q ) ; Declaring an iterator for map ; Finding various values of p for each q to satisfy n = p + ( 2 * q ) ; After finding a pair that satisfies the equation , check if both p and q are prime or not ; If both p and q are prime , store them in the map ; Displaying all pairs ( p , q ) that satisfy lemoine ' s ▁ conjecture ▁ for ▁ the ▁ number ▁ ' n ' ; Driver Function ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n < 2 ) return false ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } void lemoine ( int n ) { map < int , int > pr ; map < int , int > :: iterator it ; it = pr . begin ( ) ; for ( int q = 1 ; q <= n / 2 ; q ++ ) { int p = n - 2 * q ; if ( isPrime ( p ) && isPrime ( q ) ) pr . insert ( it , pair < int , int > ( p , q ) ) ; } for ( it = pr . begin ( ) ; it != pr . end ( ) ; ++ it ) cout << n << " ▁ = ▁ " << it -> first << " ▁ + ▁ ( 2 ▁ * ▁ " << it -> second << " ) STRNEWLINE " ; } int main ( ) { int n = 39 ; cout << n << " ▁ can ▁ be ▁ expressed ▁ as ▁ " << endl ; lemoine ( n ) ; return 0 ; }