codeparrot/xlcost-text-to-code huggingface dataset:
Tidy Number ( Digits in non | C # program to check if a number is Tidy or not . ; Returns true if num is Tidy ; To store previous digit ( Assigning initial value which is more than any digit ) ; Traverse all digits from right to left and check if any digit is smaller than previous . ; Driver Code
using System ; class GFG { static bool isTidy ( int num ) { int prev = 10 ; while ( num != 0 ) { int rem = num % 10 ; num /= 10 ; if ( rem > prev ) return false ; prev = rem ; } return true ; } public static void Main ( ) { int num = 1556 ; Console . WriteLine ( isTidy ( num ) ? " Yes " : " No " ) ; } }
Nth Square free number | C # Program to find the nth square free number ; Function to find nth square free number ; To maintain count of square free number ; Loop for square free numbers ; Checking whether square of a number is divisible by any number which is a perfect square ; If number is square free ; If the cnt becomes n , return the number ; Driver code
using System ; class GFG { public static int squareFree ( int n ) { int cnt = 0 ; for ( int i = 1 ; ; i ++ ) { bool isSqFree = true ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % ( j * j ) == 0 ) { isSqFree = false ; break ; } } if ( isSqFree == true ) { cnt ++ ; if ( cnt == n ) return i ; } } } public static void Main ( ) { int n = 10 ; Console . Write ( " " + squareFree ( n ) ) ; } }
Nth Square free number | C # Program to find the nth square free number ; Maximum prime number to be considered for square divisibility ; Maximum value of result . We do binary search from 1 to MAX_RES ; 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 ; Store all prime numbers in a [ ] ; Function to count integers upto k which are having perfect squares as factors . i is index of next prime number whose square needs to be checked . curr is current number whos square to be checked . ; variable to store square of prime ; If value of greatest integer becomes zero ; Counting integers with squares as factor ; Inclusion ( Recur for next prime number ) ; Exclusion ( Recur for next prime number ) ; Final count ; Function to return nth square free number ; Computing primes and storing it in an array a [ ] ; Applying binary search ; ' c ' contains Number of square free numbers less than or equal to ' mid ' ; If c < n , then search right side of mid else search left side of mid ; nth square free number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX_PRIME = 100000 ; static int MAX_RES = 2000000000 ; static void SieveOfEratosthenes ( List < long > a ) { bool [ ] prime = new bool [ MAX_PRIME + 1 ] ; for ( int i = 0 ; i < MAX_PRIME + 1 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= MAX_PRIME ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX_PRIME ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= MAX_PRIME ; p ++ ) if ( prime [ p ] ) a . Add ( ( long ) p ) ; } static long countSquares ( long i , long cur , long k , List < long > a ) { long square = a [ ( int ) i ] * a [ ( int ) ( i ) ] ; long newCur = square * cur ; if ( newCur > k ) return 0 ; long cnt = k / ( newCur ) ; cnt += countSquares ( i + 1 , cur , k , a ) ; cnt -= countSquares ( i + 1 , newCur , k , a ) ; return cnt ; } static long squareFree ( long n ) { List < long > a = new List < long > ( ) ; SieveOfEratosthenes ( a ) ; long low = 1 ; long high = MAX_RES ; while ( low < high ) { long mid = low + ( high - low ) / 2 ; long c = mid - countSquares ( 0 , 1 , mid , a ) ; if ( c < n ) low = mid + 1 ; else high = mid ; } return low ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( squareFree ( n ) ) ; } }
Find if n can be written as product of k numbers | C # program to find if it is possible to write a number n as product of exactly k positive numbers greater than 1. ; Prints k factors of n if n can be written as multiple of k numbers . Else prints - 1. ; A vector to store all prime factors of n ; Insert all 2 's in list ; n must be odd at this point So we skip one element ( i = i + 2 ) ; This is to handle when n > 2 and n is prime ; If size ( P ) < k , k factors are not possible ; printing first k - 1 factors ; calculating and printing product of rest of numbers ; Driver code
using System ; using System . Collections ; class GFG { static void kFactors ( int n , int k ) { ArrayList P = new ArrayList ( ) ; while ( n % 2 == 0 ) { P . Add ( 2 ) ; n /= 2 ; } for ( int i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; P . Add ( i ) ; } } if ( n > 2 ) P . Add ( n ) ; if ( P . Count < k ) { Console . WriteLine ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) Console . Write ( P [ i ] + " , ▁ " ) ; int product = 1 ; for ( int i = k - 1 ; i < P . Count ; i ++ ) product = product * ( int ) P [ i ] ; Console . WriteLine ( product ) ; } static void Main ( ) { int n = 54 , k = 3 ; kFactors ( n , k ) ; } }
Largest number smaller than or equal to n and digits in non | C # program for brute force approach to find largest number having digits in non - decreasing order . ; Returns the required number ; loop to recursively check the numbers less than or equal to given number ; Keep traversing digits from right to left . For every digit check if it is smaller than prev_dig ; We found the required number ; Driver Code
using System ; class GFG { static int nondecdigits ( int n ) { int x = 0 ; for ( x = n ; x >= 1 ; x -- ) { int no = x ; int prev_dig = 11 ; bool flag = true ; while ( no != 0 ) { if ( prev_dig < no % 10 ) { flag = false ; break ; } prev_dig = no % 10 ; no /= 10 ; } if ( flag == true ) break ; } return x ; } static public void Main ( ) { int n = 200 ; Console . WriteLine ( nondecdigits ( n ) ) ; } }
Largest number smaller than or equal to n and digits in non | C # program for efficient approach to find largest number having digits in non - decreasing order . ; Prints the largest number smaller than s and digits in non - decreasing order . ; array to store digits of number ; conversion of characters of string int number ; variable holds the value of index after which all digits are set 9 ; Checking the condition if the digit is less than its left digit ; If first digit is 0 no need to print it ; Driver code
using System ; class GFG { static void nondecdigits ( string s ) { int m = s . Length ; int [ ] a = new int [ m + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) a [ i ] = ( int ) s [ i ] - ( int ) '0' ; int level = m - 1 ; for ( int i = m - 1 ; i > 0 ; i -- ) { if ( a [ i ] < a [ i - 1 ] ) { a [ i - 1 ] -- ; level = i - 1 ; } } if ( a [ 0 ] != 0 ) { for ( int i = 0 ; i <= level ; i ++ ) Console . Write ( a [ i ] ) ; for ( int i = level + 1 ; i < m ; i ++ ) Console . Write ( "9" ) ; } else { for ( int i = 1 ; i < level ; i ++ ) Console . Write ( a [ i ] ) ; for ( int i = level + 1 ; i < m ; i ++ ) Console . Write ( "9" ) ; } } static void Main ( ) { string n = "200" ; nondecdigits ( n ) ; } }
Count Divisors of n in O ( n ^ 1 / 3 ) | C # implementation of Naive method to count all divisors ; function to count the divisors ; If divisors are equal , count only one ; Otherwise count both ; Driver program
using System ; class GFG { static int countDivisors ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) cnt ++ ; else cnt = cnt + 2 ; } } return cnt ; } public static void Main ( ) { Console . WriteLine ( " Total ▁ distinct " + " ▁ divisors ▁ of ▁ 100 ▁ are ▁ : ▁ " + countDivisors ( 100 ) ) ; } }
Check if the door is open or closed | C # implementation of doors open or closed ; Function to check whether ' n ' has even number of factors or not ; if ' n ' is a perfect square it has odd number of factors ; else ' n ' has even number of factors ; Function to find and print status of each door ; If even number of factors final status is closed ; else odd number of factors final status is open ; Driver Code
using System ; class GFG { static bool hasEvenNumberOfFactors ( int n ) { double root_n = Math . Sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } static void printStatusOfDoors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) Console . Write ( " closed " + " ▁ " ) ; else Console . Write ( " open " + " ▁ " ) ; } } static public void Main ( ) { int n = 5 ; printStatusOfDoors ( n ) ; } }
Check if frequency of each digit is less than the digit | C # program to validate a number ; Function to validate number ( Check iffrequency of a digit is less than thedigit itself or not ) ; If current digit of temp is same as i ; if frequency is greater than digit value , return false ; Driver Code
using System ; public class GFG { static bool validate ( long n ) { for ( int i = 0 ; i < 10 ; i ++ ) { long temp = n ; int count = 0 ; while ( temp > 0 ) { if ( temp % 10 == i ) count ++ ; if ( count > i ) return false ; temp /= 10 ; } } return true ; } static public void Main ( String [ ] args ) { long n = 1552793 ; if ( validate ( n ) ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }
Check if a larger number divisible by 36 | C # program to find if a number is divisible by 36 or not ; Function to check whether a number is divisible by 36 or not ; null number cannot be divisible by 36 ; single digit number other than 0 is not divisible by 36 ; number formed by the last 2 digits ; if number is not divisible by 4 ; number is divisible by 4 calculate sum of digits ; sum of digits is not divisible by 9 ; number is divisible by 4 and 9 hence , number is divisible by 36 ; main function
using System ; class GFG { static bool divisibleBy36 ( String num ) { int l = num . Length ; if ( l == 0 ) return false ; if ( l == 1 && num [ 0 ] != '0' ) return false ; int two_digit_num = ( num [ l - 2 ] - '0' ) * 10 + ( num [ l - 1 ] - '0' ) ; if ( two_digit_num % 4 != 0 ) return false ; int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) sum += ( num [ i ] - '0' ) ; if ( sum % 9 != 0 ) return false ; return true ; } public static void Main ( ) { String num = "92567812197966231384" ; if ( divisibleBy36 ( num ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if a large number is divisible by 8 or not | C # program to find if a number is divisible by 8 or not ; Function to find that number divisible by 8 or not ; Empty string ; If there is single digit ; If there is double digit ; If number formed by last three digits is divisible by 8 ; Driver Code
using System ; class IsDivisible { static bool check ( String str ) { int n = str . Length ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str [ 0 ] - '0' ) % 8 == 0 ) ; if ( n == 2 ) return ( ( ( str [ n - 2 ] - '0' ) * 10 + ( str [ n - 1 ] - '0' ) ) % 8 == 0 ) ; int last = str [ n - 1 ] - '0' ; int second_last = str [ n - 2 ] - '0' ; int third_last = str [ n - 3 ] - '0' ; return ( ( third_last * 100 + second_last * 10 + last ) % 8 == 0 ) ; } public static void Main ( ) { String str = "76952" ; if ( check ( str ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Prime points ( Points that split a number into two primes ) | C # program to print all prime points ; Function to count number of digits ; Function to check whether a number is prime or not . Returns 0 if prime else - 1 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to print prime points ; counting digits ; As single and double digit numbers do not have left and right number pairs ; Finding all left and right pairs . Printing the prime points accordingly . Discarding first and last index point ; Calculating left number ; Calculating right number ; Prime point condition ; No prime point found ; Driver Code
using System ; class GFG { static int countDigits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n / 10 ; } return count ; } static int checkPrime ( int n ) { if ( n <= 1 ) return - 1 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 n % 3 == 0 ) return - 1 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return - 1 ; return 0 ; } static void printPrimePoints ( int n ) { int count = countDigits ( n ) ; if ( count == 1 count == 2 ) { Console . Write ( " - 1" ) ; return ; } bool found = false ; for ( int i = 1 ; i < ( count - 1 ) ; i ++ ) { int left = n / ( ( int ) Math . Pow ( 10 , count - i ) ) ; int right = n % ( ( int ) Math . Pow ( 10 , count - i - 1 ) ) ; if ( checkPrime ( left ) == 0 && checkPrime ( right ) == 0 ) { Console . Write ( i + " ▁ " ) ; found = true ; } } if ( found == false ) Console . Write ( " - 1" ) ; } static public void Main ( ) { int n = 2317 ; printPrimePoints ( n ) ; } }
Find ways an Integer can be expressed as sum of n | C # program to count number of ways any given integer x can be expressed as n - th power of unique natural numbers . ; Function to calculate and return the power of any given number ; Function to check power representations recursively ; Initialize number of ways to express x as n - th powers of different natural numbers ; Calling power of ' i ' raised to ' n ' ; Recursively check all greater values of i ; If sum of powers is equal to x then increase the value of result . ; Return the final result ; Driver Code .
using System ; class GFG { static int power ( int num , int n ) { if ( n == 0 ) return 1 ; else if ( n % 2 == 0 ) return power ( num , n / 2 ) * power ( num , n / 2 ) ; else return num * power ( num , n / 2 ) * power ( num , n / 2 ) ; } static int checkRecursive ( int x , int n , int curr_num , int curr_sum ) { int results = 0 ; int p = power ( curr_num , n ) ; while ( p + curr_sum < x ) { results += checkRecursive ( x , n , curr_num + 1 , p + curr_sum ) ; curr_num ++ ; p = power ( curr_num , n ) ; } if ( p + curr_sum == x ) results ++ ; return results ; } public static void Main ( ) { int x = 10 , n = 2 ; System . Console . WriteLine ( checkRecursive ( x , n , 1 , 0 ) ) ; } }
Number Theory | Generators of finite cyclic group under addition | A simple C # program to find all generators ; Function to return gcd of a and b ; Print generators of n ; 1 is always a generator ; A number x is generator of GCD is 1 ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void printGenerators ( int n ) { Console . Write ( "1 ▁ " ) ; for ( int i = 2 ; i < n ; i ++ ) if ( gcd ( i , n ) == 1 ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 10 ; printGenerators ( n ) ; } }
Check if a large number is divisible by 3 or not | C # program to find if a number is divisible by 3 or not ; Function to find that number divisible by 3 or not ; Compute sum of digits ; Check if sum of digits is divisible by 3. ; main function
using System ; class GFG { static bool check ( string str ) { int n = str . Length ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str [ i ] - '0' ) ; return ( digitSum % 3 == 0 ) ; } public static void Main ( ) { string str = "1332" ; if ( check ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count all perfect divisors of a number | C # code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code
using System ; class GFG { static int MAX = 100001 ; static int [ ] perfectDiv = new int [ MAX ] ; static void precomputeCounts ( ) { for ( int i = 1 ; i * i < MAX ; ++ i ) { for ( int j = i * i ; j < MAX ; j += i * i ) ++ perfectDiv [ j ] ; } } static int countPerfectDivisors ( int n ) { return perfectDiv [ n ] ; } public static void Main ( ) { precomputeCounts ( ) ; int n = 16 ; Console . WriteLine ( " Total ▁ perfect ▁ divisors ▁ of ▁ " + n + " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; n = 12 ; Console . WriteLine ( " Total ▁ perfect ▁ divisors ▁ of ▁ " + n + " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; } }
Prime Factorization using Sieve O ( log n ) for multiple queries | C # program to find prime factorization of a number n in O ( Log n ) time with precomputation allowed . ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; Driver code ; precalculating Smallest Prime Factor ; calling getFactorization function
using System ; using System . Collections ; class GFG { static int MAXN = 100001 ; static int [ ] spf = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static ArrayList getFactorization ( int x ) { ArrayList ret = new ArrayList ( ) ; while ( x != 1 ) { ret . Add ( spf [ x ] ) ; x = x / spf [ x ] ; } return ret ; } public static void Main ( ) { sieve ( ) ; int x = 12246 ; Console . Write ( " prime ▁ factorization ▁ for ▁ " + x + " ▁ : ▁ " ) ; ArrayList p = getFactorization ( x ) ; for ( int i = 0 ; i < p . Count ; i ++ ) Console . Write ( p [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } }
Nearest element with at | C # program to print nearest element with at least one common prime factor . ; Pre - computation of smallest prime divisor of all numbers ; Prime number will have same divisor ; Function to calculate all divisors of input array ; Pre - compute all the divisors of array element by using prime factors ; Traverse all elements , ; For every divisor of current element , find closest element . ; Visit divisor if not visited ; Fetch the index of visited divisor ; Update the divisor index to current index ; Set the minimum distance ; Set the min distance of current index ' i ' to nearest one ; Add 1 as indexing starts from 0 ; Set the min distance of found index ' ind ' ; Add 1 as indexing starts from 0 ; Driver code ; Simple sieve to find smallest prime divisor of number from 2 to MAX ; function to calculate nearest distance of every array elements ; Print the nearest distance having GDC > 1
using System ; using System . Collections . Generic ; public class GFG { static int MAX = 100001 ; static int INF = Int32 . MaxValue ; static int [ ] primedivisor = new int [ MAX ] ; static int [ ] dist = new int [ MAX ] ; static int [ ] pos = new int [ MAX ] ; static int [ ] divInd = new int [ MAX ] ; static List < List < int > > divisors = new List < List < int > > ( ) ; static void sieveOfEratosthenes ( ) { for ( int i = 2 ; i * i < MAX ; ++ i ) { if ( primedivisor [ i ] == 0 ) { for ( int j = i * i ; j < MAX ; j += i ) { primedivisor [ j ] = i ; } } } for ( int i = 1 ; i < MAX ; ++ i ) { if ( primedivisor [ i ] == 0 ) { primedivisor [ i ] = i ; } } } static void findDivisors ( int [ ] arr , int n ) { for ( int i = 0 ; i < MAX ; ++ i ) { pos [ i ] = divInd [ i ] = - 1 ; dist [ i ] = INF ; } for ( int i = 0 ; i < n ; ++ i ) { int num = arr [ i ] ; while ( num > 1 ) { int div = primedivisor [ num ] ; divisors [ i ] . Add ( div ) ; while ( num % div == 0 ) { num /= div ; } } } } static void nearestGCD ( int [ ] arr , int n ) { findDivisors ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { foreach ( int div in divisors [ i ] ) { if ( divInd [ div ] == - 1 ) { divInd [ div ] = i ; } else { int ind = divInd [ div ] ; divInd [ div ] = i ; if ( dist [ i ] > Math . Abs ( ind - i ) ) { dist [ i ] = Math . Abs ( ind - i ) ; pos [ i ] = ind + 1 ; } if ( dist [ ind ] > Math . Abs ( ind - i ) ) { dist [ ind ] = Math . Abs ( ind - i ) ; pos [ ind ] = i + 1 ; } } } } } static public void Main ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { divisors . Add ( new List < int > ( ) ) ; } sieveOfEratosthenes ( ) ; int [ ] arr = { 2 , 9 , 4 , 3 , 13 } ; int n = arr . Length ; nearestGCD ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( pos [ i ] + " ▁ " ) ; } } }
Largest subsequence having GCD greater than 1 | Efficient C # program to find length of the largest subsequence with GCD greater than 1. ; Returns length of the largest subsequence with GCD more than 1. ; Finding the Maximum value in arr [ ] ; Iterate from 2 to maximum possible divisor of all give values ; If we found divisor , increment count ; Driver program to test above
using System ; using System . Linq ; public class GFG { static int largestGCDSubsequence ( int [ ] arr , int n ) { int ans = 0 ; int maxele = arr . Max ( ) ; for ( int i = 2 ; i <= maxele ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( arr [ j ] % i == 0 ) ++ count ; } ans = Math . Max ( ans , count ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 2 , 5 , 4 } ; int size = arr . Length ; Console . Write ( largestGCDSubsequence ( arr , size ) ) ; } }
Minimum step to reach one | C # program to get minimum step to reach 1 under given constraints ; structure represent one node in queue ; method returns minimum step to reach one ; set is used to visit numbers so that they won 't be pushed in queue again ; loop until we reach to 1 ; if current data value is 1 , return its steps from N ; check curr - 1 , only if it not visited yet ; loop from 2 to Math . Sqrt ( value ) for its divisors ; check divisor , only if it is not visited yet if i is divisor of val , then val / i will be its bigger divisor ; Driver code
using System ; using System . Collections . Generic ; class GFG { class data { public int val ; public int steps ; public data ( int val , int steps ) { this . val = val ; this . steps = steps ; } } ; static int minStepToReachOne ( int N ) { Queue < data > q = new Queue < data > ( ) ; q . Enqueue ( new data ( N , 0 ) ) ; HashSet < int > st = new HashSet < int > ( ) ; while ( q . Count != 0 ) { data t = q . Peek ( ) ; q . Dequeue ( ) ; if ( t . val == 1 ) return t . steps ; if ( ! st . Contains ( t . val - 1 ) ) { q . Enqueue ( new data ( t . val - 1 , t . steps + 1 ) ) ; st . Add ( t . val - 1 ) ; } for ( int i = 2 ; i * i <= t . val ; i ++ ) { if ( t . val % i == 0 && ! st . Contains ( t . val / i ) ) { q . Enqueue ( new data ( t . val / i , t . steps + 1 ) ) ; st . Add ( t . val / i ) ; } } } return - 1 ; } public static void Main ( String [ ] args ) { int N = 17 ; Console . Write ( minStepToReachOne ( N ) + " STRNEWLINE " ) ; } }
Queries on the sum of prime factor counts in a range | C # program to find sum prime factors in given range . ; using sieve method to evaluating the prime factor of numbers ; if i is prime ; setting number of prime factor of a prime number . ; Returns sum of counts of prime factors in range from l to r . This function mainly uses count [ ] which is filled by Sieve ( ) ; finding the sum of number of prime factor of numbers in a range . ; Driver code
using System ; class GFG { static int MAX = 1000006 ; static void sieve ( int [ ] count ) { for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( count [ i ] == 0 ) { for ( int j = 2 * i ; j < MAX ; j += i ) count [ j ] ++ ; count [ i ] = 1 ; } } } static int query ( int [ ] count , int l , int r ) { int sum = 0 ; for ( int i = l ; i <= r ; i ++ ) sum += count [ i ] ; return sum ; } public static void Main ( ) { int [ ] count = new int [ MAX ] ; sieve ( count ) ; Console . Write ( query ( count , 6 , 10 ) + " ▁ " + query ( count , 1 , 5 ) ) ; } }