codeparrot/xlcost-text-to-code huggingface dataset:
Minimum numbers needed to express every integer below N as a sum | Java program to find count of integers needed to express all numbers from 1 to N ; function to count length of binary expression of n ; Driver code
import java . io . * ; class GFG { static int countBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n >>= 1 ; } return count ; } public static void main ( String [ ] args ) { int n = 32 ; System . out . println ( " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = ▁ " + countBits ( n ) ) ; } }
Check if a number is an Achilles number or not | Program to check if the Given number is an Achilles Number ; function to check if the number is powerful number ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Utility function to check if number is a perfect power or not ; Function to check Achilles Number ; Driver Program
class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static boolean isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } static boolean isAchillesNumber ( int n ) { if ( isPowerful ( n ) && ! isPower ( n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 72 ; if ( isAchillesNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; n = 36 ; if ( isAchillesNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Program to find count of numbers having odd number of divisors in given range | Java program to find count of numbers having odd number of divisors in given range ; Function to count numbers having odd number of divisors in range [ A , B ] ; variable to odd divisor count ; iterate from a to b and count their number of divisors ; variable to divisor count ; if count of divisor is odd then increase res by 1 ; Driver code
import java . io . * ; class GFG { static int OddDivCount ( int a , int b ) { int res = 0 ; for ( int i = a ; i <= b ; ++ i ) { int divCount = 0 ; for ( int j = 1 ; j <= i ; ++ j ) { if ( i % j == 0 ) { ++ divCount ; } } if ( ( divCount % 2 ) != 0 ) { ++ res ; } } return res ; } public static void main ( String [ ] args ) { int a = 1 , b = 10 ; System . out . println ( OddDivCount ( a , b ) ) ; } }
Largest factor of a given number which is a perfect square | Java program to find the largest factor of a number which is also a perfect square ; Function to find the largest factor of a given number which is a perfect square ; Initialise the answer to 1 ; Finding the prime factors till sqrt ( num ) ; Frequency of the prime factor in the factorisation initialised to 0 ; If the frequency is odd then multiply i frequency - 1 times to the answer ; Else if it is even , multiply it frequency times ; Driver Code
class GFG { static int largestSquareFactor ( int num ) { int answer = 1 ; for ( int i = 2 ; i < Math . sqrt ( num ) ; ++ i ) { int cnt = 0 ; int j = i ; while ( num % j == 0 ) { cnt ++ ; j *= i ; } if ( ( cnt & 1 ) != 0 ) { cnt -- ; answer *= Math . pow ( i , cnt ) ; } else { answer *= Math . pow ( i , cnt ) ; } } return answer ; } public static void main ( String args [ ] ) { int N = 420 ; System . out . println ( largestSquareFactor ( N ) ) ; } }
Find the Nth term of the series 2 + 6 + 13 + 23 + . . . | Java program to find Nth term of the series 2 + 6 + 13 + 23 + 36 + ... ; calculate Nth term of given series ; Driver code
import java . io . * ; class GFG { static int Nth_Term ( int n ) { return ( int ) ( 3 * Math . pow ( n , 2 ) - n + 2 ) / ( 2 ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( Nth_Term ( N ) ) ; } }
All possible numbers of N digits and base B without leading zeros | Java implementation of the approach ; function to count all permutations ; count of all permutations ; count of permutations with leading zeros ; Return the permutations without leading zeros ; Driver code
class GFG { static void countPermutations ( int N , int B ) { int x = ( int ) Math . pow ( B , N ) ; int y = ( int ) Math . pow ( B , N - 1 ) ; System . out . println ( x - y ) ; } public static void main ( String [ ] args ) { int N = 6 ; int B = 4 ; countPermutations ( N , B ) ; } }
Absolute difference between the Product of Non | Java program to find the Absolute Difference between the Product of Non - Prime numbers and Prime numbers of an Array ; Function to find the difference between the product of non - primes and the product of primes of an array . ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the product of primes in P1 and the product of non primes in P2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference
import java . util . * ; import java . util . Arrays ; import java . util . Collections ; class GFG { public static int calculateDifference ( int [ ] arr , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; boolean [ ] prime = new boolean [ max_val + 1 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int P1 = 1 , P2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { P1 *= arr [ i ] ; } else if ( arr [ i ] != 1 ) { P2 *= arr [ i ] ; } } return Math . abs ( P2 - P1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . length ; System . out . println ( calculateDifference ( arr , n ) ) ; System . exit ( 0 ) ; } }
Maximum count of equal numbers in an array after performing given operations | Java program to find the maximum number of equal numbers in an array ; Function to find the maximum number of equal numbers in an array ; to store sum of elements ; if sum of numbers is not divisible by n ; Driver code ; size of an array
public class GFG { static int EqualNumbers ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; if ( sum % n != 0 ) return n - 1 ; return n ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 4 , 1 } ; int n = a . length ; System . out . println ( EqualNumbers ( a , n ) ) ; } }
Count number of ordered pairs with Even and Odd Sums | Java implementation of the above approach ; function to count odd sum pair ; if number is even ; if number is odd ; count of ordered pairs ; function to count even sum pair ; Driver code
class GFG { static int count_odd_pair ( int n , int a [ ] ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } int ans = odd * even * 2 ; return ans ; } static int count_even_pair ( int odd_sum_pairs , int n ) { int total_pairs = ( n * ( n - 1 ) ) ; int ans = total_pairs - odd_sum_pairs ; return ans ; } public static void main ( String [ ] args ) { int n = 6 ; int [ ] a = { 2 , 4 , 5 , 9 , 1 , 8 } ; int odd_sum_pairs = count_odd_pair ( n , a ) ; int even_sum_pairs = count_even_pair ( odd_sum_pairs , n ) ; System . out . println ( " Even ▁ Sum ▁ Pairs ▁ = ▁ " + even_sum_pairs ) ; System . out . println ( " Odd ▁ Sum ▁ Pairs = ▁ " + odd_sum_pairs ) ; } }
Steps required to visit M points in order on a circular ring of N points | Java implementation of the approach ; Function to count the steps required ; Start at 1 ; Initialize steps ; If nxt is greater than cur ; Now we are at a [ i ] ; Driver code
class GFG { static int findSteps ( int n , int m , int a [ ] ) { int cur = 1 ; int steps = 0 ; for ( int i = 0 ; i < m ; i ++ ) { if ( a [ i ] >= cur ) steps += ( a [ i ] - cur ) ; else steps += ( n - cur + a [ i ] ) ; cur = a [ i ] ; } return steps ; } public static void main ( String [ ] args ) { int n = 3 , m = 3 ; int a [ ] = { 2 , 1 , 2 } ; System . out . println ( findSteps ( n , m , a ) ) ; } }
Program to Convert Hexadecimal Number to Binary | Java program to convert Hexadecimal number to Binary ; function to convert Hexadecimal to Binary Number ; Driver code ; Get the Hexadecimal number ; Convert HexaDecimal to Binary
public class Improve { static void HexToBin ( char hexdec [ ] ) { int i = 0 ; while ( hexdec [ i ] != ' \u0000' ) { switch ( hexdec [ i ] ) { case '0' : System . out . print ( "0000" ) ; break ; case '1' : System . out . print ( "0001" ) ; break ; case '2' : System . out . print ( "0010" ) ; break ; case '3' : System . out . print ( "0011" ) ; break ; case '4' : System . out . print ( "0100" ) ; break ; case '5' : System . out . print ( "0101" ) ; break ; case '6' : System . out . print ( "0110" ) ; break ; case '7' : System . out . print ( "0111" ) ; break ; case '8' : System . out . print ( "1000" ) ; break ; case '9' : System . out . print ( "1001" ) ; break ; case ' A ' : case ' a ' : System . out . print ( "1010" ) ; break ; case ' B ' : case ' b ' : System . out . print ( "1011" ) ; break ; case ' C ' : case ' c ' : System . out . print ( "1100" ) ; break ; case ' D ' : case ' d ' : System . out . print ( "1101" ) ; break ; case ' E ' : case ' e ' : System . out . print ( "1110" ) ; break ; case ' F ' : case ' f ' : System . out . print ( "1111" ) ; break ; default : System . out . print ( " Invalid hexadecimal digit " + hexdec[i]); } i ++ ; } } public static void main ( String args [ ] ) { String s = "1AC5" ; char hexdec [ ] = new char [ 100 ] ; hexdec = s . toCharArray ( ) ; System . out . print ( " Equivalent Binary value is : "); try { HexToBin ( hexdec ) ; } catch ( ArrayIndexOutOfBoundsException e ) { System . out . print ( " " ) ; } } }
Count unordered pairs ( i , j ) such that product of a [ i ] and a [ j ] is power of two | Java program to Count unordered pairs ( i , j ) in array such that product of a [ i ] and a [ j ] can be expressed as power of two ; Function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to Count unordered pairs ; is a number can be expressed as power of two ; count total number of unordered pairs ; Driver code
import java . io . * ; class GFG { static boolean isPowerOfTwo ( int x ) { return ( x > 0 && ( ! ( ( x & ( x - 1 ) ) > 0 ) ) ) ; } static void Count_pairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) ) count ++ ; } int ans = ( count * ( count - 1 ) ) / 2 ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 5 , 8 , 16 , 128 } ; int n = a . length ; Count_pairs ( a , n ) ; } }
Ways of dividing a group into two halves such that two elements are in different groups | Java Program to count Number of ways in which two Beautiful girls are in different group ; This function will return the factorial of a given number ; This function will calculate nCr of given n and r ; This function will Calculate number of ways ; Driver Code
import java . io . * ; class GFG { static int factorial ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = result * i ; return result ; } static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } static int calculate_result ( int n ) { int result = 2 * nCr ( ( n - 2 ) , ( n / 2 - 1 ) ) ; return result ; } public static void main ( String [ ] args ) { int a = 2 , b = 4 ; System . out . println ( calculate_result ( 2 * a ) ) ; System . out . print ( calculate_result ( 2 * b ) ) ; } }
Print values of ' a ' in equation ( a + b ) <= n and a + b is divisible by x | Java program to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; function to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; least possible which is divisible by x ; run a loop to get required answer ; increase value by x ; answer is possible ; Driver code ; function call
import java . io . * ; class GFG { static void PossibleValues ( int b , int x , int n ) { int leastdivisible = ( b / x + 1 ) * x ; int flag = 1 ; while ( leastdivisible <= n ) { if ( leastdivisible - b >= 1 ) { System . out . print ( leastdivisible - b + " ▁ " ) ; leastdivisible += x ; flag = 0 ; } else break ; } if ( flag > 0 ) System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int b = 10 , x = 6 , n = 40 ; PossibleValues ( b , x , n ) ; } }
Check if all sub | Java program to check if all sub - numbers have distinct Digit product ; Function to calculate product of digits between given indexes ; Function to check if all sub - numbers have distinct Digit product ; Length of number N ; Digit array ; List to maintain digit products ; Finding all possible subarrays ; Driver code
import java . io . * ; import java . util . * ; public class GFG { static int digitProduct ( int [ ] digits , int start , int end ) { int pro = 1 ; for ( int i = start ; i <= end ; i ++ ) { pro *= digits [ i ] ; } return pro ; } static boolean isDistinct ( int N ) { String s = " " + N ; int len = s . length ( ) ; int [ ] digits = new int [ len ] ; ArrayList < Integer > products = new ArrayList < > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { digits [ i ] = Integer . parseInt ( " " + s . charAt ( i ) ) ; } for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i ; j < len ; j ++ ) { int val = digitProduct ( digits , i , j ) ; if ( products . contains ( val ) ) return false ; else products . add ( val ) ; } } return true ; } public static void main ( String args [ ] ) { int N = 324 ; if ( isDistinct ( N ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Ulam Number Sequence | JAVA code to print nth Ulam number ; Array to store Ulam Number ; function to compute ulam Number ; Set to search specific Ulam number efficiently ; push First 2 two term of the sequence in the array and set for further calculation ; loop to generate Ulam number ; traverse the array and check if i can be represented as sum of two distinct element of the array ; Check if i - arr [ j ] exist in the array or not using set If yes , Then i can be represented as sum of arr [ j ] + ( i - arr [ j ] ) ; if Count is greater than 2 break the loop ; If count is 2 that means i can be represented as sum of two distinct terms of the sequence ; i is ulam number ; Driver code ; pre compute Ulam Number sequence ; print nth Ulam number
import java . util . * ; class GFG { static final int MAX = 10000 ; static Vector < Integer > arr = new Vector < Integer > ( ) ; static void ulam ( ) { Set < Integer > s = new HashSet < Integer > ( ) ; arr . add ( 1 ) ; s . add ( 1 ) ; arr . add ( 2 ) ; s . add ( 2 ) ; for ( int i = 3 ; i < MAX ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr . size ( ) ; j ++ ) { if ( s . contains ( i - arr . get ( j ) ) && arr . get ( j ) != ( i - arr . get ( j ) ) ) count ++ ; if ( count > 2 ) break ; } if ( count == 2 ) { arr . add ( i ) ; s . add ( i ) ; } } } public static void main ( String [ ] args ) { ulam ( ) ; int n = 9 ; System . out . println ( arr . get ( n - 1 ) ) ; } }
Program to find the nth Kynea number | JAVA code to find nth Kynea number ; Function to calculate nth kynea number ; Firstly calculate 2 ^ n + 1 ; Now calculate ( 2 ^ n + 1 ) ^ 2 ; Now calculate ( ( 2 ^ n + 1 ) ^ 2 ) - 2 ; return nth Kynea number ; Driver Program ; print nth kynea number
class GFG { static long nthKyneaNumber ( int n ) { n = ( 1 << n ) + 1 ; n = n * n ; n = n - 2 ; return n ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( nthKyneaNumber ( n ) ) ; } }
Program to find the nth Kynea number | JAVA code to find nth Kynea number ; Function to calculate nth kynea number ; Calculate nth kynea number ; Driver Program ; print nth kynea number
class GFG { static long nthKyneaNumber ( int n ) { return ( ( 1 << ( 2 * n ) ) + ( 1 << ( n + 1 ) ) - 1 ) ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( nthKyneaNumber ( n ) ) ; } }
Program to check whether a number is Proth number or not | Java program to check for Proth number ; Utility function to check power of two ; Function to check if the Given number is Proth number or not ; check if k divides n or not ; Check if n / k is power of 2 or not ; update k to next odd number ; If we reach here means there exists no value of K Such that k is odd number and n / k is a power of 2 greater than k ; Driver code ; Get n ; Check n for Proth Number
class GFG { static boolean isPowerOfTwo ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static boolean isProthNumber ( int n ) { int k = 1 ; while ( k < ( n / k ) ) { if ( n % k == 0 ) { if ( isPowerOfTwo ( n / k ) ) return true ; } k = k + 2 ; } return false ; } public static void main ( String [ ] args ) { int n = 25 ; if ( isProthNumber ( n - 1 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Find last two digits of sum of N factorials | Java program to find the unit place digit of the first N natural numbers factorials ; Function to find the unit ' s ▁ and ▁ ten ' s place digit ; Let us write for cases when N is smaller than or equal to 10. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ... + 10 ! ) % 100 = 13 else ( N >= 10 ) ; Driver code
public class AAA { static int get_last_two_digit ( long N ) { if ( N <= 10 ) { long ans = 0 , fac = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fac = fac * i ; ans += fac ; } return ( int ) ans % 100 ; } return 13 ; } public static void main ( String [ ] args ) { long N = 1 ; for ( N = 1 ; N <= 10 ; N ++ ) System . out . println ( " For ▁ N ▁ = ▁ " + N + " ▁ : ▁ " + get_last_two_digit ( N ) ) ; } }
Check whether product of ' n ' numbers is even or odd | Java implementation to check whether product of ' n ' numbers is even or odd ; function to check whether product of ' n ' numbers is even or odd ; if a single even number is found , then final product will be an even number ; product is an odd number ; Driver code
public class GFG { static boolean isProductEven ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) == 0 ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 3 , 5 } ; int n = arr . length ; if ( isProductEven ( arr , n ) ) System . out . println ( " Even " ) ; else System . out . println ( " Odd " ) ; } }
Sum of squares of Fibonacci numbers | Java Program to find sum of squares of Fibonacci numbers ; Function to calculate sum of squares of Fibonacci numbers ; Initialize result ; Add remaining terms ; Driver code
public class Improve { static int calculateSquareSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = ( fibo [ 0 ] * fibo [ 0 ] ) + ( fibo [ 1 ] * fibo [ 1 ] ) ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += ( fibo [ i ] * fibo [ i ] ) ; } return sum ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " + calculateSquareSum ( n ) ) ; } }
Value of the series ( 1 ^ 3 + 2 ^ 3 + 3 ^ 3 + ... + n ^ 3 ) mod 4 for a given n | Java implementation of the approach ; function for obtaining the value of f ( n ) mod 4 ; Find the remainder of n when divided by 4 ; If n is of the form 4 k or 4 k + 3 ; If n is of the form 4 k + 1 or 4 k + 2 ; Driver code
import java . io . * ; class GFG { static int fnMod ( int n ) { int rem = n % 4 ; if ( rem == 0 rem == 3 ) return 0 ; else if ( rem == 1 rem == 2 ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . print ( fnMod ( n ) ) ; } }
Minimum increment operations to make the array in increasing order | Java program to find minimum moves required to make the array in increasing order ; function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int MinimumMoves ( int a [ ] , int n , int x ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= a [ i - 1 ] ) { int p = ( a [ i - 1 ] - a [ i ] ) / x + 1 ; ans += p ; a [ i ] += p * x ; } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 3 , 2 } ; int x = 2 ; int n = arr . length ; System . out . println ( MinimumMoves ( arr , n , x ) ) ; } }
Check if a large number is divisible by 2 , 3 and 5 or not | Java program to Check if a large number is divisible by 2 , 3 and 5 or not . ; function to return sum of digits of a number ; function to Check if a large number is divisible by 2 , 3 and 5 or not ; Driver code
class GFG { static int SumOfDigits ( String str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( str . charAt ( i ) - '0' ) ; return sum ; } static boolean Divisible ( String str , int n ) { if ( SumOfDigits ( str , n ) % 3 == 0 && str . charAt ( n - 1 ) == '0' ) return true ; return false ; } public static void main ( String [ ] args ) { String str = "263730746028908374890" ; int n = str . length ( ) ; if ( Divisible ( str , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Count all the numbers in a range with smallest factor as K | Java program to find the count of numbers in a range whose smallest factor is K ; Function to check if k is a prime number or not ; Corner case ; Check from 2 to n - 1 ; Function to check if a number is not divisible by any number between 2 and K - 1 ; to check if the num is divisible by any numbers between 2 and k - 1 ; if not divisible by any number between 2 and k - 1 but divisible by k ; Function to find count of numbers in range [ a , b ] with smallest factor as K ; a number can be divisible only by k and not by any number less than k only if k is a prime ; to check if a number has smallest factor as K ; Driver code
public class GFG { static boolean isPrime ( int k ) { if ( k <= 1 ) return false ; for ( int i = 2 ; i < k ; i ++ ) if ( k % i == 0 ) return false ; return true ; } static int check ( int num , int k ) { int flag = 1 ; for ( int i = 2 ; i < k ; i ++ ) { if ( num % i == 0 ) flag = 0 ; } if ( flag == 1 ) { if ( num % k == 0 ) return 1 ; else return 0 ; } else return 0 ; } static int findCount ( int a , int b , int k ) { int count = 0 ; if ( ! isPrime ( k ) ) return 0 ; else { int ans ; for ( int i = a ; i <= b ; i ++ ) { ans = check ( i , k ) ; if ( ans == 1 ) count ++ ; else continue ; } } return count ; } public static void main ( String args [ ] ) { int a = 2020 , b = 6300 , k = 29 ; System . out . println ( findCount ( a , b , k ) ) ; } }