codeparrot/xlcost-text-to-code huggingface dataset:
Elements of Array which can be expressed as power of prime numbers | Java program to print all elements of Array which can be expressed as power of prime numbers ; Function to mark all the exponent of prime numbers ; If number is prime then marking all of its exponent true ; Function to display all required elements ; Function to print the required numbers ; To find the largest number ; Function call to mark all the Exponential prime nos . ; Function call ; Driver code
import java . util . * ; class GFG { static void ModifiedSieveOfEratosthenes ( int N , boolean Expo_Prime [ ] ) { boolean [ ] primes = new boolean [ N ] ; Arrays . fill ( primes , true ) ; for ( int i = 2 ; i < N ; i ++ ) { if ( primes [ i ] ) { int no = i ; while ( no <= N ) { Expo_Prime [ no ] = true ; no *= i ; } for ( int j = i * i ; j < N ; j += i ) primes [ j ] = false ; } } } static void Display ( int arr [ ] , boolean Expo_Prime [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( Expo_Prime [ arr [ i ] ] ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void FindExpoPrime ( int arr [ ] , int n ) { int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) max = arr [ i ] ; } boolean [ ] Expo_Prime = new boolean [ max + 1 ] ; ModifiedSieveOfEratosthenes ( max + 1 , Expo_Prime ) ; Display ( arr , Expo_Prime , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 9 , 16 , 1 , 3 , 12 , 36 , 625 , 1000 } ; int n = arr . length ; FindExpoPrime ( arr , n ) ; } }
Elements of Array which can be expressed as power of some integer to given exponent K | Java implementation to print elements of the Array which can be expressed as power of some integer to given exponent K ; Method returns Nth power of A ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by Integer . MAX_VALUE ; x ^ K denotes current value of x ; loop untill we reach desired accuracy ; calculating current value from previous value by newton 's method ; Function to check whether its k root is an integer or not ; Function to find the numbers ; Driver code
class GFG { static double nthRoot ( long A , long N ) { double xPre = 7 ; double eps = 1e-3 ; double delX = Integer . MAX_VALUE ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static boolean check ( long no , int k ) { double kth_root = nthRoot ( no , k ) ; long num = ( long ) kth_root ; if ( Math . abs ( num - kth_root ) < 1e-4 ) return true ; return false ; } static void printExpo ( long arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( check ( arr [ i ] , k ) ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int K = 6 ; long arr [ ] = { 46656 , 64 , 256 , 729 , 16 , 1000 } ; int n = arr . length ; printExpo ( arr , n , K ) ; } }
Count of subsequences whose product is a difference of square of two integers | Java implementation to count the number of contiguous subsequences whose product can be expressed as the square of difference of two integers ; Function to count the number of contiguous subsequences whose product can be expressed as square of difference of two integers ; Iterating through the array ; Check if that number can be expressed as the square of difference of two numbers ; Variable to compute the product ; Finding the remaining subsequences ; Check if that number can be expressed as the square of difference of two numbers ; Return the number of subsequences ; Driver code
class GFG { static int CntcontSubs ( int a [ ] , int n ) { int c = 0 , d = 0 , i , sum = 1 , j ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 a [ i ] % 4 == 0 ) d ++ ; sum = a [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { sum = sum * a [ j ] ; if ( sum % 2 != 0 sum % 4 == 0 ) c ++ ; } sum = 1 ; } return c + d ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 2 , 9 , 8 } ; int n = arr . length ; System . out . print ( CntcontSubs ( arr , n ) ) ; } }
Count of subsequences whose product is a difference of square of two integers | Java implementation to count all the contiguous subsequences whose product is expressed as the square of the difference of two integers ; Function to count all the contiguous subsequences whose product is expressed as the square of the difference of two integers ; Creating vectors to store the remainders and the subsequences ; Iterating through the array ; Finding the remainder when the element is divided by 4 ; Bringing all the elements in the range [ 0 , 3 ] ; If the remainder is 2 , store the index of the ; If the remainder is 2 , store the index of the ; Finding the total number of subsequences ; If there are no numbers which yield the remainder 2 ; Iterating through the vector ; If the element is 2 , find the nearest 2 or 0 and find the number of elements between them ; Returning the count ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int CntcontSubs ( int a [ ] , int n ) { int prod = 1 ; Vector < pair > vect = new Vector < pair > ( ) ; vect . add ( new pair ( 0 , 2 ) ) ; Vector < Integer > two = new Vector < Integer > ( ) ; Vector < Integer > zero = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = a [ i ] % 4 ; if ( a [ i ] < 0 ) a [ i ] = a [ i ] + 4 ; if ( a [ i ] == 2 ) two . add ( i + 1 ) ; if ( a [ i ] == 0 ) zero . add ( i + 1 ) ; if ( a [ i ] == 0 a [ i ] == 2 ) vect . add ( new pair ( i + 1 , a [ i ] ) ) ; } vect . add ( new pair ( n + 1 , 2 ) ) ; int total = ( n * ( n + 1 ) ) / 2 ; if ( two . isEmpty ( ) ) return total ; else { int sum = 0 ; int pos1 = - 1 , pos2 = - 1 , pos3 = - 1 ; int sz = vect . size ( ) ; for ( int i = 1 ; i + 1 < sz ; i ++ ) { if ( vect . get ( i ) . second == 2 ) { sum += ( vect . get ( i ) . first - vect . get ( i - 1 ) . first ) * ( vect . get ( i + 1 ) . first - vect . get ( i ) . first ) - 1 ; } } return total - sum - two . size ( ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 2 , 9 , 8 } ; int n = a . length ; System . out . print ( CntcontSubs ( a , n ) ) ; } }
Count of possible subarrays and subsequences using given length of Array | Java implementation to count the subarray and subsequence of given length of the array ; Function to count the subarray for the given array ; Function to count the subsequence for the given array length ; Driver Code
class GFG { static int countSubarray ( int n ) { return ( ( n ) * ( n + 1 ) ) / 2 ; } static int countSubsequence ( int n ) { return ( int ) Math . pow ( 2 , n ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( ( countSubarray ( n ) ) + "NEW_LINE"); System . out . print ( ( countSubsequence ( n ) ) + "NEW_LINE"); } }
Program for finding the Integral of a given function using Boole 's Rule | Java program to implement Boole 's Rule on the given function ; Function to return the value of f ( x ) for the given value of x ; Function to computes the integrand of y at the given intervals of x with step size h and the initial limit a and final limit b ; Number of intervals ; Computing the step size ; Substituing a = 0 , b = 4 and h = 1 ; Driver code
class GFG { static float y ( float x ) { return ( 1 / ( 1 + x ) ) ; } static float BooleRule ( float a , float b ) { int n = 4 ; int h ; h = ( int ) ( ( b - a ) / n ) ; float sum = 0 ; float bl = ( 7 * y ( a ) + 32 * y ( a + h ) + 12 * y ( a + 2 * h ) + 32 * y ( a + 3 * h ) + 7 * y ( a + 4 * h ) ) * 2 * h / 45 ; sum = sum + bl ; return sum ; } public static void main ( String [ ] args ) { System . out . printf ( ( " f ( x ) ▁ = ▁ % .4f " ) , BooleRule ( 0 , 4 ) ) ; } }
Number of subsets with same AND , OR and XOR values in an Array | Java program to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Precompute the modded powers of two for subset counting ; Loop to iterate and find the modded powers of two for subset counting ; Map to store the frequency of each element ; Loop to compute the frequency ; For every element > 0 , the number of subsets formed using this element only is equal to 2 ^ ( frequency [ element ] - 1 ) . And for 0 , we have to find all the subsets , so 2 ^ ( frequency [ element ] ) - 1 ; If element is greater than 0 ; Driver code
import java . util . * ; class GFG { static int mod = 1000000007 ; static int countSubsets ( int a [ ] , int n ) { int answer = 0 ; int [ ] powerOfTwo = new int [ 100005 ] ; powerOfTwo [ 0 ] = 1 ; for ( int i = 1 ; i < 100005 ; i ++ ) powerOfTwo [ i ] = ( powerOfTwo [ i - 1 ] * 2 ) % mod ; HashMap < Integer , Integer > frequency = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( frequency . containsKey ( a [ i ] ) ) { frequency . put ( a [ i ] , frequency . get ( a [ i ] ) + 1 ) ; } else { frequency . put ( a [ i ] , 1 ) ; } for ( Map . Entry < Integer , Integer > el : frequency . entrySet ( ) ) { if ( el . getKey ( ) != 0 ) answer = ( answer % mod + powerOfTwo [ el . getValue ( ) - 1 ] ) % mod ; else answer = ( answer % mod + powerOfTwo [ el . getValue ( ) ] - 1 + mod ) % mod ; } return answer ; } public static void main ( String [ ] args ) { int N = 6 ; int A [ ] = { 1 , 3 , 2 , 1 , 2 , 1 } ; System . out . print ( countSubsets ( A , N ) ) ; } }
Sum of all Perfect numbers lying in the range [ L , R ] | Java implementation to find the sum of all perfect numbers lying in the range [ L , R ] ; Array to store the sum ; Function to check if a number is a perfect number or not ; Iterating till the square root of the number and checking if the sum of divisors is equal to the number or not ; If it is a perfect number , then return the number ; Else , return 0 ; Function to precompute the sum of perfect squares and store then in an array
class GFG { static int pref [ ] = new int [ 10000 ] ; static int isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return n ; return 0 ; } static void precomputation ( ) { for ( int i = 1 ; i < 10000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfect ( i ) ; } } public static void main ( String [ ] args ) { int L = 6 , R = 28 ; precomputation ( ) ; System . out . println ( pref [ R ] - pref [ L - 1 ] ) ; } }
Ternary number system or Base 3 numbers | Java program to convert a ternary number to decimal number ; Function to convert a ternary number to a decimal number ; If the number is greater than 0 , compute the decimal representation of the number ; Loop to iterate through the number ; Computing the decimal digit ; Driver code
class GFG { static void convertToDecimal ( int N ) { System . out . print ( " Decimal ▁ number ▁ of ▁ " + N + " ▁ is : ▁ " ) ; if ( N != 0 ) { int decimalNumber = 0 , i = 0 , remainder ; while ( N != 0 ) { remainder = N % 10 ; N /= 10 ; decimalNumber += remainder * Math . pow ( 3 , i ) ; ++ i ; } System . out . print ( decimalNumber + "NEW_LINE"); } else System . out . print ( "0" + "NEW_LINE"); } public static void main ( String [ ] args ) { int Ternary = 10202202 ; convertToDecimal ( Ternary ) ; } }
Check if a given pair of Numbers are Betrothed numbers or not | Java program for the above approach ; Function to check whether N is Perfect Square or not ; Find sqrt ; Function to check whether the given pairs of numbers is Betrothed Numbers or not ; For finding the sum of all the divisors of first number n ; For finding the sum of all the divisors of second number m ; Driver Code ; Function Call
class GFG { static boolean isPerfectSquare ( int N ) { double sr = Math . sqrt ( N ) ; return ( sr - Math . floor ( sr ) ) == 0 ; } static void BetrothedNumbers ( int n , int m ) { int Sum1 = 1 ; int Sum2 = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { Sum1 += i + ( isPerfectSquare ( n ) ? 0 : n / i ) ; } } for ( int i = 2 ; i <= Math . sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { Sum2 += i + ( isPerfectSquare ( m ) ? 0 : m / i ) ; } } if ( ( n + 1 == Sum2 ) && ( m + 1 == Sum1 ) ) { System . out . print ( " YES " + "NEW_LINE"); } else { System . out . print ( " NO " + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 9504 ; int M = 20734 ; BetrothedNumbers ( N , M ) ; } }
Count of numbers upto M with GCD equals to K when paired with M | Java program to Count of numbers between 0 to M which have GCD with M equals to K . ; Function to calculate GCD using euler totient function ; Finding the prime factors of limit to calculate it 's  euler totient function ; Calculating the euler totient function of ( m / k ) ; Function print the count of numbers whose GCD with M equals to K ; GCD of m with any integer cannot be equal to k ; 0 and m itself will be the only valid integers ; Finding the number upto which coefficient of k can come ; Driver code
import java . util . * ; class GFG { static int EulerTotientFunction ( int limit ) { int copy = limit ; Vector < Integer > primes = new Vector < Integer > ( ) ; for ( int i = 2 ; i * i <= limit ; i ++ ) { if ( limit % i == 0 ) { while ( limit % i == 0 ) { limit /= i ; } primes . add ( i ) ; } } if ( limit >= 2 ) { primes . add ( limit ) ; } int ans = copy ; for ( int it : primes ) { ans = ( ans / it ) * ( it - 1 ) ; } return ans ; } static void CountGCD ( int m , int k ) { if ( m % k != 0 ) { System . out . print ( 0 + "NEW_LINE"); return ; } if ( m == k ) { System . out . print ( 2 + "NEW_LINE"); return ; } int limit = m / k ; int ans = EulerTotientFunction ( limit ) ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int M = 9 ; int K = 1 ; CountGCD ( M , K ) ; } }
Find the product of sum of two diagonals of a square Matrix | Java program to find the product of the sum of diagonals . ; Function to find the product of the sum of diagonals . ; Initialize sums of diagonals ; Return the answer ; Driven code ; Function call
class GFG { static long product ( int [ ] [ ] mat , int n ) { long d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { d1 += mat [ i ] [ i ] ; d2 += mat [ i ] [ n - i - 1 ] ; } return 1L * d1 * d2 ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 5 , 8 , 1 } , { 5 , 10 , 3 } , { - 6 , 17 , - 9 } } ; int n = mat . length ; System . out . print ( product ( mat , n ) ) ; } }
Program to calculate Percentile of a student based on rank | Java program to calculate Percentile of a student based on rank ; Program to calculate the percentile ; flat variable to store the result ; calculate and return the percentile ; Driver Code
import java . util . * ; class GFG { static float getPercentile ( int rank , int students ) { float result = ( float ) ( students - rank ) / students * 100 ; return result ; } public static void main ( String [ ] args ) { int your_rank = 805 ; int total_students = 97481 ; System . out . print ( getPercentile ( your_rank , total_students ) ) ; } }
Sum of numbers in the Kth level of a Fibonacci triangle | Java implementation to find the Sum of numbers in the Kth level of a Fibonacci triangle ; Function to return the nth Fibonacci number ; Function to return the required sum of the array ; Using our deduced result ; Function to return the sum of fibonacci in the Kth array ; Count of fibonacci which are in the arrays from 1 to k - 1 ; Driver code
import java . util . * ; class GFG { static int fib ( int n ) { double phi = ( 1 + Math . sqrt ( 5 ) ) / 2 ; return ( int ) Math . round ( Math . pow ( phi , n ) / Math . sqrt ( 5 ) ) ; } static int calculateSum ( int l , int r ) { int sum = fib ( r + 2 ) - fib ( l + 1 ) ; return sum ; } static int sumFibonacci ( int k ) { int l = ( k * ( k - 1 ) ) / 2 ; int r = l + k ; int sum = calculateSum ( l , r - 1 ) ; return sum ; } public static void main ( String args [ ] ) { int k = 3 ; System . out . println ( sumFibonacci ( k ) ) ; } }
Print the sequence of size N in which every term is sum of previous K terms | Java implementation to find the series in which every term is sum of previous K terms ; Function to generate the series in the form of array ; Pick a starting point ; Find the sum of all elements till count < K ; Find the value of sum at i position ; Driver Code
class Sum { void sumOfPrevK ( int N , int K ) { int arr [ ] = new int [ N ] ; arr [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { int j = i - 1 , count = 0 , sum = 0 ; while ( j >= 0 && count < K ) { sum += arr [ j ] ; j -- ; count ++ ; } arr [ i ] = sum ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { Sum s = new Sum ( ) ; int N = 10 , K = 4 ; s . sumOfPrevK ( N , K ) ; } }
Print the sequence of size N in which every term is sum of previous K terms | Java implementation to find the series in which every term is sum of previous K terms ; Function to generate the series in the form of array ; Pick a starting point ; Computing the previous sum ; Loop to print the series ; Driver code
class Sum { void sumOfPrevK ( int N , int K ) { int arr [ ] = new int [ N ] ; int prevsum = 0 ; arr [ 0 ] = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( i < K ) { arr [ i + 1 ] = arr [ i ] + prevsum ; prevsum = arr [ i + 1 ] ; } else { arr [ i + 1 ] = arr [ i ] + prevsum - arr [ i + 1 - K ] ; prevsum = arr [ i + 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { Sum s = new Sum ( ) ; int N = 8 , K = 3 ; s . sumOfPrevK ( N , K ) ; } }
Maximum GCD of all subarrays of length at least 2 | Java program for the above approach ; Function to find GCD ; To store the maximum GCD ; Traverse the array ; Find GCD of the consecutive element ; If calculated GCD > maxGCD then update it ; Print the maximum GCD ; Driver Code ; Function call
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } static void findMaxGCD ( int arr [ ] , int n ) { int maxGCD = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int val = gcd ( arr [ i ] , arr [ i + 1 ] ) ; if ( val > maxGCD ) { maxGCD = val ; } } System . out . print ( maxGCD + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 18 , 9 , 9 , 5 , 15 , 8 , 7 , 6 , 9 } ; int n = arr . length ; findMaxGCD ( arr , n ) ; } }
Sum of elements of an AP in the given range | Java program to find the sum of elements of an AP in the given range ; Function to find sum in the given range ; Find the value of k ; Find the common difference ; Find the sum ; Driver code
class GFG { static int findSum ( int [ ] arr , int n , int left , int right ) { int k = right - left ; int d = arr [ 1 ] - arr [ 0 ] ; int ans = arr [ left - 1 ] * ( k + 1 ) ; ans = ans + ( d * ( k * ( k + 1 ) ) ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 } ; int queries = 3 ; int q [ ] [ ] = { { 2 , 4 } , { 2 , 6 } , { 5 , 6 } } ; int n = arr . length ; for ( int i = 0 ; i < queries ; i ++ ) System . out . print ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) + "NEW_LINE"); } }
Check if a subarray exists with sum greater than the given Array | Java program to check if a subarray exists with sum greater than the given Array ; Function to check whether there exists a subarray whose sum is greater than or equal to sum of given array elements ; Initialize sum with 0 ; Checking possible prefix subarrays . If sum of them is less than or equal to zero , then return 1 ; again reset sum to zero ; Checking possible suffix subarrays . If sum of them is less than or equal to zero , then return 1 ; Otherwise return 0 ; Driver Code
import java . util . * ; class GFG { static boolean subarrayPossible ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum <= 0 ) return true ; } sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; if ( sum <= 0 ) return true ; } return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 5 , - 12 , 7 , - 10 , 20 , 30 , - 10 , 50 , 60 } ; int size = arr . length ; if ( subarrayPossible ( arr , size ) ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
Finding Integreand using Weedle 's Rule | Java program to Implement Weedle 's Rule ; A sample function f ( x ) = 1 / ( 1 + x ^ 2 ) ; Function to find the integral value of f ( x ) with step size h , with initial lower limit and upper limit a and b ; Find step size h ; To store the final sum ; Find sum using Weedle 's Formula ; Return the final sum ; Driver Code ; lower limit and upper limit ; Function Call
import java . util . * ; class GFG { static float y ( float x ) { float num = 1 ; float denom = ( float ) 1.0 + x * x ; return num / denom ; } static float WeedleRule ( float a , float b ) { float h = ( b - a ) / 6 ; float sum = 0 ; sum = sum + ( ( ( 3 * h ) / 10 ) * ( y ( a ) + y ( a + 2 * h ) + 5 * y ( a + h ) + 6 * y ( a + 3 * h ) + y ( a + 4 * h ) + 5 * y ( a + 5 * h ) + y ( a + 6 * h ) ) ) ; return sum ; } public static void main ( String args [ ] ) { float a = 0 , b = 6 ; float num = WeedleRule ( a , b ) ; System . out . format ( " f ( x ) ▁ = ▁ " + " % .6f " , num ) ; } }
Find the minimum number to be added to N to make it a prime number | Java program to find the minimum number to be added to N to make it a prime number ; Function to check if a given number is a prime or not ; Base cases ; This is checked so that we can skip middle five numbers in below loop ; For all the remaining numbers , check if any number is a factor if the number or not ; If none of the above numbers are the factors for the number , then the given number is prime ; Function to return the smallest number to be added to make a number prime ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; If the number is not a prime , then increment the number by 1 and the counter which stores the number to be added ; Driver code
import java . util . * ; 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 findSmallest ( int N ) { if ( N == 0 ) return 2 ; if ( N == 1 ) return 1 ; int prime = N , counter = 0 ; boolean found = false ; while ( ! found ) { if ( isPrime ( prime ) ) found = true ; else { prime ++ ; counter ++ ; } } return counter ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( findSmallest ( N ) ) ; } }
Find the position of the given Prime Number | Java program to find the position of the given prime number ; Function to precompute the position of every prime number using Sieve ; 0 and 1 are not prime numbers ; Variable to store the position ; Incrementing the position for every prime number ; Driver code
class GFG { static final int limit = 10000000 ; static int [ ] position = new int [ limit + 1 ] ; static void sieve ( ) { position [ 0 ] = - 1 ; position [ 1 ] = - 1 ; int pos = 0 ; for ( int i = 2 ; i <= limit ; i ++ ) { if ( position [ i ] == 0 ) { position [ i ] = ++ pos ; for ( int j = i * 2 ; j <= limit ; j += i ) position [ j ] = - 1 ; } } } public static void main ( String [ ] args ) { sieve ( ) ; int n = 11 ; System . out . print ( position [ n ] ) ; } }