codeparrot/xlcost-text-to-code huggingface dataset:
Find prime factors of Array elements whose sum of exponents is divisible by K | C ++ program for the above approach ; To store the smallest prime factor till 10 ^ 5 ; Function to compute smallest prime factor array ; Initialize the spf array first element ; Marking smallest prime factor for every number to be itself ; Separately marking smallest prime factor for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function that finds minimum operation ; Create a spf [ ] array ; Map created to store the unique prime numbers ; To store the result ; To store minimum operations ; To store every unique prime number ; Erase 1 as a key because it is not a prime number ; First Prime Number ; Frequency is divisible by K then insert primeNum in the result [ ] ; Print the elements if it exists ; Driver Code ; Given array arr [ ] ; Given K ; Function Call
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; int spf [ 10001 ] ; void spf_array ( int spf [ ] ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } void frequent_prime ( int arr [ ] , int N , int K ) { spf_array ( spf ) ; unordered_map < int , int > Hmap ; vector < int > result ; int i = 0 ; int c = 0 ; for ( i = 0 ; i < N ; i ++ ) { int x = arr [ i ] ; while ( x != 1 ) { Hmap [ spf [ x ] ] = Hmap [ spf [ x ] ] + 1 ; x = x / spf [ x ] ; } } Hmap . erase ( 1 ) ; for ( auto x : Hmap ) { int primeNum = x . first ; int frequency = x . second ; if ( frequency % K == 0 ) { result . push_back ( primeNum ) ; } } if ( result . size ( ) > 0 ) { for ( auto & it : result ) { cout << it << ' ▁ ' ; } } else { cout << " { } " ; } } int main ( ) { int arr [ ] = { 1 , 4 , 6 } ; int K = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; frequent_prime ( arr , N , K ) ; }
Generate first K multiples of N using Bitwise operators | C ++ program to implement the above approach ; Function to print the first K multiples of N ; Print the value of N * i ; Iterate each bit of N and add pow ( 2 , pos ) , where pos is the index of each set bit ; Check if current bit at pos j is fixed or not ; For next set bit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Kmultiples ( int n , int k ) { int a = n ; for ( int i = 1 ; i <= k ; i ++ ) { cout << n << " ▁ * ▁ " << i << " ▁ = ▁ " << a << endl ; int j = 0 ; while ( n >= ( 1 << j ) ) { a += n & ( 1 << j ) ; j ++ ; } } } int main ( ) { int N = 16 , K = 7 ; Kmultiples ( N , K ) ; return 0 ; }
Least Square Regression Line | C ++ program to find the regression line ; Function to calculate b ; sum of array x ; sum of array y ; for sum of product of x and y ; sum of square of x ; Function to find the least regression line ; Finding b ; Calculating a ; Printing regression line ; Driver code ; Statistical data
#include <bits/stdc++.h> NEW_LINE using namespace std ; double calculateB ( int x [ ] , int y [ ] , int n ) { int sx = accumulate ( x , x + n , 0 ) ; int sy = accumulate ( y , y + n , 0 ) ; int sxsy = 0 ; int sx2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sxsy += x [ i ] * y [ i ] ; sx2 += x [ i ] * x [ i ] ; } double b = ( double ) ( n * sxsy - sx * sy ) / ( n * sx2 - sx * sx ) ; return b ; } void leastRegLine ( int X [ ] , int Y [ ] , int n ) { double b = calculateB ( X , Y , n ) ; int meanX = accumulate ( X , X + n , 0 ) / n ; int meanY = accumulate ( Y , Y + n , 0 ) / n ; double a = meanY - b * meanX ; cout << ( " Regression ▁ line : " ) << endl ; cout << ( " Y ▁ = ▁ " ) ; printf ( " % .3f ▁ + ▁ " , a ) ; printf ( " % .3f ▁ * X " , b ) ; } int main ( ) { int X [ ] = { 95 , 85 , 80 , 70 , 60 } ; int Y [ ] = { 90 , 80 , 70 , 65 , 60 } ; int n = sizeof ( X ) / sizeof ( X [ 0 ] ) ; leastRegLine ( X , Y , n ) ; }
Count of repeating digits in a given Number | C ++ program for the above approach ; Function that returns the count of repeating digits of the given number ; Initialize a variable to store count of Repeating digits ; Initialize cnt array to store digit count ; Iterate through the digits of N ; Retrieve the last digit of N ; Increase the count of digit ; Remove the last digit of N ; Iterate through the cnt array ; If frequency of digit is greater than 1 ; Increment the count of Repeating digits ; Return count of repeating digit ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countRepeatingDigits ( int N ) { int res = 0 ; int cnt [ 10 ] = { 0 } ; while ( N > 0 ) { int rem = N % 10 ; cnt [ rem ] ++ ; N = N / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( cnt [ i ] > 1 ) { res ++ ; } } return res ; } int main ( ) { int N = 12 ; cout << countRepeatingDigits ( N ) ; return 0 ; }
Find temperature of missing days using given sum and average | C ++ program for the above approach ; Function for finding the temperature ; Store Day1 - Day2 in diff ; Remaining from s will be Day1 ; Print Day1 and Day2 ; Driver Code ; Functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTemperature ( int x , int y , int s ) { double Day1 , Day2 ; double diff = ( x - y ) * 6 ; Day2 = ( diff + s ) / 2 ; Day1 = s - Day2 ; cout << " Day1 ▁ : ▁ " << Day1 << endl ; cout << " Day2 ▁ : ▁ " << Day2 << endl ; } int main ( ) { int x = 5 , y = 10 , s = 40 ; findTemperature ( x , y , s ) ; return 0 ; }
Find two numbers whose sum is N and does not contain any digit as K | C ++ program for the above approach ; Function to find two numbers whose sum is N and do not contain any digit as k ; Check every number i and ( n - i ) ; Check if i and n - i doesn 't  contain k in them print i and n-i ; Check if flag is 0 then print - 1 ; Driver Code ; Given N and K ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int freqCount ( string str , char k ) { int count = 0 ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { if ( str [ i ] == k ) count ++ ; } return count ; } void findAandB ( int n , int k ) { int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( freqCount ( to_string ( i ) , ( char ) ( k + 48 ) ) == 0 and freqCount ( to_string ( n - i ) , ( char ) ( k + 48 ) ) == 0 ) { cout << " ( " << i << " , ▁ " << n - i << " ) " ; flag = 1 ; break ; } } if ( flag == 0 ) cout << -1 ; } int main ( ) { int N = 100 ; int K = 0 ; findAandB ( N , K ) ; return 0 ; }
Find the value of P and modular inverse of Q modulo 998244353 | C ++ implementation to find the value of P . Q - 1 mod 998244353 ; Function to find the value of P * Q ^ - 1 mod 998244353 ; Loop to find the value until the expo is not zero ; Multiply p with q if expo is odd ; Reduce the value of expo by 2 ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long calculate ( long long p , long long q ) { long long mod = 998244353 , expo ; expo = mod - 2 ; while ( expo ) { if ( expo & 1 ) { p = ( p * q ) % mod ; } q = ( q * q ) % mod ; expo >>= 1 ; } return p ; } int main ( ) { int p = 1 , q = 4 ; cout << calculate ( p , q ) << ' ' ; return 0 ; }
Find two numbers with given sum and maximum possible LCM | C ++ program of the above approach ; Function that print two numbers with the sum X and maximum possible LCM ; variables to store the result ; If X is odd ; If X is even ; If floor ( X / 2 ) is even ; If floor ( X / 2 ) is odd ; Print the result ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxLCMWithGivenSum ( int X ) { int A , B ; if ( X & 1 ) { A = X / 2 ; B = X / 2 + 1 ; } else { if ( ( X / 2 ) % 2 == 0 ) { A = X / 2 - 1 ; B = X / 2 + 1 ; } else { A = X / 2 - 2 ; B = X / 2 + 2 ; } } cout << A << " ▁ " << B << endl ; } int main ( ) { int X = 30 ; maxLCMWithGivenSum ( X ) ; return 0 ; }
Length of longest subarray whose sum is not divisible by integer K | C ++ Program to find the length of the longest subarray whose sum is not divisible by integer K ; Function to find the longest subarray with sum is not divisible by k ; left is the index of the leftmost element that is not divisible by k ; right is the index of the rightmost element that is not divisible by k ; sum of the array ; Find the element that is not multiple of k ; left = - 1 means we are finding the leftmost element that is not divisible by k ; Updating the rightmost element ; update the sum of the array up to the index i ; Check if the sum of the array is not divisible by k , then return the size of array ; All elements of array are divisible by k , then no such subarray possible so return - 1 ; length of prefix elements that can be removed ; length of suffix elements that can be removed ; Return the length of subarray after removing the elements which have lesser number of elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxSubarrayLength ( int arr [ ] , int n , int k ) { int left = -1 ; int right ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] % k ) != 0 ) { if ( left == -1 ) { left = i ; } right = i ; } sum += arr [ i ] ; } if ( ( sum % k ) != 0 ) { return n ; } else if ( left == -1 ) { return -1 ; } else { int prefix_length = left + 1 ; int suffix_length = n - right ; return n - min ( prefix_length , suffix_length ) ; } } int main ( ) { int arr [ ] = { 6 , 3 , 12 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; cout << MaxSubarrayLength ( arr , n , K ) ; return 0 ; }
Minimum steps to convert X to Y by repeated division and multiplication | C ++ implementation to find minimum steps to convert X to Y by repeated division and multiplication ; Check if X is greater than Y then swap the elements ; Check if X equals Y ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int X , int Y ) { if ( X > Y ) { int temp = X ; X = Y ; Y = temp ; } if ( X == Y ) cout << 0 << endl ; else if ( Y % X == 0 ) cout << 1 << endl ; else cout << 2 << endl ; } int main ( ) { int X = 8 , Y = 13 ; solve ( X , Y ) ; return 0 ; }
Count quadruplets ( A , B , C , D ) till N such that sum of square of A and B is equal to that of C and D | C ++ program for the above approach ; Function to count the quadruples ; Counter variable ; Map to store the sum of pair ( a ^ 2 + b ^ 2 ) ; Iterate till N ; Calculate a ^ 2 + b ^ 2 ; Increment the value in map ; Check if this sum was also in a ^ 2 + b ^ 2 ; Return the count ; Driver Code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; ll countQuadraples ( ll N ) { ll cnt = 0 ; map < ll , ll > m ; for ( ll a = 1 ; a <= N ; a ++ ) { for ( ll b = 1 ; b <= N ; b ++ ) { ll x = a * a + b * b ; m [ x ] += 1 ; } } for ( ll c = 1 ; c <= N ; c ++ ) { for ( ll d = 1 ; d <= N ; d ++ ) { ll x = c * c + d * d ; if ( m . find ( x ) != m . end ( ) ) cnt += m [ x ] ; } } return cnt ; } int main ( ) { ll N = 2 ; cout << countQuadraples ( N ) << endl ; return 0 ; }
Count of distinct index pair ( i , j ) such that element sum of First Array is greater | C ++ program for the above problem ; function to find the number of pairs satisfying the given cond . ; variables used for traversal ; count variable to store the count of possible pairs ; Nested loop to find out the possible pairs ; Check if the given condition is satisfied or not . If yes then increment the count . ; Return the count value ; Driver Code ; Size of the arrays ; Initialise the arrays ; function call that returns the count of possible pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_pairs ( int a [ ] , int b [ ] , int N ) { int i , j ; int count = 0 ; for ( i = 0 ; i < ( N - 1 ) ; i ++ ) { for ( j = ( i + 1 ) ; j < N ; j ++ ) { if ( ( a [ i ] + a [ j ] ) > ( b [ i ] + b [ j ] ) ) { count ++ ; } } } return count ; } int main ( ) { int N = 5 ; int a [ N ] = { 1 , 2 , 3 , 4 , 5 } ; int b [ N ] = { 2 , 5 , 6 , 1 , 9 } ; cout << count_pairs ( a , b , N ) << endl ; return 0 ; }
Count of distinct index pair ( i , j ) such that element sum of First Array is greater | C ++ program of the above approach ; Function to find the number of pairs . ; Array c [ ] where c [ i ] = a [ i ] - b [ i ] ; Sort the array c ; Initialise answer as 0 ; Iterate from index 0 to n - 1 ; If c [ i ] <= 0 then in the sorted array c [ i ] + c [ pos ] can never greater than 0 where pos < i ; Find the minimum index such that c [ i ] + c [ j ] > 0 which is equivalent to c [ j ] >= - c [ i ] + 1 ; Add ( i - pos ) to answer ; return the answer ; Driver code ; Number of elements in a and b ; array a ; array b
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfPairs ( int * a , int * b , int n ) { int c [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = a [ i ] - b [ i ] ; } sort ( c , c + n ) ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( c [ i ] <= 0 ) continue ; int pos = lower_bound ( c , c + n , - c [ i ] + 1 ) - c ; answer += ( i - pos ) ; } return answer ; } int32_t main ( ) { int n = 5 ; int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int b [ ] = { 2 , 5 , 6 , 1 , 9 } ; cout << numberOfPairs ( a , b , n ) << endl ; return 0 ; }
Find K for every Array element such that at least K prefixes are â ‰¥ K | C ++ program for the above approach ; Function to find the K - value for every index in the array ; Multiset to store the array in the form of red - black tree ; Iterating over the array ; Inserting the current value in the multiset ; Condition to check if the smallest value in the set is less than it 's size ; Erase the smallest value ; h - index value will be the size of the multiset ; Driver Code ; array ; Size of the array ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int print_h_index ( int arr [ ] , int N ) { multiset < int > ms ; for ( int i = 0 ; i < N ; i ++ ) { ms . insert ( arr [ i ] ) ; if ( * ms . begin ( ) < ms . size ( ) ) { ms . erase ( ms . begin ( ) ) ; } cout << ms . size ( ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 9 , 10 , 7 , 5 , 0 , 10 , 2 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; print_h_index ( arr , N ) ; return 0 ; }
Prefix Product Array | C ++ Program to generate Prefix Product Array ; Function to generate prefix product array ; Update the array with the product of prefixes ; Print the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prefixProduct ( int a [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i ] * a [ i - 1 ] ; } for ( int j = 0 ; j < n ; j ++ ) { cout << a [ j ] << " , ▁ " ; } return 0 ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 5 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; prefixProduct ( arr , N ) ; return 0 ; }
Count of ways to distribute N items among 3 people with one person receiving maximum | C ++ program to find the number of ways to distribute N item among three people such that one person always gets the maximum value ; Function to find the number of ways to distribute N items among 3 people ; No distribution possible ; Total number of ways to distribute N items among 3 people ; Store the number of distributions which are not possible ; Count possibilities of two persons receiving the maximum ; If N is divisible by 3 ; Return the final count of ways to distribute ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( int N ) { if ( N < 4 ) return 0 ; int ans = ( ( N - 1 ) * ( N - 2 ) ) / 2 ; int s = 0 ; for ( int i = 2 ; i <= N - 3 ; i ++ ) { for ( int j = 1 ; j < i ; j ++ ) { if ( N == 2 * i + j ) s ++ ; } } if ( N % 3 == 0 ) s = 3 * s + 1 ; else s = 3 * s ; return ans - s ; } int main ( ) { int N = 10 ; cout << countWays ( N ) ; return 0 ; }
Magnanimous Numbers | C ++ implementation to check if a number is Magnanimous ; Function to check if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the number is Magnanimous or not ; converting the number to string ; finding length of string ; number should not be of single digit ; loop to find all left and right part of the string ; 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 ; } bool isMagnanimous ( int N ) { string s = to_string ( N ) ; int l = s . length ( ) ; if ( l < 2 ) return false ; for ( int i = 0 ; i < l - 1 ; i ++ ) { string left = s . substr ( 0 , i + 1 ) ; string right = s . substr ( i + 1 ) ; int x = stoi ( left ) ; int y = stoi ( right ) ; if ( ! isPrime ( x + y ) ) return false ; } return true ; } int main ( ) { int N = 12 ; isMagnanimous ( N ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Honaker Prime Number | C ++ program for the above approach ; Function to precompute the position of every prime number using Sieve ; 0 and 1 are not prime numbers ; Variable to store the position ; Incrementing the position for every prime number ; Function to get sum of digits ; Function to check whether the given number is Honaker Prime number or not ; Driver Code ; Precompute the prime numbers till 10 ^ 6 ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE #define limit  10000000 NEW_LINE using namespace std ; int position [ limit + 1 ] ; void sieve ( ) { position [ 0 ] = -1 , position [ 1 ] = -1 ; int pos = 0 ; for ( int i = 2 ; i <= limit ; i ++ ) { if ( position [ i ] == 0 ) { position [ i ] = ++ pos ; for ( int j = i * 2 ; j <= limit ; j += i ) position [ j ] = -1 ; } } } int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } bool isHonakerPrime ( int n ) { int pos = position [ n ] ; if ( pos == -1 ) return false ; return getSum ( n ) == getSum ( pos ) ; } int main ( ) { sieve ( ) ; int N = 121 ; if ( isHonakerPrime ( N ) ) cout << " Yes " ; else cout << " No " ; }
Check if Matrix sum is prime or not | C ++ implementation to check if the sum of matrix is prime or not ; Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for to find the sum of the given matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 4 , M = 5 ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } int takeSum ( int a [ N ] [ M ] ) { int s = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) s += a [ i ] [ j ] ; return s ; } int main ( ) { int a [ N ] [ M ] = { { 1 , 2 , 3 , 4 , 2 } , { 0 , 1 , 2 , 3 , 34 } , { 0 , 34 , 21 , 12 , 12 } , { 1 , 2 , 3 , 6 , 6 } } ; int sum = takeSum ( a ) ; if ( isPrime ( sum ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Sum of sum | C ++ program to implement the above approach ; Function to find the sum ; Calculate sum - series for every natural number and add them ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; static long sumOfSumSeries ( int N ) { long sum = 0L ; for ( int i = 1 ; i <= N ; i ++ ) { sum = sum + ( i * ( i + 1 ) ) / 2 ; } return sum ; } int main ( ) { int N = 5 ; cout << sumOfSumSeries ( N ) ; }
Sum of sum | C ++ program to implement the above approach ; Function to find the sum ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; long sumOfSumSeries ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } int main ( ) { int N = 5 ; cout << sumOfSumSeries ( N ) ; return 0 ; }
Tetradic Primes | C ++ implementation to print all Tetradic primes smaller than or equal to N . ; Function to check if the number N having all digits lies in the set ( 0 , 1 , 8 ) ; Function to check if the number N is palindrome ; Function to check if a number N is Tetradic ; Function to generate all primes and checking whether number is Tetradic or not ; 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 Tetradic prime numbers ; Checking whether the given number is prime Tetradic or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isContaindigit ( int n ) { while ( n > 0 ) { if ( ! ( n % 10 == 0 n % 10 == 1 n % 10 == 8 ) ) return false ; n = n / 10 ; } return true ; } bool ispalindrome ( int n ) { string temp = to_string ( n ) ; int l = temp . length ( ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) { if ( temp [ i ] != temp [ l - i - 1 ] ) return false ; } return true ; } bool isTetradic ( int n ) { if ( ispalindrome ( n ) && isContaindigit ( n ) ) return true ; return false ; } void printTetradicPrimesLessThanN ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; int p = 2 ; while ( p * p <= n ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < n + 1 ; i += p ) prime [ i ] = false ; } p += 1 ; } for ( p = 2 ; p < n + 1 ; p ++ ) { if ( prime [ p ] && isTetradic ( p ) ) cout << p << " ▁ " ; } } int main ( ) { int n = 1000 ; printTetradicPrimesLessThanN ( n ) ; return 0 ; }
Astonishing Numbers | C ++ implementation for the above approach ; Function to concatenate two integers into one ; Convert both the integers to string ; Concatenate both strings ; Convert the concatenated string to integer ; return the formed integer ; Function to check if N is a Astonishing number ; Loop to find sum of all integers from i till the sum becomes >= n ; variable to store sum of all integers from i to j and check if sum and concatenation equals n or not ; finding concatenation of i and j ; condition for Astonishing number ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int concat ( int a , int b ) { string s1 = to_string ( a ) ; string s2 = to_string ( b ) ; string s = s1 + s2 ; int c = stoi ( s ) ; return c ; } bool isAstonishing ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += j ; if ( sum == n ) { int concatenation = concat ( i , j ) ; if ( concatenation == n ) { return true ; } } } } return false ; } int main ( ) { int n = 429 ; if ( isAstonishing ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Digitally balanced numbers | C ++ implementation to check if a number is a digitally balanced number ; Function to check if the digits in the number is the same number of digits ; Loop to iterate over the digits of the number N ; Loop to iterate over the map ; Driver Code ; function to check
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkSame ( int n , int b ) { map < int , int > m ; while ( n != 0 ) { int r = n % b ; n = n / b ; m [ r ] ++ ; } int last = -1 ; for ( auto i = m . begin ( ) ; i != m . end ( ) ; i ++ ) { if ( last != -1 && i -> second != last ) { return false ; } else { last = i -> second ; } } } int main ( ) { int n = 9 ; int base = 2 ; if ( checkSame ( n , base ) ) cout << " Yes " ; else cout << " NO " ; return 0 ; }
Sum of series formed by difference between product and sum of N natural numbers | C ++ program to implement the above approach ; Function to calculate the sum upto Nth term ; Stores the sum of the series ; Stores the product of natural numbers upto the current term ; Stores the sum of natural numbers upto the upto current term ; Generate the remaining terms and calculate sum ; Update the sum ; Return the sum ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int seriesSum ( int n ) { int sum = 0 ; int currProd = 1 ; int currSum = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { currProd *= i ; currSum += i ; sum += currProd - currSum ; } return sum ; } int main ( ) { int N = 5 ; cout << seriesSum ( N ) << " ▁ " ; }