codeparrot/xlcost-text-to-code huggingface dataset:
Sum and product of k smallest and k largest composite numbers in the array | Java program to find the sum and product of k smallest and k largest composite numbers in an array ; Create a boolean vector " prime [ 0 . . n ] " . 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 ; Function that calculates the sum and product of k smallest and k largest composite numbers in an array ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val ; Set 0 and 1 as primes so that they don 't get counted as  composite numbers ; Max Heap to store all the composite numbers ; Min Heap to store all the composite numbers ; Push all the composite numbers from the array to the heaps ; Calculate the products ; Calculate the sum ; Pop the current minimum element ; Pop the current maximum element ; when maxHeap or minHeap is exhausted then this consition will run ;  ; Driver Code
import java . util . * ; class GFG { static boolean [ ] SieveOfEratosThenes ( int max_val ) { boolean [ ] prime = new boolean [ max_val + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } return prime ; } static void compositeSumAndProduct ( Integer [ ] arr , int n , int k ) { int max_val = Collections . max ( Arrays . asList ( arr ) ) ; boolean [ ] prime = SieveOfEratosThenes ( max_val ) ; prime [ 0 ] = true ; prime [ 1 ] = true ; PriorityQueue < Integer > maxHeap = new PriorityQueue < Integer > ( ( x , y ) -> y - x ) ; PriorityQueue < Integer > minHeap = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { minHeap . add ( arr [ i ] ) ; maxHeap . add ( arr [ i ] ) ; } } long minProduct = 1 , maxProduct = 1 , minSum = 0 , maxSum = 0 ; Integer lastMin = 0 , lastMax = 0 ; while ( k -- > 0 ) { if ( minHeap . peek ( ) != null || maxHeap . peek ( ) != null ) { minProduct *= minHeap . peek ( ) ; maxProduct *= maxHeap . peek ( ) ; minSum += minHeap . peek ( ) ; maxSum += maxHeap . peek ( ) ; lastMin = minHeap . poll ( ) ; lastMax = maxHeap . poll ( ) ; } else { minProduct *= lastMin ; maxProduct *= lastMax ; minSum += lastMin ; maxSum += lastMax ; } } '' ' ▁ Print ▁ results ▁ ' '' System . out . println ( " Sum ▁ of ▁ k - minimum ▁ composite " + " ▁ numbers ▁ is ▁ " + minSum ) ; System . out . println ( " Sum ▁ of ▁ k - maximum ▁ composite " + " ▁ numbers ▁ is ▁ " + maxSum ) ; System . out . println ( " Product ▁ of ▁ k - minimum ▁ composite " + " ▁ numbers ▁ is ▁ " + minProduct ) ; System . out . println ( " Product ▁ of ▁ k - maximum ▁ composite " + " ▁ numbers ▁ is ▁ " + maxProduct ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 4 , 2 , 12 , 13 , 5 , 19 } ; int n = arr . length ; int k = 3 ; compositeSumAndProduct ( arr , n , k ) ; } }
Product of all the Composite Numbers in an array | Java program to find the product of all the composite numbers in an array ; Function that returns the the product of all composite numbers ; 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 . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find the product of all composite numbers in the arr [ ] ; Driver code
import java . util . * ; class GFG { static int compositeProduct ( 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 ] = true ; prime [ 1 ] = true ; 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 product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { product *= arr [ i ] ; } } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; System . out . println ( compositeProduct ( arr , n ) ) ; } }
Primality test for the sum of digits at odd places of a number | Java program to do Primality test for the sum of digits at odd places of a number ; Function that return sum of the digits at odd places ; Function that returns true if the number is prime else false ; Corner cases ; This condition is checked so that we can skip middle five numbers in the below loop ; Driver code ; Get the sum of the digits at odd places
import java . io . * ; class GFG { static int sum_odd ( int n ) { int sum = 0 , pos = 1 ; while ( n > 0 ) { if ( pos % 2 == 1 ) sum += n % 10 ; n = n / 10 ; pos ++ ; } return sum ; } static boolean check_prime ( 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 ; } public static void main ( String [ ] args ) { int n = 223 ; int sum = sum_odd ( n ) ; if ( check_prime ( sum ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Find amount to be added to achieve target ratio in a given mixture | Java program to find amount of water to be added to achieve given target ratio . ; Driver code
public class GFG { static float findAmount ( float X , float W , float Y ) { return ( X * ( Y - W ) ) / ( 100 - Y ) ; } public static void main ( String args [ ] ) { float X = 100 , W = 50 , Y = 60 ; System . out . println ( " Water ▁ to ▁ be ▁ added ▁ = ▁ " + findAmount ( X , W , Y ) ) ; } }
Check if a number is a Mystery Number | Java implementation of above approach ; Finds reverse of given num x . ; if found print the pair , return
class GFG { static int reverseNum ( int x ) { String s = Integer . toString ( x ) ; String str = " " ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { str = str + s . charAt ( i ) ; } int rev = Integer . parseInt ( str ) ; return rev ; } static boolean isMysteryNumber ( int n ) { for ( int i = 1 ; i <= n / 2 ; i ++ ) { int j = reverseNum ( i ) ; if ( i + j == n ) { System . out . println ( i + " ▁ " + j ) ; return true ; } } System . out . println ( " Not ▁ a ▁ Mystery ▁ Number " ) ; return false ; } public static void main ( String [ ] args ) { int n = 121 ; isMysteryNumber ( n ) ; } }
Replace every element of the array by product of all other elements | Java program to Replace every element by the product of all other elements ; Calculate the product of all the elements ; Replace every element product of all other elements ; Driver code ; Print the modified array .
class GFG { static void ReplaceElements ( int arr [ ] , int n ) { int prod = 1 ; for ( int i = 0 ; i < n ; ++ i ) { prod *= arr [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { arr [ i ] = prod / arr [ i ] ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 5 , 7 } ; int n = arr . length ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( " " ) ; } }
Check if there is any pair in a given range with GCD is divisible by k | function to count such possible numbers ; if i is divisible by k ; if count of such numbers is greater than one ; Driver code
class GFG { public boolean Check_is_possible ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( i % k == 0 ) { count ++ ; } } return ( count > 1 ) ; } public static void main ( String [ ] args ) { GFG g = new GFG ( ) ; int l = 4 , r = 12 ; int k = 5 ; if ( g . Check_is_possible ( l , r , k ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Check if any permutation of N equals any power of K | Java implementation of above approach . ; function to check if N and K are anagrams ; Function to check if any permutation of N exist such that it is some power of K ; generate all power of K under 10 ^ 18 ; check if any power of K is valid ; Driver code ; function call to print required answer
import java . util . * ; class GfG { static boolean isValid ( int N , int K ) { HashSet < Integer > m1 = new HashSet < Integer > ( ) ; HashSet < Integer > m2 = new HashSet < Integer > ( ) ; while ( N > 0 ) { m1 . add ( N % 10 ) ; N /= 10 ; } while ( K > 0 ) { m2 . add ( K % 10 ) ; K /= 10 ; } if ( m1 . equals ( m2 ) ) { return true ; } return false ; } static String anyPermutation ( int N , int K ) { int powK [ ] = new int [ 100 + 1 ] , Limit = ( int ) Math . pow ( 10 , 18 ) ; powK [ 0 ] = K ; int i = 1 ; while ( powK [ i - 1 ] * K < Limit && i < 100 ) { powK [ i ] = powK [ i - 1 ] * K ; i ++ ; } for ( int j = 0 ; j < i ; j ++ ) { if ( isValid ( N , powK [ j ] ) ) { return " True " ; } } return " False " ; } public static void main ( String [ ] args ) { int N = ( int ) 96889010407L , K = 7 ; System . out . println ( anyPermutation ( N , K ) ) ; } }
Sum of first N natural numbers which are divisible by 2 and 7 | Java program to find sum of numbers from 1 to N which are divisible by 2 or 7 ; Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code
import java . io . * ; class GFG { public static int sum ( int N ) { int S1 , S2 , S3 ; S1 = ( ( N / 2 ) ) * ( 2 * 2 + ( N / 2 - 1 ) * 2 ) / 2 ; S2 = ( ( N / 7 ) ) * ( 2 * 7 + ( N / 7 - 1 ) * 7 ) / 2 ; S3 = ( ( N / 14 ) ) * ( 2 * 14 + ( N / 14 - 1 ) * 14 ) / 2 ; return S1 + S2 - S3 ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . println ( sum ( N ) ) ; } }
Ways to color a skewed tree such that parent and child have different colors | Java program to count number of ways to color a N node skewed tree with k colors such that parent and children have different colors . ; fast_way is recursive method to calculate power ; Driver program
import java . io . * ; class GFG { static int fastPow ( int N , int K ) { if ( K == 0 ) return 1 ; int temp = fastPow ( N , K / 2 ) ; if ( K % 2 == 0 ) return temp * temp ; else return N * temp * temp ; } static int countWays ( int N , int K ) { return K * fastPow ( K - 1 , N - 1 ) ; } public static void main ( String [ ] args ) { int N = 3 , K = 3 ; System . out . println ( countWays ( N , K ) ) ; } }
Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | Java program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; Driver code ; m is the size of the array
public class GFG { static int sumNth ( int A [ ] , int B [ ] , int m , int n ) { int res = 0 ; if ( n == 1 ) { for ( int i = 0 ; i < m ; i ++ ) res = res + A [ i ] ; } else if ( n == 2 ) { for ( int i = 0 ; i < m ; i ++ ) res = res + B [ i ] * m ; } else { int f [ ] = new int [ n ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; for ( int i = 0 ; i < m ; i ++ ) { res = res + ( m * ( B [ i ] * f [ n - 1 ] ) ) + ( m * ( A [ i ] * f [ n - 2 ] ) ) ; } } return res ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 4 , 5 , 6 } ; int n = 3 ; int m = A . length ; System . out . println ( sumNth ( A , B , m , n ) ) ; } }
Puzzle | Minimum distance for Lizard | Java program to find minimum distance to be travelled by lizard ; side of the cube ; understand from diagram ; understand from diagram ; minimum distance
import java . util . * ; class solution { public static void main ( String arr [ ] ) { int a = 5 ; int AC = a ; int CE = 2 * a ; double shortestDistace = Math . sqrt ( AC * AC + CE * CE ) ; System . out . println ( shortestDistace ) ; } }
Find Sum of Series 1 ^ 2 | Java Program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... ; Function to find sum of series ; If n is even ; If n is odd ; return the result ; Driver Code ; Get n ; Find the sum ; Get n ; Find the sum
import java . util . * ; import java . lang . * ; class GFG { static int sum_of_series ( int n ) { int result = 0 ; if ( n % 2 == 0 ) { result = - ( n * ( n + 1 ) ) / 2 ; } else { result = ( n * ( n + 1 ) ) / 2 ; } return result ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( sum_of_series ( n ) ) ; n = 10 ; System . out . println ( sum_of_series ( n ) ) ; } }
Chen Prime Number | JAVA program to check Chen Prime number ; Utility function to check if the given number is semi - prime or not ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; Return '1' if count is equal to '2' else return '0' ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check chen prime number ; Driver code
class GFG { static boolean isSemiPrime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) while ( num % i == 0 ) { num /= i ; ++ cnt ; } if ( num > 1 ) ++ cnt ; return cnt == 2 ? true : false ; } 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 boolean isChenPrime ( int n ) { if ( isPrime ( n ) && ( isSemiPrime ( n + 2 ) || isPrime ( n + 2 ) ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 7 ; if ( isChenPrime ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Thabit number | Java code to check for thabit number ; Utility function to check power of two ; function to check if the given number is Thabit Number ; Add 1 to the number ; Divide the number by 3 ; Check if the given number is power of 2 ; Driver Program ; Check if number is thabit number
class GFG { static boolean isPowerOfTwo ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static boolean isThabitNumber ( int n ) { n = n + 1 ; if ( n % 3 == 0 ) n = n / 3 ; else return false ; if ( isPowerOfTwo ( n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 47 ; if ( isThabitNumber ( n ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Sum of all the prime numbers in a given range | Java Program to compute sum of prime number in a given range ; Suppose the constraint is N <= 1000 ; Declare an array for dynamic approach ; Method to compute the array ; Declare an extra array as arr ; Iterate the loop till sqrt ( n ) Time Complexity is O ( log ( n ) X sqrt ( n ) ) ; if ith element of arr is 0 i . e . marked as prime ; mark all of it 's multiples till N as non-prime  by setting the locations to 1 ; Update the array ' dp ' with the running sum of prime numbers within the range [ 1 , N ] Time Complexity is O ( n ) ; Here , dp [ i ] is the sum of all the prime numbers within the range [ 1 , i ] ; Driver code ; Compute dp
public class GFG { static int N = 1000 ; static long dp [ ] = new long [ N + 1 ] ; static void sieve ( ) { int arr [ ] = new int [ N + 1 ] ; arr [ 0 ] = 1 ; arr [ 1 ] = 1 ; for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) if ( arr [ i ] == 0 ) for ( int j = i * i ; j <= N ; j += i ) arr [ j ] = 1 ; long runningPrimeSum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] == 0 ) runningPrimeSum += i ; dp [ i ] = runningPrimeSum ; } } public static void main ( String [ ] args ) { int l = 4 , r = 13 ; sieve ( ) ; System . out . println ( dp [ r ] - dp [ l - 1 ] ) ; } }
Smallest Integer to be inserted to have equal sums | Java program to find the smallest number to be added to make the sum of left and right subarrays equal ; Function to find the minimum value to be added ; Variable to store entire array sum ; Variables to store sum of subarray1 and subarray 2 ; minimum value to be added ; Traverse through the array ; Sum of both halves ; Calculate minimum number to be added ; Driver code ; Length of array
import java . io . * ; import java . util . * ; class GFG { static int findMinEqualSums ( int [ ] a , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; } int sum1 = 0 , sum2 = 0 ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { sum1 += a [ i ] ; sum2 = sum - sum1 ; if ( Math . abs ( sum1 - sum2 ) < min ) { min = Math . abs ( sum1 - sum2 ) ; } if ( min == 0 ) { break ; } } return min ; } public static void main ( String args [ ] ) { int [ ] a = { 3 , 2 , 1 , 5 , 7 , 8 } ; int N = a . length ; System . out . println ( findMinEqualSums ( a , N ) ) ; } }
Sum of the first N Prime numbers | Java implementation of above solution ; 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 ; Set all multiples of p to non - prime ; find the sum of 1 st N prime numbers ; count of prime numbers ; sum of prime numbers ; if the number is prime add it ; increase the count ; get to next number ; Driver code ; create the sieve ; find the value of 1 st n prime numbers
public class Improve { final static double MAX = 10000 ; static boolean prime [ ] = new boolean [ ( int ) ( MAX + 1.0 ) ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static int solve ( int n ) { int count = 0 , num = 1 ; long sum = 0 ; while ( count < n ) { if ( prime [ num ] ) { sum += num ; count ++ ; } num ++ ; } return ( int ) sum ; } public static void main ( String args [ ] ) { SieveOfEratosthenes ( ) ; int n = 4 ; System . out . println ( " Sum ▁ of ▁ 1st ▁ N ▁ prime ▁ numbers ▁ are ▁ : " + solve ( n ) ) ; } }
Implementation of Wilson Primality test | Java implementation to check if a number is prime or not using Wilson Primality Test ; Function to calculate the factorial ; Function to check if the number is prime or not ; Driver code
public class Main { public static long fact ( int p ) { if ( p <= 1 ) return 1 ; return p * fact ( p - 1 ) ; } public static long isPrime ( int p ) { if ( p == 4 ) return 0 ; return ( fact ( p >> 1 ) % p ) ; } public static void main ( String [ ] args ) { if ( isPrime ( 127 ) == 0 ) { System . out . println ( 0 ) ; } else { System . out . println ( 1 ) ; } } }
Find the total Number of Digits in ( N ! ) N | Java program to find the total Number of Digits in ( N ! ) ^ N ; Function to find the total Number of Digits in ( N ! ) ^ N ; Finding X ; Calculating N * X ; Floor ( N * X ) + 1 equivalent to floor ( sum ) + 1 ; Driver code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { public double CountDigits ( int n ) { if ( n == 1 ) return 1 ; double sum = 0 ; for ( int i = 2 ; i <= n ; ++ i ) { sum += ( ( double ) Math . log ( i ) / ( double ) Math . log ( 10 ) ) ; } sum *= n ; return Math . ceil ( sum ) ; } public static void main ( String args [ ] ) { GFG g = new GFG ( ) ; int N = 5 ; System . out . println ( g . CountDigits ( N ) ) ; } }
Find the value of max ( f ( x ) ) | Java implementation of above approach ; Function to calculate the value ; forming the prefix sum arrays ; Taking the query ; finding the sum in the range l to r in array a ; finding the sum in the range l to r in array b ; Finding the max value of the function ; Finding the min value of the function ; Driver code
import java . util . * ; class GFG { static final int MAX = 200006 ; static final int CONS = 32766 ; static void calc ( int a [ ] , int b [ ] , int lr [ ] , int q , int n ) { int M , m , i , j , k , l , r , suma , sumb , cc ; cc = 0 ; for ( i = 0 ; i < n - 1 ; ++ i ) { a [ i + 1 ] += a [ i ] ; b [ i + 1 ] += b [ i ] ; } while ( q != 0 ) { l = lr [ cc ++ ] ; r = lr [ cc ++ ] ; l -= 2 ; r -= 1 ; suma = a [ r ] ; sumb = b [ r ] ; if ( l >= 0 ) { suma -= a [ l ] ; sumb -= b [ l ] ; } M = Math . max ( CONS * suma + CONS * sumb , - CONS * suma - CONS * sumb ) ; M = Math . max ( M , Math . max ( CONS * suma - CONS * sumb , - CONS * suma + CONS * sumb ) ) ; m = Math . min ( CONS * suma + CONS * sumb , - CONS * suma - CONS * sumb ) ; m = Math . min ( m , Math . min ( CONS * suma - CONS * sumb , - CONS * suma + CONS * sumb ) ) ; System . out . println ( ( M - m ) ) ; q -- ; } } public static void main ( String [ ] args ) { int n = 5 , q = 2 ; int [ ] a = { 0 , 7 , 3 , 4 , 5 } ; int [ ] b = { 0 , 3 , 1 , 2 , 3 } ; int [ ] lr = new int [ q * 2 ] ; lr [ 0 ] = 1 ; lr [ 1 ] = 1 ; lr [ 2 ] = 1 ; lr [ 3 ] = 3 ; calc ( a , b , lr , q , n ) ; } }
Program to find the Nth number of the series 2 , 10 , 24 , 44 , 70. ... . | Java program to find N - th term of the series : ; function to return nth term of the series ; Driver Code ; Taking n as 4 ; Printing the nth term
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static long NthTerm ( long n ) { long x = ( 3 * n * n ) % 1000000009 ; return ( x - n + 1000000009 ) % 1000000009 ; } public static void main ( String args [ ] ) { long N = 4 ; System . out . println ( NthTerm ( N ) ) ; } }
Sum of first N natural numbers by taking powers of 2 as negative number | Java implementation of above approach ; to store power of 2 ; to store presum of the power of 2 's ; function to find power of 2 ; to store power of 2 ; to store pre sum ; Function to find the sum ; first store sum of first n natural numbers . ; find the first greater number than given number then minus double of this from answer ; Driver code ; function call ; function call
import java . io . * ; class GFG { static int power [ ] = new int [ 31 ] ; static int pre [ ] = new int [ 31 ] ; static void PowerOfTwo ( ) { int x = 1 ; for ( int i = 0 ; i < 31 ; i ++ ) { power [ i ] = x ; x *= 2 ; } pre [ 0 ] = 1 ; for ( int i = 1 ; i < 31 ; i ++ ) pre [ i ] = pre [ i - 1 ] + power [ i ] ; } static int Sum ( int n ) { int ans = n * ( n + 1 ) / 2 ; for ( int i = 0 ; i < 31 ; i ++ ) { if ( power [ i ] > n ) { ans -= 2 * pre [ i - 1 ] ; break ; } } return ans ; } public static void main ( String [ ] args ) { PowerOfTwo ( ) ; int n = 4 ; System . out . println ( Sum ( n ) ) ; } }
Check if a number is Quartan Prime or not | JAVA program to check if a number is Quartan Prime or not ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Program ; Check if number is prime and of the form 16 * n + 1
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 ; } public static void main ( String [ ] args ) { int n = 17 ; if ( isPrime ( n ) && ( n % 16 == 1 ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }