codeparrot/xlcost-text-to-code huggingface dataset:
Print all numbers less than N with at | Java program to print all the numbers less than N which have at most 2 unique digits ; Function to generate all possible numbers ; If the number is less than n ; If the number exceeds ; Check if it is not the same number ; Function to print all numbers ; All combination of digits ; Print all numbers ; Driver code
import java . util . * ; class GFG { static Set < Integer > st = new HashSet < Integer > ( ) ; static void generateNumbers ( int n , int num , int a , int b ) { if ( num > 0 && num < n ) st . add ( num ) ; if ( num >= n ) return ; if ( num * 10 + a > num ) generateNumbers ( n , num * 10 + a , a , b ) ; generateNumbers ( n , num * 10 + b , a , b ) ; } static void printNumbers ( int n ) { for ( int i = 0 ; i <= 9 ; i ++ ) for ( int j = i + 1 ; j <= 9 ; j ++ ) generateNumbers ( n , 0 , i , j ) ; System . out . print ( " The ▁ numbers ▁ are : ▁ " ) ; System . out . print ( st ) ; st . clear ( ) ; } public static void main ( String args [ ] ) { int n = 12 ; printNumbers ( n ) ; } }
Smallest prime number missing in an array | Java implementation of the above approach ; this store all prime number upto 10 ^ 5 this function find all prime ; use sieve to find prime ; if number is prime then store it in prime vector ; Function to find the smallest prime missing ; first of all find all prime ; now store all number as index of freq arr so that we can improve searching ; now check for each prime ; Driver code ; find smallest prime which is not present
import java . util . * ; class GFG { static Vector < Integer > findPrime ( int MAX ) { boolean pm [ ] = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i < pm . length ; i ++ ) pm [ i ] = true ; pm [ 0 ] = pm [ 1 ] = false ; for ( int i = 2 ; i <= MAX ; i ++ ) if ( pm [ i ] ) for ( int j = 2 * i ; j <= MAX ; j += i ) pm [ j ] = false ; Vector < Integer > prime = new Vector < Integer > ( ) ; for ( int i = 0 ; i <= MAX ; i ++ ) if ( pm [ i ] ) prime . add ( i ) ; return prime ; } static int max_element ( int arr [ ] ) { int max = arr [ 0 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) max = Math . max ( max , arr [ i ] ) ; return max ; } static int findSmallest ( int arr [ ] , int n ) { int MAX = max_element ( arr ) ; Vector < Integer > prime = findPrime ( MAX ) ; Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) s . add ( arr [ i ] ) ; long ans = - 1 ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) { if ( ! s . contains ( prime . get ( i ) ) ) { ans = ( prime . get ( i ) ) ; break ; } } return ( int ) ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 0 , 1 , 2 , 7 } ; int n = arr . length ; if ( findSmallest ( arr , n ) == - 1 ) System . out . print ( " No ▁ prime ▁ number ▁ missing " ) ; else System . out . print ( findSmallest ( arr , n ) ) ; } }
Find the number after successive division | Java implementation of the above approach ; Function to find the number ; Driver Code
public class GFG { static int findNum ( int div [ ] , int rem [ ] , int N ) { int num = rem [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { num = num * div [ i ] + rem [ i ] ; } return num ; } public static void main ( String [ ] args ) { int div [ ] = { 8 , 3 } ; int rem [ ] = { 2 , 2 } ; int N = div . length ; System . out . println ( findNum ( div , rem , N ) ) ; } }
Program to find the profit or loss when CP of N items is equal to SP of M items | Java implementation of above formula ; Function to calculate Profit or loss ; Driver Code
public class GFG { static void profitLoss ( int N , int M ) { if ( N == M ) System . out . print ( " No ▁ Profit ▁ nor ▁ Loss " ) ; else { float result = 0 ; result = ( float ) ( Math . abs ( N - M ) ) / M ; if ( N - M < 0 ) System . out . print ( " Loss ▁ = ▁ - " + result * 100 + " % " ) ; else System . out . print ( " Profit ▁ = ▁ " + result * 100 + " % " ) ; } } public static void main ( String [ ] args ) { int N = 8 , M = 9 ; profitLoss ( N , M ) ; } }
Aronson 's Sequence | Java program to generate the first n terms of Aronson 's sequence ; Returns the given number in words ; Get number of digits in given number ; Base cases ; The following arrays contain one digit ( both cardinal and ordinal forms ) , two digit ( < 20 , ordinal forms ) numbers , and multiples ( ordinal forms ) and powers of 10. ; If single digit number ; here len can be 3 or 4 ; last two digits ; Handle all powers of 10 ; Handle two digit numbers < 20 ; Function to print the first n terms of Aronson 's sequence ; check if character is alphabet or not ; convert number to words in ordinal format and append ; Driver code
import java . util . * ; class GFG { static String convert_to_words ( char [ ] num ) { int len = num . length ; if ( len == 0 len > 4 ) { return " " ; } String single_digits_temp [ ] = { " " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; String single_digits [ ] = { " " , " first " , " second " , " third " , " fourth " , " fifth " , " sixth " , " seventh " , " eighth " , " ninth " } ; String two_digits [ ] = { " " , " tenth " , " eleventh " , " twelfth " , " thirteenth " , " fourteenth " , " fifteenth " , " sixteenth " , " seventeenth " , " eighteenth " , " nineteenth " } ; String tens_multiple [ ] = { " " , " tenth " , " twentieth " , " thirtieth " , " fortieth " , " fiftieth " , " sixtieth " , " seventieth " , " eightieth " , " ninetieth " } ; String tens_power [ ] = { " hundred " , " thousand " } ; String word = " " ; if ( len == 1 ) { word += single_digits [ num [ 0 ] - '0' ] ; return word ; } int i = 0 , ctr = 0 ; String s = " ▁ " ; while ( i < len ) { if ( len >= 3 ) { if ( num [ i ] != '0' ) { word += single_digits_temp [ num [ i ] - '0' ] + " ▁ " ; word += tens_power [ len - 3 ] + " ▁ " ; ctr ++ ; } len -- ; num = Arrays . copyOfRange ( num , 1 , num . length ) ; } else { if ( ctr != 0 ) { s = " ▁ and ▁ " ; word = word . substring ( 0 , word . length ( ) - 1 ) ; } if ( num [ i + 1 ] == '0' ) if ( num [ i ] == '0' ) word = word + " th " ; else word += s + tens_multiple [ num [ i ] - '0' ] ; else if ( num [ i ] == '1' ) word += s + two_digits [ num [ i + 1 ] - '0' + 1 ] ; else { if ( num [ i ] != '0' ) word += s + tens_multiple [ num [ i ] - '0' ] . substring ( 0 , tens_multiple [ num [ i ] - '0' ] . length ( ) - 4 ) + " y ▁ " ; else word += s ; word += single_digits [ num [ i + 1 ] - '0' ] ; } i += 2 ; } if ( i == len ) { if ( word . charAt ( 0 ) == ' ▁ ' ) word = word . substring ( 1 , word . length ( ) ) ; } } return word ; } static void Aronsons_sequence ( int n ) { String str = " T ▁ is ▁ the ▁ " ; int ind = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isAlphabetic ( str . charAt ( i ) ) ) { ind += 1 ; } if ( str . charAt ( i ) == ' t ' || str . charAt ( i ) == ' T ' ) { n -= 1 ; str += convert_to_words ( String . valueOf ( ind ) . toCharArray ( ) ) + " , ▁ " ; System . out . print ( ind + " , ▁ " ) ; } if ( n == 0 ) break ; } } public static void main ( String [ ] args ) { int n = 6 ; Aronsons_sequence ( n ) ; } }
Find all good indices in the given Array | Java program to find all good indices in the given array ; Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; Driver Code
import java . util . * ; class Solution { static void niceIndices ( int A [ ] , int n ) { int sum = 0 ; Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { m . put ( A [ i ] , ( m . get ( A [ i ] ) == null ) ? 0 : m . get ( A [ i ] ) + 1 ) ; sum += A [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( m . containsKey ( k ) ) { if ( ( A [ i ] == k && m . get ( k ) > 1 ) || ( A [ i ] != k ) ) System . out . print ( ( i + 1 ) + " ▁ " ) ; } } } } public static void main ( String args [ ] ) { int A [ ] = { 8 , 3 , 5 , 2 } ; int n = A . length ; niceIndices ( A , n ) ; } }
Count pieces of circle after N cuts | Java program to find number of pieces of circle after N cuts ; Function to find number of pieces of circle after N cuts ; Driver program
import java . util . * ; class solution { static int countPieces ( int N ) { return 2 * N ; } public static void main ( String args [ ] ) { int N = 100 ; System . out . println ( countPieces ( N ) ) ; } }
Sum of all the multiples of 3 and 7 below N | Java program to find the sum of all multiples of 3 and 7 below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code
import java . util . * ; class solution { static long sumAP ( long n , long d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } static long sumMultiples ( long n ) { n -- ; return sumAP ( n , 3 ) + sumAP ( n , 7 ) - sumAP ( n , 21 ) ; } public static void main ( String args [ ] ) { long n = 24 ; System . out . println ( sumMultiples ( n ) ) ; } }
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | JAVA implementation of the above approach ; below function checks whether product of digits at even places is divisible by sum of digits at odd places ; if size is even ; if size is odd ; Driver code
class GFG { static boolean productSumDivisible ( int n , int size ) { int sum = 0 , product = 1 ; while ( n > 0 ) { if ( size % 2 == 0 ) { product *= n % 10 ; } else { sum += n % 10 ; } n = n / 10 ; size -- ; } if ( product % sum == 0 ) { return true ; } return false ; } public static void main ( String [ ] args ) { int n = 1234 ; int len = 4 ; if ( productSumDivisible ( n , len ) ) { System . out . println ( " TRUE " ) ; } else { System . out . println ( " FALSE " ) ; } } }
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Java implementation of the above approach ; Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; Converting integer to string ; Traveersing the string ; Driver code
import java . io . * ; class GFG { static boolean productSumDivisible ( int n ) { int sum = 0 ; int product = 1 ; String num = String . valueOf ( n ) ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { if ( i % 2 != 0 ) { product = product * Character . getNumericValue ( num . charAt ( i ) ) ; } else { sum = sum + Character . getNumericValue ( num . charAt ( i ) ) ; } } if ( product % sum == 0 ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 1234 ; if ( productSumDivisible ( n ) ) { System . out . println ( " TRUE " ) ; } else { System . out . println ( " FALSE " ) ; } } }
GCD of a number raised to some power and another number | Java program to find GCD of a ^ n and b . ; Returns GCD of a ^ n and b . ; Driver code
import java . io . * ; class GFG { static long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static long powGCD ( long a , long n , long b ) { for ( int i = 0 ; i < n ; i ++ ) a = a * a ; return gcd ( a , b ) ; } public static void main ( String [ ] args ) { long a = 10 , b = 5 , n = 2 ; System . out . println ( powGCD ( a , n , b ) ) ; } }
Position after taking N steps to the right and left in an alternate manner | Java program to find the last coordinate where it ends his journey ; Function to return the last destination ; Driver Code
import java . util . * ; class solution { static int lastCoordinate ( int n , int a , int b ) { return ( ( n + 1 ) / 2 ) * a - ( n / 2 ) * b ; } public static void main ( String args [ ] ) { int n = 3 , a = 5 , b = 2 ; System . out . println ( lastCoordinate ( n , a , b ) ) ; } }
Smallest number greater than or equal to N divisible by K | Java implementation of the above approach ; Function to find the smallest number greater than or equal to N that is divisible by k ; Driver Code
public class GFG { static int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } public static void main ( String [ ] args ) { int N = 45 , K = 6 ; System . out . println ( " Smallest ▁ number ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ " + N + " that is divisible by " ▁ + ▁ K ▁ + ▁ " is " + findNum(N, K)); } }
Sum and Product of digits in a number that divide the number | Java implementation of the above approach ; Print the sum and product of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static void countDigit ( int n ) { int temp = n , sum = 0 , product = 1 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d > 0 && n % d == 0 ) { sum += d ; product *= d ; } } System . out . print ( " Sum ▁ = ▁ " + sum ) ; System . out . print ( " Product = " + product); } public static void main ( String args [ ] ) { int n = 1012 ; countDigit ( n ) ; } }
Largest number smaller than or equal to N divisible by K | Java implementation of the above approach ; Function to find the largest number smaller than or equal to N that is divisible by k ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static int findNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } public static void main ( String args [ ] ) { int N = 45 , K = 6 ; System . out . print ( " Largest ▁ number ▁ smaller ▁ " + " than ▁ or ▁ equal ▁ to ▁ " + N + " that is divisible by " + K + " ▁ is ▁ " + findNum ( N , K ) ) ; } }
Check if any permutation of a number is divisible by 3 and is Palindromic | Java implementation of the above approach ; Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check if number is not divisible by 3 ; If more than one digits have odd frequency , palindromic permutation not possible ; Driver Code
public class GFG { static boolean isDivisiblePalindrome ( int n ) { int hash [ ] = new int [ 10 ] ; int digitSum = 0 ; while ( n != 0 ) { digitSum += n % 10 ; hash [ n % 10 ] ++ ; n /= 10 ; } if ( digitSum % 3 != 0 ) return false ; int oddCount = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( hash [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount > 1 ) return false ; else return true ; } public static void main ( String [ ] args ) { int n = 34734 ; System . out . print ( isDivisiblePalindrome ( n ) ) ; } }
Minimum time to return array to its original state after given modifications | Java implementation of above approach ; Function to return lcm of two numbers ; Make a graph ; Add edge ; Count reachable nodes from every node . ; Driver code
import java . util . * ; class GFG { static int lcm ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int dfs ( int src , Vector < Integer > adj [ ] , boolean [ ] visited ) { visited [ src ] = true ; int count = 1 ; for ( int i = 0 ; i < adj [ src ] . size ( ) ; i ++ ) if ( ! visited [ adj [ src ] . get ( i ) ] ) count += dfs ( adj [ src ] . get ( i ) , adj , visited ) ; return count ; } static int findMinTime ( int arr [ ] , int P [ ] , int n ) { Vector < Integer > [ ] adj = new Vector [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { adj [ arr [ i ] ] . add ( P [ i ] ) ; } boolean [ ] visited = new boolean [ n + 1 ] ; int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { ans = lcm ( ans , dfs ( i , adj , visited ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int P [ ] = { 3 , 2 , 1 } ; int n = arr . length ; System . out . print ( findMinTime ( arr , P , n ) ) ; } }
Check whether product of digits at even places of a number is divisible by K | JAVA implementation of the above approach ; below function checks whether product of digits at even places is divisible by K ; if position is even ; Driver code
class GFG { static boolean productDivisible ( int n , int k ) { int product = 1 , position = 1 ; while ( n > 0 ) { if ( position % 2 == 0 ) { product *= n % 10 ; } n = n / 10 ; position ++ ; } if ( product % k == 0 ) { return true ; } return false ; } public static void main ( String [ ] args ) { int n = 321922 ; int k = 3 ; if ( productDivisible ( n , k ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Permutations of n things taken r at a time with k things together | Java program to find the number of permutations of n different things taken r at a time with k things grouped together ; Function to find factorial of a number ; Function to calculate p ( n , r ) ; Function to find the number of permutations of n different things taken r at a time with k things grouped together ; Driver code
class GFG { static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int npr ( int n , int r ) { int pnr = factorial ( n ) / factorial ( n - r ) ; return pnr ; } static int countPermutations ( int n , int r , int k ) { return factorial ( k ) * ( r - k + 1 ) * npr ( n - k , r - k ) ; } public static void main ( String [ ] args ) { int n = 8 ; int r = 5 ; int k = 2 ; System . out . println ( countPermutations ( n , r , k ) ) ; } }
Greatest Integer Function | Java program to illustrate greatest integer Function ; Function to calculate the GIF value of a number ; GIF is the floor of a number ; Driver code
class GFG { static int GIF ( double n ) { return ( int ) Math . floor ( n ) ; } public static void main ( String [ ] args ) { double n = 2.3 ; System . out . println ( GIF ( n ) ) ; } }
Number of triangles formed from a set of points on three lines | Java program to find the possible number of triangles that can be formed from set of points on three lines ; Returns factorial of a number ; calculate c ( n , r ) ; Driver code
import java . io . * ; class GFG { static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int ncr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } public static void main ( String [ ] args ) { int m = 3 , n = 4 , k = 5 ; int totalTriangles = ncr ( m + n + k , 3 ) - ncr ( m , 3 ) - ncr ( n , 3 ) - ncr ( k , 3 ) ; System . out . println ( totalTriangles ) ; } }
Check whether sum of digits at odd places of a number is divisible by K | Java implementation of the approach ; function that checks the divisibility of the sum of the digits at odd places of the given number ; if position is odd ; Driver code
import java . util . * ; class solution { static boolean SumDivisible ( int n , int k ) { int sum = 0 , position = 1 ; while ( n > 0 ) { if ( position % 2 == 1 ) sum += n % 10 ; n = n / 10 ; position ++ ; } if ( sum % k == 0 ) return true ; return false ; } public static void main ( String arr [ ] ) { int n = 592452 ; int k = 3 ; if ( SumDivisible ( n , k ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Check whether sum of digits at odd places of a number is divisible by K | Java implementation of the above approach ; Converting integer to string ; Traversing the string ; Driver code
import java . io . * ; class GFG { static boolean sumDivisible ( int n , int k ) { int sum = 0 ; String num = Integer . toString ( n ) ; int i ; for ( i = 0 ; i < num . length ( ) ; i ++ ) { if ( i % 2 != 0 ) { sum = sum + ( num . charAt ( i ) - '0' ) ; } } if ( sum % k == 0 ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 592452 ; int k = 3 ; if ( sumDivisible ( n , k ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Check if a triangle of positive area is possible with the given angles | Java program to check if a triangle of positive area is possible with the given angles ; Checking if the sum of three angles is 180 and none of the angles is zero ; Checking if sum of any two angles is greater than equal to the third one ; Driver Code
class GFG { static String isTriangleExists ( int a , int b , int c ) { if ( a != 0 && b != 0 && c != 0 && ( a + b + c ) == 180 ) if ( ( a + b ) >= c || ( b + c ) >= a || ( a + c ) >= b ) return " YES " ; else return " NO " ; else return " NO " ; } public static void main ( String [ ] args ) { int a = 50 , b = 60 , c = 70 ; System . out . println ( isTriangleExists ( a , b , c ) ) ; } }
Find maximum value of x such that n ! % ( k ^ x ) = 0 | Java program to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code
import java . util . * ; public class GfG { private static int findX ( int n , int k ) { int r = n , v , u ; int m = ( int ) Math . sqrt ( k ) + 1 ; for ( int i = 2 ; i <= m && k > 1 ; i ++ ) { if ( i == m ) { i = k ; } for ( u = v = 0 ; k % i == 0 ; v ++ ) { k /= i ; } if ( v > 0 ) { int t = n ; while ( t > 0 ) { t /= i ; u += t ; } r = Math . min ( r , u / v ) ; } } return r ; } public static void main ( String args [ ] ) { int n = 5 ; int k = 2 ; System . out . println ( findX ( n , k ) ) ; } }
Ways of selecting men and women from a group to make a team | Java implementation of the approach ; Returns factorial of the number ; Function to calculate ncr ; Function to calculate the total possible ways ; Driver code
import java . io . * ; class GFG { static int fact ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact *= i ; return fact ; } static int ncr ( int n , int r ) { int ncr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; return ncr ; } static int ways ( int m , int w , int n , int k ) { int ans = 0 ; while ( m >= k ) { ans += ncr ( m , k ) * ncr ( w , n - k ) ; k += 1 ; } return ans ; } public static void main ( String [ ] args ) { int m , w , n , k ; m = 7 ; w = 6 ; n = 5 ; k = 3 ; System . out . println ( ways ( m , w , n , k ) ) ; } }
Product of every K ’ th prime number in an array | Java implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; product of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; Driver code ; create the sieve
class GFG { static int MAX = 1000000 ; static boolean [ ] prime = new boolean [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { prime [ 1 ] = true ; prime [ 0 ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = true ; } } } static void productOfKthPrimes ( int arr [ ] , int n , int k ) { int c = 0 ; int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { product *= arr [ i ] ; c = 0 ; } } } System . out . println ( product ) ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 5 , k = 2 ; int [ ] arr = new int [ ] { 2 , 3 , 5 , 7 , 11 } ; productOfKthPrimes ( arr , n , k ) ; } }
Sum of greatest odd divisor of numbers in given range | Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; Odd n ; Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code
class gfg { static int square ( int n ) { return n * n ; } static int sum ( int n ) { if ( n == 0 ) return 0 ; if ( n % 2 == 1 ) { return square ( ( n + 1 ) / 2 ) + sum ( n / 2 ) ; } else { return square ( n / 2 ) + sum ( n / 2 ) ; } } static int oddDivSum ( int a , int b ) { return sum ( b ) - sum ( a - 1 ) ; } public static void main ( String [ ] args ) { int a = 3 , b = 9 ; System . out . println ( oddDivSum ( a , b ) ) ; } }