codeparrot/xlcost-text-to-code huggingface dataset:
Permutations of string such that no two vowels are adjacent | CPP program to count permutations of string such that no two vowels are adjacent ; Factorial of a number ; Function to find c ( n , r ) ; Function to count permutations of string such that no two vowels are adjacent ; Finding the frequencies of the characters ; finding the no . of vowels and consonants in given word ; finding places for the vowels ; ways to fill consonants 6 ! / 2 ! ; ways to put vowels 7 C5 x 5 ! ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } int ncr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } int countWays ( string str ) { int freq [ 26 ] = { 0 } ; int nvowels = 0 , nconsonants = 0 ; int vplaces , cways , vways ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) ++ freq [ str [ i ] - ' a ' ] ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 ) nvowels += freq [ i ] ; else nconsonants += freq [ i ] ; } vplaces = nconsonants + 1 ; cways = factorial ( nconsonants ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 && freq [ i ] > 1 ) { cways = cways / factorial ( freq [ i ] ) ; } } vways = ncr ( vplaces , nvowels ) * factorial ( nvowels ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 && freq [ i ] > 1 ) { vways = vways / factorial ( freq [ i ] ) ; } } return cways * vways ; } int main ( ) { string str = " permutation " ; cout << countWays ( str ) << endl ; return 0 ; }
Pairs from an array that satisfy the given condition | C ++ implementation of the approach ; Function to return the number of set bits in n ; Function to return the count of required pairs ; Set bits for first element of the pair ; Set bits for second element of the pair ; Set bits of the resultant number which is the XOR of both the elements of the pair ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int setBits ( int n ) { int count = 0 ; while ( n ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } int countPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int setbits_x = setBits ( a [ i ] ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int setbits_y = setBits ( a [ j ] ) ; int setbits_xor_xy = setBits ( a [ i ] ^ a [ j ] ) ; if ( setbits_x + setbits_y == setbits_xor_xy ) count ++ ; } } return count ; } int main ( ) { int a [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countPairs ( a , n ) ; }
Number of array elements derivable from D after performing certain operations | CPP program to find the number of array elements which can be derived by perming ( + A , - A , + B , - B ) operations on D ; Function to return gcd of a and b ; Function to Return the number of elements of arr [ ] which can be derived from D by performing ( + A , - A , + B , - B ) ; find the gcd of A and B ; counter stores the number of array elements which can be derived from D ; arr [ i ] can be derived from D only if | arr [ i ] - D | is divisible by gcd of A and B ; 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 findPossibleDerivables ( int arr [ ] , int n , int D , int A , int B ) { int gcdAB = gcd ( A , B ) ; int counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( abs ( arr [ i ] - D ) % gcdAB ) == 0 ) { counter ++ ; } } return counter ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 7 , 13 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int D = 5 , A = 4 , B = 2 ; cout << findPossibleDerivables ( arr , n , D , A , B ) << " STRNEWLINE " ; int a [ ] = { 1 , 2 , 3 } ; n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; D = 6 , A = 3 , B = 2 ; cout << findPossibleDerivables ( a , n , D , A , B ) << " STRNEWLINE " ; return 0 ; }
Find the sum of first N terms of the series 2 * 3 * 5 , 3 * 5 * 7 , 4 * 7 * 9 , ... | C ++ program to find sum of the first n terms of the given series ; Function to return the sum of the first n terms of the given series ; As described in the approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calSum ( int n ) { return ( n * ( 2 * n * n * n + 12 * n * n + 25 * n + 21 ) ) / 2 ; } int main ( ) { int n = 3 ; cout << calSum ( n ) ; return 0 ; }
Find elements of array using XOR of consecutive elements | C ++ program to find the array elements using XOR of consecutive elements ; Function to find the array elements using XOR of consecutive elements ; array to store the original elements ; first element a i . e elements [ 0 ] = a ; To get the next elements we have to calculate xor of previous elements with given xor of 2 consecutive elements . e . g . if a ^ b = k1 so to get b xor a both side . b = k1 ^ a ; Printing the original array elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getElements ( int a , int arr [ ] , int n ) { int elements [ n + 1 ] ; elements [ 0 ] = a ; for ( int i = 0 ; i < n ; i ++ ) { elements [ i + 1 ] = arr [ i ] ^ elements [ i ] ; } for ( int i = 0 ; i < n + 1 ; i ++ ) cout << elements [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 13 , 2 , 6 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int a = 5 ; getElements ( a , arr , n ) ; return 0 ; }
Maximum GCD of N integers with given product | C ++ implementation of above approach ; Function to find maximum GCD of N integers with product P ; map to store prime factors of P ; prime factorization of P ; traverse all prime factors and multiply its 1 / N power to the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxGCD ( int N , int P ) { int ans = 1 ; unordered_map < int , int > prime_factors ; for ( int i = 2 ; i * i <= P ; i ++ ) { while ( P % i == 0 ) { prime_factors [ i ] ++ ; P /= i ; } } if ( P != 1 ) prime_factors [ P ] ++ ; for ( auto v : prime_factors ) ans *= pow ( v . first , v . second / N ) ; return ans ; } int main ( ) { int N = 3 , P = 24 ; cout << maxGCD ( N , P ) ; return 0 ; }
Check if the sum of distinct digits of two integers are equal | C ++ program to check if the sum of distinct digits of two integers are equal ; Function to return the sum of distinct digits of a number ; Take last digit ; If digit has not been used before ; Set digit as used ; Remove last digit ; Function to check whether the sum of distinct digits of two numbers are equal ; Driver code
#include <iostream> NEW_LINE using namespace std ; int distinctDigitSum ( int n ) { bool used [ 10 ] ; int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; if ( ! used [ digit ] ) { used [ digit ] = true ; sum += digit ; } n = ( int ) n / 10 ; } return sum ; } string checkSum ( int m , int n ) { int sumM = distinctDigitSum ( m ) ; int sumN = distinctDigitSum ( n ) ; if ( sumM != sumN ) return " YES " ; return " NO " ; } int main ( ) { int m = 2452 , n = 9222 ; cout << ( checkSum ( m , n ) ) ; return 0 ; }
All pairs whose xor gives unique prime | C ++ implementation of above approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the count of valid pairs ; If xor ( a [ i ] , a [ j ] ) is prime and unique ; Driver code
#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 countPairs ( int a [ ] , int n ) { int count = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( isPrime ( a [ i ] ^ a [ j ] ) && m [ a [ i ] ^ a [ j ] ] == 0 ) { m [ ( a [ i ] ^ a [ j ] ) ] ++ ; count ++ ; } } } return count ; } int main ( ) { int a [ ] = { 10 , 12 , 23 , 45 , 5 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countPairs ( a , n ) ; }
Sum of element whose prime factors are present in array | C ++ program to find the sum of the elements of an array whose prime factors are present in the same array ; Stores smallest prime factor for every number ; Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN ; Marking smallest prime factor for every number to be itself . ; Separately marking spf for every even number as 2 ; If i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to return the sum of the elements of an array whose prime factors are present in the same array ; Function call to calculate smallest prime factors of all the numbers upto MAXN ; Create map for each element ; If smallest prime factor of num is present in array ; Each factor of arr [ i ] is present in the array ; Driver program ; Function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  1000001 NEW_LINE int spf [ MAXN ] ; void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } int sumFactors ( int arr [ ] , int n ) { sieve ( ) ; std :: map < int , int > map ; for ( int i = 0 ; i < n ; ++ i ) map [ arr [ i ] ] = 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int num = arr [ i ] ; while ( num != 1 && map [ spf [ num ] ] == 1 ) { num /= spf [ num ] ; } if ( num == 1 ) sum += arr [ i ] ; } return sum ; } int main ( ) { int arr [ ] = { 5 , 11 , 55 , 25 , 100 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumFactors ( arr , n ) ; return 0 ; }
Find nth Hermite number | C ++ program to find nth Hermite number ; Utility function to calculate double factorial of a number ; Function to return nth Hermite number ; If n is even then return 0 ; If n is odd ; Calculate double factorial of ( n - 1 ) and multiply it with 2 ^ ( n / 2 ) ; If n / 2 is odd then nth Hermite number will be negative ; Return nth Hermite number ; Driver Code ; Print nth Hermite number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int doubleFactorial ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { fact = fact * i ; } return fact ; } int hermiteNumber ( int n ) { if ( n % 2 == 1 ) return 0 ; else { int number = ( pow ( 2 , n / 2 ) ) * doubleFactorial ( n - 1 ) ; if ( ( n / 2 ) % 2 == 1 ) number = number * -1 ; return number ; } } int main ( ) { int n = 6 ; cout << hermiteNumber ( n ) ; return 0 ; }
Count numbers in a range having GCD of powers of prime factors equal to 1 | C ++ implementation of the approach ; Vector to store powers greater than 3 ; Set to store perfect squares ; Set to store powers other than perfect squares ; Pushing squares ; if the values is already a perfect square means present in the set ; Run loop until some power of current number doesn 't exceed MAX ; Pushing only odd powers as even power of a number can always be expressed as a perfect square which is already present in set squares ; Inserting those sorted values of set into a vector ; Precompute the powers ; Calculate perfect squares in range using sqrtl function ; Calculate upper value of R in vector using binary search ; Calculate lower value of L in vector using binary search ; Calculate perfect powers ; Compute final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000005 NEW_LINE #define MAX  1e18 NEW_LINE vector < long int > powers ; set < long int > squares ; set < long int > s ; void powersPrecomputation ( ) { for ( long int i = 2 ; i < N ; i ++ ) { squares . insert ( i * i ) ; if ( squares . find ( i ) != squares . end ( ) ) continue ; long int temp = i ; while ( i * i <= MAX / temp ) { temp *= ( i * i ) ; s . insert ( temp ) ; } } for ( auto x : s ) powers . push_back ( x ) ; } long int calculateAnswer ( long int L , long int R ) { powersPrecomputation ( ) ; long int perfectSquares = floor ( sqrtl ( R ) ) - floor ( sqrtl ( L - 1 ) ) ; long int high = upper_bound ( powers . begin ( ) , powers . end ( ) , R ) - powers . begin ( ) ; long int low = lower_bound ( powers . begin ( ) , powers . end ( ) , L ) - powers . begin ( ) ; long perfectPowers = perfectSquares + ( high - low ) ; long ans = ( R - L + 1 ) - perfectPowers ; return ans ; } int main ( ) { long int L = 13 , R = 20 ; cout << calculateAnswer ( L , R ) ; return 0 ; }
Sum of integers upto N with given unit digit | C ++ implementation of the approach ; Function to return the required sum ; If the unit digit is d ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll getSum ( int n , int d ) { ll sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 10 == d ) sum += i ; } return sum ; } int main ( ) { int n = 30 , d = 3 ; cout << getSum ( n , d ) ; return 0 ; }
Split a number into 3 parts such that none of the parts is divisible by 3 | CPP program to split a number into three parts such than none of them is divisible by 3. ; Print x = 1 , y = 1 and z = N - 2 ; Otherwise , print x = 1 , y = 2 and z = N - 3 ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printThreeParts ( int N ) { if ( N % 3 == 0 ) cout << " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 1 , ▁ z ▁ = ▁ " << N - 2 << endl ; else cout << " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 2 , ▁ z ▁ = ▁ " << N - 3 << endl ; } int main ( ) { int N = 10 ; printThreeParts ( N ) ; return 0 ; }
Minimum absolute difference of a number and its closest prime | C ++ program to find the minimum absolute difference between a number and its closest prime ; Function to check if a number is prime or not ; Function to find the minimum absolute difference between a number and its closest prime ; Variables to store first prime above and below N ; Finding first prime number greater than N ; Finding first prime number less than N ; Variables to store the differences ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int N ) { for ( int i = 2 ; i <= sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } int getDifference ( int N ) { if ( N == 0 ) return 2 ; else if ( N == 1 ) return 1 ; else if ( isPrime ( N ) ) return 0 ; int aboveN = -1 , belowN = -1 ; int n1 ; n1 = N + 1 ; while ( true ) { if ( isPrime ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPrime ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } int diff1 = aboveN - N ; int diff2 = N - belowN ; return min ( diff1 , diff2 ) ; } int main ( ) { int N = 25 ; cout << getDifference ( N ) << endl ; return 0 ; }
Check if the sum of perfect squares in an array is divisible by x | C ++ implementation of the approach ; Function that returns true if the sum of all the perfect squares of the given array are divisible by x ; If arr [ i ] is a perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int arr [ ] , int x , int n ) { long long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { double x = sqrt ( arr [ i ] ) ; if ( floor ( x ) == ceil ( x ) ) { sum += arr [ i ] ; } } if ( sum % x == 0 ) return true ; else return false ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 9 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 13 ; if ( check ( arr , x , n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Largest Divisor of a Number not divisible by a perfect square | C ++ Program to find the largest divisor not divisible by any perfect square greater than 1 ; Function to find the largest divisor not divisible by any perfect square greater than 1 ; set to store divisors in descending order ; If the number is divisible by i , then insert it ; Vector to store perfect squares ; Check for each divisor , if it is not divisible by any perfect square , simply return it as the answer . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1e5 ; int findLargestDivisor ( int n ) { int m = n ; set < int , greater < int > > s ; s . insert ( 1 ) ; s . insert ( n ) ; for ( int i = 2 ; i < sqrt ( n ) + 1 ; i ++ ) { if ( n % i == 0 ) { s . insert ( n / i ) ; s . insert ( i ) ; while ( m % i == 0 ) m /= i ; } } if ( m > 1 ) s . insert ( m ) ; vector < int > vec ; for ( int i = 2 ; i <= MAX ; i ++ ) vec . push_back ( i * i ) ; for ( auto d : s ) { int divi = 0 ; for ( int j = 0 ; j < vec . size ( ) && vec [ j ] <= d ; j ++ ) { if ( d % vec [ j ] == 0 ) { divi = 1 ; break ; } } if ( ! divi ) return d ; } } int main ( ) { int n = 12 ; cout << findLargestDivisor ( n ) << endl ; n = 97 ; cout << findLargestDivisor ( n ) << endl ; return 0 ; }
Find the Number of Maximum Product Quadruples | CPP program to find the number of Quadruples having maximum product ; Returns the number of ways to select r objects out of available n choices ; ncr = ( n * ( n - 1 ) * ( n - 2 ) * ... . . ... ( n - r + 1 ) ) / ( r * ( r - 1 ) * ... * 1 ) ; Returns the number of quadruples having maximum product ; stores the frequency of each element ; remaining_choices denotes the remaining elements to select inorder to form quadruple ; traverse the elements of the map in reverse order ; If Frequency of element < remaining choices , select all of these elements , else select only the number of elements required ; Decrement remaining_choices acc to the number of the current elements selected ; if the quadruple is formed stop the algorithm ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int NCR ( int n , int r ) { int numerator = 1 ; int denominator = 1 ; while ( r > 0 ) { numerator *= n ; denominator *= r ; n -- ; r -- ; } return ( numerator / denominator ) ; } int findWays ( int arr [ ] , int n ) { map < int , int > count ; if ( n < 4 ) return 0 ; for ( int i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } int remaining_choices = 4 ; int ans = 1 ; for ( auto iter = count . rbegin ( ) ; iter != count . rend ( ) ; ++ iter ) { int number = iter -> first ; int frequency = iter -> second ; int toSelect = min ( remaining_choices , frequency ) ; ans = ans * NCR ( frequency , toSelect ) ; remaining_choices -= toSelect ; if ( ! remaining_choices ) { break ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 3 , 3 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int maxQuadrupleWays = findWays ( arr , n ) ; cout << maxQuadrupleWays ; return 0 ; }
Minimum and Maximum number of pairs in m teams of n people | CPP program to find minimum and maximum no . of pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MinimumMaximumPairs ( int n , int m ) { int max_pairs = ( ( n - m + 1 ) * ( n - m ) ) / 2 ; int min_pairs = m * ( ( ( n - m ) / m + 1 ) * ( ( n - m ) / m ) ) / 2 + ceil ( ( n - m ) / double ( m ) ) * ( ( n - m ) % m ) ; cout << " Minimum ▁ no . ▁ of ▁ pairs ▁ = ▁ " << min_pairs << " STRNEWLINE " ; cout << " Maximum ▁ no . ▁ of ▁ pairs ▁ = ▁ " << max_pairs << " STRNEWLINE " ; } int main ( ) { int n = 5 , m = 2 ; MinimumMaximumPairs ( n , m ) ; return 0 ; }
Larger of a ^ b or b ^ a ( a raised to power b or b raised to power a ) | C ++ code for finding greater between the a ^ b and b ^ a ; Function to find the greater value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findGreater ( int a , int b ) { long double x = ( long double ) a * ( long double ) ( log ( ( long double ) ( b ) ) ) ; long double y = ( long double ) b * ( long double ) ( log ( ( long double ) ( a ) ) ) ; if ( y > x ) { cout << " a ^ b ▁ is ▁ greater " << endl ; } else if ( y < x ) { cout << " b ^ a ▁ is ▁ greater " << endl ; } else { cout << " Both ▁ are ▁ equal " << endl ; } } int main ( ) { int a = 3 , b = 5 , c = 2 , d = 4 ; findGreater ( a , b ) ; findGreater ( c , d ) ; return 0 ; }
Expressing a fraction as a natural number under modulo ' m ' | C ++ implementation of the approach ; Function to return the GCD of given numbers ; Recursive function to return ( x ^ n ) % m ; Function to return the fraction modulo mod ; ( b ^ m - 2 ) % m ; Final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE #define m  1000000007 NEW_LINE int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } ll modexp ( ll x , ll n ) { if ( n == 0 ) { return 1 ; } else if ( n % 2 == 0 ) { return modexp ( ( x * x ) % m , n / 2 ) ; } else { return ( x * modexp ( ( x * x ) % m , ( n - 1 ) / 2 ) % m ) ; } } ll getFractionModulo ( ll a , ll b ) { ll c = gcd ( a , b ) ; a = a / c ; b = b / c ; ll d = modexp ( b , m - 2 ) ; ll ans = ( ( a % m ) * ( d % m ) ) % m ; return ans ; } int main ( ) { ll a = 2 , b = 6 ; cout << getFractionModulo ( a , b ) << endl ; return 0 ; }
Find sum of a number and its maximum prime factor | C ++ program to find sum of n and it 's largest prime factor ; Function to return the sum of n and it 's largest prime factor ; Initialise maxPrime to - 1. ; n must be odd at this point , thus skip the even numbers and iterate only odd numbers ; This condition is to handle the case when n is a prime number greater than 2 ; finally return the sum . ; Driver Program to check the above function .
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int maxPrimeFactors ( int n ) { int num = n ; int maxPrime = -1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n /= 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } } if ( n > 2 ) maxPrime = n ; int sum = maxPrime + num ; return sum ; } int main ( ) { int n = 19 ; cout << maxPrimeFactors ( n ) ; return 0 ; }
Largest number less than N with digit sum greater than the digit sum of N | C ++ implementation of the approach ; Function to return the sum of the digits of n ; Loop for each digit of the number ; Function to return the greatest number less than n such that the sum of its digits is greater than the sum of the digits of n ; Starting from n - 1 ; Check until 1 ; If i satisfies the given condition ; If the condition is not satisfied ; Driver code
#include <iostream> NEW_LINE using namespace std ; int sumOfDigits ( int n ) { int res = 0 ; while ( n > 0 ) { res += n % 10 ; n /= 10 ; } return res ; } int findNumber ( int n ) { int i = n - 1 ; while ( i > 0 ) { if ( sumOfDigits ( i ) > sumOfDigits ( n ) ) return i ; i -- ; } return -1 ; } int main ( ) { int n = 824 ; cout << findNumber ( n ) ; return 0 ; }
Find the Nth term of the series 14 , 28 , 20 , 40 , ... . . | CPP program to find the Nth term of the series 14 , 28 , 20 , 40 , ... . . ; Function to find the N - th term ; initializing the 1 st number ; loop from 2 nd term to nth term ; if i is even , double the previous number ; if i is odd , subtract 8 from previous number ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findNth ( int N ) { int b = 14 ; int i ; for ( i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) b = b * 2 ; else b = b - 8 ; } return b ; } int main ( ) { int N = 6 ; cout << findNth ( N ) ; return 0 ; }