codeparrot/xlcost-text-to-code huggingface dataset:
Check whether a number has exactly three distinct factors or not | Java program to check whether number has exactly three distinct factors or not ; Utility function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check whether given number has three distinct factors or not ; Find square root of number ; Check whether number is perfect square or not ; If number is perfect square , check whether square root is prime or not ; Driver program
public class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isThreeDisctFactors ( long n ) { int sq = ( int ) Math . sqrt ( n ) ; if ( 1L * sq * sq != n ) return false ; return isPrime ( sq ) ? true : false ; } public static void main ( String [ ] args ) { long num = 9 ; if ( isThreeDisctFactors ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; num = 15 ; if ( isThreeDisctFactors ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; num = 12397923568441L ; if ( isThreeDisctFactors ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the last digit when factorial of A divides factorial of B | Java program to find last digit of a number obtained by dividing factorial of a number with factorial of another number . ; Function which computes the last digit of resultant of B ! / A ! ; if ( A == B ) If A = B , B ! = A ! and B ! / A ! = 1 ; If difference ( B - A ) >= 5 , answer = 0 ; If non of the conditions are true , we iterate from A + 1 to B and multiply them . We are only concerned for the last digit , thus we take modulus of 10 ; driver function
import java . io . * ; class GFG { static int computeLastDigit ( long A , long B ) { int variable = 1 ; return 1 ; else if ( ( B - A ) >= 5 ) return 0 ; else { for ( long i = A + 1 ; i <= B ; i ++ ) variable = ( int ) ( variable * ( i % 10 ) ) % 10 ; return variable % 10 ; } } public static void main ( String [ ] args ) { System . out . println ( computeLastDigit ( 2632 , 2634 ) ) ; } }
Program for sum of arithmetic series | JAVA Program to find the sum of arithmetic series . ; Function to find sum of series . ; Driver function
class GFG { static float sumOfAP ( float a , float d , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + a ; a = a + d ; } return sum ; } public static void main ( String args [ ] ) { int n = 20 ; float a = 2.5f , d = 1.5f ; System . out . println ( sumOfAP ( a , d , n ) ) ; } }
Product of factors of number | Java program to calculate product of factors of number ; function to product the factors ; If factors are equal , multiply only once ; Otherwise multiply both ; Driver Code
class GFG { public static final long M = 1000000007 ; static long multiplyFactors ( int n ) { long prod = 1 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) prod = ( prod * i ) % M ; else { prod = ( prod * i ) % M ; prod = ( prod * n / i ) % M ; } } } return prod ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( multiplyFactors ( n ) ) ; } }
Product of factors of number | Java program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver Code
class GFG { public static final long M = 1000000007 ; static long power ( long x , long y ) { long res = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % M ; y = ( y >> 1 ) % M ; x = ( x * x ) % M ; } return res ; } static int countFactors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count ++ ; else count += 2 ; } } return count ; } static long multiplyFactors ( int n ) { int numFactor = countFactors ( n ) ; long product = power ( n , numFactor / 2 ) ; if ( numFactor % 2 == 1 ) product = ( product * ( int ) Math . sqrt ( n ) ) % M ; return product ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( multiplyFactors ( n ) ) ; } }
Decimal representation of given binary string is divisible by 10 or not | Java implementation to check whether decimal representation of given binary number is divisible by 10 or not ; function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position  from the right ; according to the digit 's  position, obtain the last  digit of the applicable  perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; Driver program to test above function
import java . util . * ; class GFG { static boolean isDivisibleBy10 ( String bin ) { int n = bin . length ( ) ; if ( bin . charAt ( n - 1 ) == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin . charAt ( i ) == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String bin = "11000111001110" ; if ( isDivisibleBy10 ( bin ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Tribonacci Numbers | A space optimized based Java program to print first n Tribinocci numbers . ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code
import java . io . * ; class GFG { static void printTrib ( int n ) { if ( n < 1 ) return ; int first = 0 , second = 0 ; int third = 1 ; System . out . print ( first + " ▁ " ) ; if ( n > 1 ) System . out . print ( second + " ▁ " ) ; if ( n > 2 ) System . out . print ( second + " ▁ " ) ; for ( int i = 3 ; i < n ; i ++ ) { int curr = first + second + third ; first = second ; second = third ; third = curr ; System . out . print ( curr + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 10 ; printTrib ( n ) ; } }
Prime Number of Set Bits in Binary Representation | Set 2 | Java code to find count of numbers having prime number of set bits in their binary representation in the range [ L , R ] ; function to create an array of prime numbers upto number ' n ' ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as false . A value in prime [ i ] will finally be true if i is Not a prime , else false . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; append all the prime numbers to the list ; utility function to count the number of set bits ; Driver code ; Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself .
import java . util . * ; import java . lang . Math ; class GFG { static ArrayList < Integer > SieveOfEratosthenes ( int n ) { boolean [ ] prime = new boolean [ n + 1 ] ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) for ( int i = p * 2 ; i < n + 1 ; i += p ) prime [ i ] = true ; } ArrayList < Integer > lis = new ArrayList < Integer > ( ) ; for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] == false ) lis . add ( p ) ; return lis ; } static int setBits ( int n ) { return Integer . bitCount ( n ) ; } public static int log2 ( int x ) { return ( int ) ( Math . log ( x ) / Math . log ( 2 ) + 1e-10 ) ; } public static void main ( String [ ] args ) { int x = 4 , y = 8 ; int count = 0 ; ArrayList < Integer > primeArr = new ArrayList < Integer > ( ) ; primeArr = SieveOfEratosthenes ( ( int ) Math . ceil ( log2 ( y ) ) ) ; for ( int i = x ; i < y + 1 ; i ++ ) { int temp = setBits ( i ) ; if ( primeArr . contains ( temp ) ) count += 1 ; } System . out . println ( count ) ; } }
Count trailing zeroes present in binary representation of a given number using XOR | Java implementation of the above approach ; Function to print count of trailing zeroes present in binary representation of N ; Stores XOR of N and ( N - 1 ) ; Return count of set bits in res ; Driver Code ; Function call to print the count of trailing zeroes in the binary representation of N
import java . io . * ; class GFG { public static int countTrailingZeroes ( int N ) { int res = N ^ ( N - 1 ) ; return ( int ) ( Math . log ( temp ) / Math . log ( 2 ) ) ; } public static void main ( String [ ] args ) { int N = 12 ; System . out . println ( countTrailingZeroes ( N ) ) ; } }
Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | Java program for the above approach ; Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the final score ; Function to generate all the possible permutation and get the max score ; If arr [ ] length is equal to N process the permutation ; Generating the permutations ; If the current element is chosen ; Mark the current element as true ; Recursively call for next possible permutation ; Backtracking ; Return the ans ; Driver Code ; Stores the permutation ; To display the result
import java . util . * ; class GFG { static int calcScr ( ArrayList < Integer > arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) ans += ( i ^ arr . get ( i ) ) ; return ans ; } static int getMax ( ArrayList < Integer > arr , int ans , ArrayList < Boolean > chosen , int N ) { if ( arr . size ( ) == N ) { ans = Math . max ( ans , calcScr ( arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen . get ( i ) ) continue ; chosen . set ( i , true ) ; arr . add ( i ) ; ans = getMax ( arr , ans , chosen , N ) ; chosen . set ( i , false ) ; arr . remove ( arr . size ( ) - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; int ans = - 1 ; ArrayList < Boolean > chosen = new ArrayList < Boolean > ( Collections . nCopies ( N , false ) ) ; ans = getMax ( arr , ans , chosen , N ) ; System . out . print ( ans + "NEW_LINE"); } }
Additive Congruence method for generating Pseudo Random Numbers | Java implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the additive congruential method ; Driver Code ; Seed value ; Modulus parameter ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
class GFG { static void additiveCongruentialMethod ( int Xo , int m , int c , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] + c ) % m ; } } public static void main ( String [ ] args ) { int Xo = 3 ; int m = 15 ; int c = 2 ; int noOfRandomNums = 20 ; int [ ] randomNums = new int [ noOfRandomNums ] ; additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { System . out . print ( randomNums [ i ] + " ▁ " ) ; } } }
Second decagonal numbers | Java program for the above approach ; Function to find N - th term in the series ; Driver code
class GFG { static void findNthTerm ( int n ) { System . out . println ( n * ( 4 * n + 3 ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } }
65537 | Java program for above approach ; Function to find the nth 65537 - gon Number ; Driver code
class GFG { static int gonNum65537 ( int n ) { return ( 65535 * n * n - 65533 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( gonNum65537 ( n ) ) ; } }
Hexacontatetragon numbers | Java program to find N - th Hexacontatetragon number ; Function to find the nth Hexacontatetragon number ; Driver code
class GFG { static int HexacontatetragonNum ( int n ) { return ( 62 * n * n - 60 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( HexacontatetragonNum ( n ) ) ; } }
Icosikaipentagon Number | Java program to find N - th icosikaipentagon number ; Function to find the nth icosikaipentagon number ; Driver code
class GFG { static int icosikaipentagonNum ( int N ) { return ( 23 * N * N - 21 * N ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( "3rd ▁ icosikaipentagon ▁ Number ▁ is ▁ " + icosikaipentagonNum ( n ) ) ; } }
Chiliagon Number | Java program for the above approach ; Finding the nth chiliagon number ; Driver code
class GFG { static int chiliagonNum ( int n ) { return ( 998 * n * n - 996 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ chiliagon ▁ Number ▁ is ▁ = ▁ " + chiliagonNum ( n ) ) ; } }
Pentacontagon number | Java program for above approach ; Finding the nth pentacontagon number ; Driver code
import java . util . * ; class GFG { static int pentacontagonNum ( int n ) { return ( 48 * n * n - 46 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ pentacontagon ▁ Number ▁ is ▁ = ▁ " + pentacontagonNum ( n ) ) ; } }
Array value by repeatedly replacing max 2 elements with their absolute difference | Java program to find the array value by repeatedly replacing max 2 elements with their absolute difference ; Function that return last value of array ; Build a binary max_heap ; For max 2 elements ; Iterate until queue is not empty ; If only 1 element is left ; Return the last remaining value ; Check that difference is non zero ; Finally return 0 ; Driver code
import java . util . * ; class GFG { static int lastElement ( int [ ] arr ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ( a , b ) -> b - a ) ; for ( int i = 0 ; i < arr . length ; i ++ ) pq . add ( arr [ i ] ) ; int m1 , m2 ; while ( ! pq . isEmpty ( ) ) { if ( pq . size ( ) == 1 ) { return pq . poll ( ) ; } m1 = pq . poll ( ) ; m2 = pq . poll ( ) ; if ( m1 != m2 ) pq . add ( m1 - m2 ) ; } return 0 ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 2 , 7 , 4 , 1 , 8 , 1 , 1 } ; System . out . println ( lastElement ( arr ) ) ; } }
Number formed by adding product of its max and min digit K times | Java program for the above approach ; Function to find the formed number ; K -- ; M ( 1 ) = N ; Check if minimum digit is 0 ; Function that returns the product of maximum and minimum digit of N number . ; Finds the last digit . ; Moves to next digit ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static int formed_no ( int N , int K ) { if ( K == 1 ) { return N ; } int answer = N ; while ( K != 0 ) { int a_current = prod_of_max_min ( answer ) ; if ( a_current == 0 ) break ; answer += a_current ; } return answer ; } static int prod_of_max_min ( int n ) { int largest = 0 ; int smallest = 10 ; while ( n != 0 ) { int r = n % 10 ; largest = Math . max ( r , largest ) ; smallest = Math . min ( r , smallest ) ; n = n / 10 ; } return largest * smallest ; } public static void main ( String [ ] args ) { int N = 487 , K = 100000000 ; System . out . println ( formed_no ( N , K ) ) ; } }
Logarithm tricks for Competitive Programming | Java implementation to count the number of digits in a number ; Function to count the number of digits in a number ; Driver Code
class GFG { static int countDigit ( double n ) { return ( ( int ) Math . floor ( Math . log10 ( n ) + 1 ) ) ; } public static void main ( String [ ] args ) { double N = 80 ; System . out . println ( countDigit ( N ) ) ; } }
Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | Java implementation to find the sum of series 1 + x ^ 2 + x ^ 3 + ... . + x ^ n ; Java code to print the sum of the given series ; First Term ; Loop to print the N terms of the series and compute their sum ; Driver Code
class GFG { static double sum ( int x , int n ) { double i , total = 1.0 , multi = x ; System . out . print ( "1 ▁ " ) ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; System . out . print ( multi ) ; System . out . print ( " ▁ " ) ; multi = multi * x ; } System . out . println ( ) ; return total ; } public static void main ( String [ ] args ) { int x = 2 ; int n = 5 ; System . out . printf ( " % .2f " , sum ( x , n ) ) ; } }
Find the remainder when N is divided by 4 using Bitwise AND operator | Java implementation to find N modulo 4 using Bitwise AND operator ; Driver code ; Function to find the remainder ; Bitwise AND with 3 ; return x
class Main { public static void main ( String [ ] args ) { int N = 43 ; int ans = findRemainder ( N ) ; System . out . println ( ans ) ; } public static int findRemainder ( int n ) { int x = n & 3 ; return x ; } }
Find all Autobiographical Numbers with given number of digits | Java implementation to find Autobiographical numbers with length N ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; initialize count as 0 ; Check if it is equal to the index i if true then increment the count ; It is an Autobiographical number ; Return false if the count and the index number are not equal ; Function to print autobiographical number with given number of digits ; both the boundaries are taken double , so as to satisfy Math . pow ( ) function 's signature ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code
import java . util . * ; import java . lang . Math ; public class autobio { public static boolean isAutoBio ( int num ) { String autoStr ; int index , number , i , j , cnt ; autoStr = Integer . toString ( num ) ; for ( i = 0 ; i < autoStr . length ( ) ; i ++ ) { index = Integer . parseInt ( autoStr . charAt ( i ) + " " ) ; cnt = 0 ; for ( j = 0 ; j < autoStr . length ( ) ; j ++ ) { number = Integer . parseInt ( autoStr . charAt ( j ) + " " ) ; if ( number == i ) cnt ++ ; } if ( cnt != index ) return false ; } return true ; } public static void findAutoBios ( double n ) { double high , low ; int i , flag = 0 ; low = Math . pow ( 10.0 , n - 1 ) ; high = Math . pow ( 10.0 , n ) - 1.0 ; for ( i = ( int ) low ; i <= ( int ) high ; i ++ ) if ( isAutoBio ( i ) ) { flag = 1 ; System . out . print ( i + " , ▁ " ) ; } if ( flag == 0 ) System . out . println ( " There ▁ is ▁ no ▁ Autobiographical ▁ Number " + " with ▁ " + ( int ) n + " ▁ digits " ) ; } public static void main ( String [ ] args ) { double N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; } }
Check whether the number can be made palindromic after adding K | Java program to check whether the number can be made palindrome number after adding K ; Function to check whether a number is a palindrome or not ; Convert num to string ; Comparing kth character from the beginning and N - kth character from the end . If all the characters match , then the number is a palindrome ; If all the above conditions satisfy , it means that the number is a palindrome ; Driver code
import java . util . * ; class GFG { static void checkPalindrome ( int num ) { String str = Integer . toString ( num ) ; int l = 0 , r = str . length ( ) - 1 ; while ( l < r ) { if ( str . charAt ( l ) != str . charAt ( r ) ) { System . out . print ( " No " ) ; return ; } l ++ ; r -- ; } System . out . print ( " Yes " ) ; return ; } public static void main ( String args [ ] ) { int n = 19 , k = 3 ; checkPalindrome ( n + k ) ; } }
Count of subsets with sum equal to X using Recursion | Java program to print the count of subsets with sum equal to the given value X ; Recursive function to return the count of subsets with sum equal to the given value ; The recursion is stopped at N - th level where all the subsets of the given array have been checked ; Incrementing the count if sum is equal to the subset and returning the count ; Driver code ; int s = 0 ; Initially assigning the sum of subset to be zero
import java . util . * ; class GFG { static int subsetSum ( int arr [ ] , int n , int sum , int s , int count ) { if ( n == 0 ) { if ( sum == s ) { count ++ ; } return count ; } count = subsetSum ( arr , n - 1 , sum , s , count ) ; count = subsetSum ( arr , n - 1 , sum , s + arr [ n - 1 ] , count ) ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int sum = 10 ; int n = arr . length ; System . out . print ( subsetSum ( arr , n , sum , s , 0 ) ) ; } }
Distinct Prime Factors of an Array | Java implementation of the above approach ; Function to return an array of prime numbers upto n using Sieve of Eratosthenes ; Function to return distinct prime factors from the given array ; Creating an empty array to store distinct prime factors ; Iterating through all the prime numbers and check if any of the prime numbers is a factor of the given input array ; Driver code ; Finding prime numbers upto 10000 using Sieve of Eratosthenes
import java . util . * ; class GFG { static ArrayList < Integer > sieve ( int n ) { ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime . add ( 0 ) ; int p = 2 ; while ( p * p <= n ) { if ( prime . get ( p ) == 0 ) { for ( int i = 2 * p ; i < n + 1 ; i += p ) prime . set ( i , 1 ) ; } p += 1 ; } ArrayList < Integer > allPrimes = new ArrayList < Integer > ( ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime . get ( i ) == 0 ) allPrimes . add ( i ) ; } return allPrimes ; } static ArrayList < Integer > distPrime ( ArrayList < Integer > arr , ArrayList < Integer > allPrimes ) { ArrayList < Integer > list1 = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < allPrimes . size ( ) ; i ++ ) { for ( int j = 0 ; j < arr . size ( ) ; j ++ ) { if ( arr . get ( j ) % allPrimes . get ( i ) == 0 ) { list1 . add ( allPrimes . get ( i ) ) ; break ; } } } return list1 ; } public static void main ( String args [ ] ) { ArrayList < Integer > allPrimes = new ArrayList < Integer > ( sieve ( 10000 ) ) ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; arr . add ( 15 ) ; arr . add ( 30 ) ; arr . add ( 60 ) ; ArrayList < Integer > ans = new ArrayList < Integer > ( distPrime ( arr , allPrimes ) ) ; System . out . print ( " [ " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; System . out . print ( " ] " ) ; } }
Sum of the count of number of adjacent squares in an M X N grid | Java implementation of the above approach ; function to calculate the sum of all cells adjacent value ; Driver Code
class GFG { static int sum ( int m , int n ) { return 8 * m * n - 6 * m - 6 * n + 4 ; } public static void main ( String [ ] args ) { int m = 3 , n = 2 ; System . out . println ( sum ( m , n ) ) ; } }