codeparrot/xlcost-text-to-code huggingface dataset:
Calculate speed , distance and time | C ++ Program to calculate speed distance and time ; Function to calculate speed ; Function to calculate distance traveled ; Function to calculate time taken ; Driver function ; Calling function cal_speed ( ) ; Calling function cal_dis ( ) ; Calling function cal_time ( )
#include <iostream> NEW_LINE using namespace std ; double cal_speed ( double dist , double time ) { cout << " Distance ( km ) : " cout << " Time ( hr ) : " return dist / time ; } double cal_dis ( double speed , double time ) { cout << " Time ( hr ) : " cout << " Speed ( km / hr ) : " return speed * time ; } double cal_time ( double dist , double speed ) { cout << " Distance ( km ) : " cout << " Speed ( km / hr ) : " return speed * dist ; } int main ( ) { cout << " The calculated Speed ( km / hr ) is : " << cal_speed ( 45.9 , 2.0 ) << endl ; cout << " The calculated Distance ( km ) : " << cal_dis ( 62.9 , 2.5 ) << endl ; cout << " The calculated Time ( hr ) : " << cal_time ( 48.0 , 4.5 ) << endl ; return 0 ; }
Print factorials of a range in right aligned format | CPP Program to print format of factorial ; vector for store the result ; variable for store the each number factorial ; copy of first number ; found first number factorial ; push the first number in result vector ; incerement the first number ; found the all reaming number factorial loop is working until all required number factorial founded ; store the result of factorial ; incerement the first number ; return the result ; function for print the result ; setw ( ) is used for fill the blank right is used for right justification of data ; Driver function ; number which found the factorial of between range ; store the result of factorial ; function for found factorial ; function for print format
#include <boost/multiprecision/cpp_int.hpp> NEW_LINE #include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; using boost :: multiprecision :: cpp_int ; vector < cpp_int > find_factorial ( int num1 , int num2 ) { vector < cpp_int > vec ; cpp_int fac = 1 ; int temp = num1 ; while ( 1 ) { if ( temp == 1 ) break ; fac *= temp ; temp -- ; } vec . push_back ( fac ) ; num1 ++ ; while ( num1 <= num2 ) { fac *= num1 ; vec . push_back ( fac ) ; num1 ++ ; } return ( vec ) ; } void print_format ( vector < cpp_int > & result ) { int digits = result . back ( ) . str ( ) . size ( ) ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { cout << setw ( digits + 1 ) << right << result [ i ] << endl ; } } int main ( ) { int m = 10 , n = 20 ; vector < cpp_int > result_fac ; result_fac = find_factorial ( m , n ) ; print_format ( result_fac ) ; return 0 ; }
Find n | CPP program to find n - th term of series 1 , 3 , 6 , 10 , 15 , 21. . . ; Function to find the nth term of series ; Loop to add numbers ; Driver code
#include <iostream> NEW_LINE using namespace std ; int term ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += i ; return ans ; } int main ( ) { int n = 4 ; cout << term ( n ) ; return 0 ; }
Find the average of first N natural numbers | CPP Program to find the Average of first n natural numbers ; Return the average of first n natural numbers ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; float avgOfFirstN ( int n ) { return ( float ) ( 1 + n ) / 2 ; } int main ( ) { int n = 20 ; cout << avgOfFirstN ( n ) << endl ; return 0 ; }
Find the sum of the series 1 + 11 + 111 + 1111 + ... . . upto n terms | C ++ program to find the sum of the series 1 + 11 + 111 + 1111 + ... . ; Function for finding summation ; Driver Code
#include <bits/stdc++.h> NEW_LINE int summation ( int n ) { int sum ; sum = ( pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ; return sum ; } int main ( ) { int n = 5 ; printf ( " % d " , summation ( n ) ) ; return 0 ; }
Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | C ++ program to find sum of series 1 + x ^ 2 / 2 + x ^ 3 / 3 + ... . + x ^ n / n ; C ++ code to print the sum of the series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double sum ( int x , int n ) { double i , total = 1.0 , multi = x ; for ( i = 1 ; i <= n ; i ++ ) { total = total + multi / i ; multi = multi * x ; } return total ; } int main ( ) { int x = 2 ; int n = 5 ; cout << fixed << setprecision ( 2 ) << sum ( x , n ) ; return 0 ; }
Find n | CPP program to find the nth term of the series 1 2 2 3 3 3 ... ; function to solve the quadratic equation ; calculating the Nth term ; driver code to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int term ( int n ) { int x = ( ( ( 1 ) + ( double ) sqrt ( 1 + ( 8 * n ) ) ) / 2 ) ; return x ; } int main ( ) { int n = 5 ; cout << term ( n ) ; return 0 ; }
Deserium Number | C ++ program to check whether a number is Deserium number or not ; Returns count of digits in n . ; Returns true if x is Diserium ; Compute powers of digits from right to left . ; If sum of powers is same as given number . ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int countDigits ( int n ) { int c = 0 ; do { c ++ ; n = n / 10 ; } while ( n != 0 ) ; return c ; } bool isDeserium ( int x ) { int temp = x ; int p = countDigits ( x ) ; int sum = 0 ; while ( x != 0 ) { int digit = x % 10 ; sum += pow ( digit , p ) ; p -- ; x = x / 10 ; } return ( sum == temp ) ; } int main ( ) { int x = 135 ; if ( isDeserium ( x ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Largest number by which given 3 numbers should be divided such that they leaves same remainder | C ++ program to find the largest numbers that leads to same remainder when divides given three sorted numbers ; __gcd function ; function return number which divides these three number and leaves same remainder . ; We find the differences of all three pairs ; Return GCD of three differences . ; driver program
#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 sameRemainder ( int a , int b , int c ) { int a1 = ( b - a ) , b1 = ( c - b ) , c1 = ( c - a ) ; return gcd ( a1 , gcd ( b1 , c1 ) ) ; } int main ( ) { int a = 62 , b = 132 , c = 237 ; cout << sameRemainder ( a , b , c ) << endl ; return 0 ; }
Find combined mean and variance of two series | C ++ program to find combined mean and variance of two series . ; Function to find mean of series . ; Function to find the standard deviation of series . ; Function to find combined variance of two different series . ; mean1 and mean2 are the mean of two arrays . ; sd1 and sd2 are the standard deviation of two array . ; combinedMean is variable to store the combined mean of both array . ; d1_square and d2_square are the combined mean deviation . ; combinedVar is variable to store combined variance of both array . ; Driver function . ; Function call to combined mean .
#include <bits/stdc++.h> NEW_LINE using namespace std ; float mean ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; float mean = ( float ) sum / n ; return mean ; } float sd ( int arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ; float sdd = sum / n ; return sdd ; } float combinedVariance ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { float mean1 = mean ( arr1 , n ) ; float mean2 = mean ( arr2 , m ) ; cout << " Mean1 : ▁ " << mean1 << " ▁ mean2 : ▁ " << mean2 << endl ; float sd1 = sd ( arr1 , n ) ; float sd2 = sd ( arr2 , m ) ; cout << " StandardDeviation1 : ▁ " << sd1 << " ▁ StandardDeviation2 : ▁ " << sd2 << endl ; float combinedMean = ( float ) ( n * mean1 + m * mean2 ) / ( n + m ) ; cout << " Combined ▁ Mean : ▁ " << combinedMean << endl ; float d1_square = ( mean1 - combinedMean ) * ( mean1 - combinedMean ) ; float d2_square = ( mean2 - combinedMean ) * ( mean2 - combinedMean ) ; cout << " d1 ▁ square : ▁ " << d1_square << " ▁ d2 _ square : ▁ " << d2_square << endl ; float combinedVar = ( n * ( sd1 + d1_square ) + m * ( sd2 + d2_square ) ) / ( n + m ) ; return combinedVar ; } int main ( ) { int arr1 [ ] = { 23 , 45 , 34 , 78 , 12 , 76 , 34 } ; int arr2 [ ] = { 65 , 67 , 34 , 23 , 45 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int m = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << " Combined ▁ Variance : ▁ " << combinedVariance ( arr1 , arr2 , n , m ) ; return 0 ; }
Check if a large number is divisible by 13 or not | CPP program to check whether a number is divisible by 13 or not . ; Returns true if number is divisible by 13 else returns false ; Append required 0 s . at the beginning . ; Same as strcat ( num , "00" ) ; in c . ; Same as strcat ( num , "0" ) ; in c . ; Alternatively add / subtract digits in group of three to result . ; Store group of three numbers in group variable . ; Generate alternate series of plus and minus ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool checkDivisibility ( string num ) { int length = num . size ( ) ; if ( length == 1 && num [ 0 ] == '0' ) return true ; if ( length % 3 == 1 ) { num += "00" ; length += 2 ; } else if ( length % 3 == 2 ) { num += "0" ; length += 1 ; } int sum = 0 , p = 1 ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int group = 0 ; group += num [ i -- ] - '0' ; group += ( num [ i -- ] - '0' ) * 10 ; group += ( num [ i ] - '0' ) * 100 ; sum = sum + group * p ; p *= ( -1 ) ; } sum = abs ( sum ) ; return ( sum % 13 == 0 ) ; } int main ( ) { string number = "83959092724" ; if ( checkDivisibility ( number ) ) cout << number << " ▁ is ▁ divisible ▁ by ▁ 13 . " ; else cout << number << " ▁ is ▁ not ▁ divisible ▁ by ▁ 13 . " ; return 0 ; }
Given two numbers a and b find all x such that a % x = b | C ++ program to find x such that a % x is equal to b . ; if a is less than b then no solution ; if a is equal to b then every number greater than a will be the solution so its infinity ; count variable store the number of values possible ; checking for both divisor and quotient whether they divide ( a - b ) completely and greater than b . ; Here y is added twice in the last iteration so 1 y should be decremented to get correct solution ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void modularEquation ( int a , int b ) { if ( a < b ) { cout << " No ▁ solution ▁ possible ▁ " << endl ; return ; } if ( a == b ) { cout << " Infinite ▁ Solution ▁ possible ▁ " << endl ; return ; } int count = 0 ; int n = a - b ; int y = sqrt ( a - b ) ; for ( int i = 1 ; i <= y ; ++ i ) { if ( n % i == 0 ) { if ( n / i > b ) count ++ ; if ( i > b ) count ++ ; } } if ( y * y == n && y > b ) count -- ; cout << count << endl ; } int main ( ) { int a = 21 , b = 5 ; modularEquation ( a , b ) ; return 0 ; }
Count different numbers that can be generated such that there digits sum is equal to ' n ' | C ++ program to count ways to write ' n ' as sum of digits ; Function to count ' num ' as sum of digits ( 1 , 2 , 3 , 4 ) ; Initialize dp [ ] array ; Base case ; Initialize the current dp [ ] array as '0' ; if i == j then there is only one way to write with element itself ' i ' ; If j == 1 , then there exist two ways , one from '1' and other from '4' ; if i - j is positive then pick the element from ' i - j ' element of dp [ ] array ; Check for modulas ; return the final answer ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countWays ( int num ) { int dp [ num + 1 ] ; const int MOD = 1e9 + 7 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= num ; ++ i ) { dp [ i ] = 0 ; for ( int j = 1 ; j <= 3 ; ++ j ) { if ( i - j == 0 ) dp [ i ] += 1 ; else if ( j == 1 ) dp [ i ] += dp [ i - j ] * 2 ; else if ( i - j > 0 ) dp [ i ] += dp [ i - j ] ; if ( dp [ i ] >= MOD ) dp [ i ] %= MOD ; } } return dp [ num ] ; } int main ( ) { int n = 3 ; cout << countWays ( n ) ; return 0 ; }
Check whether a number can be represented by sum of two squares | An efficient approach based implementation to find if a number can be written as sum of two squares . ; function to check if there exist two numbers sum of whose squares is n . ; store square value in hashmap ; driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sumSquare ( int n ) { unordered_map < int , int > s ; for ( int i = 0 ; i * i <= n ; ++ i ) { s [ i * i ] = 1 ; if ( s . find ( n - i * i ) != s . end ( ) ) { cout << sqrt ( n - i * i ) << " ^ 2 ▁ + ▁ " << i << " ^ 2" << endl ; return true ; } } return false ; } int main ( ) { int n = 169 ; if ( sumSquare ( n ) ) cout << " Yes " ; else cout << " No " ; }
Check whether a number can be represented by sum of two squares | Check whether a number can be represented by sum of two squares using Fermat Theorem . ; Count all the prime factors . ; Ifany prime factor of the form ( 4 k + 3 ) ( 4 k + 3 ) occurs an odd number of times . ; If n itself is a x prime number and can be expressed in the form of 4 k + 3 we return false . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool judgeSquareSum ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { int count = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { count ++ ; n /= i ; } if ( i % 4 == 3 && count % 2 != 0 ) return false ; } } return n % 4 != 3 ; } int main ( ) { int n = 17 ; if ( judgeSquareSum ( n ) ) cout << " Yes " ; else cout << " No " ; }
Total no of 1 's in numbers | c ++ code to count the frequency of 1 in numbers less than or equal to the given number . ; function to count the frequency of 1. ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigitOne ( int n ) { int countr = 0 ; for ( int i = 1 ; i <= n ; i *= 10 ) { int divider = i * 10 ; countr += ( n / divider ) * i + min ( max ( n % divider - i + 1 , 0 ) , i ) ; } return countr ; } int main ( ) { int n = 13 ; cout << countDigitOne ( n ) << endl ; n = 113 ; cout << countDigitOne ( n ) << endl ; n = 205 ; cout << countDigitOne ( n ) << endl ; }
Largest number with prime digits | CPP program to find largest number smaller than equal to n with all prime digits . ; check if character is prime ; replace with previous prime character ; if 2 erase s [ i ] and replace next with 7 ; find first non prime char ; find first char greater than 2 ; like 20 ; like 7721 ; replace remaining with 7 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( char c ) { return ( c == '2' c == '3' c == '5' c == '7' ) ; } void decrease ( string & s , int i ) { if ( s [ i ] <= '2' ) { s . erase ( i , 1 ) ; s [ i ] = '7' ; } else if ( s [ i ] == '3' ) s [ i ] = '2' ; else if ( s [ i ] <= '5' ) s [ i ] = '3' ; else if ( s [ i ] <= '7' ) s [ i ] = '5' ; else s [ i ] = '7' ; return ; } string primeDigits ( string s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! isPrime ( s [ i ] ) ) { while ( s [ i ] <= '2' && i >= 0 ) i -- ; if ( i < 0 ) { i = 0 ; decrease ( s , i ) ; } else decrease ( s , i ) ; for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) s [ j ] = '7' ; break ; } } return s ; } int main ( ) { string s = "45" ; cout << primeDigits ( s ) << endl ; s = "1000" ; cout << primeDigits ( s ) << endl ; s = "7721" ; cout << primeDigits ( s ) << endl ; s = "7221" ; cout << primeDigits ( s ) << endl ; s = "74545678912345689748593275897894708927680" ; cout << primeDigits ( s ) << endl ; return 0 ; }
Divisors of n | C ++ program to count number of divisors of n ^ 2 which are not divisible by divisor of n ; Function to count divisors of n ^ 2 having no factors of ' n ' ; Increment count of i - th prime divisor ; Find next prime divisor ; Increment count if divisor still remains ; Initialize variable for counting the factors of n ^ 2 and n as ans1 and ans2 respectively ; Range based for - loop ; Use formula as discussed in above ; return the difference of answers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factors ( long long n ) { unordered_map < int , int > prime ; for ( int i = 2 ; i <= sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { ++ prime [ i ] ; n = n / i ; } } if ( n > 2 ) ++ prime [ n ] ; int ans1 = 1 , ans2 = 1 ; for ( auto it : prime ) { ans1 *= 2 * it . second + 1 ; ans2 *= it . second + 1 ; } return ans1 - ans2 ; } int main ( ) { long long n = 5 ; cout << factors ( n ) << endl ; n = 8 ; cout << factors ( n ) ; return 0 ; }
Print digit 's position to be removed to make a number divisible by 6 | C ++ program to print digit 's position to be removed to make number divisible by 6 ; function to print the number divisible by 6 after exactly removing a digit ; stores the sum of all elements ; traverses the string and converts string to number array and sums up ; if ( a [ n - 1 ] % 2 ) ODD CHECK ; if second last is odd or sum of n - 1 elements are not divisible by 3. ; second last is even and print n - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( sum - last ) % 3 == 0 ; if no element which on removing gives sum % 3 == 0 ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void greatest ( string s ) { int n = s . length ( ) ; int a [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = s [ i ] - '0' ; sum += a [ i ] ; } { if ( a [ n - 2 ] % 2 != 0 or ( sum - a [ n - 1 ] ) % 3 != 0 ) { cout << " - 1" << endl ; } else { cout << n << endl ; } } else { int re = sum % 3 ; int del = -1 ; int flag = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] ) % 3 == re ) { if ( a [ i + 1 ] > a [ i ] ) { del = i ; flag = 1 ; break ; } else { del = i ; } } } if ( flag == 0 ) { if ( a [ n - 2 ] % 2 == 0 and re == a [ n - 1 ] % 3 ) del = n - 1 ; } if ( del == -1 ) cout << -1 << endl ; else { cout << del + 1 << endl ; } } } int main ( ) { string s = "7510222" ; greatest ( s ) ; return 0 ; }
Representation of a number in powers of other | CPP program to check if m can be represented as powers of w . ; break ; None of 3 worked . ; If m is not zero means , it can 't be  represented in terms of powers of w. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool asPowerSum ( int w , int m ) { while ( m ) { if ( ( m - 1 ) % w == 0 ) m = ( m - 1 ) / w ; else if ( ( m + 1 ) % w == 0 ) m = ( m + 1 ) / w ; else if ( m % w == 0 ) m = m / w ; else } return ( m == 0 ) ; } int main ( ) { int w = 3 , m = 7 ; if ( asPowerSum ( w , m ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Number of digits to be removed to make a number divisible by 3 | CPP program to find the minimum number of digits to be removed to make a large number divisible by 3. ; function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; Otherwise we can always make a number multiple of 2 by removing 2 digits . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divisible ( string num ) { int n = num . length ( ) ; int sum = accumulate ( begin ( num ) , end ( num ) , 0 ) - '0' * 1 ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return -1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num [ i ] - '0' ) % 3 ) return 1 ; if ( n == 2 ) return -1 ; return 2 ; } int main ( ) { string num = "1234" ; cout << divisible ( num ) ; return 0 ; }
Program for dot product and cross product of two vectors | C ++ implementation for dot product and cross product of two vector . ; Function that return dot product of two vector array . ; Loop for calculate cot product ; Function to find cross product of two vector array . ; Driver function ; dotProduct function call ; crossProduct function call ; Loop that print cross product of two vector array .
#include <bits/stdc++.h> NEW_LINE #define n  3 NEW_LINE using namespace std ; int dotProduct ( int vect_A [ ] , int vect_B [ ] ) { int product = 0 ; for ( int i = 0 ; i < n ; i ++ ) product = product + vect_A [ i ] * vect_B [ i ] ; return product ; } void crossProduct ( int vect_A [ ] , int vect_B [ ] , int cross_P [ ] ) { cross_P [ 0 ] = vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ; cross_P [ 1 ] = vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ; cross_P [ 2 ] = vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ; } int main ( ) { int vect_A [ ] = { 3 , -5 , 4 } ; int vect_B [ ] = { 2 , 6 , 5 } ; int cross_P [ n ] ; cout << " Dot ▁ product : " ; cout << dotProduct ( vect_A , vect_B ) << endl ; cout << " Cross ▁ product : " ; crossProduct ( vect_A , vect_B , cross_P ) ; for ( int i = 0 ; i < n ; i ++ ) cout << cross_P [ i ] << " ▁ " ; return 0 ; }
Number of n digit numbers that do not contain 9 | CPP program to find number of n digit numbers that do not contain 9 as it 's digit ; function to find number of n digit numbers possible ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalNumber ( int n ) { return 8 * pow ( 9 , n - 1 ) ; } int main ( ) { int n = 3 ; cout << totalNumber ( n ) ; return 0 ; }
Count ways to express even number â €˜ nâ €™ as sum of even integers | C ++ program to count ways to write number as sum of even integers ; Initialize mod variable as constant ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; 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 ; Return number of ways to write ' n ' as sum of even integers ; Driver code
#include <iostream> NEW_LINE using namespace std ; const int MOD = 1e9 + 7 ; int power ( int x , unsigned int y , int p ) { while ( y > 0 ) { if ( y & 1 ) res = ( 1LL * res * x ) % p ; x = ( 1LL * x * x ) % p ; } return res ; } int countEvenWays ( int n ) { return power ( 2 , n / 2 - 1 , MOD ) ; } int main ( ) { int n = 6 ; cout << countEvenWays ( n ) << " STRNEWLINE " ; n = 8 ; cout << countEvenWays ( n ) ; return 0 ; }
Number of steps to convert to prime factors | CPP program to count number of steps required to convert an integer array to array of factors . ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate the number of representations ; keep an count of prime factors ; traverse for every element ; count the no of factors ; subtract 1 if Ai is not 1 as the last step wont be taken into count ; driver program to test the above function ; call sieve to calculate the factors
#include <iostream> NEW_LINE using namespace std ; const int MAX = 1000001 ; int factor [ MAX ] = { 0 } ; void cal_factor ( ) { factor [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( int i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( int j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } int no_of_representations ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int temp = a [ i ] ; int flag = 0 ; while ( factor [ temp ] != 1 ) { flag = -1 ; count ++ ; temp = temp / factor [ temp ] ; } count += flag ; } return count ; } int main ( ) { cal_factor ( ) ; int a [ ] = { 4 , 4 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << no_of_representations ( a , n ) ; return 0 ; }