codeparrot/xlcost-text-to-code huggingface dataset:
Remove Nth node from end of the Linked List | C # program to delete nth node from last ; Structure of node ; Function to insert node in a linked list ; Function to remove nth node from last ; To store length of the linked list ; B > length , then we can 't remove node ; We need to remove head node ; Return head . next ; Remove len - B th node from starting ; This function prints contents of linked list starting from the given node ; Driver code
using System ; class GFG { class Node { public int data ; public Node next ; } ; static Node create ( Node head , int x ) { Node temp , ptr = head ; temp = new Node ( ) ; temp . data = x ; temp . next = null ; if ( head == null ) head = temp ; else { while ( ptr . next != null ) { ptr = ptr . next ; } ptr . next = temp ; } return head ; } static Node removeNthFromEnd ( Node head , int B ) { int len = 0 ; Node tmp = head ; while ( tmp != null ) { len ++ ; tmp = tmp . next ; } if ( B > len ) { Console . Write ( " Length ▁ of ▁ the ▁ linked ▁ list ▁ is ▁ " + len ) ; Console . Write ( " ▁ we ▁ can ' t ▁ remove ▁ " + B + " th ▁ node ▁ from ▁ the " ) ; Console . Write ( " ▁ linked ▁ list STRNEWLINE " ) ; return head ; } else if ( B == len ) { return head . next ; } else { int diff = len - B ; Node prev = null ; Node curr = head ; for ( int i = 0 ; i < diff ; i ++ ) { prev = curr ; curr = curr . next ; } prev . next = curr . next ; return head ; } } static void display ( Node head ) { Node temp = head ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } Console . Write ( " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { Node head = null ; head = create ( head , 1 ) ; head = create ( head , 2 ) ; head = create ( head , 3 ) ; head = create ( head , 4 ) ; head = create ( head , 5 ) ; Console . Write ( " Linked ▁ list ▁ before ▁ modification : ▁ STRNEWLINE " ) ; display ( head ) ; head = removeNthFromEnd ( head , 2 ) ; Console . Write ( " Linked ▁ list ▁ after ▁ modification : ▁ STRNEWLINE " ) ; display ( head ) ; } }
Find the final sequence of the array after performing given operations | C # implementation of the approach ; Function that generates the array b [ ] when n is even ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function that generates the array b [ ] when n is odd ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function to find the final array b [ ] after n operations of given type ; Create the array b ; If the array size is even ; Print the final array elements ; Driver code
using System ; class GFG { static void solveEven ( int n , int [ ] arr , int [ ] b ) { int left = n - 1 ; for ( int i = 0 ; i < ( n / 2 ) ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } int right = 0 ; for ( int i = n / 2 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } static void solveOdd ( int n , int [ ] arr , int [ ] b ) { int left = n - 1 ; for ( int i = 0 ; i < ( n / 2 ) + 1 ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } int right = 1 ; for ( int i = ( n / 2 ) + 1 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } static void solve ( int n , int [ ] arr ) { int [ ] b = new int [ n ] ; if ( n % 2 == 0 ) solveEven ( n , arr , b ) ; else solveOdd ( n , arr , b ) ; for ( int i = 0 ; i <= n - 1 ; ++ i ) { Console . Write ( b [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; solve ( n , arr ) ; } }
Find the sum of the first half and second half elements of an array | C # program to count pairs whose sum divisible by ' K ' ; Function to find the sum of the first half elements and second half elements of an array ; Add elements in first half sum ; Add elements in the second half sum ; Driver code ; Function call
using System ; class GFG { public static void sum_of_elements ( int [ ] arr , int n ) { int sumfirst = 0 , sumsecond = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) { sumfirst += arr [ i ] ; } else { sumsecond += arr [ i ] ; } } Console . WriteLine ( " Sum ▁ of ▁ first ▁ half ▁ elements ▁ is ▁ " + sumfirst ) ; Console . WriteLine ( " Sum ▁ of ▁ second ▁ half ▁ elements ▁ is ▁ " + sumsecond ) ; } static public void Main ( ) { int [ ] arr = { 20 , 30 , 60 , 10 , 25 , 15 , 40 } ; int n = arr . Length ; sum_of_elements ( arr , n ) ; } }
Sequence with sum K and minimum sum of absolute differences between consecutive elements | C # implementation of the approach ; Function to return the minimized sum ; If k is divisible by n then the answer will be 0 ; Else the answer will be 1 ; Driver code
using System ; class GFG { static int minimum_sum ( int n , int k ) { if ( k % n == 0 ) return 0 ; return 1 ; } static public void Main ( ) { int n = 3 , k = 56 ; Console . Write ( minimum_sum ( n , k ) ) ; } }
Print first N terms of Lower Wythoff sequence | C # implementation of the approach ; Function to print the first n terms of the lower Wythoff sequence ; Calculate value of phi ; Find the numbers ; a ( n ) = floor ( n * phi ) ; Print the nth numbers ; Driver code
using System ; class GFG { static void lowerWythoff ( int n ) { double phi = ( 1 + Math . Sqrt ( 5 ) ) / 2.0 ; for ( int i = 1 ; i <= n ; i ++ ) { double ans = Math . Floor ( i * phi ) ; Console . Write ( ( int ) ans ) ; if ( i != n ) Console . Write ( " ▁ , ▁ " ) ; } } static public void Main ( ) { int n = 5 ; lowerWythoff ( n ) ; } }
Create new linked list from two given linked list with greater element at each node | C # program to create a new linked list from two given linked list of the same size with the greater element among the two at each node ; Representation of node ; Function to insert node in a linked list ; Function which returns new linked list ; Compare for greater node ; Driver code ; First linked list ; Second linked list
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node insert ( Node root , int item ) { Node ptr , temp ; temp = new Node ( ) ; temp . data = item ; temp . next = null ; if ( root == null ) root = temp ; else { ptr = root ; while ( ptr . next != null ) ptr = ptr . next ; ptr . next = temp ; } return root ; } static Node newList ( Node root1 , Node root2 ) { Node ptr1 = root1 , ptr2 = root2 , ptr ; Node root = null , temp ; while ( ptr1 != null ) { temp = new Node ( ) ; temp . next = null ; if ( ptr1 . data < ptr2 . data ) temp . data = ptr2 . data ; else temp . data = ptr1 . data ; if ( root == null ) root = temp ; else { ptr = root ; while ( ptr . next != null ) ptr = ptr . next ; ptr . next = temp ; } ptr1 = ptr1 . next ; ptr2 = ptr2 . next ; } return root ; } static void display ( Node root ) { while ( root != null ) { Console . Write ( root . data + " - > " ) ; root = root . next ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { Node root1 = null , root2 = null , root = null ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 2 ) ; root1 = insert ( root1 , 3 ) ; root1 = insert ( root1 , 8 ) ; Console . Write ( " First ▁ List : ▁ " ) ; display ( root1 ) ; root2 = insert ( root2 , 1 ) ; root2 = insert ( root2 , 7 ) ; root2 = insert ( root2 , 4 ) ; root2 = insert ( root2 , 5 ) ; Console . Write ( " Second ▁ List : ▁ " ) ; display ( root2 ) ; root = newList ( root1 , root2 ) ; Console . Write ( " New ▁ List : ▁ " ) ; display ( root ) ; } }
Count of quadruplets from range [ L , R ] having GCD equal to K | C # implementation of the approach ; Function to return the gcd of a and b ; Function to return the count of quadruplets having gcd = k ; To store the required count ; Check every quadruplet pair whether its gcd is k ; Return the required count ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int countQuadruplets ( int l , int r , int k ) { int count = 0 ; for ( int u = l ; u <= r ; u ++ ) { for ( int v = l ; v <= r ; v ++ ) { for ( int w = l ; w <= r ; w ++ ) { for ( int x = l ; x <= r ; x ++ ) { if ( gcd ( gcd ( u , v ) , gcd ( w , x ) ) == k ) count ++ ; } } } } return count ; } static public void Main ( ) { int l = 1 , r = 10 , k = 2 ; Console . WriteLine ( countQuadruplets ( l , r , k ) ) ; } }
Find an index such that difference between product of elements before and after it is minimum | C # implementation of the approach ; Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code
using System ; class GFG { static int findIndex ( int [ ] a , int n ) { int res = 0 ; long min_diff = Int64 . MaxValue ; long [ ] prod = new long [ n ] ; prod [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prod [ i ] = prod [ i - 1 ] * a [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long curr_diff = Math . Abs ( ( prod [ n - 1 ] / prod [ i ] ) - prod [ i ] ) ; if ( curr_diff < min_diff ) { min_diff = curr_diff ; res = i ; } } return res ; } static void Main ( ) { int [ ] arr = { 3 , 2 , 5 , 7 , 2 , 9 } ; int N = arr . Length ; Console . WriteLine ( findIndex ( arr , N ) ) ; } }
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | C # program to implement the above approach ; Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Driver code
using System ; class GFG { static void printNumbers ( int [ ] a , int n , int x ) { bool flag = false ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; int g = __gcd ( num , x ) ; while ( g != 1 ) { num /= g ; g = __gcd ( num , x ) ; } if ( num == 1 ) { flag = true ; Console . Write ( a [ i ] + " ▁ " ) ; } } if ( ! flag ) Console . WriteLine ( " There ▁ are ▁ no ▁ such ▁ numbers " ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int x = 60 ; int [ ] a = { 2 , 5 , 10 , 7 , 17 } ; int n = a . Length ; printNumbers ( a , n , x ) ; } }
Find the final radiations of each Radiated Stations | C # implementation of the approach ; Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; Driver code ; 1 - based indexing
using System ; class GFG { static void print ( int [ ] rStation , int n ) { for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( rStation [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } static void radiated_Station ( int [ ] station , int n ) { int [ ] rStation = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { int li = i - 1 , ri = i + 1 ; int lRad = station [ i ] - 1 , rRad = station [ i ] - 1 ; rStation [ i ] += station [ i ] ; while ( li >= 1 && lRad >= 1 ) { rStation [ li -- ] += lRad -- ; } while ( ri <= n && rRad >= 1 ) { rStation [ ri ++ ] += rRad -- ; } } print ( rStation , n ) ; } public static void Main ( String [ ] args ) { int [ ] station = { 0 , 7 , 9 , 12 , 2 , 5 } ; int n = station . Length - 1 ; radiated_Station ( station , n ) ; } }
Program to find the maximum difference between the index of any two different numbers | C # implementation of the approach ; Function to return the maximum difference ; Iteratively check from back ; Different numbers ; Iteratively check from the beginning ; Different numbers ; Driver code
using System ; class GFG { static int findMaximumDiff ( int [ ] a , int n ) { int ind1 = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( a [ 0 ] != a [ i ] ) { ind1 = i ; break ; } } int ind2 = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ n - 1 ] != a [ i ] ) { ind2 = ( n - 1 - i ) ; break ; } } return Math . Max ( ind1 , ind2 ) ; } static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 2 , 3 } ; int n = a . Length ; Console . WriteLine ( findMaximumDiff ( a , n ) ) ; } }
Find the Kth node in the DFS traversal of a given subtree in a Tree | C # program to find the Kth node in the DFS traversal of the subtree of given vertex V in a Tree ; To store nodes ; To store the current index of vertex in DFS ; To store the starting index and ending index of vertex in the DFS traversal array ; To store the DFS of vertex 1 ; Function to add edge between two nodes ; Initialize the vectors ; Function to perform DFS of a vertex 1. stores the DFS of the vertex 1 in vector p , 2. store the start index of DFS of every vertex 3. store the end index of DFS of every vertex ; store staring index of node ch ; store ending index ; Function to find the Kth node in DFS of vertex V ; Check if kth number exits or not ; Driver code ; Number of nodes ; Add edges ; Store DFS of 1 st node
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int n ; static ArrayList tree = new ArrayList ( ) ; static int currentIdx ; static int [ ] startIdx ; static int [ ] endIdx ; static int [ ] p ; static void Add_edge ( int u , int v ) { ( ( ArrayList ) tree [ u ] ) . Add ( v ) ; ( ( ArrayList ) tree [ v ] ) . Add ( u ) ; } static void intisalise ( ) { startIdx = new int [ n + 1 ] ; endIdx = new int [ n + 1 ] ; p = new int [ n + 1 ] ; } static void Dfs ( int ch , int par ) { p [ currentIdx ] = ch ; startIdx [ ch ] = currentIdx ++ ; foreach ( int c in ( ArrayList ) tree [ ch ] ) { if ( c != par ) Dfs ( c , ch ) ; } endIdx [ ch ] = currentIdx - 1 ; } static int findNode ( int v , int k ) { k += startIdx [ v ] - 1 ; if ( k <= endIdx [ v ] ) return p [ k ] ; return - 1 ; } public static void Main ( string [ ] args ) { n = 9 ; for ( int i = 0 ; i <= n ; i ++ ) tree . Add ( new ArrayList ( ) ) ; Add_edge ( 1 , 2 ) ; Add_edge ( 1 , 3 ) ; Add_edge ( 1 , 4 ) ; Add_edge ( 3 , 5 ) ; Add_edge ( 3 , 7 ) ; Add_edge ( 5 , 6 ) ; Add_edge ( 5 , 8 ) ; Add_edge ( 7 , 9 ) ; intisalise ( ) ; Dfs ( 1 , 0 ) ; int v = 3 , k = 4 ; Console . Write ( findNode ( v , k ) ) ; } }
Sum of the series Kn + ( K ( n | C # implementation of above approach ; Function to return sum ; Driver code
using System ; class GFG { static int sum ( int k , int n ) { int sum = ( int ) ( Math . Pow ( k , n + 1 ) - Math . Pow ( k - 1 , n + 1 ) ) ; return sum ; } public static void Main ( ) { int n = 3 ; int K = 3 ; Console . Write ( sum ( K , n ) ) ; } }
Check whether factorial of N is divisible by sum of first N natural numbers | Function to check whether a number is prime or not . ; Count variable to store the number of factors of ' num ' ; Counting the number of factors ; If number is prime return true ; Function to check for divisibility ; if ' n ' equals 1 then divisibility is possible ; Else check whether ' n + 1' is prime or not ; If ' n + 1' is prime then ' n ! ' is not divisible by ' n * ( n + 1 ) / 2' ; else divisibility occurs ; Driver Code ; Test for n = 3 ; Test for n = 4
class GfG { static bool is_prime ( int num ) { int count = 0 ; for ( int i = 1 ; i * i <= ( num ) ; i ++ ) { if ( ( num ) % i == 0 ) { if ( i * i != ( num ) ) count += 2 ; else count ++ ; } } if ( count == 2 ) return true ; else return false ; } static string is_divisible ( int n ) { if ( n == 1 ) { return " YES " ; } else { if ( is_prime ( n + 1 ) ) return " NO " ; else return " YES " ; } } static void Main ( ) { int n ; n = 3 ; System . Console . WriteLine ( is_divisible ( n ) ) ; n = 4 ; System . Console . WriteLine ( is_divisible ( n ) ) ; } }
Number of subarrays having sum of the form k ^ m , m >= 0 | C # implementation of the above approach ; partial_sum ; Function to count number of sub - arrays whose sum is k ^ p where p >= 0 ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; b = k ^ p , p >= 0 ; k ^ m can be maximum equal to 10 ^ 14. ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 100005 ; static long [ ] partial_sum ( long [ ] prefix_sum , int [ ] arr , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { prefix_sum [ i ] = ( prefix_sum [ i - 1 ] + arr [ i - 1 ] ) ; } return prefix_sum ; } static int countSubarrays ( int [ ] arr , int n , int k ) { long [ ] prefix_sum = new long [ MAX ] ; prefix_sum [ 0 ] = 0 ; prefix_sum = partial_sum ( prefix_sum , arr , n ) ; int sum ; if ( k == 1 ) { sum = 0 ; Dictionary < long , int > mp = new Dictionary < long , int > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { if ( mp . ContainsKey ( prefix_sum [ i ] + 1 ) ) sum += mp [ prefix_sum [ i ] + 1 ] ; if ( mp . ContainsKey ( prefix_sum [ i ] ) ) mp . Add ( prefix_sum [ i ] , mp [ prefix_sum [ i ] ] + 1 ) ; else mp . Add ( prefix_sum [ i ] , 1 ) ; } return sum ; } if ( k == - 1 ) { sum = 0 ; Dictionary < long , int > map = new Dictionary < long , int > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { if ( map . ContainsKey ( prefix_sum [ i ] + 1 ) ) sum += map [ prefix_sum [ i ] + 1 ] ; if ( map . ContainsKey ( prefix_sum [ i ] - 1 ) ) sum += map [ prefix_sum [ i ] - 1 ] ; if ( map . ContainsKey ( prefix_sum [ i ] ) ) map . Add ( prefix_sum [ i ] , map [ prefix_sum [ i ] ] + 1 ) ; else map . Add ( prefix_sum [ i ] , 1 ) ; } return sum ; } sum = 0 ; long b , l = 100000000000000L ; Dictionary < long , int > m = new Dictionary < long , int > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { b = 1 ; while ( true ) { if ( b > l ) break ; if ( m . ContainsKey ( prefix_sum [ i ] + b ) ) sum += m [ prefix_sum [ i ] + b ] ; b *= k ; } if ( m . ContainsKey ( prefix_sum [ i ] ) ) m . Add ( ( prefix_sum [ i ] ) , m [ prefix_sum [ i ] ] + 1 ) ; else m . Add ( ( prefix_sum [ i ] ) , 1 ) ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 2 , 2 } ; int n = arr . Length ; int k = 2 ; Console . Write ( countSubarrays ( arr , n , k ) ) ; } }
Given two binary strings perform operation until B > 0 and print the result | C # implementation of the approach ; Function to return the required result ; Reverse the strings ; Count the number of set bits in b ; To store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; To store the final answer ; Add power [ i ] to the ans after multiplying it with the number of set bits in b ; Divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; If no more set bits in b i . e . b = 0 ; Return the required answer ; Driver code
using System ; using System . Collections ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int BitOperations ( string a , int n , string b , int m ) { char [ ] ch1 = a . ToCharArray ( ) ; Array . Reverse ( ch1 ) ; a = new string ( ch1 ) ; char [ ] ch2 = b . ToCharArray ( ) ; Array . Reverse ( ch2 ) ; b = new string ( ch2 ) ; int c = 0 ; for ( int i = 0 ; i < m ; i ++ ) if ( b [ i ] == '1' ) c ++ ; int [ ] power = new int [ n ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) power [ i ] = ( power [ i - 1 ] * 2 ) % mod ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == '1' ) { ans += c * power [ i ] ; if ( ans >= mod ) ans %= mod ; } if ( b [ i ] == '1' ) c -- ; if ( c == 0 ) break ; } return ans ; } static void Main ( ) { string a = "1001" , b = "10101" ; int n = a . Length , m = b . Length ; Console . WriteLine ( BitOperations ( a , n , b , m ) ) ; } }
Print prime numbers with prime sum of digits in an array | C # implementation of the above approach ; Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code
using System . Linq ; using System ; class GFG { static void sieve ( int maxEle , int [ ] prime ) { prime [ 0 ] = prime [ 1 ] = 1 ; for ( int i = 2 ; i * i <= maxEle ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j <= maxEle ; j += i ) prime [ j ] = 1 ; } } } static int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % 10 ; n = n / 10 ; } return sum ; } static void printAdditivePrime ( int [ ] arr , int n ) { int maxEle = arr . Max ( ) ; int [ ] prime = new int [ maxEle + 1 ] ; sieve ( maxEle , prime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 0 ) { int sum = digitSum ( arr [ i ] ) ; if ( prime [ sum ] == 0 ) Console . Write ( arr [ i ] + " ▁ " ) ; } } } static void Main ( ) { int [ ] a = { 2 , 4 , 6 , 11 , 12 , 18 , 7 } ; int n = a . Length ; printAdditivePrime ( a , n ) ; } }
Program to find nth term of the series 1 4 15 24 45 60 92 | C # implementation of the above approach ; function to calculate nth term of the series ; variable nth will store the nth term of series ; if n is even ; if n is odd ; return nth term ; Driver code
using System ; class GFG { static long nthTerm ( long n ) { long nth ; if ( n % 2 == 0 ) nth = 2 * ( ( n * n ) - n ) ; else nth = ( 2 * n * n ) - n ; return nth ; } public static void Main ( ) { long n ; n = 5 ; Console . WriteLine ( nthTerm ( n ) ) ; n = 25 ; Console . WriteLine ( nthTerm ( n ) ) ; n = 25000000 ; Console . WriteLine ( nthTerm ( n ) ) ; n = 250000007 ; Console . WriteLine ( nthTerm ( n ) ) ; } }
Find the Nth term of the series 9 , 45 , 243 , 1377 | C # implementation of the approach ; Function to return the nth term of the given series ; nth term ; Driver code
using System ; public class GFG { static int nthTerm ( int n ) { int An = ( ( int ) Math . Pow ( 1 , n ) + ( int ) Math . Pow ( 2 , n ) ) * ( int ) Math . Pow ( 3 , n ) ; return An ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( nthTerm ( n ) ) ; } }
Count the numbers < N which have equal number of divisors as K | C # implementation of the approach ; Function to return the count of the divisors of a number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ; While i divides n ; This condition is to handle the case when n is a prime number > 2 ; Count the total elements that have divisors exactly equal to as that of k 's ; Exclude k from the result if it is smaller than n . ; Driver code
using System ; public class GFG { static int countDivisors ( int n ) { int x = 0 , ans = 1 ; while ( n % 2 == 0 ) { x ++ ; n = n / 2 ; } ans = ans * ( x + 1 ) ; for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { x = 0 ; while ( n % i == 0 ) { x ++ ; n = n / i ; } ans = ans * ( x + 1 ) ; } if ( n > 2 ) ans = ans * 2 ; return ans ; } static int getTotalCount ( int n , int k ) { int k_count = countDivisors ( k ) ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( k_count == countDivisors ( i ) ) count ++ ; if ( k < n ) count = count - 1 ; return count ; } public static void Main ( ) { int n = 500 , k = 6 ; Console . WriteLine ( getTotalCount ( n , k ) ) ; } }
Find the nth term of the series 0 , 8 , 64 , 216 , 512 , . . . | C # implementation of the approach ; Function to return the nth term of the given series ; Common difference and first term ; First term ; nth term ; nth term of the given series ; Driver code
using System ; public class GFG { static int nthTerm ( int n ) { int d = 2 ; int a1 = 0 ; int An = a1 + ( n - 1 ) * d ; return ( int ) Math . Pow ( An , 3 ) ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( nthTerm ( n ) ) ; } }