codeparrot/xlcost-text-to-code huggingface dataset:
Check if number can be made prime by deleting a single digit | C ++ implementation to check if a number becomes prime by deleting any digit ; 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 delete character at index i from given string str ; Deletes character at position 4 ; Function to check if a number becomes prime by deleting any digit ; Converting the number to string ; length of string ; number should not be of single digit ; Loop to find all numbers after deleting a single digit ; Deleting ith character from the string ; converting string to int ; 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 ; } string deleteIth ( string str , int i ) { str . erase ( str . begin ( ) + i ) ; return str ; } bool isPrimePossible ( int N ) { string s = to_string ( N ) ; int l = s . length ( ) ; if ( l < 2 ) return false ; for ( int i = 0 ; i < l ; i ++ ) { string str = deleteIth ( s , i ) ; int num = stoi ( str ) ; if ( isPrime ( num ) ) return true ; } return false ; } int main ( ) { int N = 610 ; isPrimePossible ( N ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Check if a number has an odd count of odd divisors and even count of even divisors | C ++ implementation of the above approach ; Function to find the count of even and odd factors of N ; Loop runs till square root ; Condition to check if the even factors of the number N is is even and count of odd factors is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE void checkFactors ( lli N ) { lli ev_count = 0 , od_count = 0 ; for ( lli i = 1 ; i <= sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } else { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; if ( ( N / i ) % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } } } if ( ev_count % 2 == 0 && od_count % 2 == 1 ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { lli N = 36 ; checkFactors ( N ) ; return 0 ; }
Count of distinct permutations of length N having no similar adjacent characters | C ++ Program to implement the above approach ; Function to print the number of permutations possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countofPermutations ( int N ) { return int ( 3 * pow ( 2 , N - 1 ) ) ; } int main ( ) { int N = 5 ; cout << countofPermutations ( N ) ; return 0 ; }
Find two distinct numbers such that their LCM lies in given range | C ++ program for the above approach ; Function to find two distinct numbers X and Y s . t . their LCM lies between L and R and X , Y are minimum possible ; Check if 2 * L lies in range L , R ; Print the answer ; Driver Code ; Given value of ranges ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void answer ( int L , int R ) { if ( 2 * L <= R ) cout << L << " , ▁ " << 2 * L << " STRNEWLINE " ; else cout << -1 ; } int main ( ) { int L = 3 , R = 8 ; answer ( L , R ) ; return 0 ; }
Maximum count of pairwise co | C ++ program for the above approach ; Function to find the gcd of two numbers ; Function to of pairwise co - prime and common divisors of two numbers ; Initialize answer with 1 , to include 1 in the count ; Count of primes of gcd ( N , M ) ; Finding prime factors of gcd ; Increment count if it is divisible by i ; Return the total count ; Function Call for each pair to calculate the count of pairwise co - prime divisors ; Driver Code ; Given array of pairs ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int x , int y ) { if ( x % y == 0 ) return y ; else return gcd ( y , x % y ) ; } int countPairwiseCoprime ( int N , int M ) { int answer = 1 ; int g = gcd ( N , M ) ; int temp = g ; for ( int i = 2 ; i * i <= g ; i ++ ) { if ( temp % i == 0 ) { answer ++ ; while ( temp % i == 0 ) temp /= i ; } } if ( temp != 1 ) answer ++ ; return answer ; } void countCoprimePair ( int arr [ ] [ 2 ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << countPairwiseCoprime ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) << ' ▁ ' ; } } int main ( ) { int arr [ ] [ 2 ] = { { 12 , 18 } , { 420 , 660 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countCoprimePair ( arr , N ) ; return 0 ; }
Product of absolute difference of every pair in given Array | C ++ program for the above approach ; Function to return the product of abs diff of all pairs ( x , y ) ; To store product ; Iterate all possible pairs ; Find the product ; Return product ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getProduct ( int a [ ] , int n ) { int p = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { p *= abs ( a [ i ] - a [ j ] ) ; } } return p ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getProduct ( arr , N ) ; return 0 ; }
Check whether a number can be represented as difference of two consecutive cubes | C ++ program for the above approach ; Function to print the two consecutive numbers whose difference is N ; Iterate in the range [ 0 , 10 ^ 5 ] ; Function to check if N is a perfect cube ; Find floating point value of square root of x . ; If square root is an integer ; Function to check whether a number can be represented as difference of two consecutive cubes ; Check if 12 * N - 3 is a perfect square or not ; Driver Code ; Given Number N
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print ( int N ) { for ( int i = 0 ; i < 100000 ; i ++ ) { if ( pow ( i + 1 , 3 ) - pow ( i , 3 ) == N ) { cout << i << ' ▁ ' << i + 1 ; return ; } } } bool isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ; } bool diffCube ( int N ) { return isPerfectSquare ( 12 * N - 3 ) ; } int main ( ) { int N = 19 ; if ( diffCube ( N ) ) { cout << " Yes STRNEWLINE " ; print ( N ) ; } else { cout << " No STRNEWLINE " ; } return 0 ; }
Sum of bit differences for numbers from 0 to N | Set 2 | C ++ program to find the sum of bit differences between consecutive numbers from 0 to N using recursion ; Recursive function to find sum of different bits between consecutive numbers from 0 to N ; Base case ; Calculate the Nth term ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalCountDifference ( int n ) { if ( n == 1 ) return 1 ; return n + totalCountDifference ( n / 2 ) ; } int main ( ) { int N = 5 ; cout << totalCountDifference ( N ) ; return 0 ; }
Sum of elements of a Geometric Progression ( GP ) in a given range | C ++ program to find the sum of elements of an GP in the given range ; Function to find sum in the given range ; Find the value of k ; Find the common difference ; Find the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int arr [ ] , int n , int left , int right ) { int k = right - left + 1 ; int d = arr [ 1 ] / arr [ 0 ] ; int ans = arr [ left - 1 ] ; if ( d == 1 ) ans = ans * d * k ; else ans = ans * ( ( int ) pow ( d , k ) - 1 / ( d - 1 ) ) ; return ans ; } int main ( ) { int arr [ ] = { 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 } ; int queries = 3 ; int q [ ] [ 2 ] = { { 2 , 4 } , { 2 , 6 } , { 5 , 8 } } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; for ( int i = 0 ; i < queries ; i ++ ) cout << ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) << endl ; return 0 ; }
Find the first and last M digits from K | C ++ Program to implement the above approach ; Function to find a ^ b modulo M ; Function to find the first and last M digits from N ^ K ; Calculate Last M digits ; Calculate First M digits ; Extract the number after decimal ; Find 10 ^ y ; Move the Decimal Point M - 1 digits forward ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll modPower ( ll a , ll b , ll M ) { ll res = 1 ; while ( b ) { if ( b & 1 ) res = res * a % M ; a = a * a % M ; b >>= 1 ; } return res ; } void findFirstAndLastM ( ll N , ll K , ll M ) { ll lastM = modPower ( N , K , ( 1LL ) * pow ( 10 , M ) ) ; ll firstM ; double y = ( double ) K * log10 ( N * 1.0 ) ; y = y - ( ll ) y ; double temp = pow ( 10.0 , y ) ; firstM = temp * ( 1LL ) * pow ( 10 , M - 1 ) ; cout << firstM << " ▁ " << lastM << endl ; } int main ( ) { ll N = 12 , K = 12 , M = 4 ; findFirstAndLastM ( N , K , M ) ; return 0 ; }
Minimize increment / decrement of Array elements to make each modulo K equal | C ++ program for the above approach ; Function to find the minimum operations required to make the modulo of each element of the array equal to each other ; Variable to store minimum operation required ; To store operation required to make all modulo equal ; Iterating through all possible modulo value ; Iterating through all different modulo obtained so far ; Calculating oprn required to make all modulos equal to x ; Checking the operations that will cost less ; Check operation that will cost less ; Update the minimum number of operations ; Returning the answer ; Function to store different modulos ; Set to store all different modulo ; Map to store count of all different modulo obtained ; Storing all different modulo count ; Insert into the set ; Increment count ; Function call to return value of min oprn required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Find_min ( set < int > & diff_mod , map < int , int > count_mod , int k ) { int min_oprn = INT_MAX ; int oprn = 0 ; for ( int x = 0 ; x < k ; x ++ ) { oprn = 0 ; for ( auto w : diff_mod ) { if ( w != x ) { if ( w == 0 ) { oprn += min ( x , k - x ) * count_mod [ w ] ; } else { oprn += min ( abs ( x - w ) , k + x - w ) * count_mod [ w ] ; } } } if ( oprn < min_oprn ) min_oprn = oprn ; } return min_oprn ; } int Cal_min ( int arr [ ] , int n , int k ) { set < int > diff_mod ; map < int , int > count_mod ; for ( int i = 0 ; i < n ; i ++ ) { diff_mod . insert ( arr [ i ] % k ) ; count_mod [ arr [ i ] % k ] ++ ; } return Find_min ( diff_mod , count_mod , k ) ; } int main ( ) { int arr [ ] = { 2 , 35 , 48 , 23 , 52 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << Cal_min ( arr , n , k ) ; return 0 ; }
Minimize Steps required to obtain Sorted Order of an Array | C ++ Program to implement the above approach ; Function to find GCD of two numbers ; Function to calculate the LCM of array elements ; Initialize result ; Function to find minimum steps required to obtain sorted sequence ; Inititalize dat [ ] array for Direct Address Table . ; Calculating steps required for each element to reach its sorted position ; Calculate LCM of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int findlcm ( int arr [ ] , int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; return ans ; } void minimumSteps ( int arr [ ] , int n ) { int i , dat [ n + 1 ] ; for ( i = 1 ; i <= n ; i ++ ) dat [ arr [ i - 1 ] ] = i ; int b [ n + 1 ] , j = 0 , c ; for ( i = 1 ; i <= n ; i ++ ) { c = 1 ; j = dat [ i ] ; while ( j != i ) { c ++ ; j = dat [ j ] ; } b [ i ] = c ; } cout << findlcm ( b , n ) ; } int main ( ) { int arr [ ] = { 5 , 1 , 4 , 3 , 2 , 7 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumSteps ( arr , N ) ; return 0 ; }
Largest Ratio Contiguous subarray | C ++ program for the above approach ; Function to return maximum of two double values ; Check if a is greater than b then return a ; Function that returns the Ratio of max Ratio subarray ; Variable to store the maximum ratio ; Compute the product while traversing for subarrays ; Calculate the ratio ; Update max ratio ; Print the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double maximum ( double a , double b ) { if ( a > b ) return a ; return b ; } double maxSubarrayRatio ( double arr [ ] , int n ) { double maxRatio = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { double ratio = arr [ i ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { ratio = ratio / arr [ k ] ; } maxRatio = maximum ( maxRatio , ratio ) ; } } return maxRatio ; } int main ( ) { double arr [ ] = { 2 , 2 , 4 , -0.2 , -1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSubarrayRatio ( arr , n ) ; return 0 ; }
Even Perfect Number | C ++ program for the above approach ; Function to check for perfect number ; Find a number close to 2 ^ q - 1 ; Calculate q - 1 ; Condition of perfect number ; Check whether q is prime or not ; Check whether 2 ^ q - 1 is a prime number or not ; Function to check for prime number ; Check whether it is equal to 2 or 3 ; Check if it can be divided by 2 and 3 then it is not prime number ; Check whether the given number be divide by other prime numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( long n ) ; void check ( long num ) { long root = ( long ) sqrt ( num ) ; long poww = ( long ) ( log ( root ) / log ( 2 ) ) ; if ( num == ( long ) ( pow ( 2 , poww ) * ( pow ( 2 , poww + 1 ) - 1 ) ) ) { if ( isPrime ( poww + 1 ) ) { if ( isPrime ( ( long ) pow ( 2 , poww + 1 ) - 1 ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } else cout << " No " << endl ; } else cout << " No " << endl ; } bool isPrime ( long n ) { if ( n <= 1 ) return false ; else if ( n == 2 n == 3 ) return true ; else { if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i <= sqrt ( n ) ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; } return true ; } } int main ( ) { long num = 6 ; check ( num ) ; return 0 ; }
Twin Pythagorean triplets in an array | C ++ program for the above approach ; Function to check if there exist a twin pythagorean triplet in the given array ; Loop to check if there is a Pythagorean triplet in the array ; Check if there is consecutive triple ; Calculate square of array elements ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isTriplet ( int ar [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( abs ( ar [ i ] - ar [ j ] ) == 1 || abs ( ar [ j ] - ar [ k ] ) == 1 || abs ( ar [ i ] - ar [ k ] ) == 1 ) { int x = ar [ i ] * ar [ i ] , y = ar [ j ] * ar [ j ] , z = ar [ k ] * ar [ k ] ; if ( x == y + z y == x + z z == x + y ) return true ; } } } } return false ; } int main ( ) { int arr [ ] = { 3 , 1 , 4 , 6 , 5 } ; int ar_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; isTriplet ( arr , ar_size ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Pair with min absolute difference and whose product is N + 1 or N + 2 | C ++ program for the above approach ; Function to print pair ( a , b ) such that a * b = N + 1 or N + 2 ; Loop to iterate over the desired possible values ; Check for condition 1 ; Check for condition 2 ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void closestDivisors ( int n ) { for ( int i = sqrt ( n + 2 ) ; i > 0 ; i -- ) { if ( ( n + 1 ) % i == 0 ) { cout << i << " , ▁ " << ( n + 1 ) / i ; break ; } if ( ( n + 2 ) % i == 0 ) { cout << i << " , ▁ " << ( n + 2 ) / i ; break ; } } } int main ( ) { int N = 123 ; closestDivisors ( N ) ; }
Is it possible to reach N and M from 1 and 0 respectively as per given condition | C ++ program for the above approach ; Function that find given x and y is possible or not ; Check if x is less than 2 and y is not equal to 0 ; Perform subtraction ; Check if y is divisible by 2 and greater than equal to 0 ; Driver Code ; Given X and Y ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_possible ( int x , int y ) { if ( x < 2 && y != 0 ) return false ; y = y - x + 1 ; if ( y % 2 == 0 && y >= 0 ) return true ; else return false ; } int main ( ) { int x = 5 , y = 2 ; if ( is_possible ( x , y ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of integers up to N which are non divisors and non coprime with N | C ++ Program to implement the above approach ; Function to return the count of integers less than N satisfying given conditions ; Stores Euler counts ; Store Divisor counts ; Based on Sieve of Eratosthenes ; Update phi values of all multiples of i ; Update count of divisors ; Return the final count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { int phi [ n + 1 ] = { 0 } ; int divs [ n + 1 ] = { 0 } ; for ( int i = 1 ; i <= n ; i ++ ) { phi [ i ] += i ; for ( int j = i * 2 ; j <= n ; j += i ) phi [ j ] -= phi [ i ] ; for ( int j = i ; j <= n ; j += i ) divs [ j ] ++ ; } return ( n - phi [ n ] - divs [ n ] + 1 ) ; } int main ( ) { int N = 42 ; cout << count ( N ) ; return 0 ; }
Sum of all differences between Maximum and Minimum of increasing Subarrays | C ++ Program to find the sum of differences of maximum and minimum of strictly increasing subarrays ; Function to calculate and return the sum of differences of maximum and minimum of strictly increasing subarrays ; Stores the sum ; Traverse the array ; If last element of the increasing sub - array is found ; Update sum ; If the last element of the array is reached ; Update sum ; Return the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_of_differences ( int arr [ ] , int N ) { int sum = 0 ; int i , j , flag ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { flag = 0 ; for ( j = i + 1 ; j < N - 1 ; j ++ ) { if ( arr [ j ] >= arr [ j + 1 ] ) { sum += ( arr [ j ] - arr [ i ] ) ; i = j ; flag = 1 ; break ; } } if ( flag == 0 && arr [ i ] < arr [ N - 1 ] ) { sum += ( arr [ N - 1 ] - arr [ i ] ) ; break ; } } } return sum ; } int main ( ) { int arr [ ] = { 6 , 1 , 2 , 5 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sum_of_differences ( arr , N ) ; return 0 ; }
Construct a Maximum Binary Tree from two given Binary Trees | C ++ program to find the Maximum Binary Tree from two Binary Trees ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper method that allocates a new node with the given data and NULL left and right pointers . ; Given a binary tree , print its nodes in inorder ; First recur on left child ; Then print the data of node ; Now recur on right child ; Method to find the maximum binary tree from two binary trees ; Driver Code ; First Binary Tree 3 / \ 2 6 / 20 ; Second Binary Tree 5 / \ 1 8 \ \ 2 8
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * left , * right ; Node ( int data , Node * left , Node * right ) { this -> data = data ; this -> left = left ; this -> right = right ; } } ; Node * newNode ( int data ) { Node * tmp = new Node ( data , NULL , NULL ) ; return tmp ; } void inorder ( Node * node ) { if ( node == NULL ) return ; inorder ( node -> left ) ; cout << node -> data << " ▁ " ; inorder ( node -> right ) ; } Node * MaximumBinaryTree ( Node * t1 , Node * t2 ) { if ( t1 == NULL ) return t2 ; if ( t2 == NULL ) return t1 ; t1 -> data = max ( t1 -> data , t2 -> data ) ; t1 -> left = MaximumBinaryTree ( t1 -> left , t2 -> left ) ; t1 -> right = MaximumBinaryTree ( t1 -> right , t2 -> right ) ; return t1 ; } int main ( ) { Node * root1 = newNode ( 3 ) ; root1 -> left = newNode ( 2 ) ; root1 -> right = newNode ( 6 ) ; root1 -> left -> left = newNode ( 20 ) ; Node * root2 = newNode ( 5 ) ; root2 -> left = newNode ( 1 ) ; root2 -> right = newNode ( 8 ) ; root2 -> left -> right = newNode ( 2 ) ; root2 -> right -> right = newNode ( 8 ) ; Node * root3 = MaximumBinaryTree ( root1 , root2 ) ; inorder ( root3 ) ; }
Minimum size binary string required such that probability of deleting two 1 's at random is 1/X | C ++ implementation of the above approach ; Function returns the minimum size of the string ; From formula ; Left limit of r ; Right limit of r ; Smallest integer in the valid range ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumString ( int x ) { int b = 1 ; double left_lim = sqrt ( x ) + 1.0 ; double right_lim = sqrt ( x ) + 2.0 ; int r ; for ( int i = left_lim ; i <= right_lim ; i ++ ) { if ( i > left_lim and i < right_lim ) { r = i ; break ; } } return b + r ; } int main ( ) { int X = 2 ; cout << MinimumString ( X ) ; return 0 ; }
Minimum number of squares whose sum equals to a given number N | Set | C ++ program for the above approach ; Function that returns true if N is a perfect square ; Function that returns true check if N is sum of three squares ; Factor out the powers of 4 ; N is NOT of the form 4 ^ a * ( 8 b + 7 ) ; Function that finds the minimum number of square whose sum is N ; If N is perfect square ; If N is sum of 2 perfect squares ; If N is sum of 3 perfect squares ; Otherwise , N is the sum of 4 perfect squares ; Driver code ; Given number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int N ) { int floorSqrt = sqrt ( N ) ; return ( N == floorSqrt * floorSqrt ) ; } bool legendreFunction ( int N ) { while ( N % 4 == 0 ) N /= 4 ; if ( N % 8 != 7 ) return true ; else return false ; } int minSquares ( int N ) { if ( isPerfectSquare ( N ) ) return 1 ; for ( int i = 1 ; i * i < N ; i ++ ) { if ( isPerfectSquare ( N - i * i ) ) return 2 ; } if ( legendreFunction ( N ) ) return 3 ; return 4 ; } int main ( ) { int N = 123 ; cout << minSquares ( N ) ; return 0 ; }
Check if N leaves only distinct remainders on division by all values up to K | C ++ Program to check if all remainders are distinct or not ; Function to check and return if all remainders are distinct ; Stores the remainder ; Calculate the remainder ; If remainder already occurred ; Insert into the set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_distinct ( long long n , long long k ) { unordered_set < long long > s ; for ( int i = 1 ; i <= k ; i ++ ) { long long tmp = n % i ; if ( s . find ( tmp ) != s . end ( ) ) { return false ; } s . insert ( tmp ) ; } return true ; } int main ( ) { long long N = 5 , K = 3 ; if ( is_distinct ( N , K ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of multiplicative partitions of N | C ++ implementation to find the multiplicative partitions of the given number N ; Function to return number of ways of factoring N with all factors greater than 1 ; Variable to store number of ways of factoring n with all factors greater than 1 ; Driver code ; 2 is the minimum factor of number other than 1. So calling recursive function to find number of ways of factoring N with all factors greater than 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; static int getDivisors ( int min , int n ) { int total = 0 ; for ( int i = min ; i < n ; ++ i ) { if ( n % i == 0 && n / i >= i ) { ++ total ; if ( n / i > i ) total += getDivisors ( i , n / i ) ; } } return total ; } int main ( ) { int n = 30 ; cout << 1 + getDivisors ( 2 , n ) ; return 0 ; }