codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Digits of the Good Strings | C # implementation of the approach ; To store the states of the dp ; Function to fill the dp table ; Sum of digits of the string of length 1 is i as i is only number in that string and count of good strings of length 1 that end with i is also 1 ; Adjacent digits are different ; Increment the count as digit at ( i - 1 ) 'th index is k and count  of good strings is equal to this  because at the end of the strings of  length (i - 1) we are just  putting digit j as the last digit ; Driver code
using System ; class GFG { readonly static int DIGITS = 10 ; readonly static int MAX = 10000 ; readonly static int MOD = 1000000007 ; static int [ , ] dp = new int [ MAX , DIGITS ] ; static int [ , ] cnt = new int [ MAX , DIGITS ] ; static void precompute ( ) { for ( int i = 0 ; i < DIGITS ; i ++ ) { dp [ 1 , i ] = i ; cnt [ 1 , i ] = 1 ; } for ( int i = 2 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < DIGITS ; j ++ ) { for ( int k = 0 ; k < DIGITS ; k ++ ) { if ( j != k ) { dp [ i , j ] = dp [ i , j ] + ( dp [ i - 1 , k ] + ( cnt [ i - 1 , k ] * j ) % MOD ) % MOD ; dp [ i , j ] %= MOD ; cnt [ i , j ] += cnt [ i - 1 , k ] ; cnt [ i , j ] %= MOD ; } } } } } public static void Main ( String [ ] args ) { int x = 6 , y = 4 ; precompute ( ) ; Console . WriteLine ( dp [ x , y ] ) ; } }
Count non | C # implementation of the approach ; Function to pre - compute the sequence ; For N = 1 the answer will be 2 ; Starting two terms of the sequence ; Compute the rest of the sequence with the relation F [ i ] = F [ i - 1 ] + F [ i - 2 ] ; Driver code ; Pre - compute the sequence
using System ; class GFG { static int N = 10000 ; static int MOD = 1000000007 ; static int [ ] F = new int [ N ] ; static void precompute ( ) { F [ 1 ] = 2 ; F [ 2 ] = 3 ; F [ 3 ] = 4 ; for ( int i = 4 ; i < N ; i ++ ) F [ i ] = ( F [ i - 1 ] + F [ i - 2 ] ) % MOD ; } public static void Main ( String [ ] args ) { int n = 8 ; precompute ( ) ; Console . WriteLine ( F [ n ] ) ; } }
Minimize sum by dividing all elements of a subarray by K | C # implementation of the approach ; Function to return the maximum subarray sum ; Function to return the minimized sum of the array elements after performing the given operation ; Find maximum subarray sum ; Find total sum of the array ; Maximum subarray sum is already negative ; Choose the subarray whose sum is maximum and divide all elements by K ; Driver code
using System ; class GFG { static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static double minimizedSum ( int [ ] a , int n , int K ) { int sum = maxSubArraySum ( a , n ) ; double totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) totalSum += a [ i ] ; if ( sum < 0 ) return totalSum ; totalSum = totalSum - sum + ( double ) sum / ( double ) K ; return totalSum ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , - 2 , 3 } ; int n = a . Length ; int K = 2 ; Console . WriteLine ( minimizedSum ( a , n , K ) ) ; } }
Minimum numbers with one 's place as 9 to be added to get N | C # implementation of the approach ; Function to find minimum count of numbers ( with one 's digit 9)  that sum up to N ; Fetch one 's digit ; Apply Cases mentioned in approach ; If no possible answer exists ; Driver code
using System ; class GFG { static int findMin ( int N ) { int digit = N % 10 ; switch ( digit ) { case 0 : if ( N >= 90 ) return 10 ; break ; case 1 : if ( N >= 81 ) return 9 ; break ; case 2 : if ( N >= 72 ) return 8 ; break ; case 3 : if ( N >= 63 ) return 7 ; break ; case 4 : if ( N >= 54 ) return 6 ; break ; case 5 : if ( N >= 45 ) return 5 ; break ; case 6 : if ( N >= 36 ) return 4 ; break ; case 7 : if ( N >= 27 ) return 3 ; break ; case 8 : if ( N >= 18 ) return 2 ; break ; case 9 : if ( N >= 9 ) return 1 ; break ; } return - 1 ; } public static void Main ( String [ ] args ) { int N = 27 ; Console . WriteLine ( findMin ( N ) ) ; } }
Random list of M non | C # implementation of the approach ; Utility function to print the elements of an array ; Function to generate a list of m random non - negative integers whose sum is n ; Create an array of size m where every element is initialized to 0 ; To make the sum of the final list as n ; Increment any random element from the array by 1 ; Print the generated list ; Driver code
using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void randomList ( int m , int n ) { int [ ] arr = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { Random rnd = new Random ( ) ; arr [ rnd . Next ( 0 , n ) % m ] ++ ; } printArr ( arr , m ) ; } public static void Main ( ) { int m = 4 , n = 8 ; randomList ( m , n ) ; } }
Maximum String Partition | C # implementation of the above approach ; Return the count of string ; P will store the answer ; Current will store current string Previous will store the previous string that has been taken already ; Add a character to current string ; Here we will create a partition and update the previous string with current string ; Now we will clear the current string ; Increment the count of partition . ; Driver code
using System ; class GFG { static int maxPartition ( string s ) { int n = s . Length , P = 0 ; string current = " " , previous = " " ; for ( int i = 0 ; i < n ; i ++ ) { current += s [ i ] ; if ( ! current . Equals ( previous ) ) { previous = current ; current = " " ; P ++ ; } } return P ; } public static void Main ( ) { string s = " geeksforgeeks " ; int ans = maxPartition ( s ) ; Console . WriteLine ( ans ) ; } }
How to learn Pattern printing easily ? |
using System ; class GFG { public static void Main ( String [ ] args ) { int N = 4 , i , j , min ; Console . WriteLine ( " Value ▁ of ▁ N : ▁ " + N ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; Console . Write ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; Console . Write ( N - min + 1 ) ; } Console . WriteLine ( ) ; } } }
Find the smallest positive number missing from an unsorted array | Set 3 | C # program to find the smallest positive missing number ; Function to find the smallest positive missing number ; Default smallest Positive Integer ; Store values in set which are greater than variable m ; Store value when m is less than current index of given array ; Increment m when it is equal to current element ; Increment m when it is one of the element of the set ; Return the required answer ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int findMissingPositive ( int [ ] arr , int n ) { int m = 1 ; HashSet < int > x = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m < arr [ i ] ) { x . Add ( arr [ i ] ) ; } else if ( m == arr [ i ] ) { m = m + 1 ; while ( x . Contains ( m ) ) { x . Remove ( m ) ; m = m + 1 ; } } } return m ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 } ; int n = arr . Length ; Console . WriteLine ( findMissingPositive ( arr , n ) ) ; } }
Sum of all second largest divisors after splitting a number into one or more parts | C # program to Sum of all second largest divisors after splitting a number into one or more parts ; Function to find a number is prime or not ; If there is any divisor ; Function to find the sum of all second largest divisor after splitting a number into one or more parts ; If number is prime ; If n is even ; If the number is odd ; If N - 2 is prime ; There exists 3 primes x1 , x2 , x3 such that x1 + x2 + x3 = n ; Driver code ; Function call
using System ; class GFG { static bool prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; ++ i ) if ( n % i == 0 ) return false ; return true ; } static int Min_Sum ( int n ) { if ( prime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; else { if ( prime ( n - 2 ) ) return 2 ; else return 3 ; } } public static void Main ( ) { int n = 27 ; Console . WriteLine ( Min_Sum ( n ) ) ; } }
Find maximum value of the last element after reducing the array with given operations | C # implementation of the approach ; Function to return the maximized value ; Overall minimum absolute value of some element from the array ; Add all absolute values ; Count positive and negative elements ; Both positive and negative values are present ; Only positive or negative values are present ; Driver code
using System ; class GFG { static int find_maximum_value ( int [ ] a , int n ) { int sum = 0 ; int minimum = int . MaxValue ; int pos = 0 , neg = 0 ; for ( int i = 0 ; i < n ; i ++ ) { minimum = Math . Min ( minimum , Math . Abs ( a [ i ] ) ) ; sum += Math . Abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) pos += 1 ; else neg += 1 ; } if ( pos > 0 && neg > 0 ) return sum ; return ( sum - 2 * minimum ) ; } static public void Main ( ) { int [ ] a = { 5 , 4 , 6 , 2 } ; int n = a . Length ; Console . WriteLine ( find_maximum_value ( a , n ) ) ; } }
Find Nth smallest number that is divisible by 100 exactly K times | C # implementation of the approach ; Function to find the Nth smallest number ; If N is divisible by 100 then we multiply N + 1 otherwise , it will be divisible by 100 more than K times ; convert integer to string ; if N is not divisible by 100 ; convert integer to string ; add 2 * K 0 's at the end to be divisible  by 100 exactly K times ; Driver Code
using System ; class GFG { static String find_number ( int N , int K ) { String r ; if ( N % 100 == 0 ) { N += 1 ; r = N . ToString ( ) ; } else { r = N . ToString ( ) ; } for ( int i = 1 ; i <= K ; i ++ ) r += "00" ; return r ; } public static void Main ( String [ ] args ) { int N = 1000 , K = 2 ; String ans = find_number ( N , K ) ; Console . WriteLine ( ans ) ; } }
Count of adjacent Vowel Consonant Pairs | C # implementation of the approach ; Function to count the adjacent pairs of consonant and vowels in the String ; Using a set to store the vowels so that checking each character becomes easier ; Variable to store number of consonant - vowel pairs ; If the ith character is not found in the set , means it is a consonant And if the ( i + 1 ) th character is found in the set , means it is a vowel We increment the count of such pairs ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( String s ) { HashSet < char > st = new HashSet < char > ( ) ; st . Add ( ' a ' ) ; st . Add ( ' e ' ) ; st . Add ( ' i ' ) ; st . Add ( ' o ' ) ; st . Add ( ' u ' ) ; int count = 0 ; int n = s . Length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( st . Contains ( s [ i ] ) && ! st . Contains ( s [ i + 1 ] ) ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; Console . Write ( countPairs ( s ) ) ; } }
Find the original coordinates whose Manhattan distances are given | C # implementation of the approach ; Function to find the original coordinated ; Maximum of the given distances ; Sum of the given distances ; Conditions when the solution doesn 't exist ; First coordinate ; Second coordinate ; Third coordinate ; Driver code
using System ; class GFG { static void solve ( int d1 , int d2 , int d3 ) { int maxx = Math . Max ( d1 , Math . Max ( d2 , d3 ) ) ; int sum = ( d1 + d2 + d3 ) ; if ( 2 * maxx > sum sum % 2 == 1 ) { Console . WriteLine ( " - 1" ) ; return ; } int x1 = 0 , y1 = 0 ; int x2 = d1 , y2 = 0 ; int x3 = ( d1 + d2 - d3 ) / 2 ; int y3 = ( d2 + d3 - d1 ) / 2 ; Console . WriteLine ( " ( " + x1 + " , ▁ " + y1 + " ) , ▁ ( " + x2 + " , ▁ " + y2 + " ) ▁ and ▁ ( " + x3 + " , ▁ " + y3 + " ) " ) ; } static void Main ( ) { int d1 = 3 , d2 = 4 , d3 = 5 ; solve ( d1 , d2 , d3 ) ; } }
Find the largest interval that contains exactly one of the given N integers . | C # implementation of the approach ; Function to return the maximum size of the required interval ; Insert the borders for array ; Sort the elements in ascending order ; To store the maximum size ; To store the range [ L , R ] such that only v [ i ] lies within the range ; Total integers in the range ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maxSize ( List < int > v , int n ) { v . Add ( 0 ) ; v . Add ( 100001 ) ; n += 2 ; v . Sort ( ) ; int mx = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int L = v [ i - 1 ] + 1 ; int R = v [ i + 1 ] - 1 ; int cnt = R - L + 1 ; mx = Math . Max ( mx , cnt ) ; } return mx ; } public static void Main ( String [ ] args ) { int [ ] arr = { 200 , 10 , 5 } ; List < int > v = new List < int > ( arr ) ; int n = v . Count ; Console . WriteLine ( maxSize ( v , n ) ) ; } }
Maximum length of subarray such that sum of the subarray is even | C # implementation of the approach ; Function to find length of the longest subarray such that sum of the subarray is even ; Check if sum of complete array is even ; if ( sum % 2 == 0 ) total sum is already even ; Find an index i such the a [ i ] is odd and compare length of both halfs excluding a [ i ] to find max length subarray ; Driver Code
using System ; class GFG { static int maxLength ( int [ ] a , int n ) { int sum = 0 , len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } { return n ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { len = Math . Max ( len , Math . Max ( n - i - 1 , i ) ) ; } } return len ; } static public void Main ( ) { int [ ] a = { 1 , 2 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( maxLength ( a , n ) ) ; } }
Find a Symmetric matrix of order N that contain integers from 0 to N | C # implementation of the approach ; Function to generate the required matrix ; Form cyclic array of elements 1 to n - 1 ; Store initial array into final array ; Fill the last row and column with 0 's ; Swap 0 and the number present at the current indexed row ; Also make changes in the last row with the number we swapped ; Print the final array ; Driver code
using System ; class GFG { static void solve ( long n ) { long [ , ] initial_array = new long [ ( int ) n - 1 , ( int ) n - 1 ] ; long [ , ] final_array = new long [ ( int ) n , ( int ) n ] ; for ( long i = 0 ; i < n - 1 ; ++ i ) initial_array [ 0 , ( int ) i ] = i + 1 ; for ( long i = 1 ; i < n - 1 ; ++ i ) for ( long j = 0 ; j < n - 1 ; ++ j ) initial_array [ ( int ) i , ( int ) j ] = initial_array [ ( int ) i - 1 , ( int ) ( ( int ) j + 1 ) % ( ( int ) n - 1 ) ] ; for ( long i = 0 ; i < n - 1 ; ++ i ) for ( long j = 0 ; j < n - 1 ; ++ j ) final_array [ ( int ) i , ( int ) j ] = initial_array [ ( int ) i , ( int ) j ] ; for ( long i = 0 ; i < n ; ++ i ) final_array [ ( int ) i , ( int ) n - 1 ] = final_array [ ( int ) n - 1 , ( int ) i ] = 0 ; for ( long i = 0 ; i < n ; ++ i ) { long t0 = final_array [ ( int ) i , ( int ) i ] ; long t1 = final_array [ ( int ) i , ( int ) n - 1 ] ; long s = final_array [ ( int ) i , ( int ) i ] ; final_array [ ( int ) i , ( int ) i ] = final_array [ ( int ) i , ( int ) n - 1 ] ; final_array [ ( int ) i , ( int ) n - 1 ] = s ; final_array [ ( int ) n - 1 , ( int ) i ] = t0 ; } for ( long i = 0 ; i < n ; ++ i ) { for ( long j = 0 ; j < n ; ++ j ) Console . Write ( final_array [ ( int ) i , ( int ) j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { long n = 5 ; solve ( n ) ; } }
Generate original array from difference between every two consecutive elements | C # program to implement the above approach ; Function to print the required permutation ; Take x = 0 for simplicity ; Calculate aint the differences and store it in a vector ; Preserve the original array ; Check if aint the consecutive elements have difference = 1 ; If consecutive elements don 't have  difference 1 at any position then  the answer is impossible ; Else store the indices and values at those indices in a map and finainty print them ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findPerm ( int n , List < int > differences ) { List < int > ans = new List < int > ( ) ; ans . Clear ( ) ; ans . Add ( 0 ) ; int x = 0 ; for ( int i = 0 ; i <= n - 2 ; ++ i ) { int diff = differences [ i ] ; x = x + diff ; ans . Add ( x ) ; } List < int > anss = new List < int > ( ) ; foreach ( int obj in ans ) anss . Add ( obj ) ; ans . Sort ( ) ; int flag = - 1 ; for ( int i = 1 ; i <= n - 1 ; ++ i ) { int res = ans [ i ] - ans [ i - 1 ] ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { Console . Write ( - 1 ) ; return ; } else { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; mpp . Clear ( ) ; int j = 1 ; List < int > value_at_index = new List < int > ( ) ; foreach ( int x1 in ans ) { mpp . Add ( x1 , j ) ; ++ j ; } foreach ( int x2 in anss ) { value_at_index . Add ( mpp [ x2 ] ) ; } foreach ( int x3 in value_at_index ) { Console . Write ( x3 + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { List < int > differences = new List < int > ( ) ; differences . Add ( 2 ) ; differences . Add ( - 3 ) ; differences . Add ( 2 ) ; int n = differences . Count + 1 ; findPerm ( n , differences ) ; } }
Find smallest number K such that K % p = 0 and q % K = 0 | C # implementation of the approach ; Function to return the minimum value K such that K % p = 0 and q % k = 0 ; If K is possible ; No such K is possible ; Driver code
using System ; class GFG { static int getMinVal ( int p , int q ) { if ( q % p == 0 ) return p ; return - 1 ; } public static void Main ( ) { int p = 24 , q = 48 ; Console . WriteLine ( getMinVal ( p , q ) ) ; } }
Count all Prime Length Palindromic Substrings | C # implementation of the approach ; Function that returns true if sub - string starting at i and ending at j in str is a palindrome ; Function to count all palindromic substring whose lwngth is a prime number ; 0 and 1 are non - primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; To store the required number of sub - strings ; Starting from the smallest prime till the largest length of the sub - string possible ; If j is prime ; Check all the sub - strings of length j ; If current sub - string is a palindrome ; Driver code
using System ; class GfG { static bool isPalindrome ( string str , int i , int j ) { while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int countPrimePalindrome ( string str , int len ) { bool [ ] prime = new bool [ len + 1 ] ; Array . Fill ( prime , true ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= len ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i <= len ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int j = 2 ; j <= len ; j ++ ) { if ( prime [ j ] ) { for ( int i = 0 ; i + j - 1 < len ; i ++ ) { if ( isPalindrome ( str , i , i + j - 1 ) ) count ++ ; } } } return count ; } public static void Main ( ) { string s = " geeksforgeeks " ; int len = s . Length ; Console . WriteLine ( countPrimePalindrome ( s , len ) ) ; } }
Minimum operations of the given type required to make a complete graph | C # implementation of the approach ; Function to return the minimum number of steps required ; Driver Code
using System ; class GFG { static int minOperations ( int N ) { double x = Math . Log ( N , 2 ) ; int ans = ( int ) ( Math . Ceiling ( x ) ) ; return ans ; } static void Main ( ) { int N = 10 ; Console . WriteLine ( minOperations ( N ) ) ; } }
Greatest divisor which divides all natural number in range [ L , R ] | C # implementation of the approach ; Function to return the greatest divisor that divides all the natural numbers in the range [ l , r ] ; Driver Code
using System ; class GFG { static int find_greatest_divisor ( int l , int r ) { if ( l == r ) { return l ; } return 1 ; } public static void Main ( ) { int l = 2 , r = 12 ; Console . WriteLine ( find_greatest_divisor ( l , r ) ) ; } }
Probability of getting two consecutive heads after choosing a random coin among two different types of coins | C # program to get the probability of getting two consecutive heads ; Function to return the probability of getting two consecutive heads ; Formula derived from Bayes 's theorem ; Driver code ; given the probability of getting a head for both the coins
using System ; class GFG { static double getProbability ( double p , double q ) { p /= 100 ; q /= 100 ; double probability = ( p * p + q * q ) / ( p + q ) ; return probability ; } public static void Main ( ) { double p , q ; p = 80 ; q = 40 ; Console . WriteLine ( getProbability ( p , q ) ) ; } }
Check whether bitwise OR of N numbers is Even or Odd | C # implementation to check whether bitwise OR of n numbers is even or odd ; Function to check if bitwise OR of n numbers is even or odd ; if at least one odd number is found , then the bitwise OR of all numbers will be odd ; Bitwise OR is an odd number ; Driver Code
using System ; class GFG { static bool check ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { return true ; } } return false ; } public static void Main ( ) { int [ ] arr = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . Length ; if ( check ( arr , n ) ) { Console . WriteLine ( " Odd ▁ Bit - wise ▁ OR " ) ; } else { Console . WriteLine ( " Even ▁ Bit - wise ▁ OR " ) ; } } }
Time taken by Loop unrolling vs Normal loop | C # program to compare normal loops and loops with unrolling technique ; n is 8 lakhs ; t to note start time ; to store the sum ; Normal loop ; to mark end time ; to mark start time of unrolling ; Unrolling technique ( assuming that n is a multiple of 8 ) . ; to mark the end of loop
using System ; using System . Diagnostics ; class GFG { private static double nanoTime ( ) { long nano = 10000L * Stopwatch . GetTimestamp ( ) ; nano /= TimeSpan . TicksPerMillisecond ; nano *= 100L ; return nano ; } public static void Main ( String [ ] args ) { int n = 800000 ; double t = nanoTime ( ) ; long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += i ; t = nanoTime ( ) - t ; Console . WriteLine ( " sum ▁ is : ▁ " + sum ) ; Console . WriteLine ( " time ▁ taken ▁ by ▁ normal ▁ loops : " + ( t / Math . Pow ( 10.0 , 9.0 ) ) ) ; t = nanoTime ( ) ; sum = 0 ; for ( int i = 1 ; i <= n ; i += 8 ) { sum += i ; sum += ( i + 1 ) ; sum += ( i + 2 ) ; sum += ( i + 3 ) ; sum += ( i + 4 ) ; sum += ( i + 5 ) ; sum += ( i + 6 ) ; sum += ( i + 7 ) ; } t = nanoTime ( ) - t ; Console . WriteLine ( " sum ▁ is : ▁ " + ( sum ) ) ; Console . WriteLine ( " time ▁ taken ▁ by ▁ normal ▁ loops : " + ( t / Math . Pow ( 10.0 , 9.0 ) ) ) ; } }
Iterated Logarithm log * ( n ) | Recursive C # program to find value of Iterated Logarithm ; Driver code
using System ; public class GFG { static int _log ( double x , double baset ) { return ( int ) ( Math . Log ( x ) / Math . Log ( baset ) ) ; } static double recursiveLogStar ( double n , double b ) { if ( n > 1.0 ) return 1.0 + recursiveLogStar ( _log ( n , b ) , b ) ; else return 0 ; } static public void Main ( ) { int n = 100 , baset = 5 ; Console . WriteLine ( " Log * ( " + n + " ) ▁ = ▁ " + recursiveLogStar ( n , baset ) ) ; } }
Iterated Logarithm log * ( n ) | Iterative C # function to find value of Iterated Logarithm
static int iterativeLogStar ( double n , double b ) { int count = 0 ; while ( n >= 1 ) { n = _log ( n , b ) ; count ++ ; } return count ; }
Analysis of Algorithms | Set 5 ( Practice Problems ) |
static void function ( int n ) { int count = 0 ; for ( int i = n / 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j = 2 * j ) for ( int k = 1 ; k <= n ; k = k * 2 ) count ++ ; }
Auxiliary Space with Recursive Functions |
static int sum ( int sum ) { if ( n <= 0 ) return 0 ; return n + sum ( n - 1 ) ; }
Tail Recursion | An example of tail recursive function ; The last executed statement is recursive call
static void print ( int n ) { if ( n < 0 ) return ; Console . Write ( " ▁ " + n ) ; print ( n - 1 ) ; }