codeparrot/xlcost-text-to-code huggingface dataset:
Count number of integers less than or equal to N which has exactly 9 divisors | C ++ implementation of above approach ; Function to count numbers having exactly 9 divisors ; Sieve array ; initially prime [ i ] = i ; use sieve concept to store the first prime factor of every number ; mark all factors of i ; check for all numbers if they can be expressed in form p * q ; p prime factor ; q prime factor ; if both prime factors are different if p * q <= n and q != ; Check if it can be expressed as p ^ 8 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int n ) { int c = 0 ; int limit = sqrt ( n ) ; int prime [ limit + 1 ] ; for ( int i = 1 ; i <= limit ; i ++ ) prime [ i ] = i ; for ( int i = 2 ; i * i <= limit ; i ++ ) { if ( prime [ i ] == i ) { for ( int j = i * i ; j <= limit ; j += i ) if ( prime [ j ] == j ) prime [ j ] = i ; } } for ( int i = 2 ; i <= limit ; i ++ ) { int p = prime [ i ] ; int q = prime [ i / prime [ i ] ] ; if ( p * q == i && q != 1 && p != q ) { c += 1 ; } else if ( prime [ i ] == i ) { if ( pow ( i , 8 ) <= n ) { c += 1 ; } } } return c ; } int main ( ) { int n = 1000 ; cout << countNumbers ( n ) ; return 0 ; }
Interprime | CPP program to check if a number is interprime or not ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the given number is interprime or not ; Smallest Interprime is 4 So the number less than 4 can not be a Interprime ; Calculate first prime number < n ; Calculate first prime number > n ; Check if prev_prime and next_prime have the same average ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } bool isInterprime ( int n ) { if ( n < 4 ) return false ; int prev_prime = n ; int next_prime = n ; while ( ! isPrime ( prev_prime ) ) { prev_prime -- ; } while ( ! isPrime ( next_prime ) ) { next_prime ++ ; } if ( ( prev_prime + next_prime ) == 2 * n ) return true ; else return false ; } int main ( ) { int n = 9 ; if ( isInterprime ( n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Find the unit place digit of sum of N factorials | C ++ program to find the unit place digit of the first N natural numbers factorials ; Function to find the unit 's place digit ; Let us write for cases when N is smaller than or equal to 4. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ) % 10 = 3 else ( N >= 4 ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; int get_unit_digit ( long long int N ) { if ( N == 0 N == 1 ) return 1 ; else if ( N == 2 ) return 3 ; else if ( N == 3 ) return 9 ; return 3 ; } int main ( ) { long long int N = 1 ; for ( N = 0 ; N <= 10 ; N ++ ) cout << " For ▁ N ▁ = ▁ " << N << " ▁ : ▁ " << get_unit_digit ( N ) << endl ; return 0 ; }
Sum of squares of Fibonacci numbers | C ++ Program to find sum of squares of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Function to calculate sum of squares of Fibonacci numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; int f [ MAX ] = { 0 } ; int fib ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 n == 2 ) return ( f [ n ] = 1 ) ; if ( f [ n ] ) return f [ n ] ; int k = ( n & 1 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( n & 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } int calculateSumOfSquares ( int n ) { return fib ( n ) * fib ( n + 1 ) ; } int main ( ) { int n = 6 ; cout << " Sum ▁ of ▁ Squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " << calculateSumOfSquares ( n ) << endl ; return 0 ; }
Number of solutions for the equation x + y + z <= n | CPP program to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; function to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; to store answer ; for values of x ; for values of y ; maximum possible value of z ; if z value greater than equals to 0 then only it is valid ; find minimum of temp and z ; return required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int NumberOfSolutions ( int x , int y , int z , int n ) { int ans = 0 ; for ( int i = 0 ; i <= x ; i ++ ) { for ( int j = 0 ; j <= y ; j ++ ) { int temp = n - i - j ; if ( temp >= 0 ) { temp = min ( temp , z ) ; ans += temp + 1 ; } } } return ans ; } int main ( ) { int x = 1 , y = 2 , z = 3 , n = 4 ; cout << NumberOfSolutions ( x , y , z , n ) ; return 0 ; }
Program to find the Nth term of series 5 , 12 , 21 , 32 , 45. ... . . | CPP program to find the N - th term of the series : 5 , 12 , 21 , 32 , 45. . ... . ; calculate Nth term of series ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int nthTerm ( int n ) { return pow ( n , 2 ) + 4 * n ; } int main ( ) { int N = 4 ; cout << nthTerm ( N ) << endl ; return 0 ; }
Numbers less than N which are product of exactly two distinct prime numbers | C ++ program to find numbers that are product of exactly two distinct prime numbers ; Function to check whether a number is a PerfectSquare or not ; Function to check if a number is a product of exactly two distinct primes ; Function to find numbers that are product of exactly two distinct prime numbers . ; Vector to store such numbers ; insert in the vector ; Print all numbers till n from the vector ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ; } bool isProduct ( 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 ; } void findNumbers ( int N ) { vector < int > vec ; for ( int i = 1 ; i <= N ; i ++ ) { if ( isProduct ( i ) && ! isPerfectSquare ( i ) ) { vec . push_back ( i ) ; } } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { cout << vec [ i ] << " ▁ " ; } } int main ( ) { int N = 30 ; findNumbers ( N ) ; return 0 ; }
Program to find the Nth term of the series 3 , 20 , 63 , 144 , 230 , â €¦ â €¦ | CPP program to find N - th term of the series : 3 , 20 , 63 , 144 , 230 . ... . ; calculate Nth term of series ; return final sum ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int nthTerm ( int n ) { return 2 * pow ( n , 3 ) + pow ( n , 2 ) ; } int main ( ) { int N = 3 ; cout << nthTerm ( N ) ; return 0 ; }
Find Nth number of the series 1 , 6 , 15 , 28 , 45 , ... . . | CPP program to find Nth term of the series ; function to return nth term of the series ; Driver code ; Taking n as 4 ; Printing the nth term
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  1000000009 NEW_LINE int NthTerm ( long long n ) { long long x = ( 2 * n * n ) % mod ; return ( x - n + mod ) % mod ; } int main ( ) { long long N = 4 ; cout << NthTerm ( N ) ; return 0 ; }
Program to print the Sum of series | CPP program to find SUM upto N - th term of the series : - 1 , 2 , 11 , 26 , 47 , 74 , ... . . ; calculate Nth term of series ; Driver Function ; Get the value of N ; Get the sum of the series
#include <iostream> NEW_LINE using namespace std ; int findSum ( int N ) { return ( N * ( N + 1 ) * ( 2 * N - 5 ) + 4 * N ) / 2 ; } int main ( ) { int N = 3 ; cout << findSum ( N ) << endl ; return 0 ; }
Program to find the Nth term of series | CPP program to find N - th term of the series : 9 , 23 , 45 , 75 , 113 , 159. . ... . ; calculate Nth term of series ; Driver Function ; Get the value of N ; Find the Nth term and print it
#include <iostream> ; using namespace std ; int nthTerm ( int N ) { return ( ( 3 * N * N ) - ( 6 * N ) + 2 ) ; } int main ( ) { int N = 3 ; cout << nthTerm ( N ) ; return 0 ; }
Program to find the value of tan ( nÎ ˜ ) | C ++ program to find the value of cos ( n - theta ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; store required answer ; use to toggle sign in sequence . ; calculate numerator ; calculate denominator ; Driver code .
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE #define MAX  16 NEW_LINE using namespace std ; ll nCr [ MAX ] [ MAX ] = { 0 } ; void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } double findTanNTheta ( double tanTheta , ll n ) { double ans = 0 , numerator = 0 , denominator = 0 ; ll toggle = 1 ; for ( int i = 1 ; i <= n ; i += 2 ) { numerator = numerator + nCr [ n ] [ i ] * pow ( tanTheta , i ) * toggle ; toggle = toggle * -1 ; } denominator = 1 ; toggle = -1 ; for ( int i = 2 ; i <= n ; i += 2 ) { numerator = numerator + nCr [ n ] [ i ] * pow ( tanTheta , i ) * toggle ; toggle = toggle * -1 ; } ans = numerator / denominator ; return ans ; } int main ( ) { binomial ( ) ; double tanTheta = 0.3 ; ll n = 10 ; cout << findTanNTheta ( tanTheta , n ) << endl ; return 0 ; }
Program to find Sum of the series 1 * 3 + 3 * 5 + ... . | C ++ program to find sum of first n terms ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; number of terms to be included in the sum ; find the Sn
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSum ( int n ) { return ( n * ( 4 * n * n + 6 * n - 1 ) / 3 ) ; } int main ( ) { int n = 4 ; cout << " Sum ▁ = ▁ " << calculateSum ( n ) ; return 0 ; }
Find all duplicate and missing numbers in given permutation array of 1 to N | C ++ program for the above approach ; Function to find the duplicate and the missing elements over the range [ 1 , N ] ; Stores the missing and duplicate numbers in the array arr [ ] ; Traverse the given array arr [ ] ; Check if the current element is not same as the element at index arr [ i ] - 1 , then swap ; Otherwise , increment the index ; Traverse the array again ; If the element is not at its correct position ; Stores the missing and the duplicate elements ; Print the Missing Number ; Print the Duplicate Number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findElements ( int arr [ ] , int N ) { int i = 0 ; vector < int > missing ; set < int > duplicate ; set < int > :: iterator it ; while ( i != N ) { cout << i << " ▁ # ▁ " ; if ( arr [ i ] != arr [ arr [ i ] - 1 ] ) { swap ( arr [ i ] , arr [ arr [ i ] - 1 ] ) ; } else { i ++ ; } } for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != i + 1 ) { missing . push_back ( i + 1 ) ; duplicate . insert ( arr [ i ] ) ; } } cout << " Missing ▁ Numbers : ▁ " ; for ( auto & it : missing ) cout << it << ' ▁ ' ; cout << " Duplicate Numbers : " for ( auto & it : duplicate ) cout << it << ' ▁ ' ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 2 , 4 , 5 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findElements ( arr , N ) ; return 0 ; }
Triplet with no element divisible by 3 and sum N | C ++ program to print a , b and c such that a + b + c = N ; Function to print a , b and c ; first loop ; check for 1 st number ; second loop ; check for 2 nd number ; third loop ; Check for 3 rd number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCombination ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( i % 3 != 0 ) { for ( int j = 1 ; j < n ; j ++ ) { if ( j % 3 != 0 ) { for ( int k = 1 ; k < n ; k ++ ) { if ( k % 3 != 0 && ( i + j + k ) == n ) { cout << i << " ▁ " << j << " ▁ " << k ; return ; } } } } } } } int main ( ) { int n = 233 ; printCombination ( n ) ; return 0 ; }
Program to find the percentage of difference between two numbers | C ++ program to calculate the percentage ; Function to calculate the percentage ; Driver Code . ; Function calling
#include <iostream> NEW_LINE using namespace std ; int percent ( int a , int b ) { float result = 0 ; result = ( ( b - a ) * 100 ) / a ; return result ; } int main ( ) { int a = 20 , b = 25 ; cout << percent ( a , b ) << " % " ; return 0 ; }
Numbers with sum of digits equal to the sum of digits of its all prime factor | C ++ program to Find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum . ; maximum size of number ; array to store smallest prime factor of number ; array to store sum of digits of a number ; boolean array to check given number is countable for required answer or not . ; prefix array to store answer ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; Function to find sum of digits in a number ; find sum of digits of all numbers up to MAXN ; add sum of digits of least prime factor and n / spf [ n ] ; if it is valid make isValid true ; prefix sum to compute answer ; Driver code ; decleartion ; print answer for required range ; print answer for required range
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  100005 NEW_LINE int spf [ MAXN ] = { 0 } ; int sum_digits [ MAXN ] = { 0 } ; bool isValid [ MAXN ] = { 0 } ; int ans [ MAXN ] = { 0 } ; void Smallest_prime_factor ( ) { for ( int i = 1 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i <= MAXN ; i += 2 ) if ( spf [ i ] == i ) for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } int Digit_Sum ( int copy ) { int d = 0 ; while ( copy ) { d += copy % 10 ; copy /= 10 ; } return d ; } void Sum_Of_All_Digits ( ) { for ( int n = 2 ; n < MAXN ; n ++ ) { sum_digits [ n ] = sum_digits [ n / spf [ n ] ] + Digit_Sum ( spf [ n ] ) ; if ( Digit_Sum ( n ) == sum_digits [ n ] ) isValid [ n ] = true ; } for ( int n = 2 ; n < MAXN ; n ++ ) { if ( isValid [ n ] ) ans [ n ] = 1 ; ans [ n ] += ans [ n - 1 ] ; } } int main ( ) { Smallest_prime_factor ( ) ; Sum_Of_All_Digits ( ) ; int l , r ; l = 2 , r = 3 ; cout << " Valid ▁ numbers ▁ in ▁ the ▁ range ▁ " << l << " ▁ " << r << " ▁ are ▁ " << ans [ r ] - ans [ l - 1 ] << endl ; l = 2 , r = 10 ; cout << " Valid ▁ numbers ▁ in ▁ the ▁ range ▁ " << l << " ▁ " << r << " ▁ are ▁ " << ans [ r ] - ans [ l - 1 ] << endl ; return 0 ; }
Count numbers which can be represented as sum of same parity primes | Function to calculate count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate ( int * array , int size ) { int count = 0 ; for ( int i = 0 ; i < size ; i ++ ) if ( array [ i ] % 2 == 0 && array [ i ] != 0 && array [ i ] != 2 ) count ++ ; return count ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 6 } ; int size = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << calculate ( a , size ) ; }
N | C ++ program to answer queries for N - th prime factor of a number ; 2 - D vector that stores prime factors ; Function to pre - store prime factors of all numbers till 10 ^ 6 ; calculate unique prime factors for every number till 10 ^ 6 ; find prime factors ; store if prime factor ; Function that returns answer for every query ; Driver Code ; Function to pre - store unique prime factors ; 1 st query ; 2 nd query ; 3 rd query ; 4 th query
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1000001 ; vector < int > v [ N ] ; void preprocess ( ) { for ( int i = 1 ; i < N ; i ++ ) { int num = i ; for ( int j = 2 ; j <= sqrt ( num ) ; j ++ ) { if ( num % j == 0 ) { v [ i ] . push_back ( j ) ; while ( num % j == 0 ) { num = num / j ; } } } if ( num > 2 ) v [ i ] . push_back ( num ) ; } } int query ( int number , int n ) { return v [ number ] [ n - 1 ] ; } int main ( ) { preprocess ( ) ; int number = 6 , n = 1 ; cout << query ( number , n ) << endl ; number = 210 , n = 3 ; cout << query ( number , n ) << endl ; number = 210 , n = 2 ; cout << query ( number , n ) << endl ; number = 60 , n = 2 ; cout << query ( number , n ) << endl ; return 0 ; }
Program to find HCF ( Highest Common Factor ) of 2 Numbers | C ++ program to find GCD of two numbers ; Recursive function to return gcd of a and b ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int main ( ) { int a = 98 , b = 56 ; cout << " GCD ▁ of ▁ " << a << " ▁ and ▁ " << b << " ▁ is ▁ " << gcd ( a , b ) ; return 0 ; }
Egg Dropping Puzzle with 2 Eggs and K Floors | CPP program to find optimal number of trials for k floors and 2 eggs . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int twoEggDrop ( int k ) { return ceil ( ( -1.0 + sqrt ( 1 + 8 * k ) ) / 2.0 ) ; } int main ( ) { int k = 100 ; cout << twoEggDrop ( k ) ; return 0 ; }
Program to find the Area and Volume of Icosahedron | C ++ program to find the Area and volume of Icosahedron ; Function to find area of Icosahedron ; Formula to calculating area ; Function to find volume of Icosahedron ; Formula to calculating volume ; Driver Code ; Function call to find area of Icosahedron . ; Function call to find volume of Icosahedron .
#include <bits/stdc++.h> NEW_LINE using namespace std ; float findArea ( float a ) { float area ; area = 5 * sqrt ( 3 ) * a * a ; return area ; } float findVolume ( float a ) { float volume ; volume = ( ( float ) 5 / 12 ) * ( 3 + sqrt ( 5 ) ) * a * a * a ; return volume ; } int main ( ) { float a = 5 ; cout << " Area : ▁ " << findArea ( a ) << endl ; cout << " Volume : ▁ " << findVolume ( a ) ; return 0 ; }
Total number of ways to place X and Y at n places such that no two X are together | C ++ program to find the number of ways Calculate total ways to place ' x ' and ' y ' at n places such that no two ' x ' are together ; Function to return number of ways ; for n = 1 ; for n = 2 ; iterate to find Fibonacci term ; Driver Code ; total number of places
#include <iostream> NEW_LINE using namespace std ; int ways ( int n ) { int first = 2 ; int second = 3 ; int res = 0 ; for ( int i = 3 ; i <= n ; i ++ ) { res = first + second ; first = second ; second = res ; } return res ; } int main ( ) { int n = 7 ; cout << " Total ▁ ways ▁ are ▁ : ▁ " ; cout << ways ( n ) ; return 0 ; }
Number of digits in N factorial to the power N | CPP program to find count of digits in N factorial raised to N ; we take sum of logarithms as explained in the approach ; multiply the result with n ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int n ) { double ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += log10 ( i ) ; ans = ans * n ; return 1 + floor ( ans ) ; } int main ( ) { int n = 4 ; cout << countDigits ( n ) << " STRNEWLINE " ; return 0 ; }
Program to convert centimeter into meter and kilometer | C ++ program to convert centimeter into meter and kilometer ; Driver Code ; Converting centimeter into meter and kilometer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { float cm , meter , kilometer ; cm = 1000 ; meter = cm / 100.0 ; kilometer = cm / 100000.0 ; cout << " Length ▁ in ▁ meter ▁ = ▁ " << meter << " m " << " STRNEWLINE " ; cout << " Length ▁ in ▁ Kilometer ▁ = ▁ " << kilometer << " km " << " STRNEWLINE " ; return 0 ; }
Check if two people starting from different points ever meet | C ++ program to find if two people starting from different positions ever meet or not . ; If speed of a person at a position before other person is smaller , then return false . ; Making sure that x1 is greater ; checking if relative speed is a factor of relative distance or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool everMeet ( int x1 , int x2 , int v1 , int v2 ) { if ( x1 < x2 && v1 <= v2 ) return false ; if ( x1 > x2 && v1 >= v2 ) return false ; if ( x1 < x2 ) { swap ( x1 , x2 ) ; swap ( v1 , v2 ) ; } return ( ( x1 - x2 ) % ( v1 - v2 ) == 0 ) ; } int main ( ) { int x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ; if ( everMeet ( x1 , x2 , v1 , v2 ) ) printf ( " Yes " ) ; else printf ( " No " ) ; return 0 ; }
Find ceil of a / b without using ceil ( ) function | C ++ program to find ceil ( a / b ) without using ceil ( ) function ; Driver function ; taking input 1 ; example of perfect division taking input 2
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int main ( ) { int a = 4 ; int b = 3 ; int val = ( a + b - 1 ) / b ; cout << " The ▁ ceiling ▁ value ▁ of ▁ 4/3 ▁ is ▁ " << val << endl ; a = 6 ; b = 3 ; val = ( a + b - 1 ) / b ; cout << " The ▁ ceiling ▁ value ▁ of ▁ 6/3 ▁ is ▁ " << val << endl ; return 0 ; }
Sum of range in a series of first odd then even natural numbers | CPP program to find sum in the given range in the sequence 1 3 5 7. ... . N 2 4 6. . . N - 1 ; Function that returns sum in the range 1 to x in the sequence 1 3 5 7. ... . N 2 4 6. . . N - 1 ; number of odd numbers ; number of extra even numbers required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll sumTillX ( ll x , ll n ) { ll odd = ceil ( n / 2.0 ) ; if ( x <= odd ) return x * x ; ll even = x - odd ; return ( ( odd * odd ) + ( even * even ) + even ) ; } int rangeSum ( int N , int L , int R ) { return sumTillX ( R , N ) - sumTillX ( L - 1 , N ) ; } int main ( ) { ll N = 10 , L = 1 , R = 6 ; cout << rangeSum ( N , L , R ) ; return 0 ; }
Twin Prime Numbers between 1 and n | C ++ program print all twin primes 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 ; to check for twin prime numbers display the twin primes ; Driver Program to test above function ; Calling the function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printTwinPrime ( 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 = 2 ; p <= n - 2 ; p ++ ) if ( prime [ p ] && prime [ p + 2 ] ) cout << " ( " << p << " , ▁ " << p + 2 << " ) " ; } int main ( ) { int n = 25 ; printTwinPrime ( n ) ; return 0 ; }
Cube Free Numbers smaller than n | Simple C ++ Program to print all cube free numbers smaller than or equal to n . ; Returns true if n is a cube free number , else returns false . ; check for all possible divisible cubes ; Print all cube free numbers smaller than n . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCubeFree ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i * i <= n ; i ++ ) if ( n % ( i * i * i ) == 0 ) return false ; return true ; } void printCubeFree ( int n ) { for ( int i = 2 ; i <= n ; i ++ ) if ( isCubeFree ( i ) ) cout << i << " ▁ " ; } int main ( ) { int n = 20 ; printCubeFree ( n ) ; return 0 ; }
Decimal Equivalent of Gray Code and its Inverse | CPP Program to convert given decimal number of gray code into its inverse in decimal form ; Function to convert given decimal number of gray code into its inverse in decimal form ; Taking xor until n becomes zero ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int inversegrayCode ( int n ) { int inv = 0 ; for ( ; n ; n = n >> 1 ) inv ^= n ; return inv ; } int main ( ) { int n = 15 ; cout << inversegrayCode ( n ) << endl ; return 0 ; }