codeparrot/xlcost-text-to-code huggingface dataset:
Count of Prime Nodes of a Singly Linked List | C ++ implementation to find count of prime numbers in the singly linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Function to check if a number is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find count of prime nodes in a linked list ; If current node is prime ; Update count ; Driver program ; start with the empty list ; create the linked list 15 -> 5 -> 6 -> 10 -> 17 ; Function call to print require answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } 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 ; } int countPrime ( Node * * head_ref ) { int count = 0 ; Node * ptr = * head_ref ; while ( ptr != NULL ) { if ( isPrime ( ptr -> data ) ) { count ++ ; } ptr = ptr -> next ; } return count ; } int main ( ) { Node * head = NULL ; push ( & head , 17 ) ; push ( & head , 10 ) ; push ( & head , 6 ) ; push ( & head , 5 ) ; push ( & head , 15 ) ; cout << " Count ▁ of ▁ prime ▁ nodes ▁ = ▁ " << countPrime ( & head ) ; return 0 ; }
Smallest prime divisor of a number | C ++ program to count the number of subarrays that having 1 ; Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestDivisor ( int n ) { if ( n % 2 == 0 ) return 2 ; for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) return i ; } return n ; } int main ( ) { int n = 31 ; cout << smallestDivisor ( n ) ; return 0 ; }
Count Number of animals in a zoo from given number of head and legs | C ++ implementation of above approach ; Function that calculates Rabbits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countRabbits ( int Heads , int Legs ) { int count = 0 ; count = ( Legs ) -2 * ( Heads ) ; count = count / 2 ; return count ; } int main ( ) { int Heads = 100 , Legs = 300 ; int Rabbits = countRabbits ( Heads , Legs ) ; cout << " Rabbits ▁ = ▁ " << Rabbits << endl ; cout << " Pigeons ▁ = ▁ " << Heads - Rabbits << endl ; return 0 ; }
Program to evaluate the expression ( ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¹ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ Ã ¢ â ‚¬¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¦¡ X + 1 ) ^ 6 + ( ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¹ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ Ã ¢ â ‚¬¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¦¡ X | CPP program to evaluate the given expression ; Function to find the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float calculateSum ( float n ) { int a = int ( n ) ; return 2 * ( pow ( n , 6 ) + 15 * pow ( n , 4 ) + 15 * pow ( n , 2 ) + 1 ) ; } int main ( ) { float n = 1.4142 ; cout << ceil ( calculateSum ( n ) ) << endl ; return 0 ; }
Find the sum of series 3 , | C ++ program to find sum upto N term of the series : 3 , - 6 , 12 , - 24 , ... . . ; calculate sum upto N term of series ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; class gfg { public : int Sum_upto_nth_Term ( int n ) { return ( 1 - pow ( -2 , n ) ) ; } } ; int main ( ) { gfg g ; int N = 5 ; cout << g . Sum_upto_nth_Term ( N ) ; }
Count numbers whose XOR with N is equal to OR with N | C ++ program to find the XOR equals OR count ; Function to calculate count of numbers with XOR equals OR ; variable to store count of unset bits ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; class gfg { public : int xorEqualsOrCount ( int N ) { int count = 0 ; int bit ; while ( N > 0 ) { bit = N % 2 ; if ( bit == 0 ) count ++ ; N = N / 2 ; } return ( int ) pow ( 2 , count ) ; } } ; int main ( ) { gfg g ; int N = 7 ; cout << g . xorEqualsOrCount ( N ) ; return 0 ; }
Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | C ++ Program to compute sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! ; Method to find the factorial of a number ; Method to compute the sum ; Iterate the loop till n and compute the formula ; Driver code ; Get x and n ; Print output
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int fact ( int n ) { if ( n == 1 ) return 1 ; return n * fact ( n - 1 ) ; } double sum ( int x , int n ) { double i , total = 1.0 ; for ( i = 1 ; i <= n ; i ++ ) { total = total + ( pow ( x , i ) / fact ( i + 1 ) ) ; } return total ; } int main ( ) { int x = 5 , n = 4 ; cout << " Sum ▁ is : ▁ " << sum ( x , n ) ; return 0 ; }
Find Sum of Series 1 ^ 2 | C ++ program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... ; Function to find sum of series ; If i is even ; If i is odd ; return the result ; Driver Code ; Get n ; Find the sum ; Get n ; Find the sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_of_series ( int n ) { int result = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) result = result - pow ( i , 2 ) ; else result = result + pow ( i , 2 ) ; } return result ; } int main ( void ) { int n = 3 ; cout << sum_of_series ( n ) << endl ; n = 10 ; cout << sum_of_series ( n ) << endl ; }
Program to find the sum of the series 23 + 45 + 75 + ... . . upto N terms | CPP program to find sum upto N - th term of the series : 23 , 45 , 75 , 113. . . ; 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 ( 2 * N * ( N + 1 ) * ( 4 * N + 17 ) + 54 * N ) / 6 ; } int main ( ) { int N = 4 ; cout << findSum ( N ) << endl ; return 0 ; }
Program to find the value of sin ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | C ++ Program to find the value of sin ( n ? ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; find cosTheta from sinTheta ; store required answer ; use to toggle sign in sequence . ; 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 findCosNTheta ( double sinTheta , ll n ) { double cosTheta = sqrt ( 1 - sinTheta * sinTheta ) ; double ans = 0 ; ll toggle = 1 ; for ( int i = 1 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * pow ( cosTheta , n - i ) * pow ( sinTheta , i ) * toggle ; toggle = toggle * -1 ; } return ans ; } int main ( ) { binomial ( ) ; double sinTheta = 0.5 ; ll n = 10 ; cout << findCosNTheta ( sinTheta , n ) << endl ; return 0 ; }
Program to find Nth term of series 9 , 23 , 45 , 75 , 113. . . | CPP program to find N - th term of the series : 9 , 23 , 45 , 75 , 113. . . ; calculate Nth term of series ; Driver Function ; Get the value of N ; Find the Nth term and print it
#include <iostream> NEW_LINE using namespace std ; int nthTerm ( int N ) { return ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ; } int main ( ) { int N = 4 ; cout << nthTerm ( N ) ; return 0 ; }
Program to Find the value of cos ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | CPP program to find the value of cos ( n - theta ) ; Function to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of cos ( n - theta ) ; find sinTheta from cosTheta ; to store required answer ; use to toggle sign in sequence . ; Driver code
#include <bits/stdc++.h> NEW_LINE #define MAX  16 NEW_LINE using namespace std ; int 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 findCosnTheta ( double cosTheta , int n ) { double sinTheta = sqrt ( 1 - cosTheta * cosTheta ) ; double ans = 0 ; int toggle = 1 ; for ( int i = 0 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * pow ( cosTheta , n - i ) * pow ( sinTheta , i ) * toggle ; toggle = toggle * -1 ; } return ans ; } int main ( ) { binomial ( ) ; double cosTheta = 0.5 ; int n = 10 ; cout << findCosnTheta ( cosTheta , n ) << endl ; return 0 ; }
Find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | CPP program to find the sum of given series ; Function to calculate sum ; Return sum ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int findSum ( int n ) { return ( pow ( 10 , n + 1 ) * ( 9 * n - 1 ) + 10 ) / pow ( 9 , 3 ) - n * ( n + 1 ) / 18 ; } int main ( ) { int n = 3 ; cout << findSum ( n ) ; return 0 ; }
Find sum of the series 1 | C ++ program to find the sum of series 1 - 2 + 3 - 4 + ... ... ; Function to calculate sum ; when n is odd ; when n is not odd ; Driver code
#include <iostream> NEW_LINE using namespace std ; int solve_sum ( int n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } int main ( ) { int n = 8 ; cout << solve_sum ( n ) ; return 0 ; }
Check if a number can be expressed as a ^ b | Set 2 | CPP program to check if a number can be expressed as a ^ b . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = log ( a ) / log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } int main ( ) { int n = 16 ; cout << ( isPower ( n ) ? " Yes " : " No " ) ; return 0 ; }
Program to calculate Root Mean Square | CPP program to calculate Root Mean Square ; Function that Calculate Root Mean Square ; Calculate square . ; Calculate Mean . ; Calculate Root . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float rmsValue ( int arr [ ] , int n ) { int square = 0 ; float mean = 0.0 , root = 0.0 ; for ( int i = 0 ; i < n ; i ++ ) { square += pow ( arr [ i ] , 2 ) ; } mean = ( square / ( float ) ( n ) ) ; root = sqrt ( mean ) ; return root ; } int main ( ) { int arr [ ] = { 10 , 4 , 6 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << rmsValue ( arr , n ) ; return 0 ; }
Program to find the quantity after mixture replacement | C ++ implementation using above formula ; Function to calculate the Remaining amount . ; calculate Right hand Side ( RHS ) . ; calculate Amount left by multiply it with original value . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float Mixture ( int X , int Y , int Z ) { float result = 0.0 , result1 = 0.0 ; result1 = ( ( X - Y ) / ( float ) X ) ; result = pow ( result1 , Z ) ; result = result * X ; return result ; } int main ( ) { int X = 10 , Y = 2 , Z = 2 ; cout << Mixture ( X , Y , Z ) << " ▁ litres " ; return 0 ; }
Sum of sum of all subsets of a set formed by first N natural numbers | C ++ program to find Sum of all subsets of a set formed by first N natural numbers | Set - 2 ; modulo value ; 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 the result ; y must be even now y = y >> 1 ; y = y / 2 ; function to find ff ( n ) ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  (int)(1e9 + 7) NEW_LINE int power ( int x , int y , int p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int check ( int n ) { n -- ; int ans = n * n ; if ( ans >= mod ) ans %= mod ; ans += n + 2 ; if ( ans >= mod ) ans %= mod ; ans = ( power ( 2 , n , mod ) % mod * ans % mod ) % mod ; ans = ( ans - 1 + mod ) % mod ; return ans ; } int main ( ) { int n = 4 ; cout << check ( n ) << endl ; return 0 ; }
Program to find LCM of 2 numbers without using GCD | C ++ program to find LCM of 2 numbers without using GCD ; Function to return LCM of two numbers ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLCM ( int a , int b ) { int lar = max ( a , b ) ; int small = min ( a , b ) ; for ( int i = lar ; ; i += lar ) { if ( i % small == 0 ) return i ; } } int main ( ) { int a = 5 , b = 7 ; cout << " LCM ▁ of ▁ " << a << " ▁ and ▁ " << b << " ▁ is ▁ " << findLCM ( a , b ) ; return 0 ; }
Smarandache | C ++ program to print the first ' n ' terms of the Smarandache - Wellin Sequence ; Function to collect first ' n ' prime numbers ; List to store first ' n ' primes ; Function to generate Smarandache - Wellin Sequence ; Storing the first ' n ' prime numbers in a list ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void primes ( int n ) { int i = 2 ; int j = 0 ; int result [ n ] ; int z = 0 ; while ( j < n ) { bool flag = true ; for ( int item = 2 ; item <= ( int ) ( i * 1 / 2 ) ; item ++ ) if ( i % item == 0 && i != item ) { flag = false ; break ; } if ( flag ) { result [ z ++ ] = i ; j += 1 ; } i += 1 ; } for ( i = 0 ; i < 5 ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) cout << result [ j ] ; cout << " ▁ " ; } } void smar_wln ( int n ) { primes ( n ) ; } int main ( ) { int n = 5 ; cout << " First ▁ " << n << " ▁ terms ▁ of ▁ the ▁ Sequence ▁ are " << endl ; smar_wln ( n ) ; }
Pentatope number | CPP Program to find the nth Pentatope number ; function for Pentatope number ; formula for find Pentatope nth term ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Pentatope_number ( int n ) { return n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 24 ; } int main ( ) { int n = 7 ; cout << n << " th ▁ Pentatope ▁ number ▁ : " << Pentatope_number ( n ) << endl ; n = 12 ; cout << n << " th ▁ Pentatope ▁ number ▁ : " << Pentatope_number ( n ) << endl ; return 0 ; }
Program for Centered Icosahedral Number | C ++ Program to find nth Centered icosahedral number ; Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int centeredIcosahedralNum ( int n ) { return ( 2 * n + 1 ) * ( 5 * n * n + 5 * n + 3 ) / 3 ; } int main ( ) { int n = 10 ; cout << centeredIcosahedralNum ( n ) << endl ; n = 12 ; cout << centeredIcosahedralNum ( n ) << endl ; return 0 ; }
Centered Square Number | C ++ program to find nth Centered square number . ; Function to calculate Centered square number function ; Formula to calculate nth Centered square number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int centered_square_num ( int n ) { return n * n + ( ( n - 1 ) * ( n - 1 ) ) ; } int main ( ) { int n = 7 ; cout << n << " th ▁ Centered ▁ square ▁ number : ▁ " ; cout << centered_square_num ( n ) ; return 0 ; }
Sum of first n natural 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 ; }
Dodecagonal number | CPP Program to find the nth Dodecagonal number ; function for Dodecagonal number ; formula for find Dodecagonal nth term ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Dodecagonal_number ( int n ) { return 5 * n * n - 4 * n ; } int main ( ) { int n = 7 ; cout << Dodecagonal_number ( n ) << endl ; n = 12 ; cout << Dodecagonal_number ( n ) << endl ; return 0 ; }
Arithmetic Number | CPP program to check if a number is Arithmetic number or not ; Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; cnt = cnt + 1 ; incrementing power ; Calculating number of divisors If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; return ans ; Total divisors ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( int n , bool prime [ ] , bool primesquare [ ] , int a [ ] ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int i = 0 ; i <= ( n * n + 1 ) ; i ++ ) primesquare [ i ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } int j = 0 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { a [ j ] = p ; primesquare [ p * p ] = true ; j ++ ; } } } int countDivisors ( int n ) { if ( n == 1 ) return 1 ; bool prime [ n + 1 ] , primesquare [ n * n + 1 ] ; SieveOfEratosthenes ( n , prime , primesquare , a ) ; int ans = 1 ; for ( int i = 0 ; ; i ++ ) { if ( a [ i ] * a [ i ] * a [ i ] > n ) break ; int cnt = 1 ; while ( n % a [ i ] == 0 ) { n = n / a [ i ] ; } ans = ans * cnt ; } if ( prime [ n ] ) ans = ans * 2 ; else if ( primesquare [ n ] ) ans = ans * 3 ; else if ( n != 1 ) ans = ans * 4 ; } int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } bool checkArithmetic ( int n ) { int count = countDivisors ( n ) ; int sum = sumofFactors ( n ) ; return ( sum % count == 0 ) ; } int main ( ) { int n = 6 ; ( checkArithmetic ( n ) ) ? ( cout << " Yes " ) : ( cout << " No " ) ; return 0 ; }
Finding n | A formula based C ++ program to find sum of series with cubes of first n natural numbers ; Driver Function
#include <iostream> NEW_LINE using namespace std ; int magicOfSequence ( int N ) { return ( N * ( N + 1 ) / 2 ) + 2 * N ; } int main ( ) { int N = 6 ; cout << magicOfSequence ( N ) ; return 0 ; }
Form a number using corner digits of powers | C ++ program to find number formed by corner digits of powers . ; Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Storing N raised to power 0 ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nextPower ( int N , vector < int > & power ) { int carry = 0 ; for ( int i = 0 ; i < power . size ( ) ; i ++ ) { int prod = ( power [ i ] * N ) + carry ; power [ i ] = prod % 10 ; carry = prod / 10 ; } while ( carry ) { power . push_back ( carry % 10 ) ; carry = carry / 10 ; } } void printPowerNumber ( int X , int N ) { vector < int > power ; power . push_back ( 1 ) ; vector < int > res ; for ( int i = 1 ; i <= X ; i ++ ) { nextPower ( N , power ) ; res . push_back ( power . back ( ) ) ; res . push_back ( power . front ( ) ) ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] ; } int main ( ) { int N = 19 , X = 4 ; printPowerNumber ( X , N ) ; return 0 ; }
First digit in factorial of a number | A C ++ program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; derive main
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstDigit ( int n ) { long long int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact = fact * i ; while ( fact % 10 == 0 ) fact = fact / 10 ; } while ( fact >= 10 ) fact = fact / 10 ; return fact ; } int main ( ) { int n = 5 ; cout << firstDigit ( n ) ; return 0 ; }
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | CPP program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofseries ( int n ) { int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) res += ( i ) * ( i + 1 ) * ( i + 2 ) ; return res ; } int main ( ) { cout << sumofseries ( 3 ) << endl ; return 0 ; }
Find N Geometric Means between A and B | C ++ program to find n geometric means between A and B ; Prints N geometric means between A and B . ; calculate common ratio ( R ) ; for finding N the Geometric mean between A and B ; Driver code to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printGMeans ( int A , int B , int N ) { float R = ( float ) pow ( float ( B / A ) , 1.0 / ( float ) ( N + 1 ) ) ; for ( int i = 1 ; i <= N ; i ++ ) cout << A * pow ( R , i ) << " ▁ " ; } int main ( ) { int A = 3 , B = 81 , N = 2 ; printGMeans ( A , B , N ) ; return 0 ; }