codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all palindromic numbers lying in the range [ L , R ] for Q queries | C ++ program to find the sum of all palindrome numbers in the given range ; pref [ ] array to precompute the sum of all palindromic number ; Function that return number num if num is palindromic else return 0 ; Convert num to string ; Function to precompute the sum of all palindrome numbers upto 100000 ; checkPalindrome ( ) return the number i if i is palindromic else return 0 ; Function to print the sum for each query ; Function to print sum of all palindromic numbers between [ L , R ] ; Function that pre computes the sum of all palindromic numbers ; Iterate over all Queries to print the sum ; Driver code ; Queries ; Function that print the the sum of all palindromic number in Range [ L , R ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long pref [ 100001 ] ; int checkPalindrome ( int num ) { string str = to_string ( num ) ; int l = 0 , r = str . length ( ) - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { return 0 ; } l ++ ; r -- ; } return num ; } void preCompute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + checkPalindrome ( i ) ; } } void printSum ( int L , int R ) { cout << pref [ R ] - pref [ L - 1 ] << endl ; } void printSumPalindromic ( int arr [ ] [ 2 ] , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } int main ( ) { int Q = 2 ; int arr [ ] [ 2 ] = { { 10 , 13 } , { 12 , 21 } } ; printSumPalindromic ( arr , Q ) ; return 0 ; }
Sum of alternating sign Squares of first N natural numbers | C ++ program to find Sum of alternating sign Squares of first N natural numbers ; Function to calculate the alternating sign sum ; Variable to store the sum ; Loop to iterate each number from 1 to N ; The alternating sign is put by checking if the number is even or odd ; Add the square with the sign ; Add the square with the sign ; Driver code
#include <iostream> NEW_LINE using namespace std ; int summation ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 1 ) sum += ( i * i ) ; else sum -= ( i * i ) ; } return sum ; } int main ( ) { int N = 2 ; cout << summation ( N ) ; return 0 ; }
Sum of alternating sign Squares of first N natural numbers | C ++ program to find Sum of alternating sign Squares of first N natural numbers ; Function to calculate the alternating sign sum ; Variable to store the absolute sum ; Variable to store the sign ; Variable to store the resultant sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; int summation ( int n ) { int abs_sum = n * ( n + 1 ) / 2 ; int sign = n + 1 % 2 == 0 ? 1 : -1 ; int result_sum = sign * abs_sum ; return result_sum ; } int main ( ) { int N = 2 ; cout << summation ( N ) ; return 0 ; }
Count pairs from 1 to N such that their Sum is divisible by their XOR | C ++ program to count pairs from 1 to N such that their Sum is divisible by their XOR ; Function to count pairs ; variable to store count ; Generate all possible pairs such that 1 <= x < y < n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int n ) { int count = 0 ; for ( int x = 1 ; x < n ; x ++ ) { for ( int y = x + 1 ; y <= n ; y ++ ) { if ( ( y + x ) % ( y ^ x ) == 0 ) count ++ ; } } return count ; } int main ( ) { int n = 6 ; cout << countPairs ( n ) ; return 0 ; }
Count pairs in array such that one element is power of another | C ++ program to count pairs in array such that one element is power of another ; Function to check if given number number y is power of x ; log function to calculate value ; compare to the result1 or result2 both are equal ; Function to find pairs from array ; Iterate through all pairs ; Increment count if one is the power of other ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int x , int y ) { int res1 = log ( y ) / log ( x ) ; double res2 = log ( y ) / log ( x ) ; return ( res1 == res2 ) ; } int countPower ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( isPower ( arr [ i ] , arr [ j ] ) || isPower ( arr [ j ] , arr [ i ] ) ) res ++ ; return res ; } int main ( ) { int a [ ] = { 16 , 2 , 3 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countPower ( a , n ) ; return 0 ; }
Find the original Array using XOR values of all adjacent elements | C ++ implementation to find the Array from the XOR array of the adjacent elements of array ; XOR of all elements from 1 to N ; Function to find the Array from the XOR Array ; Take a vector to store the permutation ; XOR of N natural numbers ; Loop to find the XOR of adjacent elements of the XOR Array ; Loop to find the other elements of the permutation ; Finding the next and next elements ; Driver Code ; Required Permutation
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xor_all_elements ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; case 3 : return 0 ; } } vector < int > findArray ( int xorr [ ] , int n ) { vector < int > arr ; int xor_all = xor_all_elements ( n ) ; int xor_adjacent = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { xor_adjacent = xor_adjacent ^ xorr [ i ] ; } int last_element = xor_all ^ xor_adjacent ; arr . push_back ( last_element ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { last_element = xorr [ i ] ^ last_element ; arr . push_back ( last_element ) ; } return arr ; } int main ( ) { vector < int > arr ; int xorr [ ] = { 7 , 5 , 3 , 7 } ; int n = 5 ; arr = findArray ( xorr , n ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { cout << arr [ i ] << " ▁ " ; } }
Sum of non | C ++ implementation of the above approach ; Function to return a vector which consists the sum of four portions of the matrix ; Iterating through the matrix ; Condition for selecting all values before the second diagonal of metrics ; Top portion of the matrix ; Left portion of the matrix ; Bottom portion of the matrix ; Right portion of the matrix ; Adding all the four portions into a vector ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfParts ( int * arr , int N ) { int sum_part1 = 0 , sum_part2 = 0 , sum_part3 = 0 , sum_part4 = 0 ; int totalsum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i + j < N - 1 ) { if ( i < j and i != j and i + j ) sum_part1 += ( arr + i * N ) [ j ] ; else if ( i != j ) sum_part2 += ( arr + i * N ) [ j ] ; } else { if ( i > j and i + j != N - 1 ) sum_part3 += ( arr + i * N ) [ j ] ; else { if ( i + j != N - 1 and i != j ) sum_part4 += ( arr + i * N ) [ j ] ; } } } } totalsum = sum_part1 + sum_part2 + sum_part3 + sum_part4 ; return totalsum ; } int main ( ) { int N = 4 ; int arr [ N ] [ N ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; cout << sumOfParts ( ( int * ) arr , N ) ; }
Check if X and Y can be made zero by using given operation any number of times | C ++ implementation of the approach ; Function to check if X and Y can be made equal to zero by using given operation any number of times ; Check for the two conditions ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ifPossible ( int X , int Y ) { if ( X > Y ) swap ( X , Y ) ; if ( ( X + Y ) % 5 == 0 and 3 * X >= 2 * Y ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int X = 33 , Y = 27 ; ifPossible ( X , Y ) ; return 0 ; }
Largest Even and Odd N | C ++ implementation of the approach ; Function to print the largest n - digit even and odd numbers in hexadecimal number system ; Append ' F ' ( N - 1 ) times ; Append ' E ' for an even number ; Append ' F ' for an odd number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int n ) { string ans = string ( n - 1 , ' F ' ) ; string even = ans + ' E ' ; string odd = ans + ' F ' ; cout << " Even : ▁ " << even << endl ; cout << " Odd : ▁ " << odd << endl ; } int main ( ) { int n = 2 ; findNumbers ( n ) ; return 0 ; }
Program to compute log a to any base b ( logb a ) | C ++ program to find log ( a ) on any base b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int log_a_to_base_b ( int a , int b ) { return log ( a ) / log ( b ) ; } int main ( ) { int a = 3 ; int b = 2 ; cout << log_a_to_base_b ( a , b ) << endl ; a = 256 ; b = 4 ; cout << log_a_to_base_b ( a , b ) << endl ; return 0 ; }
Program to compute log a to any base b ( logb a ) | C ++ program to find log ( a ) on any base b using Recursion ; Recursive function to compute log a to the base b ; Driver code
#include <iostream> NEW_LINE using namespace std ; int log_a_to_base_b ( int a , int b ) { return ( a > b - 1 ) ? 1 + log_a_to_base_b ( a / b , b ) : 0 ; } int main ( ) { int a = 3 ; int b = 2 ; cout << log_a_to_base_b ( a , b ) << endl ; a = 256 ; b = 4 ; cout << log_a_to_base_b ( a , b ) << endl ; return 0 ; }
Largest and Smallest N | C ++ program to find the largest and smallest N - digit numbers in Hexa - Decimal Number System ; Function to return the largest N - digit number in Hexa - Decimal Number System ; Append ' F ' N times ; Function to return the smallest N - digit number in Hexa - Decimal Number System ; Append '0' ( N - 1 ) times to 1 ; Function to print the largest and smallest N - digit Hexa - Decimal number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findLargest ( int N ) { string largest = string ( N , ' F ' ) ; return largest ; } string findSmallest ( int N ) { string smallest = "1" + string ( ( N - 1 ) , '0' ) ; return smallest ; } void print ( int largest ) { cout << " Largest : ▁ " << findLargest ( largest ) << endl ; cout << " Smallest : ▁ " << findSmallest ( largest ) << endl ; } int main ( ) { int N = 4 ; print ( N ) ; return 0 ; }
Highest and Smallest power of K less than and greater than equal to N respectively | C ++ implementation of the approach ; Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Function to print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prevPowerofK ( int n , int k ) { int p = ( int ) ( log ( n ) / log ( k ) ) ; return ( int ) pow ( k , p ) ; } int nextPowerOfK ( int n , int k ) { return prevPowerofK ( n , k ) * k ; } void printResult ( int n , int k ) { cout << prevPowerofK ( n , k ) << " ▁ " << nextPowerOfK ( n , k ) << endl ; } int main ( ) { int n = 25 , k = 3 ; printResult ( n , k ) ; return 0 ; }
Check if all elements of the given array can be made 0 by decrementing value in pairs | C ++ program to make the array zero by decrementing value in pairs ; Function to check if all the elements can be made 0 in an array ; Variable to store sum and maximum element in an array ; Loop to calculate the sum and max value of the given array ; If n is 1 or sum is odd or sum - max element < max then no solution ; For the remaining case , print Yes ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canMake ( int n , int ar [ ] ) { int sum = 0 , maxx = -1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; maxx = max ( maxx , ar [ i ] ) ; } if ( n == 1 sum % 2 == 1 sum - maxx < maxx ) { cout << " No STRNEWLINE " ; } else { cout << " Yes STRNEWLINE " ; } } int main ( ) { int n = 6 ; int arr [ ] = { 1 , 1 , 2 , 3 , 6 , 11 } ; canMake ( n , arr ) ; return 0 ; }
Sum of all Perfect Squares lying in the range [ L , R ] for Q queries | C ++ program to find the sum of all perfect squares in the given range ; Array to precompute the sum of squares from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to precompute the perfect squares upto 100000. ; Function to print the sum for each query ; Driver code ; To calculate the precompute function ; Calling the printSum function for every query
#include <bits/stdc++.h> NEW_LINE #define ll  int NEW_LINE using namespace std ; long long pref [ 100010 ] ; int isPerfectSquare ( long long int x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ? x : 0 ; } void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectSquare ( i ) ; } } void printSum ( int L , int R ) { int sum = pref [ R ] - pref [ L - 1 ] ; cout << sum << " ▁ " ; } int main ( ) { compute ( ) ; int Q = 4 ; int arr [ ] [ 2 ] = { { 1 , 10 } , { 1 , 100 } , { 2 , 25 } , { 4 , 50 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } return 0 ; }
Program to find all Factors of a Number using recursion | C ++ program to find all the factors of a number using recursion ; Recursive function to print factors of a number ; Checking if the number is less than N ; Calling the function recursively for the next number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void factors ( int n , int i ) { if ( i <= n ) { if ( n % i == 0 ) { cout << i << " ▁ " ; } factors ( n , i + 1 ) ; } } int main ( ) { int N = 16 ; factors ( N , 1 ) ; }
Maximize value of ( a + b ) such that ( a * a | C ++ program to maximize the value of ( a + b ) such that ( a * a - b * b = N ) ; Function to maximize the value of ( a + b ) such that ( a * a - b * b = n ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxValue ( int n ) { return n ; } int main ( ) { int n = 1 ; cout << maxValue ( n ) ; return 0 ; }
Maximum number of edges that N | C ++ implementation to find the maximum number of edges for triangle free graph ; Function to find the maximum number of edges in a N - vertex graph . ; According to the Mantel 's theorem  the maximum number of edges will be  floor of [(n^2)/4] ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int n ) { int ans = ( n * n / 4 ) ; return ans ; } int main ( ) { int n = 10 ; cout << solve ( n ) << endl ; return 0 ; }
Multiplication table till N rows where every Kth row is table of K upto Kth term | C ++ program to print multiplication table till N rows where every Kth row is the table of K up to Kth term ; Function to print the multiplication table upto K - th term ; For loop to iterate from 1 to N where i serves as the value of K ; Inner loop which at every iteration goes till i ; Printing the table value for i ; New line after every row ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printMultiples ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { cout << ( i * j ) << " ▁ " ; } cout << endl ; } } int main ( ) { int N = 5 ; printMultiples ( N ) ; return 0 ; }
Boundary Value Analysis : Nature of Roots of a Quadratic equation | C ++ program to check the nature of the roots ; BVA for nature of roots of a quadratic equation ; If a = 0 , D / 2 a will yield exception Hence it is not a valid Quadratic Equation ; If D > 0 , it will be Real Roots ; If D == 0 , it will be Equal Roots ; If D < 0 , it will be Imaginary Roots ; Function to check for all testcases ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nature_of_roots ( int a , int b , int c ) { if ( a == 0 ) { cout << " Not ▁ a ▁ Quadratic ▁ Equation " << endl ; return ; } int D = b * b - 4 * a * c ; if ( D > 0 ) { cout << " Real ▁ Roots " << endl ; } else if ( D == 0 ) { cout << " Equal ▁ Roots " << endl ; } else { cout << " Imaginary ▁ Roots " << endl ; } } void checkForAllTestCase ( ) { cout << " Testcase " << " TABSYMBOL a TABSYMBOL b TABSYMBOL c TABSYMBOL Actual ▁ Output " << endl ; cout << endl ; int a , b , c ; int testcase = 1 ; while ( testcase <= 13 ) { if ( testcase == 1 ) { a = 0 ; b = 50 ; c = 50 ; } else if ( testcase == 2 ) { a = 1 ; b = 50 ; c = 50 ; } else if ( testcase == 3 ) { a = 50 ; b = 50 ; c = 50 ; } else if ( testcase == 4 ) { a = 99 ; b = 50 ; c = 50 ; } else if ( testcase == 5 ) { a = 100 ; b = 50 ; c = 50 ; } else if ( testcase == 6 ) { a = 50 ; b = 0 ; c = 50 ; } else if ( testcase == 7 ) { a = 50 ; b = 1 ; c = 50 ; } else if ( testcase == 8 ) { a = 50 ; b = 99 ; c = 50 ; } else if ( testcase == 9 ) { a = 50 ; b = 100 ; c = 50 ; } else if ( testcase == 10 ) { a = 50 ; b = 50 ; c = 0 ; } else if ( testcase == 11 ) { a = 50 ; b = 50 ; c = 1 ; } else if ( testcase == 12 ) { a = 50 ; b = 50 ; c = 99 ; } else if ( testcase == 13 ) { a = 50 ; b = 50 ; c = 100 ; } cout << " TABSYMBOL " << testcase << " TABSYMBOL " << a << " TABSYMBOL " << b << " TABSYMBOL " << c << " TABSYMBOL " ; nature_of_roots ( a , b , c ) ; cout << endl ; testcase ++ ; } } int main ( ) { checkForAllTestCase ( ) ; return 0 ; }
Find if the Vacation can be taken or not | C ++ program to find if the Vacation can be taken or not ; Function to find if the Vacation is possible or not ; Find the required number of hours of study ; find the hours of study that can be done if the vacation is taken ; check if the required hours are less than or equal to the hours of study that can be done if the vacation is taken ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPossible ( int N , int S , int C , int H , int L , int T ) { int total_time_required = S * C * H ; int available_time_after_vacation = ( N - L ) * T ; if ( available_time_after_vacation >= total_time_required ) return 1 ; return 0 ; } int main ( ) { int N = 12 , S = 5 , C = 8 , H = 3 , L = 2 , T = 20 ; if ( isPossible ( N , S , C , H , L , T ) ) cout << " Yes " << endl ; else cout << " No " << endl ; N = 1 , S = 2 , C = 3 , H = 4 , L = 5 , T = 6 ; if ( isPossible ( N , S , C , H , L , T ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Find Maximum and Minimum of two numbers using Absolute function | C ++ program to find maximum and minimum using Absolute function ; Function to return maximum among the two numbers ; Function to return minimum among the two numbers ; Driver code ; Displaying the maximum value ; Displaying the minimum value
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximum ( int x , int y ) { return ( ( x + y + abs ( x - y ) ) / 2 ) ; } int minimum ( int x , int y ) { return ( ( x + y - abs ( x - y ) ) / 2 ) ; } int main ( ) { int x = 99 , y = 18 ; cout << " Maximum : ▁ " << maximum ( x , y ) << endl ; cout << " Minimum : ▁ " << minimum ( x , y ) << endl ; return 0 ; }
Find an array of size N having exactly K subarrays with sum S | C ++ program to find array with K subarrays with sum S ; Function to find array with K subarrays with sum S ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SubarraysWithSumS ( int n , int k , int s ) { for ( int i = 0 ; i < k ; i ++ ) cout << s << " ▁ " ; for ( int i = k ; i < n ; i ++ ) cout << s + 1 << " ▁ " ; } int main ( ) { int n = 4 , k = 2 , s = 3 ; SubarraysWithSumS ( n , k , s ) ; return 0 ; }
Minimum number of cuts required to pay salary from N length Gold Bar | CPP Implementation to find the minimum number of cuts to pay the worker . ; Function to find the minimum number of cuts to pay the worker . ; Nearest Integer to the Log value of the number N ; Driver code ; Cuts Required in the Length of 15
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pay ( int n ) { int cuts = int ( log ( n ) / log ( 2 ) ) ; return cuts ; } int main ( ) { int n = 5 ; int cuts = pay ( n ) ; cout << cuts << endl ; n = 15 ; cuts = pay ( n ) ; cout << ( cuts ) ; return 0 ; }
Sum of Maximum and Minimum prime factor of every number in the Array | C ++ implementation of the approach ; max_prime [ i ] represent maximum prime number that divides the number i ; min_prime [ i ] represent minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; if i is a prime number min_prime number that divide prime number and max_prime number that divide prime number is the number itself . ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to find the sum of the minimum and the maximum prime factors of every number from the given array ; Pre - calculation ; For every element of the given array ; The sum of its smallest and largest prime factor ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100000 ; int max_prime [ MAX ] ; int min_prime [ MAX ] ; void sieve ( int n ) { for ( int i = 2 ; i <= n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; int j = i + i ; while ( j <= n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } void findSum ( int arr [ ] , int n ) { sieve ( MAX ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = min_prime [ arr [ i ] ] + max_prime [ arr [ i ] ] ; cout << sum << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , 10 , 15 , 20 , 25 , 30 } ; int n = sizeof ( arr ) / sizeof ( int ) ; findSum ( arr , n ) ; return 0 ; }
Pair of integers ( a , b ) which satisfy the given equations | C ++ program to count the pair of integers ( a , b ) which satisfy the equation a ^ 2 + b = n and a + b ^ 2 = m ; Function to count valid pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pairCount ( int n , int m ) { int cnt = 0 , b , a ; for ( b = 0 ; b <= sqrt ( m ) ; b ++ ) { a = m - b * b ; if ( a * a + b == n ) { cnt ++ ; } } return cnt ; } int main ( ) { int n = 9 , m = 3 ; cout << pairCount ( n , m ) << endl ; return 0 ; }
Largest Divisor for each element in an array other than 1 and the number itself | C ++ implementation of the approach ; Divisors array to keep track of the maximum divisor ; Function to pre - compute the prime numbers and largest divisors ; Visited array to keep track of prime numbers ; 0 and 1 are not prime numbers ; Initialising divisors [ i ] = i ; For all the numbers divisible by 2 the maximum divisor will be number / 2 ; If divisors [ i ] is not equal to i then this means that divisors [ i ] contains minimum prime divisor for the number ; Update the answer to i / smallest_prime_divisor [ i ] ; Condition if i is a prime number ; If divisors [ j ] is equal to j then this means that i is the first prime divisor for j so we update divi [ j ] = i ; If the current element is prime then it has no divisors other than 1 and itself ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int  long long NEW_LINE const int maxin = 100001 ; int divisors [ maxin ] ; void Calc_Max_Div ( int arr [ ] , int n ) { bool vis [ maxin ] ; memset ( vis , 1 , maxin ) ; vis [ 0 ] = vis [ 1 ] = 0 ; for ( int i = 1 ; i <= maxin ; i ++ ) divisors [ i ] = i ; for ( int i = 4 ; i <= maxin ; i += 2 ) { vis [ i ] = 0 ; divisors [ i ] = i / 2 ; } for ( int i = 3 ; i <= maxin ; i += 2 ) { if ( divisors [ i ] != i ) { divisors [ i ] = i / divisors [ i ] ; } if ( vis [ i ] == 1 ) { for ( int j = i * i ; j < maxin ; j += i ) { vis [ j ] = 0 ; if ( divisors [ j ] == j ) divisors [ j ] = i ; } } } for ( int i = 0 ; i < n ; i ++ ) { if ( divisors [ arr [ i ] ] == arr [ i ] ) cout << " - 1 ▁ " ; else cout << divisors [ arr [ i ] ] << " ▁ " ; } } int32_t main ( ) { int arr [ ] = { 5 , 6 , 7 , 8 , 9 , 10 } ; int n = sizeof ( arr ) / sizeof ( int ) ; Calc_Max_Div ( arr , n ) ; return 0 ; }
Given a number N in decimal base , find the sum of digits in any base B | C ++ Implementation to Compute Sum of Digits of Number N in Base B ; Function to compute sum of Digits of Number N in base B ; Initialize sum with 0 ; Compute unit digit of the number ; Add unit digit in sum ; Update value of Number ; Driver function
#include <iostream> NEW_LINE using namespace std ; int sumOfDigit ( int n , int b ) { int unitDigit , sum = 0 ; while ( n > 0 ) { unitDigit = n % b ; sum += unitDigit ; n = n / b ; } return sum ; } int main ( ) { int n = 50 ; int b = 2 ; cout << sumOfDigit ( n , b ) ; return 0 ; }