codeparrot/xlcost-text-to-code huggingface dataset:
Program to calculate value of nCr | CPP program To calculate The Value Of nCr ; Returns factorial of n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) ; int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int main ( ) { int n = 5 , r = 3 ; cout << nCr ( n , r ) ; return 0 ; }
Program to print the sum of the given nth term | CPP program to illustrate ... Summation of series ; function to calculate sum of series ; Sum of n terms is n ^ 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int summingSeries ( long n ) { return pow ( n , 2 ) ; } int main ( ) { int n = 100 ; cout << " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " << summingSeries ( n ) << endl ; return 0 ; }
Brahmagupta Fibonacci Identity | CPP code to verify Brahmagupta Fibonacci identity ; represent the product as sum of 2 squares ; check identity criteria ; Driver code ; 1 ^ 2 + 2 ^ 2 ; 3 ^ 2 + 4 ^ 2 ; express product of sum of 2 squares as sum of ( sum of 2 squares )
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_sum_of_two_squares ( int a , int b ) { int ab = a * b ; for ( int i = 0 ; i * i <= ab ; i ++ ) { for ( int j = i ; i * i + j * j <= ab ; j ++ ) { if ( i * i + j * j == ab ) cout << i << " ^ 2 ▁ + ▁ " << j << " ^ 2 ▁ = ▁ " << ab << " STRNEWLINE " ; } } } int main ( ) { int a = 1 * 1 + 2 * 2 ; int b = 3 * 3 + 4 * 4 ; cout << " Representation ▁ of ▁ a ▁ * ▁ b ▁ as ▁ sum " " ▁ of ▁ 2 ▁ squares : STRNEWLINE " ; find_sum_of_two_squares ( a , b ) ; }
Tetrahedral Numbers | CPP Program to find the nth tetrahedral number ; Function to find Tetrahedral Number ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int tetrahedralNumber ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } int main ( ) { int n = 5 ; cout << tetrahedralNumber ( n ) << endl ; return 0 ; }
Euler 's Four Square Identity | CPP code to verify euler 's four square identity ; function to check euler four square identity ; loops checking the sum of squares ; sum of 2 squares ; sum of 3 squares ; sum of 4 squares ; product of 2 numbers represented as sum of four squares i , j , k , l ; product of 2 numbers a and b represented as sum of four squares i , j , k , l ; Driver code ; given numbers can be represented as sum of 4 squares By euler 's four  square identity product also can be  represented as sum of 4 squares
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define show ( x )  cout << #x << " = " << x << " NEW_LINE " void check_euler_four_square_identity ( int a , int b , int ab ) { int s = 0 ; for ( int i = 0 ; i * i <= ab ; i ++ ) { s = i * i ; for ( int j = i ; j * j <= ab ; j ++ ) { s = j * j + i * i ; for ( int k = j ; k * k <= ab ; k ++ ) { s = k * k + j * j + i * i ; for ( int l = k ; l * l <= ab ; l ++ ) { s = l * l + k * k + j * j + i * i ; if ( s == ab ) { show ( i ) ; show ( j ) ; show ( k ) ; show ( l ) ; cout << " " << " Product ▁ of ▁ " << a << " ▁ and ▁ " << b ; cout << " ▁ can ▁ be ▁ written " << " ▁ as ▁ sum ▁ of ▁ squares ▁ of ▁ i , ▁ " << " j , ▁ k , ▁ l STRNEWLINE " ; cout << ab << " ▁ = ▁ " ; cout << i << " * " << i << " ▁ + ▁ " ; cout << j << " * " << j << " ▁ + ▁ " ; cout << k << " * " << k << " ▁ + ▁ " ; cout << l << " * " << l << " STRNEWLINE " ; cout << " STRNEWLINE " ; } } } } } } int main ( ) { int ab = a * b ; check_euler_four_square_identity ( a , b , ab ) ; return 0 ; }
Number of solutions to Modular Equations | C ++ Program to find number of possible values of X to satisfy A mod X = B ; Returns the number of divisors of ( A - B ) greater than B ; if N is divisible by i ; count only the divisors greater than B ; checking if a divisor isnt counted twice ; Utility function to calculate number of all possible values of X for which the modular equation holds true ; if A = B there are infinitely many solutions to equation or we say X can take infinitely many values > A . We return - 1 in this case ; if A < B , there are no possible values of X satisfying the equation ; the last case is when A > B , here we calculate the number of divisors of ( A - B ) , which are greater than B ; Wrapper function for numberOfPossibleWaysUtil ( ) ; if infinitely many solutions available ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateDivisors ( int A , int B ) { int N = ( A - B ) ; int noOfDivisors = 0 ; for ( int i = 1 ; i <= sqrt ( N ) ; i ++ ) { if ( ( N % i ) == 0 ) { if ( i > B ) noOfDivisors ++ ; if ( ( N / i ) != i && ( N / i ) > B ) noOfDivisors ++ ; } } return noOfDivisors ; } int numberOfPossibleWaysUtil ( int A , int B ) { if ( A == B ) return -1 ; if ( A < B ) return 0 ; int noOfDivisors = 0 ; noOfDivisors = calculateDivisors ( A , B ) ; return noOfDivisors ; } void numberOfPossibleWays ( int A , int B ) { int noOfSolutions = numberOfPossibleWaysUtil ( A , B ) ; if ( noOfSolutions == -1 ) { cout << " For ▁ A ▁ = ▁ " << A << " ▁ and ▁ B ▁ = ▁ " << B << " , ▁ X ▁ can ▁ take ▁ Infinitely ▁ many ▁ values " " ▁ greater ▁ than ▁ " << A << " STRNEWLINE " ; } else { cout << " For ▁ A ▁ = ▁ " << A << " ▁ and ▁ B ▁ = ▁ " << B << " , ▁ X ▁ can ▁ take ▁ " << noOfSolutions << " ▁ values STRNEWLINE " ; } } int main ( ) { int A = 26 , B = 2 ; numberOfPossibleWays ( A , B ) ; A = 21 , B = 5 ; numberOfPossibleWays ( A , B ) ; return 0 ; }
Perfect power ( 1 , 4 , 8 , 9 , 16 , 25 , 27 , ... ) | C ++ program to count number of numbers from 1 to n are of type x ^ y where x > 0 and y > 1 ; Function that keeps all the odd power numbers upto n ; We need exclude perfect squares . ; sort the vector ; Return sum of odd and even powers . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE int powerNumbers ( int n ) { vector < int > v ; for ( ll i = 2 ; i * i * i <= n ; i ++ ) { ll j = i * i ; while ( j * i <= n ) { j *= i ; ll s = sqrt ( j ) ; if ( s * s != j ) v . push_back ( j ) ; } } sort ( v . begin ( ) , v . end ( ) ) ; v . erase ( unique ( v . begin ( ) , v . end ( ) ) , v . end ( ) ) ; return v . size ( ) + ( ll ) sqrt ( n ) ; } int main ( ) { cout << powerNumbers ( 50 ) ; return 0 ; }
Variance and standard | CPP program to find mean and variance of a matrix . ; variance function declaration ; Function for calculating mean ; Calculating sum ; Returning mean ; Function for calculating variance ; subtracting mean from elements ; a [ i ] [ j ] = fabs ( a [ i ] [ j ] ) ; squaring each terms ; taking sum ; driver program ; declaring and initializing matrix ; for mean ; for variance ; for standard deviation ; displaying variance and deviation
#include <bits/stdc++.h> NEW_LINE using namespace std ; int variance ( int , int , int ) ; int mean ( int a [ ] [ 3 ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += a [ i ] [ j ] ; return sum / ( n * n ) ; } int variance ( int a [ ] [ 3 ] , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { a [ i ] [ j ] -= m ; a [ i ] [ j ] *= a [ i ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += a [ i ] [ j ] ; return sum / ( n * n ) ; } int main ( ) { int mat [ 3 ] [ 3 ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int m = mean ( mat , 3 ) ; int var = variance ( mat , 3 , m ) ; int dev = sqrt ( var ) ; cout << " Mean : ▁ " << m << " STRNEWLINE " << " Variance : ▁ " << var << " STRNEWLINE " << " Deviation : ▁ " << dev << " STRNEWLINE " ; return 0 ; }
Find N Arithmetic Means between A and B | C ++ program to find n arithmetic means between A and B ; Prints N arithmetic means between A and B . ; calculate common difference ( d ) ; for finding N the arithmetic mean between A and B ; Driver code to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAMeans ( int A , int B , int N ) { float d = ( float ) ( B - A ) / ( N + 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) cout << ( A + i * d ) << " ▁ " ; } int main ( ) { int A = 20 , B = 32 , N = 5 ; printAMeans ( A , B , N ) ; return 0 ; }
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | Efficient CPP program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; function to calculate sum of series ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofseries ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 4 ) ; } int main ( ) { cout << sumofseries ( 3 ) << endl ; return 0 ; }
Largest number in [ 2 , 3 , . . n ] which is co |  ; Returns true if i is co - prime with numbers in set [ 2 , 3 , ... m ] ; Running the loop till square root of n to reduce the time complexity from n ; Find the minimum of square root of n and m to run the loop until the smaller one ; Check from 2 to min ( m , sqrt ( n ) ) ; Function to find the largest number less than n which is Co - prime with all numbers from 2 to m ; Iterating from n to m + 1 to find the number ; checking every number for the given conditions ; The first number which satisfy the conditions is the answer ; If there is no number which satisfy the conditions , then print number does not exist . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( long long int i , long long int m ) { long long int sq_i = sqrt ( i ) ; long long int sq = min ( m , sq_i ) ; for ( long long int j = 2 ; j <= sq ; j ++ ) if ( i % j == 0 ) return false ; return true ; } void findLargestNum ( long long int n , long long int m ) { for ( long long int i = n ; i > m ; i -- ) { if ( isValid ( i , m ) ) { cout << i << ' ' ; return ; } } cout << " Number ▁ Doesn ' t ▁ Exists STRNEWLINE " ; } int main ( ) { long long int n = 16 , m = 3 ; findLargestNum ( n , m ) ; return 0 ; }
Check whether a given matrix is orthogonal or not | C ++ code to check whether a matrix is orthogonal or not ; Function to check orthogonalilty ; Find transpose ; Find product of a [ ] [ ] and its transpose ; Since we are multiplying with transpose of itself . We use ; Check if product is identity matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE bool isOrthogonal ( int a [ ] [ MAX ] , int m , int n ) { if ( m != n ) return false ; int trans [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) trans [ i ] [ j ] = a [ j ] [ i ] ; int prod [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int sum = 0 ; for ( int k = 0 ; k < n ; k ++ ) { sum = sum + ( a [ i ] [ k ] * a [ j ] [ k ] ) ; } prod [ i ] [ j ] = sum ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j && prod [ i ] [ j ] != 0 ) return false ; if ( i == j && prod [ i ] [ j ] != 1 ) return false ; } } return true ; } int main ( ) { int a [ ] [ MAX ] = { { 1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } } ; if ( isOrthogonal ( a , 3 , 3 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if given number is perfect square | CPP program to find if x is a perfect square . ; Find floating point value of square root of x . ; if product of square root is equal , then return T / F ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( long double x ) { if ( x >= 0 ) { long long sr = sqrt ( x ) ; return ( sr * sr == x ) ; } return false ; } int main ( ) { long long x = 2502 ; if ( isPerfectSquare ( x ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Program to print GP ( Geometric Progression ) | CPP program to print GP . ; function to print GP ; Driver code ; starting number ; Common ratio ; N th term to be find
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printGP ( int a , int r , int n ) { int curr_term ; for ( int i = 0 ; i < n ; i ++ ) { curr_term = a * pow ( r , i ) ; cout << curr_term << " ▁ " ; } } int main ( ) { int a = 2 ; int r = 3 ; int n = 5 ; printGP ( a , r , n ) ; return 0 ; }
HCF of array of fractions ( or rational numbers ) | CPP program to find HCF of array of rational numbers ( fractions ) . ; hcf of two number ; find hcf of numerator series ; return hcf of numerator ; find lcm of denominator series ; ans contains LCM of arr [ 0 ] [ 1 ] , . . arr [ i ] [ 1 ] ; return lcm of denominator ; Core Function ; found hcf of numerator ; found lcm of denominator ; return result ; Main function ; Initialize the every row with size 2 ( 1 for numerator and 2 for denominator ) ; function for calculate the result ; print the result
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a % b == 0 ) return b ; else return ( gcd ( b , a % b ) ) ; } int findHcf ( int * * arr , int size ) { int ans = arr [ 0 ] [ 0 ] ; for ( int i = 1 ; i < size ; i ++ ) ans = gcd ( ans , arr [ i ] [ 0 ] ) ; return ( ans ) ; } int findLcm ( int * * arr , int size ) { int ans = arr [ 0 ] [ 1 ] ; for ( int i = 1 ; i < size ; i ++ ) ans = ( ( ( arr [ i ] [ 1 ] * ans ) ) / ( gcd ( arr [ i ] [ 1 ] , ans ) ) ) ; return ( ans ) ; } int * hcfOfFraction ( int * * arr , int size ) { int hcf_of_num = findHcf ( arr , size ) ; int lcm_of_deno = findLcm ( arr , size ) ; int * result = new int [ 2 ] ; result [ 0 ] = hcf_of_num ; result [ 1 ] = lcm_of_deno ; for ( int i = result [ 0 ] / 2 ; i > 1 ; i -- ) { if ( ( result [ 1 ] % i == 0 ) && ( result [ 0 ] % i == 0 ) ) { result [ 1 ] /= i ; result [ 0 ] /= i ; } } return ( result ) ; } int main ( ) { int size = 4 ; int * * arr = new int * [ size ] ; for ( int i = 0 ; i < size ; i ++ ) arr [ i ] = new int [ 2 ] ; arr [ 0 ] [ 0 ] = 9 ; arr [ 0 ] [ 1 ] = 10 ; arr [ 1 ] [ 0 ] = 12 ; arr [ 1 ] [ 1 ] = 25 ; arr [ 2 ] [ 0 ] = 18 ; arr [ 2 ] [ 1 ] = 35 ; arr [ 3 ] [ 0 ] = 21 ; arr [ 3 ] [ 1 ] = 40 ; int * result = hcfOfFraction ( arr , size ) ; cout << result [ 0 ] << " , ▁ " << result [ 1 ] << endl ; return 0 ; }
Space efficient iterative method to Fibonacci number | C ++ code to find nth fibonacci ; get second MSB ; consectutively set all the bits ; returns the second MSB ; Multiply function ; Function to calculate F [ ] [ ] raise to the power n ; Base case ; take 2D array to store number 's ; run loop till MSB > 0 ; To return fibonacci number ; Driver Code ; Given n
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMSB ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( ( n + 1 ) >> 2 ) ; } void multiply ( int F [ 2 ] [ 2 ] , int M [ 2 ] [ 2 ] ) { int x = F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ; int y = F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ; int z = F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ; int w = F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ; F [ 0 ] [ 0 ] = x ; F [ 0 ] [ 1 ] = y ; F [ 1 ] [ 0 ] = z ; F [ 1 ] [ 1 ] = w ; } void power ( int F [ 2 ] [ 2 ] , int n ) { if ( n == 0 n == 1 ) return ; int M [ 2 ] [ 2 ] = { 1 , 1 , 1 , 0 } ; for ( int m = getMSB ( n ) ; m ; m = m >> 1 ) { multiply ( F , F ) ; if ( n & m ) { multiply ( F , M ) ; } } } int fib ( int n ) { int F [ 2 ] [ 2 ] = { { 1 , 1 } , { 1 , 0 } } ; if ( n == 0 ) return 0 ; power ( F , n - 1 ) ; return F [ 0 ] [ 0 ] ; } int main ( ) { int n = 6 ; cout << fib ( n ) << " ▁ " ; return 0 ; }
Stern | CPP program to print Brocot Sequence ; loop to create sequence ; adding sum of considered element and it 's precedent ; adding next considered element ; printing sequence . . ; Driver code ; adding first two element in the sequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SternSequenceFunc ( vector < int > & BrocotSequence , int n ) { for ( int i = 1 ; BrocotSequence . size ( ) < n ; i ++ ) { int considered_element = BrocotSequence [ i ] ; int precedent = BrocotSequence [ i - 1 ] ; BrocotSequence . push_back ( considered_element + precedent ) ; BrocotSequence . push_back ( considered_element ) ; } for ( int i = 0 ; i < 15 ; ++ i ) cout << BrocotSequence [ i ] << " ▁ " ; } int main ( ) { int n = 15 ; vector < int > BrocotSequence ; BrocotSequence . push_back ( 1 ) ; BrocotSequence . push_back ( 1 ) ; SternSequenceFunc ( BrocotSequence , n ) ; return 0 ; }
Counting numbers whose difference from reverse is a product of k | C ++ program to Count the numbers within a given range in which when you subtract a number from its reverse , the difference is a product of k ; function to check if the number and its reverse have their absolute difference divisible by k ; reverse the number ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isRevDiffDivisible ( int x , int k ) { int n = x ; int m = 0 ; int flag ; while ( x > 0 ) { m = m * 10 + x % 10 ; x /= 10 ; } return ( abs ( n - m ) % k == 0 ) ; } int countNumbers ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) if ( isRevDiffDivisible ( i , k ) ) ++ count ; return count ; } int main ( ) { int l = 20 , r = 23 , k = 6 ; cout << countNumbers ( l , r , k ) << endl ; return 0 ; }
Check for Amicable Pair | CPP program to check if two numbers are Amicable or not . ; Function to calculate sum of all proper divisors of a given number ; Sum of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add both ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Returns true if x and y are Amicable else false . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + 1 ) ; } bool areAmicable ( int x , int y ) { if ( divSum ( x ) != y ) return false ; return ( divSum ( y ) == x ) ; } int main ( ) { int x = 220 , y = 284 ; if ( areAmicable ( x , y ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Program to print non square numbers | CPP program to print first n non - square numbers . ; Function to check perfect square ; function to print all non square number ; variable which stores the count ; not perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int n ) { if ( n < 0 ) return false ; int root = round ( sqrt ( n ) ) ) ; return n == root * root ; } void printnonsquare ( int n ) { int count = 0 ; for ( int i = 1 ; count < n ; ++ i ) { if ( ! isPerfectSquare ( i ) ) { cout << i << " ▁ " ; count ++ ; } } } int main ( ) { int n = 10 ; printnonsquare ( n ) ; return 0 ; }
Program to print non square numbers | CPP program to print first n non square number ; Returns n - th non - square number . ; loop to print non squares below n ; Driver code
#include <bits/stdc++.h> NEW_LINE int nonsquare ( int n ) { return n + ( int ) ( 0.5 + sqrt ( n ) ) ; } void printNonSquare ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) printf ( " % d ▁ " , nonsquare ( i ) ) ; } int main ( ) { int n = 10 ; printNonSquare ( n ) ; return 0 ; }
Ludic Numbers | C ++ code to print Lucid number smaller than or equal to n . ; Returns a list containing all Ludic numbers smaller than or equal to n . ; ludics list contain all the ludic numbers ; Here we have to start with index 1 and will remove nothing from the list ; Here first item should be included in the list and the deletion is referred by this first item in the loop . ; Remove_index variable is used to store the next index which we want to delete ; Removing the next item ; Remove_index is updated so that we get the next index for deletion ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > getLudic ( int n ) { vector < int > ludics ; for ( int i = 1 ; i <= n ; i ++ ) ludics . push_back ( i ) ; for ( int index = 1 ; index < ludics . size ( ) ; index ++ ) { int first_ludic = ludics [ index ] ; int remove_index = index + first_ludic ; while ( remove_index < ludics . size ( ) ) { auto it = ludics . begin ( ) ; it = it + remove_index ; ludics . erase ( it ) ; remove_index = remove_index + first_ludic - 1 ; } } return ludics ; } int main ( ) { int n = 25 ; vector < int > ans = getLudic ( n ) ; cout << " [ " ; for ( int i = 0 ; i < ans . size ( ) - 1 ; i ++ ) { cout << ans [ i ] << " , ▁ " ; } cout << ans [ ans . size ( ) - 1 ] << " ] " ; return 0 ; }
Prime Triplet | C ++ program to find prime triplets smaller than or equal to n . ; function to detect prime number here we have used sieve method https : www . geeksforgeeks . org / sieve - of - eratosthenes / to detect prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; function to print prime triplets ; Finding all primes from 1 to n ; triplets of form ( p , p + 2 , p + 6 ) ; triplets of form ( p , p + 4 , p + 6 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sieve ( int n , bool prime [ ] ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } void printPrimeTriplets ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; sieve ( n , prime ) ; cout << " The ▁ prime ▁ triplets ▁ from ▁ 1 ▁ to ▁ " << n << " are ▁ : " << endl ; for ( int i = 2 ; i <= n - 6 ; ++ i ) { if ( prime [ i ] && prime [ i + 2 ] && prime [ i + 6 ] ) cout << i << " ▁ " << ( i + 2 ) << " ▁ " << ( i + 6 ) << endl ; else if ( prime [ i ] && prime [ i + 4 ] && prime [ i + 6 ] ) cout << i << " ▁ " << ( i + 4 ) << " ▁ " << ( i + 6 ) << endl ; } } int main ( ) { int n = 25 ; printPrimeTriplets ( n ) ; return 0 ; }
Program to compare two fractions | CPP program to find max between two Rational numbers ; Get max of the two fractions ; Declare nume1 and nume2 for get the value of first numerator and second numerator ; Compute ad - bc ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Fraction { int num , den ; } ; Fraction maxFraction ( Fraction first , Fraction sec ) { int a = first . num ; int b = first . den ; int c = sec . num ; int d = sec . den ; int Y = a * d - b * c ; return ( Y > 0 ) ? first : sec ; } int main ( ) { Fraction first = { 3 , 2 } ; Fraction sec = { 3 , 4 } ; Fraction res = maxFraction ( first , sec ) ; cout << res . num << " / " << res . den ; return 0 ; }
Find the nearest odd and even perfect squares of odd and even array elements respectively | C ++ program to implement the above approach ; Function to find the nearest even and odd perfect squares for even and odd array elements ; Traverse the array ; Calculate square root of current array element ; If both are of same parity ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nearestPerfectSquare ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int sr = sqrt ( arr [ i ] ) ; if ( ( sr & 1 ) == ( arr [ i ] & 1 ) ) cout << sr * sr << " ▁ " ; else { sr ++ ; cout << sr * sr << " ▁ " ; } } } int main ( ) { int arr [ ] = { 6 , 3 , 2 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; nearestPerfectSquare ( arr , N ) ; return 0 ; }
Program to check if N is a Pentagonal Number | C ++ program to check pentagonal numbers . ; Function to determine if N is pentagonal or not . ; Substitute values of i in the formula . ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool isPentagonal ( int N ) { int i = 1 , M ; do { M = ( 3 * i * i - i ) / 2 ; i += 1 ; } while ( M < N ) ; return ( M == N ) ; } int main ( ) { int N = 12 ; if ( isPentagonal ( N ) ) cout << N << " ▁ is ▁ pentagonal ▁ " << endl ; else cout << N << " ▁ is ▁ not ▁ pentagonal " << endl ; return 0 ; }
Sum of fourth powers of the first n natural numbers | CPP Program to find the sum of forth powers of first n natural numbers ; Return the sum of forth power of first n natural numbers ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int fourthPowerSum ( int n ) { long long int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + ( i * i * i * i ) ; return sum ; } int main ( ) { int n = 6 ; cout << fourthPowerSum ( n ) << endl ; return 0 ; }