codeparrot/xlcost-text-to-code huggingface dataset:
Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + …… + ( 2 + 4 + 6 + 8 + … . + 2 n ) | C ++ implementation to find the sum of the given series ; function to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 2 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } int main ( ) { int n = 5 ; cout << " Sum ▁ = ▁ " << sumOfTheSeries ( n ) ; return 0 ; }
Program to find sum of series 1 * 2 * 3 + 2 * 3 * 4 + 3 * 4 * 5 + . . . + n * ( n + 1 ) * ( n + 2 ) | Program to find the sum of series 1 * 2 * 3 + 2 * 3 * 4 + . . . + n * ( n + 1 ) * ( n + 1 ) ; Function to calculate sum of series . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + i * ( i + 1 ) * ( i + 2 ) ; return sum ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; }
Program to get the Sum of series : 1 | C ++ program to get the sum of the series ; Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code
#include <math.h> NEW_LINE #include <stdio.h> NEW_LINE double Series ( double x , int n ) { double sum = 1 , term = 1 , fct = 1 , p = 1 , multi = 1 ; for ( int i = 1 ; i < n ; i ++ ) { fct = fct * multi * ( multi + 1 ) ; p = p * x * x ; term = ( -1 ) * term ; multi += 2 ; sum = sum + ( term * p ) / fct ; } return sum ; } int main ( ) { double x = 9 ; int n = 10 ; printf ( " % .4f " , Series ( x , n ) ) ; return 0 ; }
Find the number of consecutive zero at the end after multiplying n numbers | CPP program to find the number of consecutive zero at the end after multiplying n numbers ; Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's factor ; Function to count number of zeros ; Count the two 's factor of n number ; Count the five 's factor of n number ; Return the minimum ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int two_factor ( int n ) { int twocount = 0 ; while ( n % 2 == 0 ) { twocount ++ ; n = n / 2 ; } return twocount ; } int five_factor ( int n ) { int fivecount = 0 ; while ( n % 5 == 0 ) { fivecount ++ ; n = n / 5 ; } return fivecount ; } int find_con_zero ( int arr [ ] , int n ) { int twocount = 0 ; int fivecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { twocount += two_factor ( arr [ i ] ) ; fivecount += five_factor ( arr [ i ] ) ; } if ( twocount < fivecount ) return twocount ; else return fivecount ; } int main ( ) { int arr [ ] = { 100 , 10 , 5 , 25 , 35 , 14 } ; int n = 6 ; cout << find_con_zero ( arr , n ) ; }
First occurrence of a digit in a given fraction | CPP program to find first occurrence of c in a / b ; function to print the first digit ; reduce the number to its mod ; traverse for every decimal places ; get every fraction places when ( a * 10 / b ) / c ; check if it is equal to the required integer ; mod the number ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int first ( int a , int b , int c ) { a %= b ; for ( int i = 1 ; i <= b ; i ++ ) { a = a * 10 ; if ( a / b == c ) return i ; a %= b ; } return -1 ; } int main ( ) { int a = 1 , b = 4 , c = 5 ; cout << first ( a , b , c ) ; return 0 ; }
Minimize the absolute difference of sum of two subsets | CPP program to Minimize the absolute difference of sum of two subsets ; function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subsetDifference ( int n ) { int s = n * ( n + 1 ) / 2 ; if ( n % 4 == 0 ) { cout << " First ▁ subset ▁ sum ▁ = ▁ " << s / 2 ; cout << " Second subset sum = " << s / 2 ; cout << " Difference = " } else { if ( n % 4 == 1 n % 4 == 2 ) { cout << " First ▁ subset ▁ sum ▁ = ▁ " << s / 2 ; cout << " Second subset sum = " << s / 2 + 1 ; cout << " Difference = " } else { cout << " First ▁ subset ▁ sum ▁ = ▁ " << s / 2 ; cout << " Second subset sum = " << s / 2 ; cout << " Difference = " } } } int main ( ) { int n = 6 ; subsetDifference ( n ) ; return 0 ; }
Time required to meet in equilateral triangle | CPP code to find time taken by animals to meet ; function to calculate time to meet ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void timeToMeet ( double s , double v ) { double V = 3 * v / 2 ; double time = s / V ; cout << time ; } int main ( void ) { double s = 25 , v = 56 ; timeToMeet ( s , v ) ; return 0 ; }
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + â €¦ â €¦ + ( 1 + 3 + 5 + 7 + â €¦ + ( 2 n | C ++ implementation to find the sum of the given series ; functionn to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } int main ( ) { int n = 5 ; cout << " Sum ▁ = ▁ " << sumOfTheSeries ( n ) ; return 0 ; }
Check if a number can be written as sum of three consecutive integers | CPP Program to check if a number can be written as sum of three consecutive integers . ; function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checksum ( int n ) { if ( n == 0 ) { cout << " - 1 ▁ 0 ▁ 1" << endl ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = -1 ; for ( int i = 0 ; i <= n - 2 ; i += inc ) { if ( i + i + 1 + i + 2 == n ) { cout << i << " ▁ " << i + 1 << " ▁ " << i + 2 ; return ; } } cout << " - 1" ; } int main ( ) { int n = 6 ; checksum ( n ) ; return 0 ; }
Sum of all divisors from 1 to n | C ++ program to find sum of all divisor of number up to ' n ' ; Utility function to find sum of all divisor of number up to ' n ' ; Find all divisors of i and add them ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } } return sum ; } int main ( ) { int n = 4 ; cout << " ▁ " << divisorSum ( n ) << endl ; n = 5 ; cout << " ▁ " << divisorSum ( n ) ; return 0 ; }
Program for Binomial Coefficients table | C ++ program for binomial coefficients ; Function to print binomial table ; B ( m , x ) is 1 if either m or x is is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Function
#include <stdio.h> NEW_LINE int printbinomial ( int max ) { for ( int m = 0 ; m <= max ; m ++ ) { printf ( " % 2d " , m ) ; int binom = 1 ; for ( int x = 0 ; x <= m ; x ++ ) { if ( m != 0 && x != 0 ) binom = binom * ( m - x + 1 ) / x ; printf ( " % 4d " , binom ) ; } printf ( " STRNEWLINE " ) ; } } int main ( ) { int max = 10 ; printbinomial ( max ) ; return 0 ; }
Find largest prime factor of a number | C ++ Program to find largest prime factor of number ; A function to find largest prime factor ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; n >>= 1 ; equivalent to n /= 2 ; n must be odd at this point ; now we have to iterate only for integers who does not have prime factor 2 and 3 ; This condition is to handle the case when n is a prime number greater than 4 ; Driver program to test above function
#include <iostream> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; long long maxPrimeFactors ( long long n ) { long long maxPrime = -1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; } while ( n % 3 == 0 ) { maxPrime = 3 ; n = n / 3 ; } for ( int i = 5 ; i <= sqrt ( n ) ; i += 6 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } while ( n % ( i + 2 ) == 0 ) { maxPrime = i + 2 ; n = n / ( i + 2 ) ; } } if ( n > 4 ) maxPrime = n ; return maxPrime ; } int main ( ) { long long n = 15 ; cout << maxPrimeFactors ( n ) << endl ; n = 25698751364526 ; cout << maxPrimeFactors ( n ) ; }
Count unset bits in a range | C ++ implementation to count unset bits in the given range ; Function to get no of set bits in the binary representation of ' n ' ; function to count unset bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of unset bits in the range ' l ' to ' r ' in ' n ' ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int countSetBits ( int n ) { unsigned int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } unsigned int countUnsetBitsInGivenRange ( unsigned int n , unsigned int l , unsigned int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( r - l + 1 ) - countSetBits ( n & num ) ; } int main ( ) { unsigned int n = 80 ; unsigned int l = 1 , r = 4 ; cout << countUnsetBitsInGivenRange ( n , l , r ) ; return 0 ; }
Midy 's theorem | C ++ implementation as a proof of the Midy 's theorem ; Returns repeating sequence of a fraction . If repeating sequence doesn 't exits, then returns -1 ; Create a map to store already seen remainders remainder is used as key and its position in result is stored as value . Note that we need position for cases like 1 / 6. In this case , the recurring sequence doesn 't start from first  remainder.  ; Find first remainder ; Keep finding remainder until either remainder becomes 0 or repeats ; Store this remainder ; Multiply remainder with 10 ; Append rem / denr to result ; Update remainder ; Checks whether a number is prime or not ; If all conditions are met , it proves Midy 's theorem ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string fractionToDecimal ( int numerator , int denominator ) { string res ; map < int , int > mp ; mp . clear ( ) ; int rem = numerator % denominator ; while ( ( rem != 0 ) && ( mp . find ( rem ) == mp . end ( ) ) ) { mp [ rem ] = res . length ( ) ; rem = rem * 10 ; int res_part = rem / denominator ; res += to_string ( res_part ) ; rem = rem % denominator ; } return ( rem == 0 ) ? " - 1" : res . substr ( mp [ rem ] ) ; } bool isPrime ( int n ) { for ( int i = 2 ; i <= n / 2 ; i ++ ) if ( n % i == 0 ) return false ; return true ; } void Midys ( string str , int n ) { int l = str . length ( ) ; int part1 = 0 , part2 = 0 ; if ( ! isPrime ( n ) ) { cout << " Denominator ▁ is ▁ not ▁ prime , ▁ " << " thus ▁ Midy ' s ▁ theorem ▁ is ▁ not ▁ applicable " ; } else if ( l % 2 == 0 ) { for ( int i = 0 ; i < l / 2 ; i ++ ) { part1 = part1 * 10 + ( str [ i ] - '0' ) ; part2 = part2 * 10 + ( str [ l / 2 + i ] - '0' ) ; } cout << part1 << " ▁ + ▁ " << part2 << " ▁ = ▁ " << ( part1 + part2 ) << endl ; cout << " Midy ' s ▁ theorem ▁ holds ! " ; } else { cout << " The ▁ repeating ▁ decimal ▁ is ▁ of ▁ odd ▁ length ▁ " << " thus ▁ Midy ' s ▁ theorem ▁ is ▁ not ▁ applicable " ; } } int main ( ) { int numr = 2 , denr = 11 ; string res = fractionToDecimal ( numr , denr ) ; if ( res == " - 1" ) cout << " The ▁ fraction ▁ does ▁ not ▁ have ▁ repeating ▁ decimal " ; else { cout << " Repeating ▁ decimal ▁ = ▁ " << res << endl ; Midys ( res , denr ) ; } return 0 ; }
Sum of fourth power of first n even natural numbers | CPP Program to find the sum of fourth powers of first n even natural numbers ; calculate the sum of fourth power of first n even natural numbers ; made even number ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int evenPowerSum ( int n ) { long long int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int j = 2 * i ; sum = sum + ( j * j * j * j ) ; } return sum ; } int main ( ) { int n = 5 ; cout << evenPowerSum ( n ) << endl ; return 0 ; }
Sum of fourth power of first n even natural numbers | CPP Program to find the sum of fourth powers of first n even natural numbers ; calculate the sum of fourth power of first n even natural numbers ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int evenPowerSum ( int n ) { return ( 8 * n * ( n + 1 ) * ( 2 * n + 1 ) * ( 3 * n * n + 3 * n - 1 ) ) / 15 ; } int main ( ) { int n = 4 ; cout << evenPowerSum ( n ) << endl ; return 0 ; }
Balanced Prime | CPP Program to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  501 NEW_LINE using namespace std ; int balancedprime ( int n ) { bool prime [ MAX + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } vector < int > v ; for ( int p = 3 ; p <= MAX ; p += 2 ) if ( prime [ p ] ) v . push_back ( p ) ; int count = 0 ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] == ( v [ i + 1 ] + v [ i - 1 ] ) / 2 ) count ++ ; if ( count == n ) return v [ i ] ; } } int main ( ) { int n = 4 ; cout << balancedprime ( n ) << endl ; return 0 ; }
Smallest integer which has n factors or more | c ++ program to print the smallest integer with n factors or more ; 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 number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; if its a new prime factor that is factorizing n , then we again set c = 1 and change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; function to find the smallest integer with n factors or more . ; check if no of factors is more than n or not ; Driver program to test above function ; generate prime factors of number upto 10 ^ 6
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000001 ; int factor [ MAX ] = { 0 } ; void generatePrimeFactors ( ) { 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 calculateNoOFactors ( int n ) { if ( n == 1 ) return 1 ; int ans = 1 ; int dup = factor [ n ] ; int c = 1 ; int j = n / factor [ n ] ; while ( j != 1 ) { if ( factor [ j ] == dup ) c += 1 ; else { dup = factor [ j ] ; ans = ans * ( c + 1 ) ; c = 1 ; } j = j / factor [ j ] ; } ans = ans * ( c + 1 ) ; return ans ; } int smallest ( int n ) { for ( int i = 1 ; ; i ++ ) if ( calculateNoOFactors ( i ) >= n ) return i ; } int main ( ) { generatePrimeFactors ( ) ; int n = 4 ; cout << smallest ( n ) ; return 0 ; }
Sum of squares of first n natural numbers | CPP Program to find sum of square of first n natural numbers ; Return the sum of square of first n natural numbers ; Iterate i from 1 and n finding square of i and add to sum . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int squaresum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } int main ( ) { int n = 4 ; cout << squaresum ( n ) << endl ; return 0 ; }
Break a number such that sum of maximum divisors of all parts is minimum | CPP program to break a number such that sum of maximum divisors of all parts is minimum ; Function to check if a number is prime or not . ; If n is an even number ( we can write it as sum of two primes ) ; If n is odd and n - 2 is prime . ; If n is odd , n - 3 must be even . ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isPrime ( int n ) { int i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) return false ; i ++ ; } return true ; } int minimumSum ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; if ( isPrime ( n - 2 ) ) return 2 ; return 3 ; } int main ( ) { int n = 27 ; cout << minimumSum ( n ) ; return 0 ; }
Find first and last digits of a number | Program to find first and last digits of a number ; Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstDigit ( int n ) { while ( n >= 10 ) n /= 10 ; return n ; } int lastDigit ( int n ) { return ( n % 10 ) ; } int main ( ) { int n = 98562 ; cout << firstDigit ( n ) << " ▁ " << lastDigit ( n ) << endl ; return 0 ; }
Find first and last digits of a number | Program to find first and last digits of a number ; Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; return the last digit ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstDigit ( int n ) { int digits = ( int ) log10 ( n ) ; n = ( int ) ( n / pow ( 10 , digits ) ) ; return n ; } int lastDigit ( int n ) { return ( n % 10 ) ; } int main ( ) { int n = 98562 ; cout << firstDigit ( n ) << " ▁ " << lastDigit ( n ) << endl ; return 0 ; }
Express an odd number as sum of prime numbers | CPP program to express N as sum of at - most three prime numbers . ; Function to check if a number is prime or not . ; Prints at most three prime numbers whose sum is n . ; if ( isPrime ( n ) ) CASE - I ; else if ( isPrime ( n - 2 ) ) CASE - II ; else CASE - III ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int x ) { if ( x == 0 x == 1 ) return false ; for ( int i = 2 ; i * i <= x ; ++ i ) if ( x % i == 0 ) return false ; return true ; } void findPrimes ( int n ) { cout << n << endl ; cout << 2 << " ▁ " << n - 2 << endl ; { cout << 3 << " ▁ " ; n = n - 3 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( i ) && isPrime ( n - i ) ) { cout << i << " ▁ " << ( n - i ) ; break ; } } } } int main ( ) { int n = 27 ; findPrimes ( n ) ; return 0 ; }
AKS Primality Test | C ++ code to check if number is prime . This program demonstrates concept behind AKS algorithm and doesn 't implement the actual algorithm (This works only till n = 64) ; array used to store coefficients . ; function to calculate the coefficients of ( x - 1 ) ^ n - ( x ^ n - 1 ) with the help of Pascal 's triangle . ; function to check whether the number is prime or not ; Calculating all the coefficients by the function coef and storing all the coefficients in c array . ; subtracting c [ n ] and adding c [ 0 ] by 1 as ( x - 1 ) ^ n - ( x ^ n - 1 ) , here we are subtracting c [ n ] by 1 and adding 1 in expression . ; checking all the coefficients whether they are divisible by n or not . if n is not prime , then loop breaks and ( i > 0 ) . ; Return true if all coefficients are divisible by n . ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long c [ 100 ] ; void coef ( int n ) { c [ 0 ] = 1 ; for ( int i = 0 ; i < n ; c [ 0 ] = - c [ 0 ] , i ++ ) { c [ 1 + i ] = 1 ; for ( int j = i ; j > 0 ; j -- ) c [ j ] = c [ j - 1 ] - c [ j ] ; } } bool isPrime ( int n ) { coef ( n ) ; c [ 0 ] ++ , c [ n ] -- ; int i = n ; while ( i -- && c [ i ] % n == 0 ) ; return i < 0 ; } int main ( ) { int n = 37 ; if ( isPrime ( n ) ) cout << " Prime " << endl ; else cout << " Not ▁ Prime " << endl ; return 0 ; }
Motzkin number | CPP Program to find Nth Motzkin Number . ; Return the nth Motzkin Number . ; Base Case ; Recursive step ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int motzkin ( int n ) { if ( n == 0 n == 1 ) return 1 ; return ( ( 2 * n + 1 ) * motzkin ( n - 1 ) + ( 3 * n - 3 ) * motzkin ( n - 2 ) ) / ( n + 2 ) ; } int main ( ) { int n = 8 ; cout << motzkin ( n ) << endl ; return 0 ; }
Sum of the series 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms | CPP program to find sum of 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms ; function which return the the sum of series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float sumOfSeries ( int n ) { return ( 0.666 ) * ( 1 - 1 / pow ( 10 , n ) ) ; } int main ( ) { int n = 2 ; cout << sumOfSeries ( n ) ; }
Narcissistic number | CPP program for checking of Narcissistic number ; function to count digits ; Returns true if n is Narcissistic number ; count the number of digits ; calculates the sum of digits raised to power ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigit ( int n ) { if ( n == 0 ) return 0 ; return 1 + countDigit ( n / 10 ) ; } bool check ( int n ) { int l = countDigit ( n ) ; int dup = n ; int sum = 0 ; while ( dup ) { sum += pow ( dup % 10 , l ) ; dup /= 10 ; } return ( n == sum ) ; } int main ( ) { int n = 1634 ; if ( check ( n ) ) cout << " yes " ; else cout << " no " ; return 0 ; }
Sum of squares of first n natural numbers | CPP program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... ; Function to calculate sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; int summation ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } int main ( ) { int n = 2 ; cout << summation ( n ) ; return 0 ; }
Leyland Number | CPP program to print first N Leyland Numbers . ; Print first n Leyland Number . ; Outer loop for x from 2 to n . ; Inner loop for y from 2 to x . ; Calculating x ^ y + y ^ x ; Sorting the all Leyland Number . ; Printing first n Leyland number . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  100 NEW_LINE using namespace std ; void leyland ( int n ) { vector < int > ans ; for ( int x = 2 ; x <= n ; x ++ ) { for ( int y = 2 ; y <= x ; y ++ ) { int temp = pow ( x , y ) + pow ( y , x ) ; ans . push_back ( temp ) ; } } sort ( ans . begin ( ) , ans . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) cout << ans [ i ] << " ▁ " ; } int main ( ) { int n = 6 ; leyland ( n ) ; return 0 ; }
Nicomachusâ €™ s Theorem ( Sum of k | CPP program to find sum of k - th group of positive odd integers . ; Return the sum of k - th group of positive odd integers . ; Finding first element of kth group . ; Finding the sum . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- ) { sum += cur ; cur += 2 ; } return sum ; } int main ( ) { int k = 3 ; cout << kthgroupsum ( k ) << endl ; return 0 ; }
n | CPP program to find n - th term of the series 2 , 12 , 36 , 80 , 150 , . . ; Returns n - th term of the series 2 , 12 , 36 , 80 , 150 ; driver code
#include <iostream> NEW_LINE using namespace std ; int nthTerm ( int n ) { return ( n * n ) + ( n * n * n ) ; } int main ( ) { int n = 4 ; cout << nthTerm ( n ) ; return 0 ; }
Sum of the series 1 , 3 , 6 , 10. . . ( Triangular Numbers ) | CPP program to find sum series 1 , 3 , 6 , 10 , 15 , 21. . . and then find its sum ; Function to find the sum of series ; Driver code
#include <iostream> NEW_LINE using namespace std ; int seriesSum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } int main ( ) { int n = 4 ; cout << seriesSum ( n ) ; return 0 ; }