codeparrot/xlcost-text-to-code huggingface dataset:
Check if frequency of each digit is less than the digit | A C ++ program to validate a number ; Function to validate number ( Check iffrequency of a digit is less than thedigit itself or not ) ; If current digit of temp is same as i ; if frequency is greater than digit value , return false ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool validate ( long long int n ) { for ( int i = 0 ; i < 10 ; i ++ ) { long long int temp = n ; int count = 0 ; while ( temp ) { if ( temp % 10 == i ) count ++ ; if ( count > i ) return false ; temp /= 10 ; } } return true ; } int main ( ) { long long int n = 1552793 ; if ( validate ( n ) ) cout << " True " ; else cout << " False " ; return 0 ; }
Check if a larger number divisible by 36 | C ++ implementation to check divisibility by 36 ; Function to check whether a number is divisible by 36 or not ; null number cannot be divisible by 36 ; single digit number other than 0 is not divisible by 36 ; number formed by the last 2 digits ; if number is not divisible by 4 ; number is divisible by 4 calculate sum of digits ; sum of digits is not divisible by 9 ; number is divisible by 4 and 9 hence , number is divisible by 36 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string divisibleBy36 ( string num ) { int l = num . length ( ) ; if ( l == 0 ) return " No " ; if ( l == 1 && num [ 0 ] != '0' ) return " No " ; int two_digit_num = ( num [ l - 2 ] - '0' ) * 10 + ( num [ l - 1 ] - '0' ) ; if ( two_digit_num % 4 != 0 ) return " No " ; int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) sum += ( num [ i ] - '0' ) ; if ( sum % 9 != 0 ) return " No " ; return " Yes " ; } int main ( ) { string num = "92567812197966231384" ; cout << divisibleBy36 ( num ) ; return 0 ; }
Check if a large number is divisible by 8 or not | C ++ program to find if a number is divisible by 8 or not ; Function to find that number divisible by 8 or not ; Empty string ; If there is single digit ; If there is double digit ; If number formed by last three digits is divisible by 8. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string str ) { int n = str . length ( ) ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str [ 0 ] - '0' ) % 8 == 0 ) ; if ( n == 2 ) return ( ( ( str [ n - 2 ] - '0' ) * 10 + ( str [ n - 1 ] - '0' ) ) % 8 == 0 ) ; int last = str [ n - 1 ] - '0' ; int second_last = str [ n - 2 ] - '0' ; int third_last = str [ n - 3 ] - '0' ; return ( ( third_last * 100 + second_last * 10 + last ) % 8 == 0 ) ; } int main ( ) { string str = "76952" ; check ( str ) ? cout << " Yes " : cout << " No ▁ " ; return 0 ; }
Prime points ( Points that split a number into two primes ) | C ++ program to print all prime points ; Function to count number of digits ; Function to check whether a number is prime or not . Returns 0 if prime else - 1 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to print prime points ; counting digits ; As single and double digit numbers do not have left and right number pairs ; Finding all left and right pairs . Printing the prime points accordingly . Discarding first and last index point ; Calculating left number ; Calculating right number ; Prime point condition ; No prime point found ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n / 10 ; } return count ; } int checkPrime ( int n ) { if ( n <= 1 ) return -1 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 n % 3 == 0 ) return -1 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return -1 ; return 0 ; } void printPrimePoints ( int n ) { int count = countDigits ( n ) ; if ( count == 1 count == 2 ) { cout << " - 1" ; return ; } bool found = false ; for ( int i = 1 ; i < ( count - 1 ) ; i ++ ) { int left = n / ( ( int ) pow ( 10 , count - i ) ) ; int right = n % ( ( int ) pow ( 10 , count - i - 1 ) ) ; if ( checkPrime ( left ) == 0 && checkPrime ( right ) == 0 ) { cout << i << " ▁ " ; found = true ; } } if ( found == false ) cout << " - 1" ; } int main ( ) { int n = 2317 ; printPrimePoints ( n ) ; return 0 ; }
Find ways an Integer can be expressed as sum of n | C ++ program to count number of ways any given integer x can be expressed as n - th power of unique natural numbers . ; Function to calculate and return the power of any given number ; Function to check power representations recursively ; Initialize number of ways to express x as n - th powers of different natural numbers ; Calling power of ' i ' raised to ' n ' ; Recursively check all greater values of i ; If sum of powers is equal to x then increase the value of result . ; Return the final result ; Driver Code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int num , unsigned int n ) { if ( n == 0 ) return 1 ; else if ( n % 2 == 0 ) return power ( num , n / 2 ) * power ( num , n / 2 ) ; else return num * power ( num , n / 2 ) * power ( num , n / 2 ) ; } int checkRecursive ( int x , int n , int curr_num = 1 , int curr_sum = 0 ) { int results = 0 ; int p = power ( curr_num , n ) ; while ( p + curr_sum < x ) { results += checkRecursive ( x , n , curr_num + 1 , p + curr_sum ) ; curr_num ++ ; p = power ( curr_num , n ) ; } if ( p + curr_sum == x ) results ++ ; return results ; } int main ( ) { int x = 10 , n = 2 ; cout << checkRecursive ( x , n ) ; return 0 ; }
Number Theory | Generators of finite cyclic group under addition | A simple C ++ program to find all generators ; Function to return gcd of a and b ; Print generators of n ; 1 is always a generator ; A number x is generator of GCD is 1 ; Driver program to test above function
#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 printGenerators ( unsigned int n ) { cout << "1 ▁ " ; for ( int i = 2 ; i < n ; i ++ ) if ( gcd ( i , n ) == 1 ) cout << i << " ▁ " ; } int main ( ) { int n = 10 ; printGenerators ( n ) ; return 0 ; }
Check if a large number is divisible by 3 or not | C ++ program to find if a number is divisible by 3 or not ; Function to find that number divisible by 3 or not ; Compute sum of digits ; Check if sum of digits is divisible by 3. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( string str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str [ i ] - '0' ) ; return ( digitSum % 3 == 0 ) ; } int main ( ) { string str = "1332" ; check ( str ) ? cout << " Yes " : cout << " No ▁ " ; return 0 ; }
Count all perfect divisors of a number | Below is C ++ code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100001 NEW_LINE int perfectDiv [ MAX ] ; void precomputeCounts ( ) { for ( int i = 1 ; i * i < MAX ; ++ i ) { for ( int j = i * i ; j < MAX ; j += i * i ) ++ perfectDiv [ j ] ; } } int countPerfectDivisors ( int n ) { return perfectDiv [ n ] ; } int main ( ) { precomputeCounts ( ) ; int n = 16 ; cout << " Total ▁ perfect ▁ divisors ▁ of ▁ " << n << " ▁ = ▁ " << countPerfectDivisors ( n ) << " STRNEWLINE " ; n = 12 ; cout << " Total ▁ perfect ▁ divisors ▁ of ▁ " << n << " ▁ = ▁ " << countPerfectDivisors ( n ) ; return 0 ; }
Prime Factorization using Sieve O ( log n ) for multiple queries | C ++ program to find prime factorization of a number n in O ( Log n ) time with precomputation allowed . ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf 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 ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; driver program for above function ; precalculating Smallest Prime Factor ; calling getFactorization function
#include " bits / stdc + + . h " NEW_LINE using namespace std ; #define MAXN  100001 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 ; } } } vector < int > getFactorization ( int x ) { vector < int > ret ; while ( x != 1 ) { ret . push_back ( spf [ x ] ) ; x = x / spf [ x ] ; } return ret ; } int main ( int argc , char const * argv [ ] ) { sieve ( ) ; int x = 12246 ; cout << " prime ▁ factorization ▁ for ▁ " << x << " ▁ : ▁ " ; vector < int > p = getFactorization ( x ) ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) cout << p [ i ] << " ▁ " ; cout << endl ; return 0 ; }
Nearest element with at | C ++ program to print nearest element with at least one common prime factor . ; Pre - computation of smallest prime divisor of all numbers ; Prime number will have same divisor ; Function to calculate all divisors of input array ; Pre - compute all the divisors of array element by using prime factors ; Traverse all elements , ; For every divisor of current element , find closest element . ; Visit divisor if not visited ; Fetch the index of visited divisor ; Update the divisor index to current index ; Set the minimum distance ; Set the min distance of current index ' i ' to nearest one ; Add 1 as indexing starts from 0 ; Set the min distance of found index ' ind ' ; Add 1 as indexing starts from 0 ; Driver code ; Simple sieve to find smallest prime divisor of number from 2 to MAX ; function to calculate nearest distance of every array elements ; Print the nearest distance having GDC > 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100001 ; const int INF = INT_MAX ; int primedivisor [ MAX ] , dist [ MAX ] , pos [ MAX ] , divInd [ MAX ] ; vector < int > divisors [ MAX ] ; void sieveOfEratosthenes ( ) { for ( int i = 2 ; i * i < MAX ; ++ i ) { if ( ! primedivisor [ i ] ) for ( int j = i * i ; j < MAX ; j += i ) primedivisor [ j ] = i ; } for ( int i = 1 ; i < MAX ; ++ i ) if ( ! primedivisor [ i ] ) primedivisor [ i ] = i ; } void findDivisors ( int arr [ ] , int n ) { for ( int i = 0 ; i < MAX ; ++ i ) pos [ i ] = divInd [ i ] = -1 , dist [ i ] = INF ; for ( int i = 0 ; i < n ; ++ i ) { int num = arr [ i ] ; while ( num > 1 ) { int div = primedivisor [ num ] ; divisors [ i ] . push_back ( div ) ; while ( num % div == 0 ) num /= div ; } } } void nearestGCD ( int arr [ ] , int n ) { findDivisors ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { for ( auto & div : divisors [ i ] ) { if ( divInd [ div ] == -1 ) divInd [ div ] = i ; else { int ind = divInd [ div ] ; divInd [ div ] = i ; if ( dist [ i ] > abs ( ind - i ) ) { dist [ i ] = abs ( ind - i ) ; pos [ i ] = ind + 1 ; } if ( dist [ ind ] > abs ( ind - i ) ) { dist [ ind ] = abs ( ind - i ) ; pos [ ind ] = i + 1 ; } } } } } int main ( ) { sieveOfEratosthenes ( ) ; int arr [ ] = { 2 , 9 , 4 , 3 , 13 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; nearestGCD ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) cout << pos [ i ] << " ▁ " ; return 0 ; }
Largest subsequence having GCD greater than 1 | Simple C ++ program to find length of the largest subsequence with GCD greater than 1. ; Returns length of the largest subsequence with GCD more than 1. ; Finding the Maximum value in arr [ ] ; Iterate from 2 to maximum possible divisor of all give values ; If we found divisor , increment count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestGCDSubsequence ( int arr [ ] , int n ) { int ans = 0 ; int maxele = * max_element ( arr , arr + n ) ; for ( int i = 2 ; i <= maxele ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( arr [ j ] % i == 0 ) ++ count ; } ans = max ( ans , count ) ; } return ans ; } int main ( ) { int arr [ ] = { 3 , 6 , 2 , 5 , 4 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << largestGCDSubsequence ( arr , size ) ; return 0 ; }
Minimum step to reach one | C ++ program to get minimum step to reach 1 under given constraints ; structure represent one node in queue ; method returns minimum step to reach one ; set is used to visit numbers so that they won 't be pushed in queue again ; loop until we reach to 1 ; if current data value is 1 , return its steps from N ; check curr - 1 , only if it not visited yet ; loop from 2 to sqrt ( value ) for its divisors ; check divisor , only if it is not visited yet if i is divisor of val , then val / i will be its bigger divisor ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct data { int val ; int steps ; data ( int val , int steps ) : val ( val ) , steps ( steps ) { } } ; int minStepToReachOne ( int N ) { queue < data > q ; q . push ( data ( N , 0 ) ) ; set < int > st ; while ( ! q . empty ( ) ) { data t = q . front ( ) ; q . pop ( ) ; if ( t . val == 1 ) return t . steps ; if ( st . find ( t . val - 1 ) == st . end ( ) ) { q . push ( data ( t . val - 1 , t . steps + 1 ) ) ; st . insert ( t . val - 1 ) ; } for ( int i = 2 ; i * i <= t . val ; i ++ ) { if ( t . val % i == 0 && st . find ( t . val / i ) == st . end ( ) ) { q . push ( data ( t . val / i , t . steps + 1 ) ) ; st . insert ( t . val / i ) ; } } } } int main ( ) { int N = 17 ; cout << minStepToReachOne ( N ) << endl ; }
Queries on the sum of prime factor counts in a range | C ++ program to find sum prime factors in given range . ; using sieve method to evaluating the prime factor of numbers ; if i is prime ; setting number of prime factor of a prime number . ; Returns sum of counts of prime factors in range from l to r . This function mainly uses count [ ] which is filled by Sieve ( ) ; finding the sum of number of prime factor of numbers in a range . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  1000006 NEW_LINE using namespace std ; void sieve ( int count [ ] ) { for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( count [ i ] == 0 ) { for ( int j = 2 * i ; j < MAX ; j += i ) count [ j ] ++ ; count [ i ] = 1 ; } } } int query ( int count [ ] , int l , int r ) { int sum = 0 ; for ( int i = l ; i <= r ; i ++ ) sum += count [ i ] ; return sum ; } int main ( ) { int count [ MAX ] ; memset ( count , 0 , sizeof count ) ; sieve ( count ) ; cout << query ( count , 6 , 10 ) << endl << query ( count , 1 , 5 ) ; return 0 ; }
Generation of n numbers with given set of factors | C ++ program to generate n numbers with given factors ; Generate n numbers with factors in factor [ ] ; array of k to store next multiples of given factors ; Prints n numbers int output = 0 ; Next number to print as output ; Find the next smallest multiple ; Printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; incrementing the current value by the respective factor ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateNumbers ( int factor [ ] , int n , int k ) { int next [ k ] = { 0 } ; for ( int i = 0 ; i < n ; ) { int toincrement = 0 ; for ( int j = 0 ; j < k ; j ++ ) if ( next [ j ] < next [ toincrement ] ) toincrement = j ; if ( output != next [ toincrement ] ) { output = next [ toincrement ] ; printf ( " % d ▁ " , next [ toincrement ] ) ; i ++ ; } next [ toincrement ] += factor [ toincrement ] ; } } int main ( ) { int factor [ ] = { 3 , 5 , 7 } ; int n = 10 ; int k = sizeof ( factor ) / sizeof ( factor [ 0 ] ) ; generateNumbers ( factor , n , k ) ; return 0 ; }
Reversible numbers | C ++ program to check if a given number is reversible or not ; Function to check reversible number ; Calculate reverse of n ; Calculate sum of number and its reverse ; Check for reverse number reach digit must be odd ; Driver function
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; void checkReversible ( int n ) { int rev = 0 , rem ; int flag = n ; while ( flag ) { rem = flag % 10 ; rev *= 10 ; rev += rem ; flag /= 10 ; } int sum = rev + n ; while ( sum && ( rem % 2 != 0 ) ) { rem = sum % 10 ; sum /= 10 ; } if ( sum == 0 ) cout << " Reversible ▁ Number " ; else cout << " Non - Reversible ▁ Number " ; } int main ( ) { int n = 36 ; checkReversible ( n ) ; return 0 ; }
Reversible numbers | C ++ program to find the count of reversible numbers upto a given number n ; Function to calculate the count of reversible number ; Calculate counts of reversible number of 1 to n - digits ; All four possible cases and their formula ; for i of form 2 k ; for i of form 4 k + 3 ; for i of form 4 k + 1 no solution ; Driver function ; count up - to 9 digit numbers ( 1 billion )
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; void countReversible ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { switch ( i % 4 ) { case 0 : case 2 : count += 20 * pow ( 30 , ( i / 2 - 1 ) ) ; break ; case 3 : count += 100 * pow ( 500 , i / 4 ) ; break ; case 1 : break ; } } cout << count ; } int main ( ) { int n = 9 ; countReversible ( n ) ; return 0 ; }
Multiplicative order | C ++ program to implement multiplicative order ; function for GCD ; Function return smallest + ve integer that holds condition A ^ k ( mod N ) = 1 ; result store power of A that rised to the power N - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; driver program to test above function
#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 multiplicativeOrder ( int A , int N ) { if ( GCD ( A , N ) != 1 ) return -1 ; unsigned int result = 1 ; int K = 1 ; while ( K < N ) { result = ( result * A ) % N ; if ( result == 1 ) return K ; K ++ ; } return -1 ; } int main ( ) { int A = 4 , N = 7 ; cout << multiplicativeOrder ( A , N ) ; return 0 ; }
Sum of product of x and y such that floor ( n / x ) = y | C ++ program to find sum of product of x and y such that n / x = y ( Integer Division ) ; Return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfRange ( int a , int b ) { int i = ( a * ( a + 1 ) ) >> 1 ; int j = ( b * ( b + 1 ) ) >> 1 ; return ( i - j ) ; } int sumofproduct ( int n ) { int sum = 0 ; int root = sqrt ( n ) ; for ( int i = 1 ; i <= root ; i ++ ) { int up = n / i ; int low = max ( n / ( i + 1 ) , root ) ; sum += ( i * sumOfRange ( up , low ) ) ; sum += ( i * ( n / i ) ) ; } return sum ; } int main ( ) { int n = 10 ; cout << sumofproduct ( n ) << endl ; return 0 ; }
Primitive root of a prime number n modulo n | C ++ program to find primitive root of a given number n ; Returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Iterative Function to calculate ( x ^ n ) % p in O ( logy ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Utility function to store prime factors of a number ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to find smallest primitive root of n ; Check if n is prime or not ; Find value of Euler Totient function of n Since n is a prime number , the value of Euler Totient function is n - 1 as there are n - 1 relatively prime numbers . ; Find prime factors of phi and store in a set ; Check for every number from 2 to phi ; Iterate through all prime factors of phi . and check if we found a power with value 1 ; Check if r ^ ( ( phi ) / primefactors ) mod n is 1 or not ; If there was no power with value 1. ; If no primitive root found ; 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 power ( int x , unsigned int y , int p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } void findPrimefactors ( unordered_set < int > & s , int n ) { while ( n % 2 == 0 ) { s . insert ( 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { s . insert ( i ) ; n = n / i ; } } if ( n > 2 ) s . insert ( n ) ; } int findPrimitive ( int n ) { unordered_set < int > s ; if ( isPrime ( n ) == false ) return -1 ; int phi = n - 1 ; findPrimefactors ( s , phi ) ; for ( int r = 2 ; r <= phi ; r ++ ) { bool flag = false ; for ( auto it = s . begin ( ) ; it != s . end ( ) ; it ++ ) { if ( power ( r , phi / ( * it ) , n ) == 1 ) { flag = true ; break ; } } if ( flag == false ) return r ; } return -1 ; } int main ( ) { int n = 761 ; cout << " ▁ Smallest ▁ primitive ▁ root ▁ of ▁ " << n << " ▁ is ▁ " << findPrimitive ( n ) ; return 0 ; }
Minimum number of power terms with sum equal to n | C ++ program to calculate minimum number of powers of x to make sum equal to n . ; Return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; Consider n = a * x + b where a = n / x and b = n % x . ; Update count of powers for 1 's added ; Repeat the process for reduced n ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minPower ( int n , int x ) { if ( x == 1 ) return n ; int ans = 0 ; while ( n > 0 ) { ans += ( n % x ) ; n /= x ; } return ans ; } int main ( ) { int n = 5 , x = 3 ; cout << minPower ( n , x ) << endl ; return 0 ; }
Sum of Perrin Numbers | C ++ program to calculate sum of Perrin Numbers ; function for sum of first n Perrin number . ; if ( n == 0 ) n = 0 ; if ( n == 1 ) n = 1 ; if ( n == 2 ) n = 2 ; calculate k = 5 sum of three previous step . ; Sum remaining numbers ; int d = a + b ; calculate next term ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calSum ( int n ) { int a = 3 , b = 0 , c = 2 ; return 3 ; return 3 ; return 5 ; int sum = 5 ; while ( n > 2 ) { sum += d ; a = b ; b = c ; c = d ; n -- ; } return sum ; } int main ( ) { int n = 9 ; cout << calSum ( n ) ; return 0 ; }
Print the kth common factor of two numbers | C ++ program to find kth common factor of two numbers ; Returns k 'th common factor of x and y. ; Find smaller of two numbers ; Count common factors until we either reach small or count becomes k . ; If we reached small ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findKCF ( int x , int y , int k ) { int small = min ( x , y ) ; int count = 1 ; for ( int i = 2 ; i <= small ; i ++ ) { if ( x % i == 0 && y % i == 0 ) count ++ ; if ( count == k ) return i ; } return -1 ; } int main ( ) { int x = 4 , y = 24 , k = 3 ; cout << findKHCF ( x , y , k ) ; return 0 ; }
Find minimum number to be divided to make a number a perfect square | C ++ program to find minimum number which divide n to make it a perfect square . ; Return the minimum number to be divided to make n a perfect square . ; Since 2 is only even prime , compute its power separately . ; If count is odd , it must be removed by dividing n by prime number . ; If count is odd , it must be removed by dividing n by prime number . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 ) ans *= 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 ) ans *= i ; } if ( n > 2 ) ans *= n ; return ans ; } int main ( ) { int n = 72 ; cout << findMinNumber ( n ) << endl ; return 0 ; }