codeparrot/xlcost-text-to-code huggingface dataset:
Find if a degree sequence can form a simple graph | Havel | Java implementation of the approach ; Function that returns true if a simple graph exists ; Keep performing the operations until one of the stopping condition is met ; Sort the list in non - decreasing order ; Check if all the elements are equal to 0 ; Store the first element in a variable and delete it from the list ; Check if enough elements are present in the list ; Subtract first element from next v elements ; Check if negative element is encountered after subtraction ; Driver Code
import java . util . * ; @ SuppressWarnings ( " unchecked " ) class GFG { static boolean graphExists ( ArrayList a , int n ) { while ( true ) { Collections . sort ( a , Collections . reverseOrder ( ) ) ; if ( ( int ) a . get ( 0 ) == 0 ) return true ; int v = ( int ) a . get ( 0 ) ; a . remove ( a . get ( 0 ) ) ; if ( v > a . size ( ) ) return false ; for ( int i = 0 ; i < v ; i ++ ) { a . set ( i , ( int ) a . get ( i ) - 1 ) ; if ( ( int ) a . get ( i ) < 0 ) return false ; } } } public static void main ( String [ ] args ) { ArrayList a = new ArrayList ( ) ; a . add ( 3 ) ; a . add ( 3 ) ; a . add ( 3 ) ; a . add ( 3 ) ; int n = a . size ( ) ; if ( graphExists ( a , n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " NO " ) ; } } }
Number of sub arrays with negative product | Java implementation of the approach ; Function to return the count of subarrays with negative product ; Replace current element with 1 if it is positive else replace it with - 1 instead ; Take product with previous element to form the prefix product ; Count positive and negative elements in the prefix product array ; Return the required count of subarrays ; Driver code
class GFG { static int negProdSubArr ( int arr [ ] , int n ) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 4 , - 3 , 2 , - 5 } ; int n = arr . length ; System . out . println ( negProdSubArr ( arr , n ) ) ; } }
Repeated sum of first N natural numbers | Java implementation of the approach ; Function to return the sum of the first n natural numbers ; Function to return the repeated sum ; Perform the operation exactly k times ; Update n with the sum of first n natural numbers ; Driver code
class GFG { static int sum ( int n ) { int sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } static int repeatedSum ( int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { n = sum ( n ) ; } return n ; } public static void main ( String [ ] args ) { int n = 2 , k = 2 ; System . out . println ( repeatedSum ( n , k ) ) ; } }
Difference between Sum of Cubes and Sum of First N Natural Numbers | Java program to find the difference between the sum of the cubes of the first N natural numbers and the sum of the first N natural number ; Sum of first n natural numbers ; Find the required difference ; Driver Code
class GFG { static int difference ( int n ) { int S , res ; S = ( n * ( n + 1 ) ) / 2 ; res = S * ( S - 1 ) ; return res ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( difference ( n ) ) ; } }
Check if the sum of digits of number is divisible by all of its digits | Java implementation of the approach ; Function that returns true if all the digits of n divide the sum of the digits of n ; Store a copy of the original number ; Find the sum of the digits of n ; Restore the original value ; Check if all the digits divide the calculated sum ; If current digit doesn 't  divide the sum ; Driver code
class GFG { static boolean isDivisible ( long n ) { long temp = n ; int sum = 0 ; while ( n != 0 ) { int digit = ( int ) n % 10 ; sum += digit ; n /= 10 ; } n = temp ; while ( n != 0 ) { int digit = ( int ) n % 10 ; if ( sum % digit != 0 ) return false ; n /= 10 ; } return true ; } public static void main ( String [ ] args ) { long n = 123 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Program for Mobius Function | Set 2 | Java implementation of the approach ; Function to calculate least prime factor of each number ; If it is a prime number ; For all multiples which are not visited yet . ; Function to find the value of Mobius function for all the numbers from 1 to n ; To store the values of Mobius function ; If number is one ; If number has a squared prime factor ; Multiply - 1 with the previous number ; Driver code ; Function to find least prime factor ; Function to find mobius function
import java . util . * ; class GFG { static int N = 100005 ; static int [ ] lpf = new int [ N ] ; static void least_prime_factor ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] % 2 != 1 ) for ( int j = i ; j < N ; j += i ) if ( lpf [ j ] % 2 != 0 ) lpf [ j ] = i ; } static void Mobius ( int n ) { int [ ] mobius = new int [ N ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ i / lpf [ i ] ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ i / lpf [ i ] ] ; } } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( mobius [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 ; Arrays . fill ( lpf , - 1 ) ; least_prime_factor ( ) ; Mobius ( n ) ; } }
Make the list non | Java implementation of the approach ; Function to return the minimum element from the range [ prev , MAX ] such that it differs in at most 1 digit with cur ; To start with the value we have achieved in the last step ; Store the value with which the current will be compared ; Current value ; There are at most 4 digits ; If the current digit differs in both the numbers ; Eliminate last digits in both the numbers ; If the number of different digits is at most 1 ; If we can 't find any number for which  the number of change is less than or  equal to 1 then return -1 ; Function to get the non - decreasing list ; Creating a vector for the updated list ; Let 's assume that it is possible to  make the list non-decreasing ; Element of the original array ; Can 't make the list non-decreasing ; If possible then print the list ; Driver code
import java . util . * ; class GFG { static int DIGITS = 4 , MIN = 1000 , MAX = 9999 ; static int getBest ( int prev , int cur ) { int maximum = Math . max ( MIN , prev ) ; for ( int i = maximum ; i <= MAX ; i ++ ) { int cnt = 0 ; int a = i ; int b = cur ; for ( int k = 0 ; k < DIGITS ; k ++ ) { if ( a % 10 != b % 10 ) cnt += 1 ; a /= 10 ; b /= 10 ; } if ( cnt <= 1 ) return i ; } return - 1 ; } static void getList ( int arr [ ] , int n ) { Vector < Integer > myList = new Vector < Integer > ( ) ; int i , cur ; boolean possible = true ; myList . add ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { cur = arr [ i ] ; myList . add ( getBest ( myList . lastElement ( ) , cur ) ) ; if ( myList . lastElement ( ) == - 1 ) { possible = false ; break ; } } if ( possible ) { for ( i = 1 ; i < myList . size ( ) ; i ++ ) System . out . print ( myList . get ( i ) + " ▁ " ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1095 , 1094 , 1095 } ; int n = arr . length ; getList ( arr , n ) ; } }
Maximum items that can be bought with the given type of coins | Java implementation of the approach ; Function to find maximum fruits Can buy from given values of x , y , z . ; Items of type 1 that can be bought ; Update the coins ; Items of type 2 that can be bought ; Update the coins ; Items of type 3 that can be bought ; Update the coins ; Items of type 4 that can be bought To buy a type 4 item , a coin of each type is required ; Total items that can be bought ; Driver code
import java . io . * ; class GFG { static int COST = 3 ; static int maxItems ( int x , int y , int z ) { int type1 = x / COST ; x %= COST ; int type2 = y / COST ; y %= COST ; int type3 = z / COST ; z %= COST ; int type4 = Math . min ( x , Math . min ( y , z ) ) ; int maxItems = type1 + type2 + type3 + type4 ; return maxItems ; } public static void main ( String [ ] args ) { int x = 4 , y = 5 , z = 6 ; System . out . println ( maxItems ( x , y , z ) ) ; } }
Count occurrences of a prime number in the prime factorization of every element from the given range | Java implementation of the approach ; Function to return the highest power of p that divides n ; Function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; To store the required count ; For every element of the range ; Add the highest power of p that divides i ; Driver code
class GFG { static int countFactors ( int n , int p ) { int pwr = 0 ; while ( n > 0 && n % p == 0 ) { n /= p ; pwr ++ ; } return pwr ; } static int getCount ( int l , int r , int p ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { cnt += countFactors ( i , p ) ; } return cnt ; } public static void main ( String [ ] args ) { int l = 2 , r = 8 , p = 2 ; System . out . println ( getCount ( l , r , p ) ) ; } }
Count occurrences of a prime number in the prime factorization of every element from the given range | Java implementation of the approach ; Function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; To store the required count ; Number of values in the range [ 0 , r ] that are divisible by val ; Number of values in the range [ 0 , l - 1 ] that are divisible by val ; Increment the power of the val ; ( a - b ) is the count of numbers in the range [ l , r ] that are divisible by val ; No values that are divisible by val thus exiting from the loop ; Driver code
import java . util . * ; class GFG { static int getCount ( int l , int r , int p ) { int cnt = 0 ; int val = p ; while ( true ) { int a = r / val ; int b = ( l - 1 ) / val ; val *= p ; if ( ( a - b ) > 0 ) { cnt += ( a - b ) ; } else break ; } return cnt ; } public static void main ( String [ ] args ) { int l = 2 , r = 8 , p = 2 ; System . out . println ( getCount ( l , r , p ) ) ; } }
Check if the number is valid when flipped upside down | Java implementation of the approach ; Function that returns true if str is Topsy Turvy ; For every character of the string ; If the current digit cannot form a valid digit when turned upside - down ; Driver code
import java . util . * ; class GFG { static boolean topsyTurvy ( char [ ] str ) { for ( int i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == '2' str [ i ] == '4' str [ i ] == '5' str [ i ] == '6' str [ i ] == '7' str [ i ] == '9' ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = "1234" ; if ( topsyTurvy ( str . toCharArray ( ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the count of subsequences where each element is divisible by K | Java implementation of the approach ; Function to return the count of all valid subsequences ; To store the count of elements which are divisible by k ; If current element is divisible by k then increment the count ; Total ( 2 ^ n - 1 ) non - empty subsequences are possible with n element ; Driver code
import java . util . * ; class GFG { static int countSubSeq ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { count ++ ; } } return ( int ) ( Math . pow ( 2 , count ) - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 6 } ; int n = arr . length ; int k = 3 ; System . out . println ( countSubSeq ( arr , n , k ) ) ; } }
Count of numbers below N whose sum of prime divisors is K | Java implementation of the approach ; Function to return the count of numbers below N whose sum of prime factors is K ; To store the sum of prime factors for all the numbers ; If i is prime ; Add i to all the numbers which are divisible by i ; To store the count of required numbers ; Return the required count ; Driver code
import java . util . * ; class GFG { static int MAX = 1000001 ; static int countNum ( int N , int K ) { int [ ] sumPF = new int [ MAX ] ; for ( int i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == 0 ) { for ( int j = i ; j < N ; j += i ) { sumPF [ j ] += i ; } } } int count = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == K ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int N = 20 , K = 7 ; System . out . println ( countNum ( N , K ) ) ; } }
Queries for the smallest and the largest prime number of given digit | Java implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to return the smallest prime number with d digits ; check if prime ; Function to return the largest prime number with d digits ; check if prime ; Driver code ; Perform queries
import java . util . * ; class GFG { static int MAX = 100000 ; static boolean [ ] prime = new boolean [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX + 1 ; i ++ ) { prime [ i ] = true ; } for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static int smallestPrime ( int d ) { int l = ( int ) Math . pow ( 10 , d - 1 ) ; int r = ( int ) Math . pow ( 10 , d ) - 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( prime [ i ] ) { return i ; } } return - 1 ; } static int largestPrime ( int d ) { int l = ( int ) Math . pow ( 10 , d - 1 ) ; int r = ( int ) Math . pow ( 10 , d ) - 1 ; for ( int i = r ; i >= l ; i -- ) { if ( prime [ i ] ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int queries [ ] = { 2 , 5 } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( smallestPrime ( queries [ i ] ) + " ▁ " + largestPrime ( queries [ i ] ) ) ; } } }
Find a Square Matrix such that sum of elements in every row and column is K | Java implementation of the approach ; Function to print the required matrix ; Print k for the left diagonal elements ; Print 0 for the rest ; Driver code
import java . util . * ; class GFG { static void printMatrix ( int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) System . out . print ( k + " ▁ " ) ; else System . out . print ( "0 ▁ " ) ; } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 3 , k = 7 ; printMatrix ( n , k ) ; } }
Divide first N natural numbers into 3 equal sum subsets | Java implementation of the approach ; Function that returns true if the subsets are possible ; If n <= 3 then it is not possible to divide the elements in three subsets satisfying the given conditions ; Sum of all the elements in the range [ 1 , n ] ; If the sum is divisible by 3 then it is possible ; Driver code
import java . math . * ; class GFG { public static boolean possible ( int n ) { if ( n > 3 ) { int sum = ( n * ( n + 1 ) ) / 2 ; if ( sum % 3 == 0 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 5 ; if ( possible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the Nth element of the modified Fibonacci series | Java implementation of the approach ; Function to return the Nth number of the modified Fibonacci series where A and B are the first two terms ; To store the current element which is the sum of previous two elements of the series ; This loop will terminate when the Nth element is found ; Return the Nth element ; Driver code
import java . util . * ; class GFG { static int findNthNumber ( int A , int B , int N ) { int sum = 0 ; for ( int i = 2 ; i < N ; i ++ ) { sum = A + B ; A = B ; B = sum ; } return sum ; } public static void main ( String [ ] args ) { int A = 5 , B = 7 , N = 10 ; System . out . println ( findNthNumber ( A , B , N ) ) ; } }
Check if an array is increasing or decreasing | Java implementation of the approach ; Function to check the type of the array ; If the first two and the last two elements of the array are in increasing order ; If the first two and the last two elements of the array are in decreasing order ; If the first two elements of the array are in increasing order and the last two elements of the array are in decreasing order ; If the first two elements of the array are in decreasing order and the last two elements of the array are in increasing order ; Driver code
import java . math . * ; class GFG { public static void checkType ( int arr [ ] , int n ) { if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] <= arr [ n - 1 ] ) System . out . println ( " Increasing " ) ; else if ( arr [ 0 ] >= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) System . out . println ( " Decreasing " ) ; else if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) System . out . println ( " Increasing ▁ then ▁ decreasing " ) ; else System . out . println ( " Decreasing ▁ then ▁ increasing " ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 } ; int n = arr . length ; checkType ( arr , n ) ; } }
Calculate the IST : Indian Standard Time | Java implementation of the approach ; Function to calculate Indian Standard Time ; Separate integer part ; Separate float part and return ceil value ; Driver code ; Number of hours ( 1 - 24 ) ; Rotations in degrees
import java . math . * ; class GFG { public static void cal_IST ( int h , double r ) { double IST = ( h * r * 1.0 ) / 360 ; int int_IST = ( int ) IST ; int float_IST = ( int ) Math . ceil ( ( int ) ( ( IST - int_IST ) * 60 ) ) ; System . out . println ( int_IST + " : " + float_IST ) ; } public static void main ( String [ ] args ) { int h = 20 ; double r = 150 ; cal_IST ( h , r ) ; } }
Check if it is possible to perform the given Grid Division | Java implementation of the approach ; Function that returns true if it is possible to divide the grid satisfying the given conditions ; To store the sum of all the cells of the given parts ; If the sum is equal to the total number of cells in the given grid ; Driver code
import java . util . * ; class GFG { static boolean isPossible ( int arr [ ] , int p , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < p ; i ++ ) sum += arr [ i ] ; if ( sum == ( n * m ) ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 3 , m = 4 ; int arr [ ] = { 6 , 3 , 2 , 1 } ; int p = arr . length ; if ( isPossible ( arr , p , n , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Minimum steps to make the product of the array equal to 1 | Java implementation of the approach ; Function to return the minimum steps required ; To store the count of 0 s , positive and negative numbers ; To store the ans ; If array element is equal to 0 ; If array element is a negative number ; Extra cost needed to make it - 1 ; If array element is a positive number ; Extra cost needed to make it 1 ; Now the array will have - 1 , 0 and 1 only ; As count of negative is even so we will change all 0 to 1 total cost here will be count of 0 s ; If there are zeroes present in the array ; Change one zero to - 1 and rest of them to 1 Total cost here will be count of '0' ; If there are no zeros in the array ; As no 0 s are available so we have to change one - 1 to 1 which will cost 2 to change - 1 to 1 ; Driver code
class GFG { static int MinStep ( int a [ ] , int n ) { int positive = 0 , negative = 0 , zero = 0 ; int step = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } else if ( a [ i ] < 0 ) { negative ++ ; step = step + ( - 1 - a [ i ] ) ; } else { positive ++ ; step = step + ( a [ i ] - 1 ) ; } } if ( negative % 2 == 0 ) { step = step + zero ; } else { if ( zero > 0 ) { step = step + zero ; } else { step = step + 2 ; } } return step ; } public static void main ( String [ ] args ) { int a [ ] = { 0 , - 2 , - 1 , - 3 , 4 } ; int n = a . length ; System . out . println ( MinStep ( a , n ) ) ; } }
Find the first N integers such that the sum of their digits is equal to 10 | Java implementation of the approach ; Function to return the sum of digits of n ; Add the last digit to the sum ; Remove last digit ; Return the sum of digits ; Function to print the first n numbers whose sum of digits is 10 ; First number of the series is 19 ; If the sum of digits of the current number is equal to 10 ; Print the number ; Add 9 to the previous number ; Driver code
import java . util . * ; class GFG { static int sum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static void firstN ( int n ) { int num = 19 , cnt = 1 ; while ( cnt != n ) { if ( sum ( num ) == 10 ) { System . out . print ( num + " ▁ " ) ; cnt ++ ; } num += 9 ; } } public static void main ( String [ ] args ) { int n = 10 ; firstN ( n ) ; } }
Sum of last digit of all integers from 1 to N divisible by M | Java implementation of the approach ; Function to return the required sum ; Number of element between 1 to n divisible by m ; Array to store the last digit of elements in a cycle ; Storing and adding last digit of cycle ; Number of elements present in last cycle ; Sum of k / 10 cycle ; Adding value of digits of last cycle to the answer ; Driver Code ; input n and m
import java . io . * ; class GFG { static long sumOfLastDig ( long n , long m ) { long sum = 0 , k ; k = n / m ; long [ ] arr = new long [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { arr [ i ] = m * ( i + 1 ) % 10 ; sum += arr [ i ] ; } long rem = k % 10 ; long ans = ( k / 10 ) * sum ; for ( int i = 0 ; i < rem ; i ++ ) { ans += arr [ i ] ; } return ans ; } public static void main ( String [ ] args ) { long n = 100 , m = 3 ; System . out . println ( sumOfLastDig ( n , m ) ) ; } }
Number of Subsequences with Even and Odd Sum | Set 2 | Java program to find number of Subsequences with Even and Odd Sum ; Function to find number of Subsequences with Even and Odd Sum ; Counting number of odds ; Even count ; Total Subsequences is ( 2 ^ n - 1 ) For NumberOfEvenSubsequences subtract NumberOfOddSubsequences from total ; Driver code ; Calling the function
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static pair countSum ( int arr [ ] , int n ) { int NumberOfOdds = 0 , NumberOfEvens = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 1 ) NumberOfOdds ++ ; NumberOfEvens = n - NumberOfOdds ; int NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ; int NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ; return new pair ( NumberOfEvenSubsequences , NumberOfOddSubsequences ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = arr . length ; pair ans = countSum ( arr , n ) ; System . out . print ( " EvenSum ▁ = ▁ " + ans . first ) ; System . out . print ( " ▁ OddSum ▁ = ▁ " + ans . second ) ; } }
Program to find the next prime number | Java implementation of the approach ; Function that returns true if n is prime else returns false ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; Driver code
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 int nextPrime ( int N ) { if ( N <= 1 ) return 2 ; int prime = N ; boolean found = false ; while ( ! found ) { prime ++ ; if ( isPrime ( prime ) ) found = true ; } return prime ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( nextPrime ( N ) ) ; } }