codeparrot/xlcost-text-to-code huggingface dataset:
Frobenius coin problem | Java program to find the largest number that cannot be formed from given two coins ; Utility function to find gcd ; Function to print the desired output ; Solution doesn 't exist   if GCD is not 1 ; Else apply the formula ; Driver Code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { int c ; while ( a != 0 ) { c = a ; a = b % a ; b = c ; } return b ; } static void forbenius ( int X , int Y ) { if ( gcd ( X , Y ) != 1 ) { System . out . println ( " NA " ) ; return ; } int A = ( X * Y ) - ( X + Y ) ; int N = ( X - 1 ) * ( Y - 1 ) / 2 ; System . out . println ( " Largest ▁ Amount ▁ = ▁ " + A ) ; System . out . println ( " Total ▁ Count ▁ = ▁ " + N ) ; } public static void main ( String [ ] args ) { int X = 2 , Y = 5 ; forbenius ( X , Y ) ; X = 5 ; Y = 10 ; System . out . println ( ) ; forbenius ( X , Y ) ; } }
Gray to Binary and Binary to Gray conversion | Java program for Binary To Gray and Gray to Binary conversion ; Helper function to xor two characters ; Helper function to flip the bit ; function to convert binary string to gray string ; MSB of gray code is same as binary code ; Compute remaining bits , next bit is computed by doing XOR of previous and current in Binary ; Concatenate XOR of previous bit with current bit ; function to convert gray code string to binary string ; MSB of binary code is same as gray code ; Compute remaining bits ; If current bit is 0 , concatenate previous bit ; Else , concatenate invert of previous bit ; Driver program to test above functions
import java . io . * ; class code_conversion { char xor_c ( char a , char b ) { return ( a == b ) ? '0' : '1' ; } char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } String binarytoGray ( String binary ) { String gray = " " ; gray += binary . charAt ( 0 ) ; for ( int i = 1 ; i < binary . length ( ) ; i ++ ) { gray += xor_c ( binary . charAt ( i - 1 ) , binary . charAt ( i ) ) ; } return gray ; } String graytoBinary ( String gray ) { String binary = " " ; binary += gray . charAt ( 0 ) ; for ( int i = 1 ; i < gray . length ( ) ; i ++ ) { if ( gray . charAt ( i ) == '0' ) binary += binary . charAt ( i - 1 ) ; else binary += flip ( binary . charAt ( i - 1 ) ) ; } return binary ; } public static void main ( String args [ ] ) throws IOException { code_conversion ob = new code_conversion ( ) ; String binary = "01001" ; System . out . println ( " Gray ▁ code ▁ of ▁ " + binary + " ▁ is ▁ " + ob . binarytoGray ( binary ) ) ; String gray = "01101" ; System . out . println ( " Binary ▁ code ▁ of ▁ " + gray + " ▁ is ▁ " + ob . graytoBinary ( gray ) ) ; } }
Solving f ( n ) = ( 1 ) + ( 2 * 3 ) + ( 4 * 5 * 6 ) . . . n using Recursion | Java Program to print the solution of the series f ( n ) = ( 1 ) + ( 2 * 3 ) + ( 4 * 5 * 6 ) . . . n using recursion ; * * Recursive method for finding * sum of series * * @ param calculated number of terms * till which sum of terms has been * calculated @ param current number of * terms for which sum has to be calculated . * @ param N Number of terms in the function * to be calculated @ return sum ; checking termination condition ; product of terms till current ; recursive call for adding terms next in the series ; Driver Code ; input number of terms in the series ; invoking the method to calculate the sum
class GFG { static int seriesSum ( int calculated , int current , int N ) { int i , cur = 1 ; if ( current == N + 1 ) return 0 ; for ( i = calculated ; i < calculated + current ; i ++ ) cur *= i ; return cur + seriesSum ( i , current + 1 , N ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( seriesSum ( 1 , 1 , N ) ) ; } }
How to avoid overflow in modular multiplication ? | Java program for modular multiplication without any overflow ; To compute ( a * b ) % mod ; long res = 0 ; Initialize result ; If b is odd , add ' a ' to result ; Multiply ' a ' with 2 ; Divide b by 2 ; Return result ; Driver code
class GFG { static long mulmod ( long a , long b , long mod ) { a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) { res = ( res + a ) % mod ; } a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } public static void main ( String [ ] args ) { long a = 9223372036854775807L , b = 9223372036854775807L ; System . out . println ( mulmod ( a , b , 100000000000L ) ) ; } }
Count inversions in an array | Set 3 ( Using BIT ) | Java program to count inversions using Binary Indexed Tree ; Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] . ; Initialize result ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Converts an array to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a copy of arrp [ ] in temp and sort the temp array in increasing order ; Traverse all array elements ; lower_bound ( ) Returns pointer to the first element greater than or equal to arr [ i ] ; Returns inversion count arr [ 0. . n - 1 ] ; Initialize result ; Convert arr [ ] to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a BIT with size equal to maxElement + 1 ( Extra one is used so that elements can be directly be used as index ) ; Traverse all elements from right . ; Get count of elements smaller than arr [ i ] ; Add current element to BIT ; Driver code
import java . util . * ; class GFG { static int getSum ( int BITree [ ] , int index ) { int sum = 0 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } static void updateBIT ( int BITree [ ] , int n , int index , int val ) { while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } static void convert ( int arr [ ] , int n ) { int [ ] temp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = arr [ i ] ; Arrays . sort ( temp ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = lower_bound ( temp , 0 , n , arr [ i ] ) + 1 ; } } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int getInvCount ( int arr [ ] , int n ) { int invcount = 0 ; convert ( arr , n ) ; int [ ] BIT = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BIT [ i ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { invcount += getSum ( BIT , arr [ i ] - 1 ) ; updateBIT ( BIT , n , arr [ i ] , 1 ) ; } return invcount ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 4 , 2 , 1 } ; int n = arr . length ; System . out . print ( " Number ▁ of ▁ inversions ▁ are ▁ : ▁ " + getInvCount ( arr , n ) ) ; } }
Compute n ! under modulo p | Simple method to compute n ! % p ; Returns value of n ! % p ; Driver Code
import java . io . * ; class GFG { static int modFact ( int n , int p ) { if ( n >= p ) return 0 ; int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = ( result * i ) % p ; return result ; } public static void main ( String [ ] args ) { int n = 25 , p = 29 ; System . out . print ( modFact ( n , p ) ) ; } }
Chinese Remainder Theorem | Set 2 ( Inverse Modulo based Implementation ) | A Java program to demonstrate working of Chinise remainder Theorem ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm . Refer below post for details : https : www . geeksforgeeks . org / multiplicative - inverse - under - modulo - m / ; Apply extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as euclid 's algo ; Make x1 positive ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd for every pair is 1 ) ; Compute product of all numbers ; Initialize result ; Apply above formula ; Driver method
import java . io . * ; class GFG { static int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static int findMinX ( int num [ ] , int rem [ ] , int k ) { int prod = 1 ; for ( int i = 0 ; i < k ; i ++ ) prod *= num [ i ] ; int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int pp = prod / num [ i ] ; result += rem [ i ] * inv ( pp , num [ i ] ) * pp ; } return result % prod ; } public static void main ( String args [ ] ) { int num [ ] = { 3 , 4 , 5 } ; int rem [ ] = { 2 , 3 , 1 } ; int k = num . length ; System . out . println ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }
Chinese Remainder Theorem | Set 1 ( Introduction ) | A Java program to demonstrate the working of Chinese remainder Theorem ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd for every pair is 1 ) ; As per the Chinese remainder theorem , this loop will always break . ; Check if remainder of x % num [ j ] is rem [ j ] or not ( for all j from 0 to k - 1 ) ; If all remainders matched , we found x ; Else try next number ; Driver method
import java . io . * ; class GFG { static int findMinX ( int num [ ] , int rem [ ] , int k ) { while ( true ) { int j ; for ( j = 0 ; j < k ; j ++ ) if ( x % num [ j ] != rem [ j ] ) break ; if ( j == k ) return x ; x ++ ; } } public static void main ( String args [ ] ) { int num [ ] = { 3 , 4 , 5 } ; int rem [ ] = { 2 , 3 , 1 } ; int k = num . length ; System . out . println ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }
Compute nCr % p | Set 2 ( Lucas Theorem ) | A Lucas Theorem based solution to compute nCr % p ; Returns nCr % p . In this Lucas Theorem based program , this function is only called for n < p and r < p . ; The array C is going to store last row of pascal triangle at the end . And last entry of last row is nCr ; One by constructs remaining rows of Pascal Triangle from top to bottom ; Fill entries of current row using previous row values ; nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; ; Lucas Theorem based function that returns nCr % p This function works like decimal to binary conversion recursive function . First we compute last digits of n and r in base p , then recur for remaining digits ; Base case ; Compute last digits of n and r in base p ; Compute result for last digits computed above , and for remaining digits . Multiply the two results and compute the result of multiplication in modulo p . return ( nCrModpLucas ( n / p , r / p , p ) * Last digits of n and r nCrModpDP ( ni , ri , p ) ) % p ; Remaining digits ; Driver program
class GFG { static int nCrModpDP ( int n , int r , int p ) { int [ ] C = new int [ r + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; } static int nCrModpLucas ( int n , int r , int p ) { if ( r == 0 ) return 1 ; int ni = n % p ; int ri = r % p ; } public static void main ( String [ ] args ) { int n = 1000 , r = 900 , p = 13 ; System . out . println ( " Value ▁ of ▁ nCr ▁ % ▁ p ▁ is ▁ " + nCrModpLucas ( n , r , p ) ) ; } }
Fibonacci Coding | Java program for Fibonacci Encoding of a positive integer n ; To limit on the largest Fibonacci number to be used ; Array to store fibonacci numbers . fib [ i ] is going to store ( i + 2 ) 'th Fibonacci number ; Stores values in fib and returns index of the largest fibonacci number smaller than n . ; Fib [ 0 ] stores 2 nd Fibonacci No . ; Fib [ 1 ] stores 3 rd Fibonacci No . ; Keep Generating remaining numbers while previously generated number is smaller ; Return index of the largest fibonacci number smaller than or equal to n . Note that the above loop stopped when fib [ i - 1 ] became larger . ; Returns pointer to the char string which corresponds to code for n ; Allocate memory for codeword ; Index of the largest Fibonacci f <= n ; Mark usage of Fibonacci f ( 1 bit ) ; Subtract f from n ; Move to Fibonacci just smaller than f ; Mark all Fibonacci > n as not used ( 0 bit ) , progress backwards ; Additional '1' bit ; Return pointer to codeword ; Driver code
import java . io . * ; class GFG { public static int N = 30 ; public static int [ ] fib = new int [ N ] ; public static int largestFiboLessOrEqual ( int n ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; int i ; for ( i = 2 ; fib [ i - 1 ] <= n ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } return ( i - 2 ) ; } public static String fibonacciEncoding ( int n ) { int index = largestFiboLessOrEqual ( n ) ; char [ ] codeword = new char [ index + 3 ] ; int i = index ; while ( n > 0 ) { codeword [ i ] = '1' ; n = n - fib [ i ] ; i = i - 1 ; while ( i >= 0 && fib [ i ] > n ) { codeword [ i ] = '0' ; i = i - 1 ; } } codeword [ index + 1 ] = '1' ; codeword [ index + 2 ] = ' \0' ; String string = new String ( codeword ) ; return string ; } public static void main ( String [ ] args ) { int n = 143 ; System . out . println ( " Fibonacci ▁ code ▁ word ▁ for ▁ " + n + " ▁ is ▁ " + fibonacciEncoding ( n ) ) ; } }
Print all Good numbers in given range | Java program to print good numbers in a given range [ L , R ] ; Function to check whether n is a good number and doesn ' t ▁ contain ▁ ▁ digit ▁ ' d ' ; Get last digit and initialize sum from right side ; If last digit is d , return ; Traverse remaining digits ; Current digit ; If digit is d or digit is less than or equal to sum of digits on right side ; Update sum and n ; Print Good numbers in range [ L , R ] ; Traverse all numbers in given range ; If current numbers is good , print it ; Driver program ; Print good numbers in [ L , R ]
import java . io . * ; class Numbers { static boolean isValid ( int n , int d ) { int digit = n % 10 ; int sum = digit ; if ( digit == d ) return false ; n /= 10 ; while ( n > 0 ) { digit = n % 10 ; if ( digit == d digit <= sum ) return false ; else { sum += digit ; n /= 10 ; } } return true ; } static void printGoodNumber ( int L , int R , int d ) { for ( int i = L ; i <= R ; i ++ ) { if ( isValid ( i , d ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int L = 410 , R = 520 , d = 3 ; printGoodNumber ( L , R , d ) ; } }
Count number of squares in a rectangle | Java program to count squares in a rectangle of size m x n ; Returns count of all squares in a rectangle of size m x n ; If n is smaller , swap m and n ; Now n is greater dimension , apply formula ; Driver Code
import java . util . * ; class GFG { static int countSquares ( int m , int n ) { if ( n < m ) { int temp = m ; m = n ; n = temp ; } return n * ( n + 1 ) * ( 3 * m - n + 1 ) / 6 ; } public static void main ( String [ ] args ) { int m = 4 ; int n = 3 ; System . out . print ( " Count ▁ of ▁ squares ▁ is ▁ " + countSquares ( m , n ) ) ; } }
Zeckendorf 's Theorem (Non | Java program for Zeckendorf 's theorem. It finds representation of n as sum of non-neighbouring Fibonacci Numbers. ; Corner cases ; Find the greatest Fibonacci Number smaller than n . ; Prints Fibonacci Representation of n using greedy algorithm ; Find the greates Fibonacci Number smaller than or equal to n ; Print the found fibonacci number ; Reduce n ; Driver method to test
class GFG { public static int nearestSmallerEqFib ( int n ) { if ( n == 0 n == 1 ) return n ; int f1 = 0 , f2 = 1 , f3 = 1 ; while ( f3 <= n ) { f1 = f2 ; f2 = f3 ; f3 = f1 + f2 ; } return f2 ; } public static void printFibRepresntation ( int n ) { while ( n > 0 ) { int f = nearestSmallerEqFib ( n ) ; System . out . print ( f + " ▁ " ) ; n = n - f ; } } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( " Non - neighbouring ▁ Fibonacci ▁ " + " ▁ Representation ▁ of ▁ " + n + " ▁ is " ) ; printFibRepresntation ( n ) ; } }
Count number of ways to divide a number in 4 parts | A Dynamic Programming based solution to count number of ways to represent n as sum of four numbers ; " parts " is number of parts left , n is the value left " nextPart " is starting point from where we start trying for next part . ; Base cases ; If this subproblem is already solved ; Count number of ways for remaining number n - i remaining parts " parts - 1" , and for all part varying from ' nextPart ' to ' n ' ; Store computed answer in table and return result ; This function mainly initializes dp table and calls countWaysUtil ( ) ; Driver program
class GFG { static int dp [ ] [ ] [ ] = new int [ 5001 ] [ 5001 ] [ 5 ] ; static int countWaysUtil ( int n , int parts , int nextPart ) { if ( parts == 0 && n == 0 ) return 1 ; if ( n <= 0 parts <= 0 ) return 0 ; if ( dp [ n ] [ nextPart ] [ parts ] != - 1 ) return dp [ n ] [ nextPart ] [ parts ] ; for ( int i = nextPart ; i <= n ; i ++ ) ans += countWaysUtil ( n - i , parts - 1 , i ) ; return ( dp [ n ] [ nextPart ] [ parts ] = ans ) ; } static int countWays ( int n ) { for ( int i = 0 ; i < 5001 ; i ++ ) { for ( int j = 0 ; j < 5001 ; j ++ ) { for ( int l = 0 ; l < 5 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } return countWaysUtil ( n , 4 , 1 ) ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( countWays ( n ) ) ; } }
Segmented Sieve | This functions finds all primes smaller than ' limit ' using simple sieve of eratosthenes . ; Create a boolean array " mark [ 0 . . limit - 1 ] " and initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; One by one traverse all numbers so that their multiples can be marked as composite . ; If p is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers and store them in prime
static void simpleSieve ( int limit ) { boolean [ ] mark = new boolean [ limit ] ; Arrays . fill ( mark , true ) ; for ( int p = 2 ; p * p < limit ; p ++ ) { if ( mark [ p ] == true ) { for ( int i = p * p ; i < limit ; i += p ) mark [ i ] = false ; } } for ( int p = 2 ; p < limit ; p ++ ) if ( mark [ p ] == true ) System . out . print ( p + " ▁ " ) ; }
Segmented Sieve | Java program to print print all primes smaller than n using segmented sieve ; This methid finds all primes smaller than ' limit ' using simple sieve of eratosthenes . It also stores found primes in vector prime [ ] ; Create a boolean array " mark [ 0 . . n - 1 ] " and initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; If p is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers and store them in prime ; Prints all prime numbers smaller than ' n ' ; Compute all primes smaller than or equal to square root of n using simple sieve ; Divide the range [ 0. . n - 1 ] in different segments We have chosen segment size as sqrt ( n ) . ; While all segments of range [ 0. . n - 1 ] are not processed , process one segment at a time ; To mark primes in current range . A value in mark [ i ] will finally be false if ' i - low ' is Not a prime , else true . ; Use the found primes by simpleSieve ( ) to find primes in current range ; Find the minimum number in [ low . . high ] that is a multiple of prime . get ( i ) ( divisible by prime . get ( i ) ) For example , if low is 31 and prime . get ( i ) is 3 , we start with 33. ; Mark multiples of prime . get ( i ) in [ low . . high ] : We are marking j - low for j , i . e . each number in range [ low , high ] is mapped to [ 0 , high - low ] so if range is [ 50 , 100 ] marking 50 corresponds to marking 0 , marking 51 corresponds to 1 and so on . In this way we need to allocate space only for range ; Numbers which are not marked as false are prime ; Update low and high for next segment ; Driver method
import java . util . Vector ; import static java . lang . Math . sqrt ; import static java . lang . Math . floor ; class Test { static void simpleSieve ( int limit , Vector < Integer > prime ) { boolean mark [ ] = new boolean [ limit + 1 ] ; for ( int i = 0 ; i < mark . length ; i ++ ) mark [ i ] = true ; for ( int p = 2 ; p * p < limit ; p ++ ) { if ( mark [ p ] == true ) { for ( int i = p * p ; i < limit ; i += p ) mark [ i ] = false ; } } for ( int p = 2 ; p < limit ; p ++ ) { if ( mark [ p ] == true ) { prime . add ( p ) ; System . out . print ( p + " ▁ " ) ; } } } static void segmentedSieve ( int n ) { int limit = ( int ) ( floor ( sqrt ( n ) ) + 1 ) ; Vector < Integer > prime = new Vector < > ( ) ; simpleSieve ( limit , prime ) ; int low = limit ; int high = 2 * limit ; while ( low < n ) { if ( high >= n ) high = n ; boolean mark [ ] = new boolean [ limit + 1 ] ; for ( int i = 0 ; i < mark . length ; i ++ ) mark [ i ] = true ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) { int loLim = ( int ) ( floor ( low / prime . get ( i ) ) * prime . get ( i ) ) ; if ( loLim < low ) loLim += prime . get ( i ) ; for ( int j = loLim ; j < high ; j += prime . get ( i ) ) mark [ j - low ] = false ; } for ( int i = low ; i < high ; i ++ ) if ( mark [ i - low ] == true ) System . out . print ( i + " ▁ " ) ; low = low + limit ; high = high + limit ; } } public static void main ( String args [ ] ) { int n = 100 ; System . out . println ( " Primes ▁ smaller ▁ than ▁ " + n + " : " ) ; segmentedSieve ( n ) ; } }
Find the smallest twins in given range | Java program to find the smallest twin in given range ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the smallest twin in range ; Driver program
class GFG { static void printTwins ( int low , int high ) { boolean prime [ ] = new boolean [ high + 1 ] , twin = false ; for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p <= Math . floor ( Math . sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= high ; i += p ) { prime [ i ] = false ; } } } for ( int i = low ; i <= high ; i ++ ) { if ( prime [ i ] && prime [ i + 2 ] ) { int a = i + 2 ; System . out . print ( " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " + i + " , ▁ " + a + " ) " ) ; twin = true ; break ; } } if ( twin == false ) { System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } } public static void main ( String [ ] args ) { printTwins ( 10 , 100 ) ; } }
Check if a given number is Fancy | Java program to find if a given number is fancy or not ; To store mappings of fancy pair characters . For example 6 is paired with 9 and 9 is paired with 6. ; Find number of digits in given number ; Traverse from both ends , and compare characters one by one ; If current characters at both ends are not fancy pairs ; Driver Code
import java . util . * ; class GFG { static boolean isFancy ( String num ) { Map < Character , Character > fp = new HashMap < Character , Character > ( ) ; fp . put ( '0' , '0' ) ; fp . put ( '1' , '1' ) ; fp . put ( '6' , '9' ) ; fp . put ( '8' , '8' ) ; fp . put ( '9' , '6' ) ; int n = num . length ( ) ; int l = 0 , r = n - 1 ; while ( l <= r ) { if ( ! fp . containsKey ( num . charAt ( l ) ) || fp . get ( num . charAt ( l ) ) != num . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } public static void main ( String [ ] args ) { String str = "9088806" ; if ( isFancy ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }