codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs ( a , b ) whose sum of cubes is N ( a ^ 3 + b ^ 3 = N ) | Java program to count pairs whose sum cubes is N ; method to count the pairs satisfying a ^ 3 + b ^ 3 = N ; Check for each number 1 to cbrt ( N ) ; Store cube of a number ; Subtract the cube from given N ; Check if the difference is also a perfect cube ; If yes , then increment count ; Return count ; Driver method ; Loop to Count no . of pairs satisfying a ^ 3 + b ^ 3 = i for N = 1 to 10
class Test { static int countPairs ( int N ) { int count = 0 ; for ( int i = 1 ; i <= Math . cbrt ( N ) ; i ++ ) { int cb = i * i * i ; int diff = N - cb ; int cbrtDiff = ( int ) Math . cbrt ( diff ) ; if ( cbrtDiff * cbrtDiff * cbrtDiff == diff ) count ++ ; } return count ; } public static void main ( String args [ ] ) { for ( int i = 1 ; i <= 10 ; i ++ ) System . out . println ( " For ▁ n ▁ = ▁ " + i + " , ▁ " + + countPairs ( i ) + " ▁ pair ▁ exists " ) ; } }
Smallest number S such that N is a factor of S factorial or S ! | Java Program to find factorial that N belongs to ; Calculate prime factors for a given number ; Container for prime factors ; Iterate from 2 to i ^ 2 finding all factors ; If we still have a remainder it is also a prime factor ; Calculate occurrence of an element in factorial of a number ; Iterate through prime factors ; Check if factorial contains less occurrences of prime factor ; Method to binary search 1 to N ; Prime factors are not in the factorial Increase the lowerbound ; We have reached smallest occurrence ; Smaller factorial satisfying requirements may exist , decrease upperbound ; Calculate prime factors and search ; Driver method
import java . util . HashMap ; import java . util . Iterator ; import java . util . Set ; class Test { static HashMap < Long , Integer > primeFactors ( long num ) { HashMap < Long , Integer > ans = new HashMap < Long , Integer > ( ) { @ Override public Integer get ( Object key ) { if ( containsKey ( key ) ) { return super . get ( key ) ; } return 0 ; } } ; for ( long i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { num /= i ; ans . put ( i , ans . get ( i ) + 1 ) ; } } if ( num > 1 ) ans . put ( num , ans . get ( num ) + 1 ) ; ; return ans ; } static long legendre ( long factor , long num ) { long count = 0 , fac2 = factor ; while ( num >= factor ) { count += num / factor ; factor *= fac2 ; } return count ; } static boolean possible ( HashMap < Long , Integer > factors , long num ) { Set < Long > s = factors . keySet ( ) ; Iterator < Long > itr = s . iterator ( ) ; while ( itr . hasNext ( ) ) { long temp = itr . next ( ) ; if ( legendre ( temp , num ) < factors . get ( temp ) ) return false ; } return true ; } static long search ( long start , long end , HashMap < Long , Integer > factors ) { long mid = ( start + end ) / 2 ; if ( ! possible ( factors , mid ) ) return search ( mid + 1 , end , factors ) ; if ( start == mid ) return mid ; return search ( start , mid , factors ) ; } static long findFact ( long num ) { HashMap < Long , Integer > factors = primeFactors ( num ) ; return search ( 1 , num , factors ) ; } public static void main ( String args [ ] ) { System . out . println ( findFact ( 6 ) ) ; System . out . println ( findFact ( 997587429953L ) ) ; } }
Finding ' k ' such that its modulus with each array element is same | Java implementation of finding all k such that arr [ i ] % k is same for each i ; Prints all k such that arr [ i ] % k is same for all i ; sort the numbers ; max difference will be the difference between first and last element of sorted array ; Case when all the array elements are same ; Find all divisors of d and store in a vector v [ ] ; check for each v [ i ] if its modulus with each array element is same or not ; checking for each array element if its modulus with k is equal to k or not ; if check is true print v [ i ] ; Driver method
import java . util . Arrays ; import java . util . Vector ; class Test { static void printEqualModNumbers ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int d = arr [ n - 1 ] - arr [ 0 ] ; if ( d == 0 ) { System . out . println ( " Infinite ▁ solution " ) ; return ; } Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i * i <= d ; i ++ ) { if ( d % i == 0 ) { v . add ( i ) ; if ( i != d / i ) v . add ( d / i ) ; } } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int temp = arr [ 0 ] % v . get ( i ) ; int j ; for ( j = 1 ; j < n ; j ++ ) if ( arr [ j ] % v . get ( i ) != temp ) break ; if ( j == n ) System . out . print ( v . get ( i ) + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 38 , 6 , 34 } ; printEqualModNumbers ( arr , arr . length ) ; } }
First digit in product of an array of numbers | Java implementation of finding first digit of product of n numbers ; returns the first digit of product of elements of arr [ ] ; stores the logarithm of product of elements of arr [ ] ; fractional ( s ) = s - floor ( s ) ; ans = 10 ^ fract_s ; Driver method
class Test { static int FirstDigit ( int arr [ ] , int n ) { double S = 0 ; for ( int i = 0 ; i < n ; i ++ ) S = S + Math . log10 ( arr [ i ] * 1.0 ) ; double fract_S = S - Math . floor ( S ) ; int ans = ( int ) Math . pow ( 10 , fract_S ) ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 8 , 3 , 7 } ; System . out . println ( FirstDigit ( arr , arr . length ) ) ; } }
Find count of digits in a number that divide the number | Java program to count number of digits that divides the number . ; Return the number of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver method
class Test { static int countDigit ( int n ) { int temp = n , count = 0 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d > 0 && n % d == 0 ) count ++ ; } return count ; } public static void main ( String args [ ] ) { int n = 1012 ; System . out . println ( countDigit ( n ) ) ; } }
Minimum positive integer to divide a number such that the result is an odd | Java program to make a number odd ; Function to find the value ; Return 1 if already odd ; Check how many times it is divided by 2 ; Driver code
import java . io . * ; class GFG { static int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; int ans = 1 ; while ( n % 2 == 0 ) { n /= 2 ; ans *= 2 ; } return ans ; } public static void main ( String [ ] args ) { int n = 36 ; int res = makeOdd ( n ) ; System . out . println ( res ) ; } }
Multiple of x closest to n | Java program to calculate the smallest multiple of x closest to a given number ; Function to calculate the smallest multiple ; driver program
import java . io . * ; class Solution { static int closestMultiple ( int n , int x ) { if ( x > n ) return x ; n = n + x / 2 ; n = n - ( n % x ) ; return n ; } public static void main ( String [ ] args ) { int n = 56287 , x = 27 ; System . out . println ( closestMultiple ( n , x ) ) ; } }
Perfect cubes in a range | Java progroam for Efficient method to print cubes between a and b ; An efficient solution to print perfect cubes between a and b ; Find cube root of both a and b ; Print cubes between acrt and bcrt ; Driver method
class Test { static void printCubes ( int a , int b ) { int acrt = ( int ) Math . cbrt ( a ) ; int bcrt = ( int ) Math . cbrt ( b ) ; for ( int i = acrt ; i <= bcrt ; i ++ ) if ( i * i * i >= a && i * i * i <= b ) System . out . print ( i * i * i + " ▁ " ) ; } public static void main ( String [ ] args ) { int a = 24 , b = 576 ; System . out . println ( " Perfect ▁ cubes ▁ in ▁ given ▁ range : " ) ; printCubes ( a , b ) ; } }
Number of occurrences of 2 as a digit in numbers from 0 to n | Java program to count 2 s from 0 to n ; Counts the number of '2' digits in a single number ; Counts the number of '2' digits between 0 and n ; Initialize result ; Count 2 's in every number  from 2 to n ; Driver code
class GFG { static int number0f2s ( int n ) { int count = 0 ; while ( n > 0 ) { if ( n % 10 == 2 ) count ++ ; n = n / 10 ; } return count ; } static int numberOf2sinRange ( int n ) { int count = 0 ; for ( int i = 2 ; i <= n ; i ++ ) count += number0f2s ( i ) ; return count ; } public static void main ( String [ ] args ) { System . out . print ( numberOf2sinRange ( 22 ) ) ; System . out . println ( ) ; System . out . print ( numberOf2sinRange ( 100 ) ) ; } }
Minimum toggles to partition a binary array so that it has first 0 s then 1 s | Java program to find minimum toggle required ; Function to calculate minimum toggling required by using Dynamic programming ; Fill entries in zero [ ] such that zero [ i ] stores count of zeroes to the left of i ( exl ; If zero found update zero [ ] array ; Finding the minimum toggle required from every index ( 0 to n - 1 ) ; Driver Program
import java . io . * ; class GFG { static int minToggle ( int arr [ ] , int n ) { int zero [ ] = new int [ n + 1 ] ; zero [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { if ( arr [ i - 1 ] == 0 ) zero [ i ] = zero [ i - 1 ] + 1 ; else zero [ i ] = zero [ i - 1 ] ; } int ans = n ; for ( int i = 1 ; i <= n ; ++ i ) ans = Math . min ( ans , i - zero [ i ] + zero [ n ] - zero [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 1 , 0 } ; int n = arr . length ; System . out . println ( minToggle ( arr , n ) ) ; } }
Check if a large number is divisible by 6 or not | Java program to find if a number is divisible by 6 or not ; Function to find that number divisible by 6 or not ; Return false if number is not divisible by 2. ; Compute sum of digits ; Check if sum of digits is divisible by 3 ; main function
class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; if ( ( str . charAt ( n - 1 ) - '0' ) % 2 != 0 ) return false ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - '0' ) ; return ( digitSum % 3 == 0 ) ; } public static void main ( String [ ] args ) { String str = "1332" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find ways an Integer can be expressed as sum of n | Java program to find number of ways to express a number as sum of n - th powers of numbers . ; Wrapper over checkRecursive ( ) ; Driver Code
import java . io . * ; import java . util . * ; public class Solution { static int res = 0 ; static int checkRecursive ( int num , int x , int k , int n ) { if ( x == 0 ) res ++ ; int r = ( int ) Math . floor ( Math . pow ( num , 1.0 / n ) ) ; for ( int i = k + 1 ; i <= r ; i ++ ) { int a = x - ( int ) Math . pow ( i , n ) ; if ( a >= 0 ) checkRecursive ( num , x - ( int ) Math . pow ( i , n ) , i , n ) ; } return res ; } static int check ( int x , int n ) { return checkRecursive ( x , x , 0 , n ) ; } public static void main ( String [ ] args ) { System . out . println ( check ( 10 , 2 ) ) ; } }
N 'th palindrome of K digits | A naive approach of Java program of finding nth palindrome of k digit ; Utility function to reverse the number n ; Boolean Function to check for palindromic number ; Function for finding nth palindrome of k digits ; Get the smallest k digit number ; check the number is palindrom or not ; if n 'th palindrome found break the loop ; Increment number for checking next palindrome ; Driver code
import java . util . * ; class GFG { static int reverseNum ( int n ) { int rem , rev = 0 ; while ( n > 0 ) { rem = n % 10 ; rev = rev * 10 + rem ; n /= 10 ; } return rev ; } static boolean isPalindrom ( int num ) { return num == reverseNum ( num ) ; } static int nthPalindrome ( int n , int k ) { int num = ( int ) Math . pow ( 10 , k - 1 ) ; while ( true ) { if ( isPalindrom ( num ) ) -- n ; if ( n == 0 ) break ; ++ num ; } return num ; } public static void main ( String [ ] args ) { int n = 6 , k = 5 ; System . out . println ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " + nthPalindrome ( n , k ) ) ; n = 10 ; k = 6 ; System . out . println ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " + nthPalindrome ( n , k ) ) ; } }
N 'th palindrome of K digits | Java program of finding nth palindrome of k digit ; Determine the first half digits ; Print the first half digits of palindrome ; If k is odd , truncate the last digit ; print the last half digits of palindrome ; Driver code
class GFG { static void nthPalindrome ( int n , int k ) { int temp = ( k & 1 ) != 0 ? ( k / 2 ) : ( k / 2 - 1 ) ; int palindrome = ( int ) Math . pow ( 10 , temp ) ; palindrome += n - 1 ; System . out . print ( palindrome ) ; if ( ( k & 1 ) > 0 ) palindrome /= 10 ; while ( palindrome > 0 ) { System . out . print ( palindrome % 10 ) ; palindrome /= 10 ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { int n = 6 , k = 5 ; System . out . print ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " ) ; nthPalindrome ( n , k ) ; n = 10 ; k = 6 ; System . out . print ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " ) ; nthPalindrome ( n , k ) ; } }
Maximum value in an array after m range increment operations | Java implementation of simple approach to find maximum value after m range increments . ; Function to find the maximum element after m operations ; Start performing m operations ; Store lower and upper index i . e . range ; Add ' k [ i ] ' value at this operation to whole range ; Find maximum value after all operations and return ; Driver Code ; Number of values ; Value of k to be added at each operation
import java . util . * ; class GFG { static int findMax ( int n , int a [ ] , int b [ ] , int k [ ] , int m ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < m ; i ++ ) { int lowerbound = a [ i ] ; int upperbound = b [ i ] ; for ( int j = lowerbound ; j <= upperbound ; j ++ ) arr [ j ] += k [ i ] ; } int res = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) res = Math . max ( res , arr [ i ] ) ; return res ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 0 , 1 , 2 } ; int b [ ] = { 1 , 4 , 3 } ; int k [ ] = { 100 , 100 , 100 } ; int m = a . length ; System . out . println ( " Maximum ▁ value ▁ after ▁ ' m ' ▁ " + " operations ▁ is ▁ " + findMax ( n , a , b , k , m ) ) ; } }
Summation of GCD of all the pairs up to N | Java approach of finding sum of GCD of all pairs . ; phi [ i ] stores euler totient function for i result [ j ] stores result for value j ; Precomputation of phi [ ] numbers . Refer below link for details : https : goo . gl / LUqdtY ; Refer https : goo . gl / LUqdtY ; Precomputes result for all numbers till MAX ; Precompute all phi value ; Iterate throght all the divisors of i . ; Add summation of previous calculated sum ; Driver code ; Function to calculate sum of all the GCD pairs
import java . lang . * ; class GFG { static final int MAX = 100001 ; static long phi [ ] = new long [ MAX ] ; static long result [ ] = new long [ MAX ] ; static void computeTotient ( ) { phi [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( phi [ i ] == 0 ) { phi [ i ] = i - 1 ; for ( int j = ( i << 1 ) ; j < MAX ; j += i ) { if ( phi [ j ] == 0 ) phi [ j ] = j ; phi [ j ] = ( phi [ j ] / i ) * ( i - 1 ) ; } } } } static void sumOfGcdPairs ( ) { computeTotient ( ) ; for ( int i = 1 ; i < MAX ; ++ i ) { for ( int j = 2 ; i * j < MAX ; ++ j ) result [ i * j ] += i * phi [ j ] ; } for ( int i = 2 ; i < MAX ; i ++ ) result [ i ] += result [ i - 1 ] ; } public static void main ( String [ ] args ) { sumOfGcdPairs ( ) ; int N = 4 ; System . out . println ( " Summation ▁ of ▁ " + N + " ▁ = ▁ " + result [ N ] ) ; N = 12 ; System . out . println ( " Summation ▁ of ▁ " + N + " ▁ = ▁ " + result [ N ] ) ; N = 5000 ; System . out . print ( " Summation ▁ of ▁ " + N + " ▁ = ▁ " + + result [ N ] ) ; } }
Find coordinates of the triangle given midpoint of each side | Java program to find coordinate of the triangle given midpoint of each side ; Return after solving the equations and finding the vertices coordinate . ; Finding sum of all three coordinate . ; Solving the equation . ; Finds vertices of a triangles from given middle vertices . ; Find X coordinates of vertices . ; Find Y coordinates of vertices . ; Output the solution . ; Driver code
import java . util . Vector ; class GFG { static final int N = 3 ; static Vector < Integer > solve ( int v [ ] ) { Vector < Integer > res = new Vector < Integer > ( ) ; int all3 = v [ 0 ] + v [ 1 ] + v [ 2 ] ; res . add ( all3 - v [ 1 ] * 2 ) ; res . add ( all3 - v [ 2 ] * 2 ) ; res . add ( all3 - v [ 0 ] * 2 ) ; return res ; } static void findVertex ( int xmid [ ] , int ymid [ ] ) { Vector < Integer > V1 = solve ( xmid ) ; Vector < Integer > V2 = solve ( ymid ) ; for ( int i = 0 ; i < 3 ; i ++ ) { System . out . println ( V1 . get ( i ) + " ▁ " + V2 . get ( i ) ) ; } } public static void main ( String [ ] args ) { int xmid [ ] = { 5 , 4 , 5 } ; int ymid [ ] = { 3 , 4 , 5 } ; findVertex ( xmid , ymid ) ; } }
N | Java program to find n - th number in the sorted list of multiples of two numbers . ; Return the n - th number in the sorted list of multiples of two numbers . ; Generating first n multiple of a . ; Sorting the sequence . ; Generating and storing first n multiple of b and storing if not present in the sequence . ; If not present in the sequence ; Storing in the sequence . ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int nthElement ( int a , int b , int n ) { ArrayList < Integer > seq = new ArrayList < Integer > ( n * n + 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) seq . add ( a * i ) ; Collections . sort ( seq ) ; for ( int i = 1 , k = n ; i <= n && k > 0 ; i ++ ) { if ( seq . indexOf ( b * i ) == - 1 ) { seq . add ( b * i ) ; Collections . sort ( seq ) ; k -- ; } } return seq . get ( n - 1 ) ; } public static void main ( String [ ] args ) { int a = 3 , b = 5 , n = 5 ; System . out . println ( nthElement ( a , b , n ) ) ; } }
Count pairs of natural numbers with GCD equal to given number | Java program to count pair in range of natural number having GCD equal to given number . ; Return the GCD of two numbers . ; Return the count of pairs having GCD equal to g . ; Setting the value of L , R . ; For each possible pair check if GCD is 1. ; Driver code
import java . util . * ; class GFG { static int gcd ( int a , int b ) { return b > 0 ? gcd ( b , a % b ) : a ; } static int countGCD ( int L , int R , int g ) { L = ( L + g - 1 ) / g ; R = R / g ; int ans = 0 ; for ( int i = L ; i <= R ; i ++ ) for ( int j = L ; j <= R ; j ++ ) if ( gcd ( i , j ) == 1 ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int L = 1 , R = 11 , g = 5 ; System . out . println ( countGCD ( L , R , g ) ) ; } }
Last non | Java program to find last non - zero digit in n ! ; Initialize values of last non - zero digit of numbers from 0 to 9 ; Check whether tens ( or second last ) digit is odd or even If n = 375 , So n / 10 = 37 and ( n / 10 ) % 10 = 7 Applying formula for even and odd cases . ; Driver code
class GFG { static int dig [ ] = { 1 , 1 , 2 , 6 , 4 , 2 , 2 , 4 , 2 , 8 } ; static int lastNon0Digit ( int n ) { if ( n < 10 ) return dig [ n ] ; if ( ( ( n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ; else return ( 4 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . print ( lastNon0Digit ( n ) ) ; } }
Find the first natural number whose factorial is divisible by x | Efficient Java program to find first natural number whose factorial divides x . ; GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial divides x . ; int i = 1 ; Result ; Remove common factors ; We found first i . ; Driver code
class GFG { static int gcd ( int a , int b ) { if ( ( a % b ) == 0 ) return b ; return gcd ( b , a % b ) ; } static int firstFactorialDivisibleNumber ( int x ) { int new_x = x ; for ( i = 1 ; i < x ; i ++ ) { new_x /= gcd ( i , new_x ) ; if ( new_x == 1 ) break ; } return i ; } public static void main ( String [ ] args ) { int x = 16 ; System . out . print ( firstFactorialDivisibleNumber ( x ) ) ; } }
Find the highest occurring digit in prime numbers in a range | Java program to find the highest occurring digit in prime numbers in a range L to R . ; Sieve of Eratosthenes ; Returns maximum occurring digits in primes from l to r . ; Finding the prime number up to R . ; Initialise frequency of all digit to 0. ; For all number between L to R , check if prime or not . If prime , incrementing the frequency of digits present in the prime number . ; int p = i ; If i is prime ; Finding digit with highest frequency . ; Driver code
import java . util . Arrays ; class GFG { static void sieve ( boolean prime [ ] , int n ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = true ; } } static int maxDigitInPrimes ( int L , int R ) { boolean prime [ ] = new boolean [ R + 1 ] ; Arrays . fill ( prime , false ) ; sieve ( prime , R ) ; int freq [ ] = new int [ 10 ] ; int val ; for ( int i = L ; i <= R ; i ++ ) { if ( ! prime [ i ] ) { while ( p > 0 ) { freq [ p % 10 ] ++ ; p /= 10 ; } } } int max = freq [ 0 ] , ans = 0 ; for ( int j = 1 ; j < 10 ; j ++ ) { if ( max <= freq [ j ] ) { max = freq [ j ] ; ans = j ; } } return ( max != 0 ) ? ans : - 1 ; } public static void main ( String [ ] args ) { int L = 1 , R = 20 ; System . out . println ( maxDigitInPrimes ( L , R ) ) ; } }
Sphenic Number | Java program to check whether a number is a Sphenic number or not ; create a global array of size 10001 ; ; This functions finds all primes smaller than ' limit ' using simple sieve of eratosthenes . ; 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 ; to store the 8 divisors ; to count the number of divisor ; finally check if there re 8 divisor and all the numbers are distinct prime no return 1 else return 0 ) ; ; Driver code
import java . util . * ; class GFG { static boolean [ ] arr = new boolean [ 1001 ] ; static void simpleSieve ( ) { Arrays . fill ( arr , true ) ; for ( int p = 2 ; p * p < 1001 ; p ++ ) { if ( arr [ p ] ) { for ( int i = p * 2 ; i < 1001 ; i = i + p ) arr [ i ] = false ; } } } static int find_sphene ( int N ) { int [ ] arr1 = new int [ 8 ] ; int count = 0 ; int j = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( N % i == 0 && count < 8 ) { count ++ ; arr1 [ j ++ ] = i ; } } if ( count == 8 && ( arr [ arr1 [ 1 ] ] && arr [ arr1 [ 2 ] ] && arr [ arr1 [ 3 ] ] ) ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { int n = 60 ; simpleSieve ( ) ; int ans = find_sphene ( n ) ; if ( ans == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " NO " ) ; } }
Common Divisors of Two Numbers | Java implementation of program ; method to calculate gcd of two numbers ; method to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Count divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver method
class Test { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int commDiv ( int a , int b ) { int n = gcd ( a , b ) ; int result = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) result += 1 ; else result += 2 ; } } return result ; } public static void main ( String args [ ] ) { int a = 12 , b = 24 ; System . out . println ( commDiv ( a , b ) ) ; } }