codeparrot/xlcost-text-to-code huggingface dataset:
First element of every K sets having consecutive elements with exactly K prime factors less than N | Java program for the above approach ; For storing smallest prime factor ; Function consmallest prime factor array ; Mark smallest prime factor for every number to be itself . ; Separately mark spf for every even number as 2 ; Check if i is prime ; Mark SPF for all numbers divisible by i ; Mark spf [ j ] if it is not previously marked ; Function for counts total number of prime factors ; Function to print elements of sets of K consecutive elements having K prime factors ; To store the result ; Count number of prime factors of number ; If number has exactly K factors push in result [ ] ; Iterate till we get K consecutive elements in result [ ] ; Count sequence until K ; Print the element if count >= K ; Driver code ; To construct spf [ ] ; Given N and K ; Function call
import java . util . * ; class GFG { static final int x = 2000021 ; static int [ ] v = new int [ x ] ; static void sieve ( ) { v [ 1 ] = 1 ; for ( int i = 2 ; i < x ; i ++ ) v [ i ] = i ; for ( int i = 4 ; i < x ; i += 2 ) v [ i ] = 2 ; for ( int i = 3 ; i * i < x ; i ++ ) { if ( v [ i ] == i ) { for ( int j = i * i ; j < x ; j += i ) { if ( v [ j ] == j ) { v [ j ] = i ; } } } } } static int prime_factors ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; while ( n != 1 ) { s . add ( v [ n ] ) ; n = n / v [ n ] ; } return s . size ( ) ; } static void distinctPrimes ( int m , int k ) { Vector < Integer > result = new Vector < Integer > ( ) ; for ( int i = 14 ; i < m + k ; i ++ ) { long count = prime_factors ( i ) ; if ( count == k ) { result . add ( i ) ; } } int p = result . size ( ) ; for ( int index = 0 ; index < p - 1 ; index ++ ) { long element = result . get ( index ) ; int count = 1 , z = index ; while ( z < p - 1 && count <= k && result . get ( z ) + 1 == result . get ( z + 1 ) ) { count ++ ; z ++ ; } if ( count >= k ) System . out . print ( element + " ▁ " ) ; } } public static void main ( String [ ] args ) { sieve ( ) ; int N = 1000 , K = 3 ; distinctPrimes ( N , K ) ; } }
Product of Complex Numbers using three Multiplication Operation | Java program for the above approach ; Function to multiply Complex Numbers with just three multiplications ; Find value of prod1 , prod2 and prod3 ; Real Part ; Imaginary Part ; Print the result ; Driver Code ; Given four numbers ; Function call
class GFG { static void print_product ( int a , int b , int c , int d ) { int prod1 = a * c ; int prod2 = b * d ; int prod3 = ( a + b ) * ( c + d ) ; int real = prod1 - prod2 ; int imag = prod3 - ( prod1 + prod2 ) ; System . out . println ( real + " ▁ + ▁ " + imag + " i " ) ; } public static void main ( String [ ] args ) { int a = 2 ; int b = 3 ; int c = 4 ; int d = 5 ; print_product ( a , b , c , d ) ; } }
Insolite Numbers | Java implementation for the above approach ; Function to check if a number is an Insolite numbers ; To store sum of squares of digits ; To store product of squares of digits ; extracting digit ; Driver Code ; Function Call
class GFG { static boolean isInsolite ( int n ) { int N = n ; int sum = 0 ; int product = 1 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r * r ; product = product * r * r ; n = n / 10 ; } return ( N % sum == 0 ) && ( N % product == 0 ) ; } public static void main ( String [ ] args ) { int N = 111 ; if ( isInsolite ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Superabundant Numbers | Java implementation to check if a number is Superabundant ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors which divides ' num ' ; If ' i ' is divisor of ' n ' ; If both divisors are same then add it once else add both ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Function to check if N is a superabundant number ; To check all numbers from 1 to N ; Driver code
class GFG { static int sigma ( int n ) { if ( n == 1 ) return 1 ; int result = 0 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } static boolean isSuperabundant ( int N ) { for ( double i = 1 ; i < N ; i ++ ) { double x = sigma ( ( int ) ( i ) ) / i ; double y = sigma ( ( int ) ( N ) ) / ( N * 1.0 ) ; if ( x > y ) return false ; } return true ; } public static void main ( String [ ] args ) { int N = 4 ; if ( isSuperabundant ( N ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
D Numbers | Java implementation for the above approach ; Function to find the N - th icosikaipentagon number ; Number should be greater than 3 ; Check every k in range 2 to n - 1 ; Condition for D - Number ; Driver Code
import java . util . * ; class GFG { static boolean isDNum ( int n ) { if ( n < 4 ) return false ; int numerator = 0 , hcf = 0 ; for ( int k = 2 ; k <= n ; k ++ ) { numerator = ( int ) ( Math . pow ( k , n - 2 ) - k ) ; hcf = __gcd ( n , k ) ; } if ( hcf == 1 && ( numerator % n ) != 0 ) return false ; return true ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 15 ; boolean a = isDNum ( n ) ; if ( a ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Ruth | Java implementation of the above approach ; Function to find prime divisors of all numbers from 1 to N ; if the number is prime ; add this prime to all it 's multiples ; Function to check Ruth - Aaron number ; Driver code
class GFG { static int Sum ( int N ) { int SumOfPrimeDivisors [ ] = new int [ N + 1 ] ; for ( int i = 2 ; i <= N ; ++ i ) { if ( SumOfPrimeDivisors [ i ] == 1 ) { for ( int j = i ; j <= N ; j += i ) { SumOfPrimeDivisors [ j ] += i ; } } } return SumOfPrimeDivisors [ N ] ; } static boolean RuthAaronNumber ( int n ) { if ( Sum ( n ) == Sum ( n + 1 ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int N = 714 ; if ( RuthAaronNumber ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Maximize sum of absolute difference between adjacent elements in Array with sum K | Java program to maximize the sum of absolute differences between adjacent elements ; Function for maximising the sum ; Difference is 0 when only one element is present in array ; Difference is K when two elements are present in array ; Otherwise ; Driver code
import java . util . * ; class GFG { static int maxAdjacentDifference ( int N , int K ) { if ( N == 1 ) { return 0 ; } if ( N == 2 ) { return K ; } return 2 * K ; } public static void main ( String [ ] args ) { int N = 6 ; int K = 11 ; System . out . print ( maxAdjacentDifference ( N , K ) ) ; } }
Sum of all divisors from 1 to N | Set 2 | Java program for the above approach ; Functions returns sum of numbers from 1 to n ; Functions returns sum of numbers from a + 1 to b ; Function returns total sum of divisors ; Stores total sum ; Finding numbers and its occurence ; Sum of product of each number and its occurence ; Driver code
class GFG { static final int mod = 1000000007 ; public static int linearSum ( int n ) { return ( n * ( n + 1 ) / 2 ) % mod ; } public static int rangeSum ( int b , int a ) { return ( linearSum ( b ) - linearSum ( a ) ) % mod ; } public static int totalSum ( int n ) { int result = 0 ; int i = 1 ; while ( true ) { result += rangeSum ( n / i , n / ( i + 1 ) ) * ( i % mod ) % mod ; result %= mod ; if ( i == n ) break ; i = n / ( n / ( i + 1 ) ) ; } return result ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( totalSum ( N ) ) ; N = 12 ; System . out . println ( totalSum ( N ) ) ; } }
Nontrivial undulant Numbers | Java implementation to check if N is a Nontrivial undulant number ; Function to check if a string is double string or not ; a and b should not be equal ; Condition to check if length is odd make length even ; First half of s ; Second half of s ; Double string if first and last half are equal ; Function to check if N is an Nontrivial undulant number ; Driver code
class GFG { static boolean isDouble ( int num ) { String s = Integer . toString ( num ) ; int l = s . length ( ) ; if ( s . charAt ( 0 ) == s . charAt ( 1 ) ) return false ; if ( l % 2 == 1 ) { s = s + s . charAt ( 1 ) ; l ++ ; } String s1 = s . substring ( 0 , l / 2 ) ; String s2 = s . substring ( l / 2 ) ; return s1 . equals ( s2 ) ; } static boolean isNontrivialUndulant ( int N ) { return N > 100 && isDouble ( N ) ; } public static void main ( String [ ] args ) { int n = 121 ; if ( isNontrivialUndulant ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Megagon number | Java program for the above approach ; Function to find the nth Megagon Number ; Driver code
class GFG { static int MegagonNum ( int n ) { return ( 999998 * n * n - 999996 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( MegagonNum ( n ) ) ; } }
Product of all the pairs from the given array | Java implementation to find the product of all the pairs from the given array ; Function to return the product of the elements of all possible pairs from the array ; To store the required product ; Nested loop to calculate all possible pairs ; Multiply the product of the elements of the current pair ; Return the final result ; Driver code
import java . util . * ; class GFG { static final int mod = 1000000007 ; static int productPairs ( int arr [ ] , int n ) { int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { product *= ( arr [ i ] % mod * arr [ j ] % mod ) % mod ; product = product % mod ; } } return product % mod ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . print ( productPairs ( arr , n ) ) ; } }
Product of all the pairs from the given array | Java implementation to Find the product of all the pairs from the given array ; Function to calculate ( x ^ y ) % 1000000007 ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; Return the final result ; Function to return the product of the elements of all possible pairs from the array ; To store the required product ; Iterate for every element of the array ; Each element appears ( 2 * n ) times ; Driver code
import java . util . * ; class GFG { static final int mod = 1000000007 ; static int power ( int x , int y ) { int p = 1000000007 ; int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int productPairs ( int arr [ ] , int n ) { int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product = ( product % mod * ( int ) power ( arr [ i ] , ( 2 * n ) ) % mod ) % mod ; } return product % mod ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . print ( productPairs ( arr , n ) ) ; } }
Construct an Array such that cube sum of all element is a perfect square | Java program to construct array that cube sum of all element is a perfect square ; Function to create and print the array ; Initialise the array of size N ; Print the array ; Driver code
import java . util . * ; class GFG { static void constructArray ( int N ) { int arr [ ] = new int [ N ] ; for ( int i = 1 ; i <= N ; i ++ ) { arr [ i - 1 ] = i ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 6 ; constructArray ( N ) ; } }
Count of all subsequence whose product is a Composite number | Java implementation to count all subsequence whose product is Composite number ; Function to check whether a number is prime or not ; Function to find number of subsequences whose product is a composite number ; Find total non empty subsequence ; Find count of prime number and ones ; Calculate the non empty one subsequence ; Find count of composite subsequence ; Driver code
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int countSubsequences ( int arr [ ] , int n ) { int totalSubsequence = ( int ) ( Math . pow ( 2 , n ) - 1 ) ; int countPrime = 0 , countOnes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) countOnes ++ ; else if ( isPrime ( arr [ i ] ) ) countPrime ++ ; } int compositeSubsequence ; int onesSequence = ( int ) ( Math . pow ( 2 , countOnes ) - 1 ) ; compositeSubsequence = totalSubsequence - countPrime - onesSequence - onesSequence * countPrime ; return compositeSubsequence ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 2 } ; int n = arr . length ; System . out . print ( countSubsequences ( arr , n ) ) ; } }
Find K consecutive integers such that their sum is N | Java implementation to check if a number can be expressed as sum of K consecutive integer ; Function to check if a number can be expressed as the sum of k consecutive ; Finding the first term of AP ; Checking if first term is an integer ; Loop to print the K consecutive integers ; Driver Code
class GFG { static void checksum ( int n , int k ) { float first_term = ( float ) ( ( ( 2 * n ) / k + ( 1 - k ) ) / 2.0 ) ; if ( first_term - ( int ) ( first_term ) == 0 ) { for ( int i = ( int ) first_term ; i <= first_term + k - 1 ; i ++ ) { System . out . print ( i + " ▁ " ) ; } } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 33 , k = 6 ; checksum ( n , k ) ; } }
Represent N as sum of K even numbers | Java implementation to represent N as sum of K even numbers ; Function to print the representation ; N must be greater than equal to 2 * K and must be even ; Driver Code
import java . util . * ; class GFG { static void sumEvenNumbers ( int N , int K ) { int check = N - 2 * ( K - 1 ) ; if ( check > 0 && check % 2 == 0 ) { for ( int i = 0 ; i < K - 1 ; i ++ ) { System . out . print ( "2 ▁ " ) ; } System . out . println ( check ) ; } else { System . out . println ( " - 1" ) ; } } public static void main ( String args [ ] ) { int N = 8 ; int K = 2 ; sumEvenNumbers ( N , K ) ; } }
Count of contiguous subarrays possible for every index by including the element at that index | Java program to find the number of contiguous subarrays including the element at every index of the array of size N ; Function to find the number of subarrays including the element at every index of the array ; Creating an array of size N ; The loop is iterated till half the length of the array ; Condition to avoid overwriting the middle element for the array with even length . ; Computing the number of subarrays ; The ith element from the beginning and the ending have the same number of possible subarray ; Function to print the vector ; Driver code
import java . util . Scanner ; class contiguous_subarrays { public static int [ ] calculateWays ( int n ) { int x = 0 ; int [ ] v = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) v [ i ] = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( n % 2 == 0 && i == n / 2 ) break ; x = n * ( i + 1 ) - ( i + 1 ) * i ; v [ i ] = x ; v [ n - i - 1 ] = x ; } return v ; } public static void printArray ( int [ ] v ) { for ( int i = 0 ; i < v . length ; i ++ ) System . out . print ( v [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int [ ] v ; v = calculateWays ( 4 ) ; printArray ( v ) ; } }
Smallest number greater than or equal to X whose sum of digits is divisible by Y | Java program to find the smallest number greater than or equal to X and divisible by Y ; Function that returns the sum of digits of a number ; Initialize variable to store the sum ; Add the last digit of the number ; Remove the last digit from the number ; Function that returns the smallest number greater than or equal to X and divisible by Y ; Initialize result variable ; Loop through numbers greater than equal to X ; Calculate sum of digits ; Check if sum of digits is divisible by Y ; Driver code
class GFG { static final int MAXN = 10000000 ; static int sumOfDigits ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % 10 ; n /= 10 ; } return sum ; } static int smallestNum ( int X , int Y ) { int res = - 1 ; for ( int i = X ; i < MAXN ; i ++ ) { int sum_of_digit = sumOfDigits ( i ) ; if ( sum_of_digit % Y == 0 ) { res = i ; break ; } } return res ; } public static void main ( String [ ] args ) { int X = 5923 , Y = 13 ; System . out . print ( smallestNum ( X , Y ) ) ; } }
Count the numbers which can convert N to 1 using given operation | Java program to count the numbers which can convert N to 1 using the given operation ; Function to count the numbers which can convert N to 1 using the given operation ; Store all the divisors of N ; If i is a divisor ; If i is not equal to N / i ; Iterate through all the divisors of N - 1 and count them in answer ; Check if N - 1 is a divisor or not ; Iterate through all divisors and check for N mod d = 1 or ( N - 1 ) mod d = 0 ; Driver code
import java . util . * ; class GFG { static int countValues ( int N ) { Vector < Integer > div = new Vector < > ( ) ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { div . add ( i ) ; if ( N != i * i ) { div . add ( N / i ) ; } } } int answer = 0 ; for ( int i = 1 ; i * i <= N - 1 ; i ++ ) { if ( ( N - 1 ) % i == 0 ) { if ( i * i == N - 1 ) answer ++ ; else answer += 2 ; } } for ( int d : div ) { int K = N ; while ( K % d == 0 ) K /= d ; if ( ( K - 1 ) % d == 0 ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( countValues ( N ) ) ; } }
Maximum possible prime divisors that can exist in numbers having exactly N divisors | Java implementation to find the maximum possible prime divisor of a number can have N divisors ; Function to find the maximum possible prime divisors of a number can have with N divisors ; Number of time number divided by 2 ; Divide by other prime numbers ; If the last number of also prime then also include it ; Driver Code ; Function Call
import java . util . * ; class GFG { static void findMaxPrimeDivisor ( int n ) { int max_possible_prime = 0 ; while ( n % 2 == 0 ) { max_possible_prime ++ ; n = n / 2 ; } for ( int i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { max_possible_prime ++ ; n = n / i ; } } if ( n > 2 ) { max_possible_prime ++ ; } System . out . print ( max_possible_prime + "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 4 ; findMaxPrimeDivisor ( n ) ; } }
Count ways to express a number as sum of exactly two numbers | Java program to count ways to express a number as sum of two numbers . ; Function returns the count of ways express a number as sum of two numbers . ; Driver code
class GFG { static int CountWays ( int n ) { int ans = ( n - 1 ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . print ( CountWays ( N ) ) ; } }
Divide array in two maximum equal length arrays of similar and dissimilar elements | Java program to find the max - size to which an array can be divided into 2 equal parts such that one part contains unique elements while another contains similar elements ; Function to find the max - size to which an array can be divided into 2 equal parts ; Array to find the frequency of each element of array ; Find the index maximum frequency element present in array arr [ ] ; Find total unique elements present in array arr [ ] ; Find the Max - Size to which an array arr [ ] can be splitted ; Find the first array containing same elements ; Find the second array containing unique elements ; Driver Code ; initialise n ; array declaration ; size of array
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void Solve ( int arr [ ] , int size , int n ) { int [ ] v = new int [ n + 1 ] ; for ( int i = 0 ; i < size ; i ++ ) v [ arr [ i ] ] ++ ; int max1 = - 1 , mx = - 1 ; for ( int i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] > mx ) { mx = v [ i ] ; max1 = i ; } } int cnt = 0 ; for ( int i : v ) { if ( i == 0 ) ++ cnt ; } int diff1 = n + 1 - cnt ; int max_size = Math . max ( Math . min ( v [ max1 ] - 1 , diff1 ) , Math . min ( v [ max1 ] , diff1 - 1 ) ) ; System . out . println ( " Maximum ▁ size ▁ is : ▁ " + max_size ) ; System . out . println ( " First ▁ Array ▁ is " ) ; for ( int i = 0 ; i < max_size ; i ++ ) { System . out . print ( max1 + " ▁ " ) ; v [ max1 ] -= 1 ; } System . out . println ( ) ; System . out . println ( " The ▁ Second ▁ Array ▁ Is ▁ : " ) ; for ( int i = 0 ; i < ( n + 1 ) ; i ++ ) { if ( v [ i ] > 0 ) { System . out . print ( i + " ▁ " ) ; max_size -- ; } if ( max_size < 1 ) break ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 7 ; int arr [ ] = new int [ ] { 1 , 2 , 1 , 5 , 1 , 6 , 7 , 2 } ; int size = arr . length ; Solve ( arr , size , n ) ; } }
Find sum of xor of all unordered triplets of the array | Java program to find sum of xor of all unordered triplets of the array ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns n ^ ( - 1 ) mod p ; Returns nCr % p using Fermat 's little theorem. ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function returns sum of xor of all unordered triplets of the array ; Iterating over the bits ; Number of elements whith k 'th bit  1 and 0 respectively ; Checking if k 'th bit is 1 ; Adding this bit 's part to the answer ; Driver code
class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static int modInverse ( int n , int p ) { return power ( n , p - 2 , p ) ; } static int nCrModPFermat ( int n , int r , int p ) { if ( r == 0 ) return 1 ; if ( n < r ) return 0 ; int fac [ ] = new int [ n + 1 ] ; fac [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ n - r ] , p ) % p ) % p ; } static int SumOfXor ( int a [ ] , int n ) { int mod = 10037 ; int answer = 0 ; for ( int k = 0 ; k < 32 ; k ++ ) { int x = 0 , y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & ( 1 << k ) ) != 0 ) x ++ ; else y ++ ; } answer += ( ( 1 << k ) % mod * ( nCrModPFermat ( x , 3 , mod ) + x * nCrModPFermat ( y , 2 , mod ) ) % mod ) % mod ; } return answer ; } public static void main ( String [ ] args ) { int n = 5 ; int A [ ] = { 3 , 5 , 2 , 18 , 7 } ; System . out . println ( SumOfXor ( A , n ) ) ; } }
Probability of not getting two consecutive heads together in N tosses of coin | Java implementation to find the probability of not getting two consecutive heads together when N coins are tossed ; Function to compute the N - th Fibonacci number in the sequence where a = 2 and b = 3 ; The first two numbers in the sequence are initialized ; Base cases ; Loop to compute the fibonacci sequence based on the first two initialized numbers ; Function to find the probability of not getting two consecutive heads when N coins are tossed ; Computing the number of favourable cases ; Computing the number of all possible outcomes for N tosses ; Driver code
class GFG { public static float round ( float var , int digit ) { float value = ( int ) ( var * Math . pow ( 10 , digit ) + .5 ) ; return ( float ) value / ( float ) Math . pow ( 10 , digit ) ; } public static int probability ( int N ) { int a = 2 ; int b = 3 ; if ( N == 1 ) { return a ; } else if ( N == 2 ) { return b ; } else { for ( int i = 3 ; i <= N ; i ++ ) { int c = a + b ; a = b ; b = c ; } return b ; } } public static float operations ( int N ) { int x = probability ( N ) ; int y = ( int ) Math . pow ( 2 , N ) ; return round ( ( float ) x / ( float ) y , 2 ) ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( ( operations ( N ) ) ) ; } }