codeparrot/xlcost-text-to-code huggingface dataset:
Number of digits in the nth number made of given four digits | C ++ program to count number of digits in n - th number made of given four digits . ; Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int number_of_digits ( int n ) { int i , res , sum = 0 ; for ( i = 4 , res = 1 ; ; i *= 4 , res ++ ) { sum += i ; if ( sum >= n ) break ; } return res ; } int main ( ) { int n = 21 ; cout << number_of_digits ( n ) << endl ; return 0 ; }
Print prime numbers from 1 to N in reverse order | C ++ program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers in reverse order ; Driver Program ; static input ; to display ; Reverseorder ( N ) ; calling the function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Reverseorder ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = n ; p >= 2 ; p -- ) if ( prime [ p ] ) cout << p << " ▁ " ; } int main ( ) { int N = 25 ; cout << " Prime ▁ number ▁ in ▁ reverse ▁ order " << endl ; if ( N == 1 ) cout << " No ▁ prime ▁ no ▁ exist ▁ in ▁ this ▁ range " ; else return 0 ; }
Vantieghems Theorem for Primality Test | C ++ code to verify Vantieghem 's Theorem ; Check if above condition is satisfied ; product of previous powers of 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkVantieghemsTheorem ( int limit ) { long long unsigned prod = 1 ; for ( long long unsigned n = 2 ; n < limit ; n ++ ) { if ( ( ( prod - n ) % ( ( 1LL << n ) - 1 ) ) == 0 ) cout << n << " ▁ is ▁ prime STRNEWLINE " ; prod *= ( ( 1LL << n ) - 1 ) ; } } int main ( ) { checkVantieghemsTheorem ( 10 ) ; return 0 ; }
Count numbers formed by given two digit with sum having given digits | C ++ program to count the number of numbers formed by digits a and b exactly of a length N such that the sum of the digits of the number thus formed is of digits a and b . ; function to check if sum of digits is made of a and b ; sum of digits is 0 ; if any of digits in sum is other than a and b ; calculate the modInverse V / of a number in O ( log n ) ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Update y and x ; Make x positive ; function to pregenerate factorials ; function to pre calculate the modInverse of factorials ; calculates the modInverse of the last factorial ; precalculates the modInverse of all factorials by formulae ; function that returns the value of nCi ; function that returns the count of numbers ; function call to pre - calculate the factorials and modInverse of factorials ; if a and b are same ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1e9 + 7 ; const int N = 1000005 ; int fact [ N ] , invfact [ N ] ; int check ( int x , int a , int b ) { if ( x == 0 ) return 0 ; while ( x ) { if ( x % 10 != a and x % 10 != b ) return 0 ; x /= 10 ; } return 1 ; } int modInverse ( int a , int m ) { int m0 = m ; int y = 0 , x = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { int q = a / m ; int t = m ; m = a % m , a = t ; t = y ; y = x - q * y ; x = t ; } if ( x < 0 ) x += m0 ; return x ; } void pregenFact ( ) { fact [ 0 ] = fact [ 1 ] = 1 ; for ( int i = 1 ; i <= 1000000 ; ++ i ) fact [ i ] = ( long long ) fact [ i - 1 ] * i % mod ; } void pregenInverse ( ) { invfact [ 0 ] = invfact [ 1 ] = 1 ; invfact [ 1000000 ] = modInverse ( fact [ 1000000 ] , mod ) ; for ( int i = 999999 ; i > 1 ; -- i ) invfact [ i ] = ( ( long long ) invfact [ i + 1 ] * ( long long ) ( i + 1 ) ) % mod ; } int comb ( int big , int small ) { return ( long long ) fact [ big ] * invfact [ small ] % mod * invfact [ big - small ] % mod ; } int count ( int a , int b , int n ) { pregenFact ( ) ; pregenInverse ( ) ; if ( a == b ) return ( check ( a * n , a , b ) ) ; int ans = 0 ; for ( int i = 0 ; i <= n ; ++ i ) if ( check ( i * a + ( n - i ) * b , a , b ) ) ans = ( ans + comb ( n , i ) ) % mod ; return ans ; } int main ( ) { int a = 3 , b = 4 , n = 11028 ; cout << count ( a , b , n ) ; return 0 ; }
Finding n | C ++ program to find n - th term of series 3 , 13 , 42 , 108 , 235. . . ; Function to generate a fixed number ; Driver Method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int magicOfSequence ( int N ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) sum += ( i * i * i + i * 2 ) ; return sum ; } int main ( ) { int N = 4 ; cout << magicOfSequence ( N ) << endl ; return 0 ; }
Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | C ++ program to count number of ways to express N as sum of consecutive numbers . ; returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; Driver Code ; N as sum of consecutive numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOddFactors ( long long n ) { int odd_factors = 0 ; for ( int i = 1 ; 1ll * i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( 1ll * i * i == n ) { if ( i & 1 ) odd_factors ++ ; } else { if ( i & 1 ) odd_factors ++ ; int factor = n / i ; if ( factor & 1 ) odd_factors ++ ; } } } return odd_factors - 1 ; } int main ( ) { long long int N = 15 ; cout << countOddFactors ( N ) << endl ; N = 10 ; cout << countOddFactors ( N ) << endl ; return 0 ; }
Making zero array by decrementing pairs of adjacent | CPP program to find if it is possible to make all array elements 0 by decrement operations . ; used for storing the sum of even and odd position element in array . ; if position is odd , store sum value of odd position in odd ; if position is even , store sum value of even position in even ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossibleToZero ( int a [ ] , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i & 1 ) odd += a [ i ] ; else even += a [ i ] ; } return ( odd == even ) ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPossibleToZero ( arr , n ) ) cout << " YES " ; else cout << " NO " ; }
Program for sum of cos ( x ) series | CPP program to find the sum of cos ( x ) series ; here x is in degree . we have to convert it to radian for using it with series formula , as in series expansion angle is in radian ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const double PI = 3.142 ; double cosXSertiesSum ( double x , int n ) { x = x * ( PI / 180.0 ) ; double res = 1 ; double sign = 1 , fact = 1 , pow = 1 ; for ( int i = 1 ; i < 5 ; i ++ ) { sign = sign * -1 ; fact = fact * ( 2 * i - 1 ) * ( 2 * i ) ; pow = pow * x * x ; res = res + sign * pow / fact ; } return res ; } int main ( ) { float x = 50 ; int n = 5 ; cout << cosXSertiesSum ( x , 5 ) ; return 0 ; }
Sum of digits written in different bases from 2 to n | CPP program to find sum of digits of n in different bases from 2 to n - 1. ; function to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple bases ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int n , int base ) { int result = 0 ; while ( n > 0 ) { int remainder = n % base ; result = result + remainder ; n = n / base ; } return result ; } void printSumsOfDigits ( int n ) { for ( int base = 2 ; base < n ; ++ base ) cout << solve ( n , base ) << " ▁ " ; } int main ( ) { int n = 8 ; printSumsOfDigits ( n ) ; return 0 ; }
Possible two sets from first N natural numbers difference of sums as D | C ++ program for implementing above approach ; Function returns true if it is possible to split into two sets otherwise returns false ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int N , int D ) { int temp = ( N * ( N + 1 ) ) / 2 + D ; return ( temp % 2 == 0 ) ; } int main ( ) { int N = 5 ; int M = 7 ; if ( check ( N , M ) ) cout << " yes " ; else cout << " no " ; return 0 ; }
Minimum digits to remove to make a number Perfect Square | C ++ program to find required minimum digits need to remove to make a number perfect square ; function to check minimum number of digits should be removed to make this number a perfect square ; size of the string ; our final answer ; to store string which is perfect square . ; We make all possible subsequences ; to check jth bit is set or not . ; we do not consider a number with leading zeros ; convert our temporary string into integer ; checking temp is perfect square or not . ; taking maximum sized string ; print PerfectSquare ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int perfectSquare ( string s ) { int n = s . size ( ) ; int ans = -1 ; string num ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { string str = " " ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i >> j ) & 1 ) { str += s [ j ] ; } } if ( str [ 0 ] != '0' ) { int temp = 0 ; for ( int j = 0 ; j < str . size ( ) ; j ++ ) temp = temp * 10 + ( int ) ( str [ j ] - '0' ) ; int k = sqrt ( temp ) ; if ( k * k == temp ) { if ( ans < ( int ) str . size ( ) ) { ans = ( int ) str . size ( ) ; num = str ; } } } } if ( ans == -1 ) return ans ; else { cout << num << " ▁ " ; return n - ans ; } } int main ( ) { cout << perfectSquare ( "8314" ) << endl ; cout << perfectSquare ( "753" ) << endl ; return 0 ; }
Lagrange 's four square theorem | CPP program for Lagrange 's four square identity ; Prints all the possible combinations 4 numbers whose sum of squares is equal to the given no . ; loops checking the sum of squares ; if sum of four squares equals the given no . ; printing the numbers ; Driver Code ; 74 = 0 * 0 + 0 * 0 + 5 * 5 + 7 * 7 74 = 0 * 0 + 1 * 1 + 3 * 3 + 8 * 8 74 = 0 * 0 + 3 * 3 + 4 * 4 + 7 * 7 74 = 1 * 1 + 1 * 1 + 6 * 6 + 6 * 6 74 = 2 * 2 + 3 * 3 + 5 * 5 + 6 * 6
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printFourSquares ( int a ) { for ( int i = 0 ; i * i <= a ; i ++ ) { for ( int j = i ; j * j <= a ; j ++ ) { for ( int k = j ; k * k <= a ; k ++ ) { for ( int l = k ; l * l <= a ; l ++ ) { if ( i * i + j * j + k * k + l * l == a ) { cout << a << " ▁ = ▁ " << i << " * " << i << " ▁ + ▁ " << j << " * " << j << " ▁ + ▁ " ; cout << k << " * " << k << " ▁ + ▁ " << l << " * " << l << " STRNEWLINE " ; } } } } } } int main ( ) { int a = 74 ; printFourSquares ( a ) ; return 0 ; }
Hardy | CPP program to count all prime factors ; A function to count prime factors of a given number n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; This condition is to handle the case when n is a prime number greater than 2 ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int exactPrimeFactorCount ( int n ) { int count = 0 ; if ( n % 2 == 0 ) { count ++ ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { count ++ ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) count ++ ; return count ; } int main ( ) { int n = 51242183 ; cout << " The ▁ number ▁ of ▁ distinct ▁ prime ▁ factors ▁ is / are ▁ " << exactPrimeFactorCount ( n ) << endl ; cout << " The ▁ value ▁ of ▁ log ( log ( n ) ) ▁ is ▁ " << log ( log ( n ) ) << endl ; return 0 ; }
Number of Digits in a ^ b | CPP Program to calculate no . of digits in a ^ b ; function to calculate number of digits in a ^ b ; driver program
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int no_of_digit ( int a , int b ) { return ( ( int ) ( b * log10 ( a ) ) + 1 ) ; } int main ( ) { int a = 2 , b = 100 ; cout << " no . ▁ of ▁ digits ▁ = ▁ " << no_of_digit ( a , b ) ; }
Check whether a number is Emirpimes or not | CPP code to check whether a number is Emirpimes or not ; Checking whether a number is semi - prime or not ; Increment count of prime numbers ; If number is still greater than 1 , after exiting the for loop add it to the count variable as it indicates the number is a prime number ; Return '1' if count is equal to '2' else return '0' ; Checking whether a number is emirpimes or not ; Number itself is not semiprime . ; Finding reverse of n . ; The definition of emirpimes excludes palindromes , hence we do not check further , if the number entered is a palindrome ; Checking whether the reverse of the semi prime number entered is also a semi prime number or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkSemiprime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num /= i ; ++ cnt ; } } if ( num > 1 ) ++ cnt ; return cnt == 2 ; } bool isEmirpimes ( int n ) { if ( checkSemiprime ( n ) == false ) return false ; int r = 0 ; for ( int t = n ; t != 0 ; t = t / n ) r = r * 10 + t % 10 ; if ( r == n ) return false ; return ( checkSemiprime ( r ) ) ; } int main ( ) { int n = 15 ; if ( isEmirpimes ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Booth ’ s Multiplication Algorithm | CPP code to implement booth 's algorithm ; function to perform adding in the accumulator ; updating accumulator with A = A + BR ; function to find the number 's complement ; function to perform right shift ; function to display operations ; accumulator content ; multiplier content ; Function to implement booth 's algo ; SECOND CONDITION ; subtract BR from accumulator ; THIRD CONDITION ; add BR to accumulator ; FIRST CONDITION ; decrement counter ; driver code ; Number of multiplicand bit ; multiplicand ; copy multiplier to temp array mt [ ] ; No . of multiplier bit ; sequence counter ; multiplier
#include <bits/stdc++.h> NEW_LINE using namespace std ; void add ( int ac [ ] , int x [ ] , int qrn ) { int i , c = 0 ; for ( i = 0 ; i < qrn ; i ++ ) { ac [ i ] = ac [ i ] + x [ i ] + c ; if ( ac [ i ] > 1 ) { ac [ i ] = ac [ i ] % 2 ; c = 1 ; } else c = 0 ; } } void complement ( int a [ ] , int n ) { int i ; int x [ 8 ] = { 0 } ; x [ 0 ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + 1 ) % 2 ; } add ( a , x , n ) ; } void rightShift ( int ac [ ] , int qr [ ] , int & qn , int qrn ) { int temp , i ; temp = ac [ 0 ] ; qn = qr [ 0 ] ; cout << " TABSYMBOL TABSYMBOL rightShift TABSYMBOL " ; for ( i = 0 ; i < qrn - 1 ; i ++ ) { ac [ i ] = ac [ i + 1 ] ; qr [ i ] = qr [ i + 1 ] ; } qr [ qrn - 1 ] = temp ; } void display ( int ac [ ] , int qr [ ] , int qrn ) { int i ; for ( i = qrn - 1 ; i >= 0 ; i -- ) cout << ac [ i ] ; cout << " TABSYMBOL " ; for ( i = qrn - 1 ; i >= 0 ; i -- ) cout << qr [ i ] ; } void boothAlgorithm ( int br [ ] , int qr [ ] , int mt [ ] , int qrn , int sc ) { int qn = 0 , ac [ 10 ] = { 0 } ; int temp = 0 ; cout << " qn TABSYMBOL q [ n + 1 ] TABSYMBOL TABSYMBOL BR TABSYMBOL TABSYMBOL AC TABSYMBOL QR TABSYMBOL TABSYMBOL sc STRNEWLINE " ; cout << " TABSYMBOL TABSYMBOL TABSYMBOL initial TABSYMBOL TABSYMBOL " ; display ( ac , qr , qrn ) ; cout << " TABSYMBOL TABSYMBOL " << sc << " STRNEWLINE " ; while ( sc != 0 ) { cout << qr [ 0 ] << " TABSYMBOL " << qn ; if ( ( qn + qr [ 0 ] ) == 1 ) { if ( temp == 0 ) { add ( ac , mt , qrn ) ; cout << " TABSYMBOL TABSYMBOL A ▁ = ▁ A ▁ - ▁ BR TABSYMBOL " ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) cout << ac [ i ] ; temp = 1 ; } else if ( temp == 1 ) { add ( ac , br , qrn ) ; cout << " TABSYMBOL TABSYMBOL A ▁ = ▁ A ▁ + ▁ BR TABSYMBOL " ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) cout << ac [ i ] ; temp = 0 ; } cout << " STRNEWLINE TABSYMBOL " ; rightShift ( ac , qr , qn , qrn ) ; } else if ( qn - qr [ 0 ] == 0 ) rightShift ( ac , qr , qn , qrn ) ; display ( ac , qr , qrn ) ; cout << " TABSYMBOL " ; sc -- ; cout << " TABSYMBOL " << sc << " STRNEWLINE " ; } } int main ( int argc , char * * arg ) { int mt [ 10 ] , sc ; int brn , qrn ; brn = 4 ; int br [ ] = { 0 , 1 , 1 , 0 } ; for ( int i = brn - 1 ; i >= 0 ; i -- ) mt [ i ] = br [ i ] ; reverse ( br , br + brn ) ; complement ( mt , brn ) ; qrn = 4 ; sc = qrn ; int qr [ ] = { 1 , 0 , 1 , 0 } ; reverse ( qr , qr + qrn ) ; boothAlgorithm ( br , qr , mt , qrn , sc ) ; cout << endl << " Result ▁ = ▁ " ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) cout << qr [ i ] ; }
Connell Sequence | CPP code to generate first ' n ' terms of Connell Sequence ; Function to generate a fixed number of even or odd terms . The size of r decides whether numbers to be generated even or odd . ; Generating the first ' n ' terms of Connell Sequence ; A dummy 0 is inserted at the beginning for consistency ; Calling function gen ( ) to generate ' k ' number of terms ; Checking if ' n ' terms are already generated ; Removing the previously inserted dummy 0 ; Driver Method
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < long long int > gen ( long long int n , vector < long long int > r ) { long long int a = r [ r . size ( ) - 1 ] ; a ++ ; for ( int i = 1 ; i <= n ; a += 2 , i ++ ) r . push_back ( a ) ; return r ; } vector < long long int > conell ( long long int n ) { vector < long long int > res ; long long int k = 1 ; res . push_back ( 0 ) ; while ( 1 ) { res = gen ( k , res ) ; k ++ ; int j = res . size ( ) - 1 ; while ( j != n && j + k > n ) k -- ; if ( j >= n ) break ; } res . erase ( res . begin ( ) ) ; return res ; } int main ( ) { long long int n = 10 ; cout << " The ▁ first ▁ " << n << " ▁ terms ▁ are " << endl ; vector < long long int > res = conell ( n ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] << " ▁ " ; cout << endl ; return 0 ; }
Generate a list of n consecutive composite numbers ( An interesting method ) | CPP program to print n consecutive composite numbers . ; function to find factorial of given number ; Prints n consecutive numbers . ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; unsigned long long int factorial ( unsigned int n ) { unsigned long long int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } void printNComposite ( int n ) { unsigned long long int fact = factorial ( n + 1 ) ; for ( int i = 2 ; i <= n + 1 ; ++ i ) cout << fact + i << " ▁ " ; } int main ( ) { int n = 4 ; printNComposite ( n ) ; return 0 ; }
Frugal Number | Program to check for Frugal number ; Finding primes upto entered number ; Finding primes by Sieve of Eratosthenes method ; If prime [ i ] is not changed , then it is prime ; Update all multiples of p ; Forming array of the prime numbers found ; Returns number of digits in n ; Checking whether a number is Frugal or not ; Finding number of digits in prime factorization of the number ; Exponent for current factor ; Counting number of times this prime factor divides ( Finding exponent ) ; Finding number of digits in the exponent Avoiding exponents of value 1 ; Checking condition for frugal number ; Driver Method to check for frugal number
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < long long int > primes ( long long int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } vector < long long int > arr ; for ( int i = 2 ; i < n ; i ++ ) if ( prime [ i ] ) arr . push_back ( i ) ; return arr ; } int countDigits ( long long int n ) { long long int temp = n ; int c = 0 ; while ( temp != 0 ) { temp = temp / 10 ; c ++ ; } return c ; } bool frugal ( long long int n ) { vector < long long int > r = primes ( n ) ; long long int t = n ; long long int s = 0 ; for ( int i = 0 ; i < r . size ( ) ; i ++ ) { if ( t % r [ i ] == 0 ) { long long int k = 0 ; while ( t % r [ i ] == 0 ) { t = t / r [ i ] ; k ++ ; } if ( k == 1 ) s = s + countDigits ( r [ i ] ) ; else if ( k != 1 ) s = s + countDigits ( r [ i ] ) + countDigits ( k ) ; } } return ( countDigits ( n ) > s && s != 0 ) ; } int main ( ) { long long int n = 343 ; if ( frugal ( n ) ) cout << " A ▁ Frugal ▁ number STRNEWLINE " ; else cout << " Not ▁ a ▁ frugal ▁ number STRNEWLINE " ; return 0 ; }
N | C ++ program to find n - th number which is both square and cube . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthSquareCube ( int n ) { return n * n * n * n * n * n ; } int main ( ) { int n = 5 ; cout << nthSquareCube ( n ) ; return 0 ; }
Squared triangular number ( Sum of cubes ) | C ++ program to check if a given number is sum of cubes of natural numbers . ; Function to find if the given number is sum of the cubes of first n natural numbers ; Start adding cubes of the numbers from 1 ; If sum becomes equal to s return n ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return -1 ; } int main ( ) { int s = 9 ; int n = findS ( s ) ; n == -1 ? cout << " - 1" : cout << n ; return 0 ; }
Number with even sum of digits | C ++ program to find n - th Good number . ; Function to find kth good number . ; Find the last digit of n . ; If last digit is between 0 to 4 then return 2 * n . ; If last digit is between 5 to 9 then return 2 * n + 1. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int findKthGoodNo ( long long int n ) { int lastDig = n % 10 ; if ( lastDig >= 0 && lastDig <= 4 ) return n << 1 ; else return ( n << 1 ) + 1 ; } int main ( ) { long long int n = 10 ; cout << findKthGoodNo ( n ) ; return 0 ; }
Nicomachu 's Theorem | CPP program to verify Nicomachu 's Theorem ; Compute sum of cubes ; Check if sum is equal to given formula . ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void NicomachuTheorum_sum ( int n ) { int sum = 0 ; for ( int k = 1 ; k <= n ; k ++ ) sum += k * k * k ; int triNo = n * ( n + 1 ) / 2 ; if ( sum == triNo * triNo ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int n = 5 ; NicomachuTheorum_sum ( n ) ; return 0 ; }
Largest even digit number not greater than N | CPP program to print the largest integer not greater than N with all even digits ; function to check if all digits are even of a given number ; iterate for all digits ; if ( ( n % 10 ) % 2 ) if digit is odd ; all digits are even ; function to return the largest number with all digits even ; iterate till we find a number with all digits even ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkDigits ( int n ) { while ( n ) { return 0 ; n /= 10 ; } return 1 ; } int largestNumber ( int n ) { for ( int i = n ; ; i -- ) if ( checkDigits ( i ) ) return i ; } int main ( ) { int N = 23 ; cout << largestNumber ( N ) ; return 0 ; }
Largest even digit number not greater than N | CPP program to print the largest integer not greater than N with all even digits ; function to return the largest number with all digits even ; convert the number to a string for easy operations ; find first odd digit ; if no digit , then N is the answer ; till first odd digit , add all even numbers ; decrease 1 from the odd digit ; add 0 in the rest of the digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestNumber ( int n ) { string s = " " ; int duplicate = n ; while ( n ) { s = char ( n % 10 + 48 ) + s ; n /= 10 ; } int index = -1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( s [ i ] - '0' ) % 2 & 1 ) { index = i ; break ; } } if ( index == -1 ) return duplicate ; int num = 0 ; for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( s [ i ] - '0' ) ; num = num * 10 + ( s [ index ] - '0' - 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 + 8 ; return num ; } int main ( ) { int N = 24578 ; cout << largestNumber ( N ) ; return 0 ; }