codeparrot/xlcost-text-to-code huggingface dataset:
Finding power of prime number p in n ! | C ++ implementation of finding power of p in n ! ; Returns power of p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } int main ( ) { cout << PowerOFPINnfactorial ( 4 , 2 ) << endl ; return 0 ; }
Program for Decimal to Binary Conversion | C ++ implementation of the approach ; Function to return the binary equivalent of decimal value N ; To store the binary number ; Count used to store exponent value ; Driver code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; #define ull  unsigned long long int NEW_LINE int decimalToBinary ( int N ) { ull B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; ull c = pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } return B_Number ; } int main ( ) { int N = 17 ; cout << decimalToBinary ( N ) ; return 0 ; }
Program for Binary To Decimal Conversion | C ++ program to convert binary to decimal ; Function to convert binary to decimal ; Initializing base value to 1 , i . e 2 ^ 0 ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int binaryToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base = 1 ; int temp = num ; while ( temp ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base ; base = base * 2 ; } return dec_value ; } int main ( ) { int num = 10101001 ; cout << binaryToDecimal ( num ) << endl ; }
Calculating Factorials using Stirling Approximation | CPP program for calculating factorial of a number using Stirling Approximation ; function for calculating factorial ; evaluating factorial using stirling approximation ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long int stirlingFactorial ( int n ) { if ( n == 1 ) return 1 ; long int z ; z = sqrt ( 2 * 3.14 * n ) * pow ( ( n / e ) , n ) ; return z ; } int main ( ) { cout << stirlingFactorial ( 1 ) << endl ; cout << stirlingFactorial ( 2 ) << endl ; cout << stirlingFactorial ( 3 ) << endl ; cout << stirlingFactorial ( 4 ) << endl ; cout << stirlingFactorial ( 5 ) << endl ; cout << stirlingFactorial ( 6 ) << endl ; cout << stirlingFactorial ( 7 ) << endl ; return 0 ; }
Count pairs with Odd XOR | C ++ program to count pairs in array whose XOR is odd ; A function will return number of pair whose XOR is odd ; To store count of odd and even numbers ; Increase even if number is even otherwise increase odd ; Return number of pairs ; Driver program to test countXorPair ( )
#include <iostream> NEW_LINE using namespace std ; int countXorPair ( int arr [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return odd * even ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countXorPair ( arr , n ) ; return 0 ; }
Lychrel Number Implementation | C ++ Program to check whether the given number is Lychrel Number or not with given limit on number of iterations . ; Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; Driver program
#include <iostream> NEW_LINE using namespace std ; long reverse ( long ) ; bool isPalindrome ( long ) ; static int MAX_ITERATIONS = 20 ; string isLychrel ( long number ) { for ( int i = 0 ; i < MAX_ITERATIONS ; i ++ ) { number = number + reverse ( number ) ; if ( isPalindrome ( number ) ) return " false " ; } return " true " ; } bool isPalindrome ( long number ) { return number == reverse ( number ) ; } long reverse ( long number ) { long reverse = 0 ; while ( number > 0 ) { long remainder = number % 10 ; reverse = ( reverse * 10 ) + remainder ; number = number / 10 ; } return reverse ; } int main ( ) { long number = 295 ; cout << number << " ▁ is ▁ lychrel ? ▁ " << isLychrel ( number ) ; }
Rectangular ( or Pronic ) Numbers | CPP Program to find n - th rectangular number ; Returns n - th rectangular number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findRectNum ( int n ) { return n * ( n + 1 ) ; } int main ( ) { int n = 6 ; cout << findRectNum ( n ) ; return 0 ; }
Program for Muller Method | C ++ Program to find root of a function , f ( x ) ; Function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver main function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_ITERATIONS = 10000 ; float f ( float x ) { return 1 * pow ( x , 3 ) + 2 * x * x + 10 * x - 20 ; } void Muller ( float a , float b , float c ) { int i ; float res ; for ( i = 0 ; ; ++ i ) { float f1 = f ( a ) ; float f2 = f ( b ) ; float f3 = f ( c ) ; float d1 = f1 - f3 ; float d2 = f2 - f3 ; float h1 = a - c ; float h2 = b - c ; float a0 = f3 ; float a1 = ( ( ( d2 * pow ( h1 , 2 ) ) - ( d1 * pow ( h2 , 2 ) ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; float a2 = ( ( ( d1 * h2 ) - ( d2 * h1 ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; float x = ( ( -2 * a0 ) / ( a1 + abs ( sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; float y = ( ( -2 * a0 ) / ( a1 - abs ( sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; if ( x >= y ) res = x + c ; else res = y + c ; float m = res * 100 ; float n = c * 100 ; m = floor ( m ) ; n = floor ( n ) ; if ( m == n ) break ; a = b ; b = c ; c = res ; if ( i > MAX_ITERATIONS ) { cout << " Root ▁ cannot ▁ be ▁ found ▁ using " << " ▁ Muller ' s ▁ method " ; break ; } } if ( i <= MAX_ITERATIONS ) cout << " The ▁ value ▁ of ▁ the ▁ root ▁ is ▁ " << res ; } int main ( ) { float a = 0 , b = 1 , c = 2 ; Muller ( a , b , c ) ; return 0 ; }
Optimized Euler Totient Function for Multiple Evaluations | C ++ program to efficiently compute values of euler totient function for multiple inputs . ; Stores prime numbers upto MAX - 1 values ; Finds prime numbers upto MAX - 1 and stores them in vector p ; if prime [ i ] is not marked before ; fill vector for every newly encountered prime ; run this loop till square root of MAX , mark the index i * j as not prime ; function to find totient of n ; this loop runs sqrt ( n / ln ( n ) ) times ; subtract multiples of p [ i ] from r ; Remove all occurrences of p [ i ] in n ; when n has prime factor greater than sqrt ( n ) ; Driver code ; preprocess all prime numbers upto 10 ^ 5
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE const int MAX = 100001 ; vector < ll > p ; void sieve ( ) { ll isPrime [ MAX + 1 ] ; for ( ll i = 2 ; i <= MAX ; i ++ ) { if ( isPrime [ i ] == 0 ) { p . push_back ( i ) ; for ( ll j = 2 ; i * j <= MAX ; j ++ ) isPrime [ i * j ] = 1 ; } } } ll phi ( ll n ) { ll res = n ; for ( ll i = 0 ; p [ i ] * p [ i ] <= n ; i ++ ) { if ( n % p [ i ] == 0 ) { res -= ( res / p [ i ] ) ; while ( n % p [ i ] == 0 ) n /= p [ i ] ; } } if ( n > 1 ) res -= ( res / n ) ; return res ; } int main ( ) { sieve ( ) ; cout << phi ( 11 ) << " STRNEWLINE " ; cout << phi ( 21 ) << " STRNEWLINE " ; cout << phi ( 31 ) << " STRNEWLINE " ; cout << phi ( 41 ) << " STRNEWLINE " ; cout << phi ( 51 ) << " STRNEWLINE " ; cout << phi ( 61 ) << " STRNEWLINE " ; cout << phi ( 91 ) << " STRNEWLINE " ; cout << phi ( 101 ) << " STRNEWLINE " ; return 0 ; }
Finding n | C ++ implementation for finding nth number made of prime digits only ; Prints n - th number where each digit is a prime number ; Finding the length of n - th number ; Count of numbers with len - 1 digits ; Count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; Finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else print the ith digit and break ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nthprimedigitsnumber ( long long n ) { long long len = 1 ; long long prev_count = 0 ; while ( true ) { long long curr_count = prev_count + pow ( 4 , len ) ; if ( prev_count < n && curr_count >= n ) break ; len ++ ; prev_count = curr_count ; } for ( int i = 1 ; i <= len ; i ++ ) { for ( long long j = 1 ; j <= 4 ; j ++ ) { if ( prev_count + pow ( 4 , len - i ) < n ) prev_count += pow ( 4 , len - i ) ; else { if ( j == 1 ) cout << "2" ; else if ( j == 2 ) cout << "3" ; else if ( j == 3 ) cout << "5" ; else if ( j == 4 ) cout << "7" ; break ; } } } cout << endl ; } int main ( ) { nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; return 0 ; }
Cassiniâ €™ s Identity | C ++ implementation to demonstrate working of CassiniaTMs Identity ; Returns ( - 1 ) ^ n ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cassini ( int n ) { return ( n & 1 ) ? -1 : 1 ; } int main ( ) { int n = 5 ; cout << cassini ( n ) ; return 0 ; }
Find if a number is divisible by every number in a list | C ++ program which check is a number divided with every element in list or not ; Function which check is a number divided with every element in list or not ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findNoIsDivisibleOrNot ( int a [ ] , int n , int l ) { for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] % n != 0 ) return false ; } return true ; } int main ( ) { int a [ ] = { 14 , 12 , 4 , 18 } ; int n = 2 ; int l = ( sizeof ( a ) / sizeof ( a [ 0 ] ) ) ; if ( findNoIsDivisibleOrNot ( a , n , l ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find a range of composite numbers of given length | C ++ program to find a range of composite numbers of given length ; method to find factorial of given number ; to print range of length n having all composite integers ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } int printRange ( int n ) { int a = factorial ( n + 2 ) + 2 ; int b = a + n - 1 ; cout << " [ " << a << " , ▁ " << b << " ] " ; return 0 ; } int main ( ) { int n = 3 ; printRange ( n ) ; return 0 ; }
Find minimum value to assign all array elements so that array product becomes greater | C ++ program to find minimum value that can be assigned to all elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val ld val = 0 ; where ld is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE #define ld  long double NEW_LINE using namespace std ; ll findMinValue ( ll arr [ ] , ll n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) val += ( ld ) ( log ( ( ld ) ( arr [ i ] ) ) ) ; ll left = arr [ 0 ] , right = arr [ n - 1 ] + 1 ; ll ans ; while ( left <= right ) { ll mid = ( left + right ) / 2 ; ld temp = ( ld ) n * ( ld ) ( log ( ( ld ) ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else left = mid + 1 ; } return ans ; } int main ( ) { ll arr [ ] = { 4 , 2 , 1 , 10 , 6 } ; ll n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinValue ( arr , n ) << endl ; return 0 ; }
Find the sum of all the terms in the n | C ++ implementation to find the sum of all the terms in the nth row of the given series ; function to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfTermsInNthRow ( int n ) { int sum = n * ( 2 * pow ( n , 2 ) + 1 ) ; return sum ; } int main ( ) { int n = 4 ; cout << " Sum ▁ of ▁ all ▁ the ▁ terms ▁ in ▁ nth ▁ row ▁ = ▁ " << sumOfTermsInNthRow ( n ) ; return 0 ; }
First digit in product of an array of numbers | C ++ implementation to find first digit of a single number ; Keep dividing by 10 until it is greater than equal to 10 ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstDigit ( int x ) { while ( x >= 10 ) x = x / 10 ; return x ; } int main ( ) { cout << firstDigit ( 12345 ) << endl ; cout << firstDigit ( 5432 ) << endl ; }
Find the occurrences of digit d in the range [ 0. . n ] | C ++ program to count appearances of a digit ' d ' in range from [ 0. . n ] ; Count appearances in numbers starting from d . ; When the last digit is equal to d ; When the first digit is equal to d then ; increment result as well as number ; In case of reverse of number such as 12 and 21 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getOccurence ( int n , int d ) { int itr = d ; while ( itr <= n ) { if ( itr % 10 == d ) result ++ ; if ( itr != 0 && itr / 10 == d ) { result ++ ; itr ++ ; } else if ( itr / 10 == d - 1 ) itr = itr + ( 10 - d ) ; else itr = itr + 10 ; } return result ; } int main ( void ) { int n = 11 , d = 1 ; cout << getOccurence ( n , d ) ; return 0 ; }
Numbers having Unique ( or Distinct ) digits | C ++ code for the above approach ; Function to print unique numbers ; Iterate from l to r ; Convert the no . to string ; Convert string to set using stl ; Output if condition satisfies ; Driver Code ; Input of the lower and higher limits ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printUnique ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { string s = to_string ( i ) ; set < int > uniDigits ( s . begin ( ) , s . end ( ) ) ; if ( s . size ( ) == uniDigits . size ( ) ) { cout << i << " ▁ " ; } } } int main ( ) { int l = 1 , r = 20 ; printUnique ( l , r ) ; return 0 ; }
Program to calculate the value of sin ( x ) and cos ( x ) using Expansion | CPP code for implementing cos function ; Function for calculation ; Converting degrees to radian ; maps the sum along the series ; holds the actual value of sin ( n ) ; Main function
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void cal_cos ( float n ) { float accuracy = 0.0001 , x1 , denominator , cosx , cosval ; n = n * ( 3.142 / 180.0 ) ; x1 = 1 ; cosx = x1 ; cosval = cos ( n ) ; int i = 1 ; do { denominator = 2 * i * ( 2 * i - 1 ) ; x1 = - x1 * n * n / denominator ; cosx = cosx + x1 ; i = i + 1 ; } while ( accuracy <= fabs ( cosval - cosx ) ) ; cout << cosx ; } int main ( ) { float n = 30 ; cal_cos ( n ) ; }
Find sum of digits in factorial of a number | C ++ program to find sum of digits in factorial of a number ; Function to multiply x with large number stored in vector v . Result is stored in v . ; Calculate res + prev carry ; updation at ith position ; Returns sum of digits in n ! ; One by one multiply i to current vector and update the vector . ; Find sum of digits in vector v [ ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void multiply ( vector < int > & v , int x ) { int carry = 0 , res ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { int res = carry + v [ i ] * x ; v [ i ] = res % 10 ; carry = res / 10 ; } while ( carry != 0 ) { v . push_back ( carry % 10 ) ; carry /= 10 ; } } int findSumOfDigits ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) multiply ( v , i ) ; int sum = 0 ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) sum += v [ i ] ; return sum ; } int main ( ) { int n = 1000 ; cout << findSumOfDigits ( n ) ; return 0 ; }
Find other two sides of a right angle triangle | C ++ program to print other two sides of right angle triangle given one side ; Finds two sides of a right angle triangle if it exist . ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOtherSides ( int n ) { if ( n & 1 ) { if ( n == 1 ) cout << -1 << endl ; else { int b = ( n * n - 1 ) / 2 ; int c = ( n * n + 1 ) / 2 ; cout << " b ▁ = ▁ " << b << " , ▁ c ▁ = ▁ " << c << endl ; } } else { if ( n == 2 ) cout << -1 << endl ; else { int b = n * n / 4 - 1 ; int c = n * n / 4 + 1 ; cout << " b ▁ = ▁ " << b << " , ▁ c ▁ = ▁ " << c << endl ; } } } int main ( ) { int a = 3 ; printOtherSides ( a ) ; return 0 ; }
Minimum positive integer to divide a number such that the result is an odd | C ++ program to make a number odd ; Return 1 if already odd ; Check on dividing with a number when the result becomes odd Return that number ; If n is divided by i and n / i is odd then return i ; Driver code
#include <iostream> NEW_LINE using namespace std ; int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; for ( int i = 2 ; i <= n ; i ++ ) if ( ( n % i == 0 ) && ( ( n / i ) % 2 == 1 ) ) return i ; } int main ( ) { int n = 36 ; cout << makeOdd ( n ) ; return 0 ; }
XOR of all subarray XORs | Set 2 | C ++ program to get total xor of all subarray xors ; Returns XOR of all subarray xors ; if even number of terms are there , all numbers will appear even number of times . So result is 0. ; else initialize result by 0 as ( a xor 0 = a ) ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getTotalXorOfSubarrayXors ( int arr [ ] , int N ) { if ( N % 2 == 0 ) return 0 ; int res = 0 ; for ( int i = 0 ; i < N ; i += 2 ) res ^= arr [ i ] ; return res ; } int main ( ) { int arr [ ] = { 3 , 5 , 2 , 4 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getTotalXorOfSubarrayXors ( arr , N ) ; return 0 ; }
Fill array with 1 's using minimum iterations of filling neighbors | C ++ program to find number of iterations to fill with all 1 s ; Returns count of iterations to fill arr [ ] with 1 s . ; Start traversing the array ; Traverse until a 0 is found ; Count contiguous 0 s ; Condition for Case 3 ; Condition to check if Case 1 satisfies : ; If count_zero is even ; If count_zero is odd ; Reset count_zero ; Case 2 ; Update res ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countIterations ( int arr [ ] , int n ) { bool oneFound = false ; int res = 0 ; for ( int i = 0 ; i < n ; ) { if ( arr [ i ] == 1 ) oneFound = true ; while ( i < n && arr [ i ] == 1 ) i ++ ; int count_zero = 0 ; while ( i < n && arr [ i ] == 0 ) { count_zero ++ ; i ++ ; } if ( oneFound == false && i == n ) return -1 ; int curr_count ; if ( i < n && oneFound == true ) { if ( count_zero & 1 == 0 ) curr_count = count_zero / 2 ; else curr_count = ( count_zero + 1 ) / 2 ; count_zero = 0 ; } else { curr_count = count_zero ; count_zero = 0 ; } res = max ( res , curr_count ) ; } return res ; } int main ( ) { int arr [ ] = { 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countIterations ( arr , n ) ; return 0 ; }
Interesting facts about Fibonacci numbers | C ++ program to demonstrate that Fibonacci numbers that are divisible by their indexes have indexes as either power of 5 or multiple of 12. ; storing Fibonacci numbers
#include <iostream> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE int main ( ) { long long int arr [ MAX ] ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] ; cout << " Fibonacci ▁ numbers ▁ divisible ▁ by ▁ " " their ▁ indexes ▁ are ▁ : STRNEWLINE " ; for ( int i = 1 ; i < MAX ; i ++ ) if ( arr [ i ] % i == 0 ) cout << " ▁ " << i ; }
Express a number as sum of consecutive numbers | C ++ program to print a consecutive sequence to express N if possible . ; Print consecutive numbers from last to first ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printConsecutive ( int last , int first ) { cout << first ++ ; for ( int x = first ; x <= last ; x ++ ) cout << " ▁ + ▁ " << x ; } void findConsecutive ( int N ) { for ( int last = 1 ; last < N ; last ++ ) { for ( int first = 0 ; first < last ; first ++ ) { if ( 2 * N == ( last - first ) * ( last + first + 1 ) ) { cout << N << " ▁ = ▁ " ; printConsecutive ( last , first + 1 ) ; return ; } } } cout << " - 1" ; } int main ( ) { int n = 12 ; findConsecutive ( n ) ; return 0 ; }
Find n | C ++ program to find n - th number in a series made of digits 4 and 7 ; Return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; If i is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printNthElement ( int n ) { int arr [ n + 1 ] ; arr [ 1 ] = 4 ; arr [ 2 ] = 7 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 4 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 7 ; } return arr [ n ] ; } int main ( ) { int n = 6 ; cout << printNthElement ( n ) ; return 0 ; }
Maximum sum of distinct numbers such that LCM of these numbers is N | C ++ program to find the max sum of numbers whose lcm is n ; Returns maximum sum of numbers with LCM as N ; Finding a divisor of n and adding it to max_sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSumLCM ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { max_sum += i ; if ( n / i != i ) max_sum += ( n / i ) ; } } return max_sum ; } int main ( ) { int n = 2 ; cout << MaxSumLCM ( n ) << endl ; return 0 ; }
Square root of a number using log | C ++ program to demonstrate finding square root of a number using sqrt ( )
#include <bits/stdc++.h> NEW_LINE int main ( void ) { double n = 12 ; printf ( " % lf ▁ " , sqrt ( n ) ) ; return 0 ; }
Check if LCM of array elements is divisible by a prime number or not | C ++ program to find LCM of array of number is divisible by a prime number k or not ; Function to check any number of array is divisible by k or not ; If any array element is divisible by k , then LCM of whole array should also be divisible . ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool func ( int a [ ] , int k , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] % k == 0 ) return true ; return false ; } int main ( ) { int a [ ] = { 14 , 27 , 38 , 76 , 84 } ; int k = 19 ; bool res = func ( a , k , 5 ) ; if ( res ) cout << " true " ; else cout << " false " ; return 0 ; }
Find the closest and smaller tidy number | C ++ program to find closest tidy number smaller than the given number ; check whether string violates tidy property ; if string violates tidy property , then decrease the value stored at that index by 1 and replace all the value stored right to that index by 9 ; Driver code ; num will store closest tidy number
#include <bits/stdc++.h> NEW_LINE using namespace std ; char * tidyNum ( char str [ ] , int len ) { for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { ( char ) str [ i ] -- ; for ( int j = i + 1 ; j < len ; j ++ ) str [ j ] = '9' ; } } return str ; } int main ( ) { char str [ ] = "11333445538" ; int len = strlen ( str ) ; char * num = tidyNum ( str , len ) ; printf ( " % s STRNEWLINE " , num ) ; return 0 ; }