codeparrot/xlcost-text-to-code huggingface dataset:
Program for quotient and remainder of big number | CPP program to find quotient and remainder when a number is divided by large number represented as string . ; Function to calculate the modulus ; Store the modulus of big number ; Do step by step division ; Update modulo by concatenating current digit . ; Update quotient ; Update mod for next iteration . ; Flag used to remove starting zeros ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; void modBigNumber ( string num , ll m ) { vector < int > vec ; ll mod = 0 ; for ( int i = 0 ; i < num . size ( ) ; i ++ ) { int digit = num [ i ] - '0' ; mod = mod * 10 + digit ; int quo = mod / m ; vec . push_back ( quo ) ; mod = mod % m ; } cout << " Remainder : " ▁ < < ▁ mod ▁ < < ▁ " " cout << " Quotient ▁ : ▁ " ; bool zeroflag = 0 ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { if ( vec [ i ] == 0 && zeroflag == 0 ) continue ; zeroflag = 1 ; cout << vec [ i ] ; } return ; } int main ( ) { string num = "14598499948265358486" ; ll m = 487 ; modBigNumber ( num , m ) ; return 0 ; }
Queries to find whether a number has exactly four distinct factors or not | C ++ program to check whether number has exactly four distinct factors or not ; Initialize global variable according to given condition so that it can be accessible to all function ; Function to calculate all number having four distinct distinct factors ; 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 ; Initialize prime [ ] array which will contains all the primes from 1 - N ; Iterate over all the prime numbers ; Mark cube root of prime numbers ; Mark product of prime numbers ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1e6 ; bool fourDiv [ N + 1 ] ; memset ( fourDiv , false , sizeof ( fourDiv ) ) ; void fourDistinctFactors ( ) { bool primeAll [ N + 1 ] ; memset ( primeAll , true , sizeof ( primeAll ) ) ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( primeAll [ p ] == true ) { for ( int i = p * 2 ; i <= N ; i += p ) primeAll [ i ] = false ; } } vector < int > prime ; for ( int p = 2 ; p <= N ; p ++ ) if ( primeAll [ p ] ) prime . push_back ( p ) ; for ( int i = 0 ; i < prime . size ( ) ; ++ i ) { int p = prime [ i ] ; if ( 1LL * p * p * p <= N ) fourDiv [ p * p * p ] = true ; for ( int j = i + 1 ; j < prime . size ( ) ; ++ j ) { int q = prime [ j ] ; if ( 1LL * p * q > N ) break ; fourDiv [ p * q ] = true ; } } } int main ( ) { fourDistinctFactors ( ) ; int num = 10 ; if ( fourDiv [ num ] ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; num = 12 ; if ( fourDiv [ num ] ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Leonardo Number | A simple recursive program to find n - th leonardo number . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int leonardo ( int n ) { int dp [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + 1 ; return dp [ n ] ; } int main ( ) { cout << leonardo ( 3 ) ; return 0 ; }
Cholesky Decomposition : Matrix Decomposition | CPP program to decompose a matrix using Cholesky Decomposition ; Decomposing a matrix into Lower Triangular ; if ( j == i ) summation for diagonals ; Evaluating L ( i , j ) using L ( j , j ) ; Displaying Lower Triangular and its Transpose ; Lower Triangular ; Transpose of Lower Triangular ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void Cholesky_Decomposition ( int matrix [ ] [ MAX ] , int n ) { int lower [ n ] [ n ] ; memset ( lower , 0 , sizeof ( lower ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { int sum = 0 ; { for ( int k = 0 ; k < j ; k ++ ) sum += pow ( lower [ j ] [ k ] , 2 ) ; lower [ j ] [ j ] = sqrt ( matrix [ j ] [ j ] - sum ) ; } else { for ( int k = 0 ; k < j ; k ++ ) sum += ( lower [ i ] [ k ] * lower [ j ] [ k ] ) ; lower [ i ] [ j ] = ( matrix [ i ] [ j ] - sum ) / lower [ j ] [ j ] ; } } } cout << setw ( 6 ) << " ▁ Lower ▁ Triangular " << setw ( 30 ) << " Transpose " << endl ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << setw ( 6 ) << lower [ i ] [ j ] << " TABSYMBOL " ; cout << " TABSYMBOL " ; for ( int j = 0 ; j < n ; j ++ ) cout << setw ( 6 ) << lower [ j ] [ i ] << " TABSYMBOL " ; cout << endl ; } } int main ( ) { int n = 3 ; int matrix [ ] [ MAX ] = { { 4 , 12 , -16 } , { 12 , 37 , -43 } , { -16 , -43 , 98 } } ; Cholesky_Decomposition ( matrix , n ) ; return 0 ; }
Program for sum of arithmetic series | Efficient solution to find sum of arithmetic series . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float sumOfAP ( float a , float d , float n ) { float sum = ( n / 2 ) * ( 2 * a + ( n - 1 ) * d ) ; return sum ; } int main ( ) { float n = 20 ; float a = 2.5 , d = 1.5 ; cout << sumOfAP ( a , d , n ) ; return 0 ; }
Program for cube sum of first n natural numbers | Efficient CPP program to find sum of cubes of first n natural numbers that avoids overflow if result is going to be withing limits . ; Returns sum of first n natural numbers ; Driver code
#include <iostream> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { int x ; if ( n % 2 == 0 ) x = ( n / 2 ) * ( n + 1 ) ; else x = ( ( n + 1 ) / 2 ) * n ; return x * x ; } int main ( ) { int n = 5 ; cout << sumOfSeries ( n ) ; return 0 ; }
Maximum value of | arr [ i ] |  ; Return maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Iterating two for loop , one for i and another for j . ; Evaluating | arr [ i ] - arr [ j ] | + | i - j | and compare with previous maximum . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10 NEW_LINE int findValue ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) ans = max ( ans , abs ( arr [ i ] - arr [ j ] ) + abs ( i - j ) ) ; return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findValue ( arr , n ) << endl ; return 0 ; }
Maximum value of | arr [ i ] | Efficient CPP program to find maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Return maximum | arr [ i ] - arr [ j ] | + | i - j | ; Calculating first_array and second_array ; Finding maximum and minimum value in first_array ; Storing the difference between maximum and minimum value in first_array ; Finding maximum and minimum value in second_array ; Storing the difference between maximum and minimum value in second_array ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findValue ( int arr [ ] , int n ) { int a [ n ] , b [ n ] , tmp ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( arr [ i ] + i ) ; b [ i ] = ( arr [ i ] - i ) ; } int x = a [ 0 ] , y = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) x = a [ i ] ; if ( a [ i ] < y ) y = a [ i ] ; } int ans1 = ( x - y ) ; x = b [ 0 ] ; y = b [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] > x ) x = b [ i ] ; if ( b [ i ] < y ) y = b [ i ] ; } int ans2 = ( x - y ) ; return max ( ans1 , ans2 ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findValue ( arr , n ) << endl ; return 0 ; }
Number of subarrays having product less than K | CPP program to count subarrays having product less than k . ; Counter for single element ; Multiple subarray ; If this multiple is less than k , then increment ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int countsubarray ( int array [ ] , int n , int k ) { int count = 0 ; int i , j , mul ; for ( i = 0 ; i < n ; i ++ ) { if ( array [ i ] < k ) count ++ ; mul = array [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { mul = mul * array [ j ] ; if ( mul < k ) count ++ ; else break ; } } return count ; } int main ( ) { int array [ ] = { 1 , 2 , 3 , 4 } ; int k = 10 ; int size = sizeof ( array ) / sizeof ( array [ 0 ] ) ; int count = countsubarray ( array , size , k ) ; cout << count << " STRNEWLINE " ; }
Perfect Cube factors of a Number | C ++ program for the above approach ; Function that returns the count of factors that are perfect cube ; To store the count of number of times a prime number divides N . ; To store the number of factors that are perfect cube ; Count number of 2 's that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Loop to check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Driver Code ; Given number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int noOfFactors ( int N ) { if ( N == 1 ) return 1 ; int count = 0 ; int ans = 1 ; while ( N % 2 == 0 ) { count ++ ; N = N / 2 ; } ans *= ( count / 3 + 1 ) ; for ( int i = 3 ; i * i <= N ; i = i + 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } ans *= ( count / 3 + 1 ) ; } return ans ; } int main ( ) { int N = 216 ; cout << noOfFactors ( N ) ; return 0 ; }
Efficient program to print the number of factors of n numbers | C ++ program to count number of factors of an array of integers ; 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 ; 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 main ( ) { generatePrimeFactors ( ) ; int a [ ] = { 10 , 30 , 100 , 450 , 987 } ; int q = sizeof ( a ) / sizeof ( a [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << calculateNoOFactors ( a [ i ] ) << " ▁ " ; return 0 ; }
Digit | CPP program for Digit Product Sequence ; function to produce and print Digit Product Sequence ; Array which store sequence ; Temporary variable to store product ; Initialize first element of the array with 1 ; Run a loop from 1 to N . Check if previous number is single digit or not . If yes then product = 1 else take modulus . Then again check if previous number is single digit or not if yes then store previous number , else store its first value Then for every i store value in the array . ; Print sequence ; Driver Code ; Value of N ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void digit_product_Sum ( int N ) { int a [ N ] ; int product = 1 ; a [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { product = a [ i - 1 ] / 10 ; if ( product == 0 ) product = 1 ; else product = a [ i - 1 ] % 10 ; int val = a [ i - 1 ] / 10 ; if ( val == 0 ) val = a [ i - 1 ] ; a [ i ] = a [ i - 1 ] + ( val * product ) ; } for ( int i = 0 ; i < N ; i ++ ) cout << a [ i ] << " ▁ " ; } int main ( ) { int N = 10 ; digit_product_Sum ( N ) ; return 0 ; }
Geometric mean ( Two Methods ) | Program to calculate the geometric mean of the given array elements . ; function to calculate geometric mean and return float value . ; declare product variable and initialize it to 1. ; Compute the product of all the elements in the array . ; compute geometric mean through formula pow ( product , 1 / n ) and return the value to main function . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; float geometricMean ( int arr [ ] , int n ) { float product = 1 ; for ( int i = 0 ; i < n ; i ++ ) product = product * arr [ i ] ; float gm = pow ( product , ( float ) 1 / n ) ; return gm ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << geometricMean ( arr , n ) ; return 0 ; }
Check whether a number can be expressed as a product of single digit numbers | C ++ implementation to check whether a number can be expressed as a product of single digit numbers ; Number of single digit prime numbers ; function to check whether a number can be expressed as a product of single digit numbers ; if ' n ' is a single digit number , then it can be expressed ; define single digit prime numbers array ; repeatedly divide ' n ' by the given prime numbers until all the numbers are used or ' n ' > 1 ; if true , then ' n ' can be expressed ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  4 NEW_LINE bool productOfSingelDgt ( int n ) { if ( n >= 0 && n <= 9 ) return true ; int prime [ ] = { 2 , 3 , 5 , 7 } ; for ( int i = 0 ; i < SIZE && n > 1 ; i ++ ) while ( n % prime [ i ] == 0 ) n = n / prime [ i ] ; return ( n == 1 ) ; } int main ( ) { int n = 24 ; productOfSingelDgt ( n ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Program to find sum of first n natural numbers | Efficient CPP program to find sum of first n natural numbers . ; Returns sum of first n natural numbers ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findSum ( int n ) { return n * ( n + 1 ) / 2 ; } int main ( ) { int n = 5 ; cout << findSum ( n ) ; return 0 ; }
Program to find sum of first n natural numbers | Efficient CPP program to find sum of first n natural numbers that avoids overflow if result is going to be within limits . ; Returns sum of first n natural numbers ; If n is odd , ( n + 1 ) must be even ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findSum ( int n ) { if ( n % 2 == 0 ) return ( n / 2 ) * ( n + 1 ) ; else return ( ( n + 1 ) / 2 ) * n ; } int main ( ) { int n = 5 ; cout << findSum ( n ) ; return 0 ; }
Maximum number of unique prime factors | C ++ program to find maximum number of prime factors in first N natural numbers ; Return maximum number of prime factors for any number in [ 1 , N ] ; Based on Sieve of Eratosthenes ; If p is prime ; We simply multiply first set of prime numbers while the product is smaller than N . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPrimefactorNum ( int N ) { if ( N < 2 ) return 0 ; bool arr [ N + 1 ] ; memset ( arr , true , sizeof ( arr ) ) ; int prod = 1 , res = 0 ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( arr [ p ] == true ) { for ( int i = p * 2 ; i <= N ; i += p ) arr [ i ] = false ; prod *= p ; if ( prod > N ) return res ; res ++ ; } } return res ; } int main ( ) { int N = 500 ; cout << maxPrimefactorNum ( N ) << endl ; return 0 ; }
Program to print multiplication table of a number | CPP program to print table over a range . ; Driver code ; Change here to change input number ; Change here to change result .
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int n = 8 ; int range = 12 ; for ( int i = 1 ; i <= range ; ++ i ) cout << n << " ▁ * ▁ " << i << " ▁ = ▁ " << n * i << endl ; return 0 ; }
To check a number is palindrome or not without using any extra space | C ++ program to find number is palindrome or not without using any extra space ; Function to check if given number is palindrome or not without using the extra space ; Find the appropriate divisor to extract the leading digit ; If first and last digit not same return false ; Removing the leading and trailing digit from number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( int ) ; bool isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } int main ( ) { isPalindrome ( 1001 ) ? cout << " Yes , ▁ it ▁ is ▁ Palindrome " : cout << " No , ▁ not ▁ Palindrome " ; return 0 ; }
Find whether a given integer is a power of 3 or not | C ++ program to check if a number is power of 3 or not . ; Returns true if n is power of 3 , else false ; The maximum power of 3 value that integer can hold is 1162261467 ( 3 ^ 19 ) . ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool check ( int n ) { if ( n <= 0 ) return false ; return 1162261467 % n == 0 ; } int main ( ) { int n = 9 ; if ( check ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Demlo number ( Square of 11. . .1 ) | CPP program to print DemloNumber ; To return demlo number . This function assumes that the length of str is smaller than 10. ; Add numbers to res upto size of str and then add number reverse to it ; Driver program to test printDemlo ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; string printDemlo ( string str ) { int len = str . length ( ) ; string res = " " ; for ( int i = 1 ; i <= len ; i ++ ) res += char ( i + '0' ) ; for ( int i = len - 1 ; i >= 1 ; i -- ) res += char ( i + '0' ) ; return res ; } int main ( ) { string str = "111111" ; cout << printDemlo ( str ) ; return 0 ; }
Discrete Cosine Transform ( Algorithm and Program ) | CPP program to perform discrete cosine transform ; Function to find discrete cosine transform and print it ; dct will store the discrete cosine transform ; ci and cj depends on frequency as well as number of row and columns of specified matrix ; sum will temporarily store the sum of cosine signals ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define pi  3.142857 NEW_LINE const int m = 8 , n = 8 ; int dctTransform ( int matrix [ ] [ n ] ) { int i , j , k , l ; float dct [ m ] [ n ] ; float ci , cj , dct1 , sum ; for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( i == 0 ) ci = 1 / sqrt ( m ) ; else ci = sqrt ( 2 ) / sqrt ( m ) ; if ( j == 0 ) cj = 1 / sqrt ( n ) ; else cj = sqrt ( 2 ) / sqrt ( n ) ; sum = 0 ; for ( k = 0 ; k < m ; k ++ ) { for ( l = 0 ; l < n ; l ++ ) { dct1 = matrix [ k ] [ l ] * cos ( ( 2 * k + 1 ) * i * pi / ( 2 * m ) ) * cos ( ( 2 * l + 1 ) * j * pi / ( 2 * n ) ) ; sum = sum + dct1 ; } } dct [ i ] [ j ] = ci * cj * sum ; } } for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { printf ( " % f TABSYMBOL " , dct [ i ] [ j ] ) ; } printf ( " STRNEWLINE " ) ; } } int main ( ) { int matrix [ m ] [ n ] = { { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } } ; dctTransform ( matrix ) ; return 0 ; }
Number of times a number can be replaced by the sum of its digits until it only contains one digit | C ++ program to count number of times we need to add digits to get a single digit . ; Here the count variable store how many times we do sum of digits and temporary_sum always store the temporary sum we get at each iteration . ; In this loop we always compute the sum of digits in temporary_ sum variable and convert it into string str till its length become 1 and increase the count in each iteration . ; computing sum of its digits ; converting temporary_sum into string str again . ; increase the count ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int NumberofTimes ( string str ) { int temporary_sum = 0 , count = 0 ; while ( str . length ( ) > 1 ) { temporary_sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) temporary_sum += ( str [ i ] - '0' ) ; str = to_string ( temporary_sum ) ; count ++ ; } return count ; } int main ( ) { string s = "991" ; cout << NumberofTimes ( s ) ; return 0 ; }
Climb n | C ++ program to count total number of ways to reach n - th stair with all jumps allowed ; Function to calculate leaps ; Driver code
#include <iostream> NEW_LINE int calculateLeaps ( int n ) { if ( n == 0 n == 1 ) { return 1 ; } else { int leaps = 0 ; for ( int i = 0 ; i < n ; i ++ ) leaps += calculateLeaps ( i ) ; return leaps ; } } int main ( ) { int calculateLeaps ( int ) ; std :: cout << calculateLeaps ( 4 ) << std :: endl ; return 0 ; }
Print last k digits of a ^ b ( a raised to power b ) | C ++ code to find last k digits of a ^ b ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; C ++ function to calculate number of digits in x ; C ++ function to print last k digits of a ^ b ; Generating 10 ^ k ; Calling modular exponentiation ; Printing leftmost zeros . Since ( a ^ b ) % k can have digits less then k . In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; int power ( long long int x , long long int y , long long int p ) { long long int res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int numberOfDigits ( int x ) { int i = 0 ; while ( x ) { x /= 10 ; i ++ ; } return i ; } void printLastKDigits ( int a , int b , int k ) { cout << " Last ▁ " << k ; cout << " ▁ digits ▁ of ▁ " << a ; cout << " ^ " << b << " ▁ = ▁ " ; int temp = 1 ; for ( int i = 1 ; i <= k ; i ++ ) temp *= 10 ; temp = power ( a , b , temp ) ; for ( int i = 0 ; i < k - numberOfDigits ( temp ) ; i ++ ) cout << 0 ; if ( temp ) cout << temp ; } int main ( ) { int a = 11 ; int b = 3 ; int k = 2 ; printLastKDigits ( a , b , k ) ; return 0 ; }
Adam Number | CPP program to check Adam Number ; To reverse Digits of numbers ; To square number ; To check Adam Number ; Square first number and square reverse digits of second number ; If reverse of b equals a then given number is Adam number ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reverseDigits ( int num ) { int rev = 0 ; while ( num > 0 ) { rev = rev * 10 + num % 10 ; num /= 10 ; } return rev ; } int square ( int num ) { return ( num * num ) ; } bool checkAdamNumber ( int num ) { int a = square ( num ) ; int b = square ( reverseDigits ( num ) ) ; if ( a == reverseDigits ( b ) ) return true ; return false ; } int main ( ) { int num = 12 ; if ( checkAdamNumber ( num ) ) cout << " Adam ▁ Number " ; else cout << " Not ▁ a ▁ Adam ▁ Number " ; return 0 ; }