codeparrot/xlcost-text-to-code huggingface dataset:
Find four factors of N with maximum product and sum equal to N | Set 3 | C ++ implementation of above approach ; Function to find primes ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find factors ; run a loop upto square root of that number ; if the n is perfect square ; otherwise push it 's two divisors ; sort the divisors ; Function to find max product ; To store factors of ' n ' ; find factors ; if it is divisible by 4. ; if it is prime ; otherwise answer will be possible ; include last third factor ; nested loop to find other two factors ; Driver code ; function call
#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 ; } void factors ( int N , vector < int > & v [ ] ) { for ( int i = 2 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( i / j == j ) v [ i ] . push_back ( j ) ; else { v [ i ] . push_back ( j ) ; v [ i ] . push_back ( i / j ) ; } } } sort ( v [ i ] . begin ( ) , v [ i ] . end ( ) ) ; } } int product ( int n ) { vector < int > v [ n + 100 ] ; factors ( n + 100 , v ) ; if ( n % 4 == 0 ) { int x = n / 4 ; x *= x ; return x * x ; } else { if ( isPrime [ n ] ) return -1 ; else { int ans = -1 ; if ( v [ n ] . size ( ) > 2 ) { int fac = v [ n ] [ v [ n ] . size ( ) - 3 ] ; for ( int i = v [ n ] . size ( ) - 1 ; i >= 0 ; i -- ) { for ( int j = v [ n ] . size ( ) - 1 ; j >= 0 ; j -- ) { if ( ( fac * 2 ) + ( v [ n ] [ j ] + v [ n ] [ i ] ) == n ) ans = max ( ans , fac * fac * v [ n ] [ j ] * v [ n ] [ i ] ) ; } } return ans ; } } } } int main ( ) { int n = 24 ; cout << product ( n ) ; return 0 ; }
Ratio of mth and nth terms of an A . P . with given ratio of sums | C ++ code to calculate ratio ; function to calculate ratio of mth and nth term ; ratio will be tm / tn = ( 2 * m - 1 ) / ( 2 * n - 1 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float CalculateRatio ( float m , float n ) { return ( 2 * m - 1 ) / ( 2 * n - 1 ) ; } int main ( ) { float m = 6 , n = 2 ; cout << CalculateRatio ( m , n ) ; return 0 ; }
Find the sum of n terms of the series 1 , 8 , 27 , 64 ... . | C ++ program to find the sum of n terms ; Function to calculate the sum ; Return total sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSum ( int n ) { return pow ( n * ( n + 1 ) / 2 , 2 ) ; } int main ( ) { int n = 4 ; cout << calculateSum ( n ) ; return 0 ; }
Sum of Digits in a ^ n till a single digit | CPP program to find single digit sum of a ^ n . ; This function finds single digit sum of n . ; Returns single digit sum of a ^ n . We use modular exponentiation technique . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } int powerDigitSum ( int a , int n ) { int res = 1 ; while ( n ) { if ( n % 2 == 1 ) { res = res * digSum ( a ) ; res = digSum ( res ) ; } a = digSum ( digSum ( a ) * digSum ( a ) ) ; n /= 2 ; } return res ; } int main ( ) { int a = 9 , n = 4 ; cout << powerDigitSum ( a , n ) ; return 0 ; }
Program to find total number of edges in a Complete Graph | C ++ implementation to find the number of edges in a complete graph ; Function to find the total number of edges in a complete graph with N vertices ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totEdge ( int n ) { int result = 0 ; result = ( n * ( n - 1 ) ) / 2 ; return result ; } int main ( ) { int n = 6 ; cout << totEdge ( n ) ; return 0 ; }
Find minimum number of Log value needed to calculate Log upto N | C ++ program to find number of log values needed to calculate all the log values from 1 to N ; In this vector prime [ i ] will store true if prime [ i ] is prime , else store false ; Using sieve of Eratosthenes to find all prime upto N ; Function to find number of log values needed to calculate all the log values from 1 to N ; calculate primes upto N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000005 NEW_LINE vector < bool > prime ( MAX , true ) ; void sieve ( int N ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i <= N ; i ++ ) { if ( prime [ i ] ) { for ( int j = 2 ; i * j <= N ; j ++ ) prime [ i * j ] = false ; } } } int countLogNeeded ( int N ) { int count = 0 ; sieve ( N ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( prime [ i ] ) count ++ ; } return count ; } int main ( ) { int N = 6 ; cout << countLogNeeded ( N ) << endl ; return 0 ; }
Program to find the count of coins of each type from the given ratio | C ++ implementation of above approach ; function to calculate coin . ; Converting each of them in rupees . As we are given totalRupees = 1800 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int coin ( int totalRupees , int X , int Y , int Z ) { float one = 0 , fifty = 0 , twentyfive = 0 , result = 0 , total = 0 ; one = X * 1 ; fifty = ( ( Y * 1 ) / 2.0 ) ; twentyfive = ( ( Z * 1 ) / 4.0 ) ; total = one + fifty + twentyfive ; result = ( ( totalRupees ) / total ) ; return result ; } int main ( ) { int totalRupees = 1800 ; int X = 1 , Y = 2 , Z = 4 ; int Rupees = coin ( totalRupees , X , Y , Z ) ; cout << "1 ▁ rupess ▁ coins ▁ = ▁ " << Rupees * 1 << endl ; cout << "50 ▁ paisa ▁ coins ▁ = ▁ " << Rupees * 2 << endl ; cout << "25 ▁ paisa ▁ coins ▁ = ▁ " << Rupees * 4 << endl ; return 0 ; }
Find the sum of series 0. X + 0. XX + 0. XXX + ... upto k terms | C ++ program for sum of the series 0. x , 0. xx , 0. xxx , ... upto k terms ; function which return the sum of series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float sumOfSeries ( int x , int k ) { return ( float ( x ) / 81 ) * ( 9 * k - 1 + pow ( 10 , ( -1 ) * k ) ) ; } int main ( ) { int x = 9 ; int k = 20 ; cout << sumOfSeries ( x , k ) ; return 0 ; }
Find four factors of N with maximum product and sum equal to N | Set | C ++ program to find four factors of N with maximum product and sum equal to N ; Function to find factors and to print those four factors ; push all the factors in the container ; number of factors ; Initial maximum ; hash - array to mark the pairs ; form all the pair sums ; if the pair sum is less than n ; push in another container ; mark the sum with the elements formed ; mark in the map that v [ i ] + v [ j ] is present ; new size of all the pair sums ; iterate for all pair sum ; the required part ; if the required part is also present in pair sum ; find the elements with which the first pair is formed ; find the elements with which the second pair is formed ; check for previous maximum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findfactors ( int n ) { unordered_map < int , int > mpp ; vector < int > v , v1 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . push_back ( i ) ; if ( i != ( n / i ) && i != 1 ) v . push_back ( n / i ) ; } } int s = v . size ( ) ; int maxi = -1 ; pair < int , int > mp1 [ n + 5 ] ; for ( int i = 0 ; i < s ; i ++ ) { for ( int j = i ; j < s ; j ++ ) { if ( v [ i ] + v [ j ] < n ) { v1 . push_back ( v [ i ] + v [ j ] ) ; mp1 [ v [ i ] + v [ j ] ] = { v [ i ] , v [ j ] } ; mpp [ v [ i ] + v [ j ] ] = 1 ; } } } s = v1 . size ( ) ; for ( int i = 0 ; i < s ; i ++ ) { int el = n - ( v1 [ i ] ) ; if ( mpp [ el ] == 1 ) { int a = mp1 [ v1 [ i ] ] . first ; int b = mp1 [ v1 [ i ] ] . second ; int c = mp1 [ n - v1 [ i ] ] . first ; int d = mp1 [ n - v1 [ i ] ] . second ; maxi = max ( a * b * c * d , maxi ) ; } } if ( maxi == -1 ) cout << " Not ▁ Possible STRNEWLINE " ; else { cout << " The ▁ maximum ▁ product ▁ is ▁ " << maxi << endl ; } } int main ( ) { int n = 50 ; findfactors ( n ) ; return 0 ; }
Maximize the product of four factors of a Number | C ++ implementation of above approach ; Declare the vector of factors for storing the ; function to find out the factors of a number ; Loop until the i reaches the sqrt ( n ) ; Check if i is a factor of n ; if both the factors are same we only push one factor ; factor1 is pushed ; factor2 is pushed ; Function to find the maximum product ; Initialize the product with - 1 ; Find the sum of factors and store it in s ; Compare whether it is equal to the n ; product of factors ; Check whether we have a better p now if yes update ; Driver code ; initializes the vectors with the divisors of n ; prints out the maximised product .
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > factors ; void findFactors ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i ) == i ) factors . push_back ( i ) ; else { factors . push_back ( n / i ) ; factors . push_back ( i ) ; } } } } int findProduct ( int n ) { int product = -1 ; int si = factors . size ( ) ; for ( int i = 0 ; i < si ; i ++ ) for ( int j = 0 ; j < si ; j ++ ) for ( int k = 0 ; k < si ; k ++ ) for ( int l = 0 ; l < si ; l ++ ) { int s = factors [ i ] + factors [ j ] + factors [ k ] + factors [ l ] ; if ( s == n ) { int p = factors [ i ] * factors [ j ] * factors [ k ] * factors [ l ] ; if ( p > product ) product = p ; } } return product ; } int main ( ) { int n = 10 ; findFactors ( n ) ; cout << findProduct ( n ) ; return 0 ; }
Maximize the product of four factors of a Number | C ++ implementation of above approach ; For calculation of a ^ b ; Function to check ; every odd and number less than 3. ; every number divisible by 4. ; every number divisible by 6. ; every number divisible by 10. ; for every even number which is not divisible by above values . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int modExp ( int a , int b ) { int result = 1 ; while ( b > 0 ) { if ( b & 1 ) result = result * a ; a = a * a ; b /= 2 ; } return result ; } int check ( int num ) { if ( num & 1 num < 3 ) return -1 ; else if ( num % 4 == 0 ) return modExp ( num / 4 , 4 ) ; else if ( num % 6 == 0 ) return modExp ( num / 3 , 2 ) * modExp ( num / 6 , 2 ) ; else if ( num % 10 == 0 ) return modExp ( num / 5 , 2 ) * ( num / 10 ) * ( num / 2 ) ; else return -1 ; } int main ( ) { int num = 10 ; cout << check ( num ) ; return 0 ; }
Check if any large number is divisible by 17 or not | CPP Program to validate the above logic ; Function to check if the number is divisible by 17 or not ; Extracting the last digit ; Truncating the number ; Subtracting the five times the last digit from the remaining number ; Return n is divisible by 17 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisible ( long long int n ) { while ( n / 100 ) { int d = n % 10 ; n /= 10 ; n -= d * 5 ; } return ( n % 17 == 0 ) ; } int main ( ) { long long int n = 19877658 ; if ( isDivisible ( n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Minimum number of elements to be removed to make XOR maximum | C ++ implementation to find minimum number of elements to remove to get maximum XOR value ; First n in the below condition is for the case where n is 0 ; Function to find minimum number of elements to be removed . ; Driver code ; print minimum number of elements to be removed
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int nextPowerOf2 ( unsigned int n ) { unsigned count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } int removeElement ( unsigned int n ) { if ( n == 1 n == 2 ) return 0 ; unsigned int a = nextPowerOf2 ( n ) ; if ( n == a n == a - 1 ) return 1 ; else if ( n == a - 2 ) return 0 ; else if ( n % 2 == 0 ) return 1 ; else return 2 ; } int main ( ) { unsigned int n = 5 ; cout << removeElement ( n ) ; return 0 ; }
Program to find Length of Bridge using Speed and Length of Train | C ++ Program to implement above code . ; function to calculate the length of bridge . ; Driver Code ; Assuming the input variables
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bridge_length ( int trainLength , int Speed , int Time ) { return ( ( Time * Speed ) - trainLength ) ; } int main ( ) { int trainLength = 120 ; int Speed = 30 ; int Time = 18 ; cout << " Length ▁ of ▁ bridge ▁ = ▁ " << bridge_length ( trainLength , Speed , Time ) << " ▁ meters " ; return 0 ; }
Program to find sum of the given sequence | CPP program to find the sum of the given sequence ; function to find moudulo inverse under 10 ^ 9 + 7 ; Function to find the sum of the given sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const long long MOD = 1000000007 ; long long modInv ( long long x ) { long long n = MOD - 2 ; long long result = 1 ; while ( n ) { if ( n & 1 ) result = result * x % MOD ; x = x * x % MOD ; n = n / 2 ; } return result ; } long long getSum ( long long n , long long k ) { long long ans = 1 ; for ( long long i = n + 1 ; i > n - k ; i -- ) ans = ans * i % MOD ; ans = ans * modInv ( k + 1 ) % MOD ; return ans ; } int main ( ) { long long n = 3 , k = 2 ; cout << getSum ( n , k ) ; return 0 ; }
Triplet with no element divisible by 3 and sum N | C ++ program to print a , b and c such that a + b + c = N ; Function to print a , b and c ; check if n - 2 is divisible by 3 or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCombination ( int n ) { cout << 1 << " ▁ " ; if ( ( n - 2 ) % 3 == 0 ) cout << 2 << " ▁ " << n - 3 ; else cout << 1 << " ▁ " << n - 2 ; } int main ( ) { int n = 233 ; printCombination ( n ) ; return 0 ; }
Pairs with GCD equal to one in the given range | C ++ program to print all pairs ; Function to print all pairs ; check if even ; We can print all adjacent pairs for ( int i = l ; i < r ; i += 2 ) { cout << " { " << i << " , ▁ " << i + 1 << " } , ▁ " ; } ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPairs ( int l , int r ) { if ( ( l - r ) % 2 == 0 ) return false ; return true ; } int main ( ) { int l = 1 , r = 8 ; if ( checkPairs ( l , r ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if a number with even number of digits is palindrome or not | C ++ program to find number is palindrome or not without using any extra space ; Function to check if the number is palindrome ; if divisible by 11 then true ; if not divisible by 11 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( int n ) { if ( n % 11 == 0 ) { return true ; } return false ; } int main ( ) { isPalindrome ( 123321 ) ? cout << " Palindrome " : cout << " Not ▁ Palindrome " ; return 0 ; }
Count number of triplets with product equal to given number with duplicates allowed | C ++ program for above implementation ; The target value for which we have to find the solution ; This variable contains the total count of triplets found ; Loop from the first to the third last integer in the list ; Check if arr [ i ] is a factor of target or not . If not , skip to the next element ; Check if the pair ( arr [ i ] , arr [ j ] ) can be a part of triplet whose product is equal to the target ; Find the remaining element of the triplet ; If element is found . increment the total count of the triplets
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int target = 93 ; int arr [ ] = { 1 , 31 , 3 , 1 , 93 , 3 , 31 , 1 , 93 } ; int length = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int totalCount = 0 ; for ( int i = 0 ; i < length - 2 ; i ++ ) { if ( target % arr [ i ] == 0 ) { for ( int j = i + 1 ; j < length - 1 ; j ++ ) { if ( target % ( arr [ i ] * arr [ j ] ) == 0 ) { int toFind = target / ( arr [ i ] * arr [ j ] ) ; for ( int k = j + 1 ; k < length ; k ++ ) { if ( arr [ k ] == toFind ) { totalCount ++ ; } } } } } } cout << " Total ▁ number ▁ of ▁ triplets ▁ found ▁ : ▁ " << totalCount ; return 0 ; }
Number of Permutations such that no Three Terms forms Increasing Subsequence | C ++ program to find the nth catalan number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long int binomialCoeff ( unsigned int n , unsigned int k ) { unsigned long int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } unsigned long int catalan ( unsigned int n ) { unsigned long int c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } int main ( ) { int n = 3 ; cout << catalan ( n ) << endl ; return 0 ; }
Fascinating Number | C ++ program to implement fascinating number ; function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Driver code ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isFascinating ( int num ) { int freq [ 10 ] = { 0 } ; string val = " " + to_string ( num ) + to_string ( num * 2 ) + to_string ( num * 3 ) ; for ( int i = 0 ; i < val . length ( ) ; i ++ ) { int digit = val [ i ] - '0' ; if ( freq [ digit ] and digit != 0 > 0 ) return false ; else freq [ digit ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { if ( freq [ i ] == 0 ) return false ; } return true ; } int main ( ) { int num = 192 ; if ( num < 100 ) cout << " No " << endl ; else { bool ans = isFascinating ( num ) ; if ( ans ) cout << " Yes " ; else cout << " No " ; } }
Count ways to distribute m items among n people | C ++ code for calculating number of ways to distribute m mangoes amongst n people where all mangoes and people are identical ; function used to generate binomial coefficient time complexity O ( m ) ; helper function for generating no of ways to distribute m mangoes amongst n people ; not enough mangoes to be distributed ; ways -> ( n + m - 1 ) C ( n - 1 ) ; Driver function ; m represents number of mangoes n represents number of people
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomial_coefficient ( int n , int m ) { int res = 1 ; if ( m > n - m ) m = n - m ; for ( int i = 0 ; i < m ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } int calculate_ways ( int m , int n ) { if ( m < n ) return 0 ; int ways = binomial_coefficient ( n + m - 1 , n - 1 ) ; return ways ; } int main ( ) { int m = 7 , n = 5 ; int result = calculate_ways ( m , n ) ; printf ( " % d STRNEWLINE " , result ) ; return 0 ; }
Queries to count the number of unordered co | C ++ program to find number of unordered coprime pairs of integers from 1 to N ; to store euler 's totient function ; to store required answer ; Computes and prints totient of all numbers smaller than or equal to N . ; Initialise the phi [ ] with 1 ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; function to compute number coprime pairs ; function call to compute euler totient function ; prefix sum of all euler totient function values ; Driver code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE int phi [ N ] ; int S [ N ] ; void computeTotient ( ) { for ( int i = 1 ; i < N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } void CoPrimes ( ) { computeTotient ( ) ; for ( int i = 1 ; i < N ; i ++ ) S [ i ] = S [ i - 1 ] + phi [ i ] ; } int main ( ) { CoPrimes ( ) ; int q [ ] = { 3 , 4 } ; int n = sizeof ( q ) / sizeof ( q [ 0 ] ) ; for ( int i = 0 ; i < n ; i ++ ) cout << " Number ▁ of ▁ unordered ▁ coprime STRNEWLINE " << " pairs ▁ of ▁ integers ▁ from ▁ 1 ▁ to ▁ " << q [ i ] << " ▁ are ▁ " << S [ q [ i ] ] << endl ; return 0 ; }
N | C ++ program to find the N - th term in 1 , 11 , 55 , 239 , 991 , ... . ; Function to return the decimal value of a binary number ; Initializing base value to 1 , i . e 2 ^ 0 ; find the binary representation of the N - th number in sequence ; base case ; answer string ; add n - 1 1 's ; add 0 ; add n 1 's at end ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binaryToDecimal ( string n ) { string num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } int numberSequence ( int n ) { if ( n == 1 ) return 1 ; string s = " " ; for ( int i = 1 ; i < n ; i ++ ) s += '1' ; s += '0' ; for ( int i = 1 ; i <= n ; i ++ ) s += '1' ; int num = binaryToDecimal ( s ) ; return num ; } int main ( ) { int n = 4 ; cout << numberSequence ( n ) ; return 0 ; }
N | C ++ program to find the N - th term in 1 , 11 , 55 , 239 , 991 , ... . ; Function to find the N - th term ; calculates the N - th term ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberSequence ( int n ) { int num = pow ( 4 , n ) - pow ( 2 , n ) - 1 ; return num ; } int main ( ) { int n = 4 ; cout << numberSequence ( n ) ; return 0 ; }
Alternate Primes till N | C ++ program to print all primes smaller than or equal to n using Naive approach . ; Function for checking number is prime or not ; if flag = 0 then number is prime and return 1 otherwise return 0 ; Function for printing alternate prime number ; counter is initialize with 0 ; looping through 2 to n - 1 ; function calling along with if condition ; if counter is multiple of 2 then only print prime number ; Driver code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prime ( int num ) { int i , flag = 0 ; for ( i = 2 ; i <= num / 2 ; i ++ ) { if ( num % i == 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) return 1 ; else return 0 ; } void print_alternate_prime ( int n ) { int counter = 0 ; for ( int num = 2 ; num < n ; num ++ ) { if ( prime ( num ) == 1 ) { if ( counter % 2 == 0 ) cout << num << " ▁ " ; counter ++ ; } } } int main ( ) { int n = 15 ; cout << " Following ▁ are ▁ the ▁ alternate ▁ prime " << " ▁ number ▁ smaller ▁ than ▁ or ▁ equal ▁ to ▁ " << n << endl ; print_alternate_prime ( n ) ; }
Alternate Primes till N | C ++ program to print all primes smaller than or equal to n using Sieve of Eratosthenes ; 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 ; Update all multiples of p ; Print all prime numbers ; for next prime to get printed ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( 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 ; } } bool flag = true ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { if ( flag ) { cout << p << " ▁ " ; flag = false ; } else { flag = true ; } } } } int main ( ) { int n = 15 ; cout << " Following ▁ are ▁ the ▁ alternate " << " ▁ prime ▁ numbers ▁ smaller ▁ " << " ▁ than ▁ or ▁ equal ▁ to ▁ " << n << endl ; SieveOfEratosthenes ( n ) ; return 0 ; }
Find maximum among x ^ ( y ^ 2 ) or y ^ ( x ^ 2 ) where x and y are given | C ++ program to find the greater value ; Function to find maximum ; Case 1 ; Case 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findGreater ( int x , int y ) { if ( x > y ) { return false ; } else { return true ; } } int main ( ) { int x = 4 ; int y = 9 ; findGreater ( x , y ) ? cout << "1 STRNEWLINE " : cout << "2 STRNEWLINE " ; return 0 ; }