codeparrot/xlcost-text-to-code huggingface dataset:
Program to calculate Electricity Bill | C # implementation to calculate the electricity bill ; Function to calculate the electricity bill ; Condition to find the charges bar in which the units consumed is fall ; Driver Code
using System ; class ComputeElectricityBill { public static int calculateBill ( int units ) { if ( units <= 100 ) { return units * 10 ; } else if ( units <= 200 ) { return ( 100 * 10 ) + ( units - 100 ) * 15 ; } else if ( units <= 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( units - 200 ) * 20 ; } else if ( units > 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( 100 * 20 ) + ( units - 300 ) * 25 ; } return 0 ; } public static void Main ( String [ ] args ) { int units = 250 ; Console . WriteLine ( calculateBill ( units ) ) ; } }
Find all divisors of first N natural numbers | C # implementation to find all the divisors of the first N natural numbers ; Function to find the factors of the numbers from 1 to N ; Loop to find the factors of the first N natural numbers of the integer ; Driver Code
using System ; class GFG { static void factors ( int n ) { int i , j ; Console . Write ( "1 ▁ - - > 1 STRNEWLINE " ) ; for ( i = 2 ; i <= n ; i ++ ) { Console . Write ( i + " ▁ - - > " ) ; for ( j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { Console . Write ( j + " , ▁ " ) ; if ( i / j != j ) Console . Write ( i / j + " , ▁ " ) ; } } Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( ) { int n = 5 ; factors ( n ) ; } }
Find all divisors of first N natural numbers | C # implementation to find the factors of first N natural numbers ; Initialize global divisor vector array of sequence container ; Calculate all divisors of number ; Function to find the factors of first n natural numbers ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; public class GFG { static int MAX = ( int ) 1e5 ; static List < int > [ ] divisor = new List < int > [ MAX + 1 ] ; static void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . Add ( i ) ; } } static void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( i + " - - > " ) ; foreach ( int divi in divisor [ i ] ) { Console . Write ( divi + " , ▁ " ) ; } Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < divisor . Length ; i ++ ) divisor [ i ] = new List < int > ( ) ; sieve ( ) ; findNFactors ( n ) ; } }
Maximum number of prime factors a number can have with exactly x factors | C # implementation to find the maximum count of the prime factors by the count of factors of number ; Function to count number of prime factors of x ; Count variable is incremented form every prime factor of x ; Loop to count the number of the prime factors of the given number ; Driver Code
using System ; class GFG { static int countPrimeFactors ( int n ) { if ( n == 1 ) return 0 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { cnt ++ ; n = n / i ; } } if ( n > 2 ) cnt ++ ; return cnt ; } static public void Main ( ) { int x = 8 ; int prime_factor_cnt = countPrimeFactors ( x ) ; Console . Write ( prime_factor_cnt ) ; } }
Count of N digit palindromic numbers divisible by 9 | C # implementation to count the number of N digit palindromic numbers divisible by 9 ; Function to find the count of N digits palindromic numbers which are divisible by 9 ; If N is odd ; If N is even ; Driver Code
using System ; class GFG { static int countPalindromic ( int n ) { int count ; if ( n % 2 == 1 ) { count = ( int ) Math . Pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = ( int ) Math . Pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } public static void Main ( ) { int n = 3 ; Console . Write ( countPalindromic ( n ) ) ; } }
Count of sub | C # program to find the count of sub - arrays with odd product ; Function that returns the count of sub - arrays with odd product ; Initialize the count variable ; Initialize variable to store the last index with even number ; Initialize variable to store count of continuous odd numbers ; Loop through the array ; Check if the number is even or not ; Calculate count of continuous odd numbers ; Increase the count of sub - arrays with odd product ; Store the index of last even number ; N considered as index of even number ; Driver code ; Function call
using System ; class GFG { static int countSubArrayWithOddProduct ( int [ ] A , int N ) { int count = 0 ; int last = - 1 ; int K = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { K = ( i - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; last = i ; } } K = ( N - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; return count ; } static void Main ( ) { int [ ] arr = { 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 } ; int n = arr . Length ; Console . WriteLine ( countSubArrayWithOddProduct ( arr , n ) ) ; } }
Calculate the CGPA and CGPA % of marks obtained by a Student in N subjects | C # program to calculate the CGPA and CGPA percentage of a student ; Variable to store the grades in every subject ; Variables to store CGPA and the sum of all the grades ; Computing the grades ; Computing the sum of grades ; Computing the CGPA ; Driver code
using System ; class GFG { public static double CgpaCalc ( double [ ] marks , int n ) { double [ ] grade = new double [ n ] ; double cgpa , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { grade [ i ] = ( marks [ i ] / 10 ) ; } for ( int i = 0 ; i < n ; i ++ ) { sum += grade [ i ] ; } cgpa = sum / n ; return cgpa ; } public static void Main ( String [ ] args ) { int n = 5 ; double [ ] marks = { 90 , 80 , 70 , 80 , 90 } ; double cgpa = CgpaCalc ( marks , n ) ; Console . WriteLine ( " CGPA ▁ = ▁ " + cgpa ) ; Console . WriteLine ( " CGPA ▁ Percentage ▁ = ▁ { 0 : F2 } " , cgpa * 9.5 ) ; } }
Find the subarray of size K with minimum XOR | C # implementation to find the subarray with minimum XOR ; Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Driver Code ; Subarray size ; Function Call
using System ; class GFG { static void findMinXORSubarray ( int [ ] arr , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } Console . Write ( min_xor + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = arr . Length ; findMinXORSubarray ( arr , n , k ) ; } }
Greatest number that can be formed from a pair in a given Array | C # implementation to find the greatest number from the given pairs of the array ; Function to find the greatest number formed from the pairs ; First append Y at the end of X ; Then append X at the end of Y ; Now see which of the two formed numbers is greater than other ; Function to find pairs from array ; Iterate through all pairs ; Driver code
using System ; class GFG { static String getNumber ( int a , int b ) { String X = a . ToString ( ) ; String Y = b . ToString ( ) ; String XY = X + Y ; String YX = Y + X ; return XY . CompareTo ( YX ) > 0 ? XY : YX ; } static void printMaxPair ( int [ ] arr , int n ) { int largest = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int number = Int32 . Parse ( getNumber ( arr [ i ] , arr [ j ] ) ) ; largest = Math . Max ( largest , number ) ; } Console . WriteLine ( largest ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 23 , 14 , 16 , 25 , 3 , 9 } ; int n = a . Length ; printMaxPair ( a , n ) ; } }
Check whether two numbers are in golden ratio | C # implementation to check whether two numbers are in golden ratio with each other ; Function to check that two numbers are in golden ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in golden ratio ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool checkGoldenRatio ( float a , float b ) { if ( a <= b ) { float temp = a ; a = b ; b = temp ; } string ratio1 = String . Format ( " { 0:0.000 } " , a / b ) ; string ratio2 = String . Format ( " { 0:0.000 } " , ( a + b ) / a ) ; if ( ratio1 == ratio2 && ratio1 == "1.618" ) { Console . WriteLine ( " Yes " ) ; return true ; } else { Console . WriteLine ( " No " ) ; return false ; } } static void Main ( ) { float a = ( float ) 0.618 ; float b = 1 ; checkGoldenRatio ( a , b ) ; } }
Find the minimum number to be added to N to make it a power of K | C # program to find the minimum number to be added to N to make it a power of K ; Function to return the minimum number to be added to N to make it a power of K . ; Computing the difference between then next greater power of K and N ; Driver code
using System ; class GFG { static int minNum ( int n , int k ) { int x = ( int ) ( Math . Log ( n ) / Math . Log ( k ) ) + 1 ; int mn = ( int ) ( Math . Pow ( k , x ) - n ) ; return mn ; } public static void Main ( string [ ] args ) { int n = 20 , k = 5 ; Console . Write ( minNum ( n , k ) ) ; } }
Previous perfect square and cube number smaller than number N | C # implementation to find the previous perfect square and cube smaller than the given number ; Function to find the previous perfect square of the number N ; If N is already a perfect square decrease prevN by 1. ; Function to find the previous perfect cube ; If N is already a perfect cube decrease prevN by 1. ; Driver Code
using System ; class GFG { static int previousPerfectSquare ( int N ) { int prevN = ( int ) Math . Floor ( Math . Sqrt ( N ) ) ; if ( prevN * prevN == N ) prevN -= 1 ; return prevN * prevN ; } static int previousPerfectCube ( int N ) { int prevN = ( int ) Math . Floor ( Math . Cbrt ( N ) ) ; if ( prevN * prevN * prevN == N ) prevN -= 1 ; return prevN * prevN * prevN ; } public static void Main ( String [ ] args ) { int n = 30 ; Console . WriteLine ( previousPerfectSquare ( n ) ) ; Console . WriteLine ( previousPerfectCube ( n ) ) ; } }
Find the count of even odd pairs in a given Array | C # program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; Variable to store count of such pairs ; Iterate through all pairs ; Increment count if condition is satisfied ; Driver code
using System ; class GFG { static int findCount ( int [ ] arr , int n ) { int res = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] % 2 == 0 ) && ( arr [ j ] % 2 == 1 ) ) { res ++ ; } return res ; } public static void Main ( String [ ] args ) { int [ ] a = { 5 , 4 , 1 , 2 , 3 } ; int n = a . Length ; Console . Write ( findCount ( a , n ) ) ; } }
Find the count of even odd pairs in a given Array | C # program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; Check if number is even or not ; Driver code
using System ; class GFG { static int findCount ( int [ ] arr , int n ) { int count = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { count ++ ; } else { ans = ans + count ; } } return ans ; } public static void Main ( ) { int [ ] a = { 5 , 4 , 1 , 2 , 3 } ; int n = a . Length ; Console . WriteLine ( findCount ( a , n ) ) ; } }
Product of all non repeating Subarrays of an Array | C # program to find the product of all non - repeating Subarrays of an Array ; Function to find the product of all non - repeating Subarrays of an Array ; Finding the occurrence of every element ; Iterating through the array and finding the product ; We are taking the power of each element in array with the occurrence and then taking product of those . ; Driver code
using System ; class GFG { private static long product ( int [ ] arr ) { double occurrence = Math . Pow ( 2 , arr . Length - 1 ) ; double product = 1 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { product *= Math . Pow ( arr [ i ] , occurrence ) ; } return ( long ) product ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 3 , 7 } ; Console . WriteLine ( product ( arr ) ) ; } }
Sum of all N | C # program for the above approach ; Function to find a ^ b efficiently ; Base Case ; To store the value of a ^ b ; Multiply temp by a until b is not 0 ; Return the readonly ans a ^ b ; Function to find sum of all N - digit palindromic number divisible by 9 ; Base Case ; If N is even , decrease ways by 1 ; Find the total number of ways ; Iterate over [ 1 , N ] and find the sum at each index ; Print the readonly sum ; Driver Code
using System ; class GFG { static int MOD = 1000000007 ; static int power ( int a , int b ) { if ( b == 0 ) return 1 ; int temp = power ( a , b / 2 ) ; temp = ( temp * temp ) % MOD ; if ( b % 2 != 0 ) { temp = ( temp * a ) % MOD ; } return temp ; } static void palindromicSum ( int N ) { int sum = 0 , res , ways ; if ( N == 1 ) { Console . Write ( "9" + " STRNEWLINE " ) ; return ; } if ( N == 2 ) { Console . Write ( "99" + " STRNEWLINE " ) ; return ; } ways = N / 2 ; if ( N % 2 == 0 ) ways -- ; res = power ( 9 , ways - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum = sum * 10 + 45 * res ; sum %= MOD ; } Console . Write ( sum + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 3 ; palindromicSum ( N ) ; } }
Maximize array sum by concatenating corresponding elements of given two arrays | C # implementation to find the maximum array sum by concatenating corresponding elements of given two arrays ; Function to join the two numbers ; Loop to reverse the digits of the one number ; Loop to join two numbers ; Function to find the maximum array sum ; Loop to iterate over the two elements of the array ; Find the array sum ; Return the array sum ; Driver Code
using System ; class GFG { static int joinNumbers ( int numA , int numB ) { int revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = numB / 10 ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = revB / 10 ; } return numA ; } static int findMaxSum ( int [ ] A , int [ ] B , int n ) { int [ ] maxArr = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { int X = joinNumbers ( A [ i ] , B [ i ] ) ; int Y = joinNumbers ( B [ i ] , A [ i ] ) ; int mx = Math . Max ( X , Y ) ; maxArr [ i ] = mx ; } int maxAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] A = { 11 , 23 , 38 , 43 , 59 } ; int [ ] B = { 36 , 24 , 17 , 40 , 56 } ; Console . WriteLine ( findMaxSum ( A , B , N ) ) ; } }
Split N natural numbers into two sets having GCD of their sums greater than 1 | C # program to split N natural numbers into two sets having GCD of their sums greater than 1 ; Function to create and print the two sets ; For n <= 2 such sets can never be formed ; Check if N is even or odd and store the element which divides the sum of N natural numbers accordingly ; First set ; Print elements of second set ; Driver code
using System ; class GFG { static void createSets ( long n ) { if ( n <= 2 ) { Console . Write ( " - 1" ) ; return ; } else { long x = ( n % 2 == 0 ) ? ( n / 2 ) : ( ( n + 1 ) / 2 ) ; Console . Write ( x + " STRNEWLINE " ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i == x ) continue ; Console . Write ( i + " ▁ " ) ; } } return ; } public static void Main ( String [ ] args ) { long N = 7 ; createSets ( N ) ; } }
Count the factors of K present in the given Array | C # implementation to find the count of factors of K present in array ; Function to find the count of factors of K present in array ; Loop to consider every element of array ; Driver Code ; Function Call
using System ; class GFG { static int calcCount ( int [ ] arr , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 5 , 6 } ; int n = arr . Length ; int k = 6 ; Console . Write ( calcCount ( arr , n , k ) ) ; } }
Length of largest subarray whose all elements are Perfect Number | C # program to find the length of the largest sub - array of an array every element of whose is a perfect number ; Function that returns true if n is perfect ; Variable to store sum of divisors ; Find all divisors and add them ; Check if sum of divisors is equal to n , then n is a perfect number ; Function to return the length of the largest sub - array of an array every element of whose is a perfect number ; Check if arr [ i ] is a perfect number ; Driver code
using System ; class GFG { static bool isPerfect ( int n ) { int sum = 1 ; int i ; for ( i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) { return true ; } return false ; } static int contiguousPerfectNumber ( int [ ] arr , int n ) { int current_length = 0 ; int max_length = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( isPerfect ( arr [ i ] ) ) { current_length ++ ; } else { current_length = 0 ; } max_length = Math . Max ( max_length , current_length ) ; } return max_length ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = arr . Length ; Console . Write ( contiguousPerfectNumber ( arr , n ) ) ; } }
Length of largest subarray whose all elements Powerful number | C # program to find the length of the largest sub - array of an array every element of whose is a powerful number ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; If n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to return the length of the largest sub - array of an array every element of whose is a powerful number ; If arr [ i ] is a Powerful number ; Driver code
using System ; class solution { static bool isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . Sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static int contiguousPowerfulNumber ( int [ ] arr , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = Math . Max ( max_length , current_length ) ; } return max_length ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = arr . Length ; Console . WriteLine ( contiguousPowerfulNumber ( arr , n ) ) ; } }
Maximize sum of K corner elements in Array | C # program to maximize the sum of K elements in the array by taking only corner elements ; Function to return maximum sum ; Base case ; Pick the start index ; Pick the end index ; Recursive function call ; Return the readonly answer ; Function to find the maximized sum ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int K , int start , int end , int max_sum ) { if ( K == 0 ) return max_sum ; int max_sum_start = max_sum + arr [ start ] ; int max_sum_end = max_sum + arr [ end ] ; int ans = Math . Max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) ; return ans ; } static void maximizeSum ( int [ ] arr , int K , int n ) { int max_sum = 0 ; int start = 0 ; int end = n - 1 ; Console . Write ( maxSum ( arr , K , start , end , max_sum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = arr . Length ; maximizeSum ( arr , K , n ) ; } }
Maximize sum of K corner elements in Array | C # program to maximize the sum of K elements in the array by taking only corner elements ; Function to return maximum sum ; Initialize variables ; Iterate over first K elements of array and update the value for curr_points ; Update value for max_points ; j points to the end of the array ; Return the readonly result ; Driver code
using System ; class GFG { public static int maxPointCount ( int [ ] arr , int K , int size ) { int curr_points = 0 ; int max_points = 0 ; for ( int i = 0 ; i < K ; i ++ ) curr_points += arr [ i ] ; max_points = curr_points ; int j = size - 1 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { curr_points = curr_points + arr [ j ] - arr [ i ] ; max_points = Math . Max ( curr_points , max_points ) ; j -- ; } return max_points ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = arr . Length ; Console . Write ( maxPointCount ( arr , K , n ) ) ; } }
Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9 | C # implementation to find the sum of all the N digit palindromic numbers divisible by 9 ; Function for finding count of N digits palindrome which are divisible by 9 ; If N is odd ; If N is even ; Function for finding sum of N digits palindrome which are divisible by 9 ; Count the possible number of palindrome ; Driver Code
using System ; class GFG { static int countPalindrome ( int n ) { int count ; if ( n % 2 == 1 ) { count = ( int ) Math . Pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = ( int ) Math . Pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } static int sumPalindrome ( int n ) { int count = countPalindrome ( n ) ; int res = 0 ; if ( n == 1 ) return 9 ; if ( n == 2 ) return 99 ; for ( int i = 0 ; i < n ; i ++ ) { res = res * 10 + count * 5 ; } return res ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( sumPalindrome ( n ) ) ; } }
Find the total count of numbers up to N digits in a given base B | C # implementation to find the count of natural numbers upto N digits ; Function to return the count of natural numbers upto N digits ; Loop to iterate from 1 to N and calculating number of natural numbers for every ' i ' th digit . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int count ( int N , int B ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( int ) ( ( B - 1 ) * Math . Pow ( B , i - 1 ) ) ; } return sum ; } public static void Main ( String [ ] args ) { int N = 2 , B = 10 ; Console . Write ( count ( N , B ) ) ; } }
Find smallest perfect square number A such that N + A is also a perfect square number | C # code to find out the smallest perfect square X which when added to N yields another perfect square number . ; X is the smallest perfect square number ; Loop from 1 to square root of N ; Condition to check whether i is factor of N or not ; Condition to check whether factors satisfies the equation or not ; Stores minimum value ; Return if X * X if X is not equal to 1e9 else return - 1 ; Driver code
using System ; class GFG { static long SmallestPerfectSquare ( long N ) { long X = ( long ) 1e9 ; long ans ; for ( int i = 1 ; i < Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { long a = i ; long b = N / i ; if ( ( b - a != 0 ) && ( ( b - a ) % 2 == 0 ) ) { X = Math . Min ( X , ( b - a ) / 2 ) ; } } } if ( X != 1e9 ) ans = X * X ; else ans = - 1 ; return ans ; } public static void Main ( string [ ] args ) { long N = 3 ; Console . WriteLine ( SmallestPerfectSquare ( N ) ) ; } }