codeparrot/xlcost-text-to-code huggingface dataset:
Pointer | C ++ implementation for the above approach ; Function to find the product of digits of a number N ; Function that returns true if n is prime else returns false ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; Function to check Pointer - Prime numbers ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } 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 nextPrime ( int N ) { if ( N <= 1 ) return 2 ; int prime = N ; bool found = false ; while ( ! found ) { prime ++ ; if ( isPrime ( prime ) ) found = true ; } return prime ; } bool isPointerPrime ( int n ) { if ( isPrime ( n ) && ( n + digProduct ( n ) == nextPrime ( n ) ) ) return true ; else return false ; } int main ( ) { int N = 23 ; if ( isPointerPrime ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Perfect Digital Invariants number | C ++ program for the above approach ; Function to calculate x raised to the power y ; Function to check whether the given number is Perfect Digital Invariant number or not ; For each digit in temp ; If satisfies Perfect Digital Invariant condition ; If sum exceeds n , then not possible ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , unsigned int y ) { if ( y == 0 ) { return 1 ; } if ( y % 2 == 0 ) { return ( power ( x , y / 2 ) * power ( x , y / 2 ) ) ; } return ( x * power ( x , y / 2 ) * power ( x , y / 2 ) ) ; } bool isPerfectDigitalInvariant ( int x ) { for ( int fixed_power = 1 ; ; fixed_power ++ ) { int temp = x , sum = 0 ; while ( temp ) { int r = temp % 10 ; sum += power ( r , fixed_power ) ; temp = temp / 10 ; } if ( sum == x ) { return true ; } if ( sum > x ) { return false ; } } } int main ( ) { int N = 4150 ; if ( isPerfectDigitalInvariant ( N ) ) cout << " Yes " ; else cout << " No " ; }
Brilliant Numbers | C ++ implementation for the above approach ; Function to generate all prime numbers less than n ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to return the number of digits in a number ; Function to check if N is a Brilliant number ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool SieveOfEratosthenes ( int n , bool isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } int countDigit ( long long n ) { return floor ( log10 ( n ) + 1 ) ; } bool isBrilliant ( int n ) { int flag = 0 ; bool isPrime [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 2 ; i < n ; i ++ ) { int x = n / i ; if ( isPrime [ i ] && isPrime [ x ] and x * i == n ) { if ( countDigit ( i ) == countDigit ( x ) ) return true ; } } return false ; } int main ( ) { int n = 1711 ; if ( isBrilliant ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Icosikaienneagonal Number | C ++ implementation for above approach ; Function to Find the Nth icosikaienneagonal Number ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int icosikaienneagonalNum ( int n ) { return ( 27 * n * n - 25 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << icosikaienneagonalNum ( n ) ; return 0 ; }
Find the largest contiguous pair sum in given Array | C ++ program to find the a contiguous pair from the which has the largest sum ; Function to find and return the largest sum contiguous pair ; Stores the contiguous pair ; Initialize maximum sum ; Compare sum of pair with max_sum ; Insert the pair ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > largestSumpair ( int arr [ ] , int n ) { vector < int > pair ; int max_sum = INT_MIN , i ; for ( i = 1 ; i < n ; i ++ ) { if ( max_sum < ( arr [ i ] + arr [ i - 1 ] ) ) { max_sum = arr [ i ] + arr [ i - 1 ] ; if ( pair . empty ( ) ) { pair . push_back ( arr [ i - 1 ] ) ; pair . push_back ( arr [ i ] ) ; } else { pair [ 0 ] = arr [ i - 1 ] ; pair [ 1 ] = arr [ i ] ; } } } return pair ; } int main ( ) { int arr [ ] = { 11 , -5 , 9 , -3 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < int > pair = largestSumpair ( arr , N ) ; for ( auto it = pair . begin ( ) ; it != pair . end ( ) ; ++ it ) { cout << * it << ' ▁ ' ; } return 0 ; }
Sum of minimum value of x and y satisfying the equation ax + by = c | C ++ program for the above approach ; x and y store solution of equation ax + by = g ; Euclidean Algorithm ; store_gcd returns the gcd of a and b ; Function to find any possible solution ; Condition if solution does not exists ; Adjusting the sign of x0 and y0 ; Function to shift solution ; Shifting to obtain another solution ; Function to find minimum value of x and y ; g is the gcd of a and b ; Store sign of a and b ; If x is less than 0 , then shift solution ; If y is less than 0 , then shift solution ; Find intersection such that both x and y are positive ; miny is value of y corresponding to minx ; Returns minimum value of x + y ; Driver Code ; Given a , b , and c ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b , int * x , int * y ) { if ( b == 0 ) { * x = 1 ; * y = 0 ; return a ; } int x1 , y1 ; int store_gcd = gcd ( b , a % b , & x1 , & y1 ) ; * x = y1 ; * y = x1 - y1 * ( a / b ) ; return store_gcd ; } int possible_solution ( int a , int b , int c , int * x0 , int * y0 , int * g ) { * g = gcd ( fabs ( a ) , fabs ( b ) , x0 , y0 ) ; if ( c % * g ) { return 0 ; } * x0 *= c / * g ; * y0 *= c / * g ; if ( a < 0 ) * x0 *= -1 ; if ( b < 0 ) * y0 *= -1 ; return 1 ; } void shift_solution ( int * x , int * y , int a , int b , int shift_var ) { * x += shift_var * b ; * y -= shift_var * a ; } int find_min_sum ( int a , int b , int c ) { int x , y , g ; if ( ! possible_solution ( a , b , c , & x , & y , & g ) ) return -1 ; a /= g ; b /= g ; int sign_a = a > 0 ? +1 : -1 ; int sign_b = b > 0 ? +1 : -1 ; shift_solution ( & x , & y , a , b , - x / b ) ; if ( x < 0 ) shift_solution ( & x , & y , a , b , sign_b ) ; int minx1 = x ; shift_solution ( & x , & y , a , b , y / a ) ; if ( y < 0 ) shift_solution ( & x , & y , a , b , - sign_a ) ; int minx2 = x ; if ( minx2 > x ) swap ( minx2 , x ) ; int minx = max ( minx1 , minx2 ) ; if ( minx > x ) return -1 ; int miny = ( c - a * x ) / b ; return ( miny + minx ) ; } int main ( ) { int a = 2 , b = 2 , c = 0 ; cout << find_min_sum ( a , b , c ) << " STRNEWLINE " ; return 0 ; }
Second hexagonal numbers | C ++ implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void findNthTerm ( int n ) { cout << n * ( 2 * n + 1 ) << endl ; } int main ( ) { int N = 4 ; findNthTerm ( N ) ; return 0 ; }
Duffinian Numbers | C ++ program for the above approach ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors of num ; if ' i ' is divisor of ' n ' ; If both divisors are same then add it once ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Function to check if n is an Duffinian number ; Calculate the sum of divisors ; If number is prime return false ; Find the gcd of n and sum of divisors of n ; Returns true if N and sumDivisors are relatively prime ; Driver Code ; Given Number ; Function Call
#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 + n + 1 ) ; } bool isDuffinian ( int n ) { int sumDivisors = divSum ( n ) ; if ( sumDivisors == n + 1 ) return false ; int hcf = __gcd ( n , sumDivisors ) ; return hcf == 1 ; } int main ( ) { int n = 36 ; if ( isDuffinian ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Kummer Numbers | C ++ program to find Kummer number upto n terms ; vector to store all prime less than and equal to 10 ^ 6 ; Function for the Sieve of Sundaram . This function stores all prime numbers less than MAX in primes ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to calculate nth Kummer number ; Multiply first n primes ; return nth Kummer number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000000 ; vector < int > primes ; void sieveSundaram ( ) { bool marked [ MAX / 2 + 1 ] = { 0 } ; for ( int i = 1 ; i <= ( sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j += 2 * i + 1 ) marked [ j ] = true ; primes . push_back ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . push_back ( 2 * i + 1 ) ; } int calculateKummer ( int n ) { int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result = result * primes [ i ] ; return -1 + result ; } int main ( ) { int n = 5 ; sieveSundaram ( ) ; for ( int i = 1 ; i <= n ; i ++ ) cout << calculateKummer ( i ) << " , ▁ " ; return 0 ; }
Check if given number contains a digit which is the average of all other digits | C ++ Program to check whether a given number contains a digit which is the average of all other digits ; Function which checks if a digits exists in n which is the average of all other digits ; Calculate sum of digits in n ; Store the digits ; Increase the count of digits in n ; If average of all digits is an integer ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int n ) { set < int > digits ; int temp = n ; int sum = 0 ; int count = 0 ; while ( temp > 0 ) { sum += temp % 10 ; digits . insert ( temp % 10 ) ; count ++ ; temp = temp / 10 ; } if ( sum % count == 0 && digits . find ( sum / count ) != digits . end ( ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int n = 42644 ; check ( n ) ; }
Count of lexicographically smaller characters on right |  ; function to count the smaller characters at the right of index i ; store the length of string ; for each index initialize count as zero ; increment the count if characters are smaller than at ith index ; print the count of characters smaller than the index i ; Driver code ; input string
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSmaller ( string str ) { int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( str [ j ] < str [ i ] ) { cnt += 1 ; } } cout << cnt << " ▁ " ; } } int main ( ) { string str = " edcba " ; countSmaller ( str ) ; }
Find the smallest contiguous sum pair in an Array | C ++ program to find the smallest sum contiguous pair ; Function to find the smallest sum contiguous pair ; Contiguous pair ; Initialize minimum sum with maximum value ; Checking for minimum value ; Add to pair ; Updating pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > smallestSumpair ( int arr [ ] , int n ) { vector < int > pair ; int min_sum = INT_MAX ; for ( int i = 1 ; i < n ; i ++ ) { if ( min_sum > ( arr [ i ] + arr [ i - 1 ] ) ) { min_sum = arr [ i ] + arr [ i - 1 ] ; if ( pair . empty ( ) ) { pair . push_back ( arr [ i - 1 ] ) ; pair . push_back ( arr [ i ] ) ; } else { pair [ 0 ] = arr [ i - 1 ] ; pair [ 1 ] = arr [ i ] ; } } } return pair ; } int main ( ) { int arr [ ] = { 4 , 9 , -3 , 2 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < int > pair = smallestSumpair ( arr , n ) ; cout << pair [ 0 ] << " ▁ " << pair [ 1 ] ; }
Add two integers of different base and represent sum in smaller base of the two | Program to find the sum of two integers of different bases . ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; Maximum base - 36 ; To reverse the string ; Driver Code ; Integer in base 10 ; Sum of integers ; Minimum Base ; Sum of integers in Min Base
#include <bits/stdc++.h> NEW_LINE using namespace std ; int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } int convert ( string num , int base ) { int len = ( num . size ( ) ) ; int power = 1 ; int res = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { res += val ( num [ i ] ) * power ; power = power * base ; } return res ; } int dec_to_base ( int num , int base ) { string base_num = " " ; while ( num > 0 ) { int dig = int ( num % base ) ; if ( dig < 10 ) base_num += to_string ( dig ) ; else base_num += to_string ( ' A ' + dig - 10 ) ; num /= base ; } reverse ( base_num . begin ( ) , base_num . end ( ) ) ; return stoi ( base_num ) ; } int main ( ) { string a = "123" ; string b = "234" ; int base_a = 6 ; int base_b = 8 ; int a10 = convert ( a , base_a ) ; int b10 = convert ( b , base_b ) ; int summ = a10 + b10 ; int min_base = min ( base_a , base_b ) ; cout << ( dec_to_base ( summ , min_base ) ) ; }
Sum of the products of same placed digits of two numbers | C ++ program to calculate the sum of same placed digits of two numbers ; Function to find the sum of the products of their corresponding digits ; Loop until one of the numbers have no digits remaining ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfProductOfDigits ( int n1 , int n2 ) { int sum = 0 ; while ( n1 > 0 && n2 > 0 ) { sum += ( ( n1 % 10 ) * ( n2 % 10 ) ) ; n1 /= 10 ; n2 /= 10 ; } return sum ; } int main ( ) { int n1 = 25 ; int n2 = 1548 ; cout << sumOfProductOfDigits ( n1 , n2 ) ; }
Print all permutations of a number N greater than itself | C ++ implementation to print all the permutation greater than the integer N ; Function to print all the permutation which are greater than N itself ; Iterate and count the number of digits in N ; vector to print the permutations of N ; Store digits of N in the vector num ; Iterate over every permutation of N which is greater than N ; Print the current permutation of N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPermutation ( int N ) { int temp = N , count = 0 ; while ( temp > 0 ) { count ++ ; temp /= 10 ; } vector < int > num ( count ) ; while ( N > 0 ) { num [ count -- - 1 ] = N % 10 ; N = N / 10 ; } while ( next_permutation ( num . begin ( ) , num . end ( ) ) ) { for ( int i = 0 ; i < num . size ( ) ; i ++ ) cout << num [ i ] ; cout << " STRNEWLINE " ; } } int main ( ) { int N = 324 ; printPermutation ( N ) ; return 0 ; }
Count of ways to represent N as sum of a prime number and twice of a square | C ++ implementation to count the number of ways a number can be written as sum of prime number and twice a square ; Function to mark all the prime numbers using sieve ; Initially all the numbers are marked as prime ; Loop to mark the prime numbers upto the Square root of N ; Loop to store the prime numbers in an array ; Function to find the number ways to represent a number as the sum of prime number and square of a number ; Loop to iterate over all the possible prime numbers ; Increment the count if the given number is a valid number ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int n = 500000 - 2 ; vector < long long int > v ; void sieveoferanthones ( ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( long long int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( prime [ i ] ) for ( long long int j = i * i ; j <= n ; j += i ) { prime [ j ] = false ; } } for ( long long int i = 2 ; i < n ; i ++ ) { if ( prime [ i ] ) v . push_back ( i ) ; } } void numberOfWays ( long long int n ) { long long int count = 0 ; for ( long long int j = 1 ; 2 * ( pow ( j , 2 ) ) < n ; j ++ ) { for ( long long int i = 1 ; v [ i ] + 2 <= n ; i ++ ) { if ( n == v [ i ] + ( 2 * ( pow ( j , 2 ) ) ) ) count ++ ; } } cout << count << endl ; } int main ( ) { sieveoferanthones ( ) ; long long int n = 9 ; numberOfWays ( n ) ; return 0 ; }
Program to convert Degree to Radian | C ++ program to convert degree to radian ; Function for conversion ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; double Convert ( double degree ) { double pi = 3.14159265359 ; return ( degree * ( pi / 180 ) ) ; } int main ( ) { double degree = 30 ; double radian = Convert ( degree ) ; cout << radian ; return 0 ; }
Program to convert a Binary Number to Hexa | C ++ code to convert Binary to its HexaDecimal number ( base 16 ) . ; Function to convert Binary to HexaDecimal ; Iterating through the bits backwards ; Computing the HexaDecimal Number formed so far and storing it in a vector . ; Reinitializing all variables for next group . ; Printing the Hexadecimal number formed so far . ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void bToHexaDecimal ( string s ) { int len = s . length ( ) , check = 0 ; int num = 0 , sum = 0 , mul = 1 ; vector < char > ans ; for ( int i = len - 1 ; i >= 0 ; i -- ) { sum += ( s [ i ] - '0' ) * mul ; mul *= 2 ; check ++ ; if ( check == 4 i == 0 ) { if ( sum <= 9 ) ans . push_back ( sum + '0' ) ; else ans . push_back ( sum + 55 ) ; check = 0 ; sum = 0 ; mul = 1 ; } } len = ans . size ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) cout << ans [ i ] ; } int main ( ) { string s = "100000101111" ; bToHexaDecimal ( s ) ; return 0 ; }
Find the sum of the first N Dodecagonal Numbers | C ++ program to find the sum of the first N dodecagonal numbers ; Function to find the N - th dodecagonal number ; Formula to calculate N - th dodecagonal number ; Function to find the sum of the first N dodecagonal numbers ; Variable to get the sum ; Iterating through the first N numbers ; Compute the sum ; Driver Code ; Display first Nth centered_decagonal number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Dodecagonal_num ( int n ) { return ( 5 * n * n - 4 * n ) ; } int sum_Dodecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Dodecagonal_num ( i ) ; } return summ ; } int main ( ) { int n = 5 ; cout << ( sum_Dodecagonal_num ( n ) ) ; return 0 ; }
Program to check if N is a Myriagon Number | C ++ program for the above approach ; Function to check if N is a Myriagon Number ; Condition to check if the number is a Myriagon number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isMyriagon ( int N ) { float n = ( 9996 + sqrt ( 79984 * N + 99920016 ) ) / 19996 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 10000 ; if ( isMyriagon ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Heptagonal Number | C ++ program for the above approach ; Function to check if N is a Heptagonal number ; Condition to check if the number is a heptagonal number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isheptagonal ( int N ) { float n = ( 3 + sqrt ( 40 * N + 9 ) ) / 10 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 7 ; if ( isheptagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Enneadecagonal Number | C ++ program for the above approach ; Function to check if number N is a enneadecagonal number ; Condition to check if N is a enneadecagonal number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isenneadecagonal ( int N ) { float n = ( 15 + sqrt ( 136 * N + 225 ) ) / 34 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 19 ; if ( isenneadecagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Centered Tridecagonal Number | C ++ program for the above approach ; Function to check if the number N is a Centered tridecagonal number ; Condition to check if the N is a Centered tridecagonal number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCenteredtridecagonal ( int N ) { float n = ( 13 + sqrt ( 104 * N + 65 ) ) / 26 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 14 ; if ( isCenteredtridecagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Orbit counting theorem or Burnside 's Lemma | C ++ program for implementing the Orbit counting theorem or Burnside 's Lemma ; Function to find result using Orbit counting theorem or Burnside 's Lemma ; According to Burnside 's Lemma  calculate distinct ways for each  rotation ; Find GCD ; Divide By N ; Print the distinct ways ; Driver Code ; N stones and M colors ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countDistinctWays ( int N , int M ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int K = __gcd ( i , N ) ; ans += pow ( M , K ) ; } ans /= N ; cout << ans << endl ; } int main ( ) { int N = 4 , M = 3 ; countDistinctWays ( N , M ) ; return 0 ; }
Find the Nth natural number which is not divisible by A | C ++ code to Find the Nth number which is not divisible by A from the series ; Find the quotient and the remainder when k is divided by n - 1 ; If the remainder is not 0 multiply n by q and subtract 1 if remainder is 0 then multiply n by q and add the remainder ; Print the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNum ( int n , int k ) { int q = k / ( n - 1 ) ; int r = k % ( n - 1 ) ; int a ; if ( r != 0 ) a = ( n * q ) + r ; else a = ( n * q ) - 1 ; cout << a ; } int main ( ) { int A = 4 , N = 6 ; findNum ( A , N ) ; return 0 ; }
Count the nodes in the given Tree whose weight is a Perfect Number | C ++ implementation to Count the nodes in the given tree whose weight is a Perfect Number ; Function that returns true if n is perfect ; Variable to store sum of divisors ; Find all divisors and add them ; Check if sum of divisors is equal to n , then n is a perfect number ; Function to perform dfs ; If weight of the current node is a perfect number ; Driver code ; Weights of the node ; Edges of the tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = 0 ; vector < int > graph [ 100 ] ; vector < int > weight ( 100 ) ; bool isPerfect ( long long int n ) { long long int sum = 1 ; for ( long long int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } void dfs ( int node , int parent ) { if ( isPerfect ( weight [ node ] ) ) ans += 1 ; for ( int to : graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; } } int main ( ) { weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . push_back ( 2 ) ; graph [ 2 ] . push_back ( 3 ) ; graph [ 2 ] . push_back ( 4 ) ; graph [ 1 ] . push_back ( 5 ) ; dfs ( 1 , 1 ) ; cout << ans ; return 0 ; }