codeparrot/xlcost-text-to-code huggingface dataset:
Newton 's Divided Difference Interpolation Formula | Java program for implementing Newton divided difference formula ; Function to find the product term ; Function for calculating divided difference table ; Function for applying Newton 's divided difference formula ; Function for displaying divided difference table ; Driver Function ; number of inputs given ; y [ ] [ ] is used for divided difference table where y [ ] [ 0 ] is used for input ; calculating divided difference table ; displaying divided difference table ; value to be interpolated ; printing the value
import java . text . * ; import java . math . * ; class GFG { static float proterm ( int i , float value , float x [ ] ) { float pro = 1 ; for ( int j = 0 ; j < i ; j ++ ) { pro = pro * ( value - x [ j ] ) ; } return pro ; } static void dividedDiffTable ( float x [ ] , float y [ ] [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { y [ j ] [ i ] = ( y [ j ] [ i - 1 ] - y [ j + 1 ] [ i - 1 ] ) / ( x [ j ] - x [ i + j ] ) ; } } } static float applyFormula ( float value , float x [ ] , float y [ ] [ ] , int n ) { float sum = y [ 0 ] [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum = sum + ( proterm ( i , value , x ) * y [ 0 ] [ i ] ) ; } return sum ; } static void printDiffTable ( float y [ ] [ ] , int n ) { DecimalFormat df = new DecimalFormat ( " # . # # # # " ) ; df . setRoundingMode ( RoundingMode . HALF_UP ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { String str1 = df . format ( y [ i ] [ j ] ) ; System . out . print ( str1 + " TABSYMBOL ▁ " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; float value , sum ; float y [ ] [ ] = new float [ 10 ] [ 10 ] ; float x [ ] = { 5 , 6 , 9 , 11 } ; y [ 0 ] [ 0 ] = 12 ; y [ 1 ] [ 0 ] = 13 ; y [ 2 ] [ 0 ] = 14 ; y [ 3 ] [ 0 ] = 16 ; dividedDiffTable ( x , y , n ) ; printDiffTable ( y , n ) ; value = 7 ; DecimalFormat df = new DecimalFormat ( " # . # # " ) ; df . setRoundingMode ( RoundingMode . HALF_UP ) ; System . out . println ( " Value at " + df . format ( value ) + " is " + df . format ( applyFormula ( value , x , y , n ) ) ) ; } }
Centered heptagonal number | Java program to find n - th Centered heptagonal number ; Function to find Centered heptagonal number ; Formula to calculate nth Centered heptagonal number ; Driver Code
import java . io . * ; class GFG { static long centered_heptagonal_num ( long n ) { return ( 7 * n * n - 7 * n + 2 ) / 2 ; } public static void main ( String [ ] args ) { long n = 5 ; System . out . println ( n + " th ▁ Centered ▁ " + " heptagonal ▁ number ▁ : ▁ " + centered_heptagonal_num ( n ) ) ; } }
Sum of square | Java Program to find the sum of sum of squares of first n natural number ; Function to find sum of sum of square of first n natural number ; Driver Program
class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( ( i * ( i + 1 ) * ( 2 * i + 1 ) ) / 6 ) ; return sum ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( findSum ( n ) ) ; } }
Check if a given matrix is Hankel or not | Java Program to check if given matrix is Hankel Matrix or not . ; Function to check if given matrix is Hankel Matrix or not . ; for each row ; for each column ; checking if i + j is less than n ; checking if the element is equal to the corresponding diagonal constant ; checking if the element is equal to the corresponding diagonal constant ; Drivers code
import java . io . * ; import java . util . * ; class GFG { static boolean checkHankelMatrix ( int n , int m [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i + j < n ) { if ( m [ i ] [ j ] != m [ i + j ] [ 0 ] ) return false ; } else { if ( m [ i ] [ j ] != m [ i + j - n + 1 ] [ n - 1 ] ) return false ; } } } return true ; } public static void main ( String args [ ] ) { int n = 4 ; int m [ ] [ ] = { { 1 , 2 , 3 , 5 } , { 2 , 3 , 5 , 8 } , { 3 , 5 , 8 , 0 } , { 5 , 8 , 0 , 9 } } ; if ( checkHankelMatrix ( n , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if a number can be expressed as power | Set 2 ( Using Log ) | Java program to find if a number can be expressed as x raised to power y . ; Find Log n in different bases and check if the value is an integer ; Driver code
class GFG { static boolean isPower ( int n ) { for ( int x = 2 ; x <= ( int ) Math . sqrt ( n ) ; x ++ ) { float f = ( float ) Math . log ( n ) / ( float ) Math . log ( x ) ; if ( ( f - ( int ) f ) == 0.0 ) return true ; } return false ; } public static void main ( String args [ ] ) { for ( int i = 2 ; i < 100 ; i ++ ) if ( isPower ( i ) ) System . out . print ( i + " ▁ " ) ; } }
Queries on sum of odd number digit sums of all the factors of a number | Java Program to answer queries on sum of sum of odd number digits of all the factors of a number ; finding sum of odd digit number in each integer . ; for each number ; using previous number sum , finding the current number num of odd digit also , adding last digit if it is odd . ; finding sum of sum of odd digit of all the factors of a number . ; for each possible factor ; adding the contribution . ; Wrapper function ; Driver Code
class GFG { static int N = 1000005 ; static void sumOddDigit ( int digitSum [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { digitSum [ i ] = digitSum [ i / 10 ] + ( i & 1 ) * ( i % 10 ) ; } } static void sumFactor ( int digitSum [ ] , int factorDigitSum [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { factorDigitSum [ j ] += digitSum [ i ] ; } } } static void wrapper ( int q , int n [ ] ) { int digitSum [ ] = new int [ N ] ; int factorDigitSum [ ] = new int [ N ] ; sumOddDigit ( digitSum ) ; sumFactor ( digitSum , factorDigitSum ) ; for ( int i = 0 ; i < q ; i ++ ) System . out . print ( factorDigitSum [ n [ i ] ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int q = 2 ; int n [ ] = new int [ ] { 10 , 36 } ; wrapper ( q , n ) ; } }
Program for Gauss | Java Implementation for Gauss - Jordan Elimination Method ; Function to print the matrix ; function to reduce matrix to reduced row echelon form . ; Performing elementary operations ; Excluding all i == j ; Converting Matrix to reduced row echelon form ( diagonal matrix ) ; Function to print the desired result if unique solutions exists , otherwise prints no solution or infinite solutions depending upon the input given . ; Printing the solution by dividing constants by their respective diagonal elements ; To check whether infinite solutions exists or no solution exists ; flag == 2 for infinite solution flag == 3 for No solution ; Driver code ; Order of Matrix ( n ) ; Performing Matrix transformation ; Printing Final Matrix ; Printing Solutions ( if exist )
class GFG { static int M = 10 ; static void PrintMatrix ( float a [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static int PerformOperation ( float a [ ] [ ] , int n ) { int i , j , k = 0 , c , flag = 0 , m = 0 ; float pro = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] [ i ] == 0 ) { c = 1 ; while ( ( i + c ) < n && a [ i + c ] [ i ] == 0 ) c ++ ; if ( ( i + c ) == n ) { flag = 1 ; break ; } for ( j = i , k = 0 ; k <= n ; k ++ ) { float temp = a [ j ] [ k ] ; a [ j ] [ k ] = a [ j + c ] [ k ] ; a [ j + c ] [ k ] = temp ; } } for ( j = 0 ; j < n ; j ++ ) { if ( i != j ) { float p = a [ j ] [ i ] / a [ i ] [ i ] ; for ( k = 0 ; k <= n ; k ++ ) a [ j ] [ k ] = a [ j ] [ k ] - ( a [ i ] [ k ] ) * p ; } } } return flag ; } static void PrintResult ( float a [ ] [ ] , int n , int flag ) { System . out . print ( " Result ▁ is ▁ : ▁ " ) ; if ( flag == 2 ) System . out . println ( " Infinite ▁ Solutions ▁ Exists " ) ; else if ( flag == 3 ) System . out . println ( " No ▁ Solution ▁ Exists " ) ; else { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] [ n ] / a [ i ] [ i ] + " ▁ " ) ; } } static int CheckConsistency ( float a [ ] [ ] , int n , int flag ) { int i , j ; float sum ; flag = 3 ; for ( i = 0 ; i < n ; i ++ ) { sum = 0 ; for ( j = 0 ; j < n ; j ++ ) sum = sum + a [ i ] [ j ] ; if ( sum == a [ i ] [ j ] ) flag = 2 ; } return flag ; } public static void main ( String [ ] args ) { float a [ ] [ ] = { { 0 , 2 , 1 , 4 } , { 1 , 1 , 2 , 6 } , { 2 , 1 , 1 , 7 } } ; int n = 3 , flag = 0 ; flag = PerformOperation ( a , n ) ; if ( flag == 1 ) flag = CheckConsistency ( a , n , flag ) ; System . out . println ( " Final ▁ Augumented ▁ Matrix ▁ is ▁ : ▁ " ) ; PrintMatrix ( a , n ) ; System . out . println ( " " ) ; PrintResult ( a , n , flag ) ; } }
Number of digits in the nth number made of given four digits | Java program to count number of digits in n - th number made of given four digits . ; Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver Code
import java . io . * ; class GFG { static int number_of_digits ( int n ) { int i ; int res ; int sum = 0 ; for ( i = 4 , res = 1 ; ; i *= 4 , res ++ ) { sum += i ; if ( sum >= n ) break ; } return res ; } public static void main ( String [ ] args ) { int n = 21 ; System . out . println ( number_of_digits ( n ) ) ; } }
Print prime numbers from 1 to N in reverse order | Java program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; 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 ; Print all prime numbers ; Driver Program to test above function ; static input ; To display ; reverseorder ( N ) ; calling the function
import java . io . * ; import java . util . * ; class GFG { static void reverseorder ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = n ; i >= 2 ; i -- ) if ( prime [ i ] == true ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int N = 25 ; System . out . println ( " Prime ▁ number ▁ in ▁ reverse ▁ order " ) ; if ( N == 1 ) System . out . println ( " No ▁ prime ▁ no ▁ exist ▁ in ▁ this ▁ range " ) ; else } }
Vantieghems Theorem for Primality Test | Java code to verify Vantieghem 's Theorem ; Check if above condition is satisfied ; product of previous powers of 2 ; Driver code
import java . util . * ; class GFG { static void checkVantieghemsTheorem ( int limit ) { long prod = 1 ; for ( long n = 2 ; n < limit ; n ++ ) { if ( ( ( prod - n < 0 ? 0 : prod - n ) % ( ( 1 << n ) - 1 ) ) == 0 ) System . out . print ( n + " is primeNEW_LINE"); prod *= ( ( 1 << n ) - 1 ) ; } } public static void main ( String [ ] args ) { checkVantieghemsTheorem ( 10 ) ; } }
Count numbers formed by given two digit with sum having given digits | Java program to count the number of numbers formed by digits a and b exactly of a length N such that the sum of the digits of the number thus formed is of digits a and b . ; function to check if sum of digits is made of a and b ; sum of digits is 0 ; if any of digits in sum is other than a and b ; calculate the modInverse V / of a number in O ( log n ) ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Update y and x ; Make x positive ; function to pregenerate factorials ; function to pre calculate the modInverse of factorials ; calculates the modInverse of the last factorial ; precalculates the modInverse of all factorials by formulae ; function that returns the value of nCi ; function that returns the count of numbers ; function call to pre - calculate the factorials and modInverse of factorials ; if a and b are same ; Driver Code
class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int N = 1000005 ; static int fact [ ] = new int [ N ] , invfact [ ] = new int [ N ] ; static int check ( int x , int a , int b ) { if ( x == 0 ) { return 0 ; } while ( x > 0 ) { if ( x % 10 != a & x % 10 != b ) { return 0 ; } x /= 10 ; } return 1 ; } static int modInverse ( int a , int m ) { int m0 = m ; int y = 0 , x = 1 ; if ( m == 1 ) { return 0 ; } while ( a > 1 ) { int q = a / m ; int t = m ; m = a % m ; a = t ; t = y ; y = x - q * y ; x = t ; } if ( x < 0 ) { x += m0 ; } return x ; } static void pregenFact ( ) { fact [ 0 ] = fact [ 1 ] = 1 ; for ( int i = 1 ; i <= 1000000 ; ++ i ) { fact [ i ] = ( int ) ( ( long ) fact [ i - 1 ] * i % mod ) ; } } static void pregenInverse ( ) { invfact [ 0 ] = invfact [ 1 ] = 1 ; invfact [ 1000000 ] = modInverse ( fact [ 1000000 ] , mod ) ; for ( int i = 999999 ; i > 1 ; -- i ) { invfact [ i ] = ( int ) ( ( ( long ) invfact [ i + 1 ] * ( long ) ( i + 1 ) ) % mod ) ; } } static int comb ( int big , int small ) { return ( int ) ( ( long ) fact [ big ] * invfact [ small ] % mod * invfact [ big - small ] % mod ) ; } static int count ( int a , int b , int n ) { pregenFact ( ) ; pregenInverse ( ) ; if ( a == b ) { return ( check ( a * n , a , b ) ) ; } int ans = 0 ; for ( int i = 0 ; i <= n ; ++ i ) { if ( check ( i * a + ( n - i ) * b , a , b ) == 1 ) { ans = ( ans + comb ( n , i ) ) % mod ; } } return ans ; } public static void main ( String [ ] args ) { int a = 3 , b = 4 , n = 11028 ; System . out . println ( count ( a , b , n ) ) ; } }
Finding n | Java Program to Finding n - th term of series 3 , 13 , 42 , 108 , 235 . . . ; Function to generate a fixed number ; Driver Method
class GFG { public static int magicOfSequence ( int N ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) sum += ( i * i * i + i * 2 ) ; return sum ; } public static void main ( String args [ ] ) { int N = 4 ; System . out . println ( magicOfSequence ( N ) ) ; } }
Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | Java program to count number of ways to express N as sum of consecutive numbers . ; returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; Driver Code ; N as sum of consecutive numbers
import java . io . * ; class GFG { static int countOddFactors ( long n ) { int odd_factors = 0 ; for ( int i = 1 ; 1 * i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( 1 * i * i == n ) { if ( ( i & 1 ) == 1 ) odd_factors ++ ; } else { if ( ( i & 1 ) == 1 ) odd_factors ++ ; int factor = ( int ) n / i ; if ( ( factor & 1 ) == 1 ) odd_factors ++ ; } } } return odd_factors - 1 ; } public static void main ( String args [ ] ) { long N = 15 ; System . out . println ( countOddFactors ( N ) ) ; N = 10 ; System . out . println ( countOddFactors ( N ) ) ; } }
Making zero array by decrementing pairs of adjacent | Java program to find if it is possible to make all array elements 0 by decrement operations . ; used for storing the sum of even and odd position element in array . ; if position is odd , store sum value of odd position in odd ; if position is even , store sum value of even position in even ; Driver Code
import java . io . * ; class GFG { static boolean isPossibleToZero ( int a [ ] , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) == 0 ) odd += a [ i ] ; else even += a [ i ] ; } return ( odd == even ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 } ; int n = arr . length ; if ( isPossibleToZero ( arr , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Program for sum of cos ( x ) series | Java program to find the sum of cos ( x ) series ; here x is in degree . we have to convert it to radian for using it with series formula , as in series expansion angle is in radian ; Driver Code
import java . lang . Math . * ; class GFG { static final double PI = 3.142 ; static double cosXSertiesSum ( double x , int n ) { x = x * ( PI / 180.0 ) ; double res = 1 ; double sign = 1 , fact = 1 , pow = 1 ; for ( int i = 1 ; i < 5 ; i ++ ) { sign = sign * - 1 ; fact = fact * ( 2 * i - 1 ) * ( 2 * i ) ; pow = pow * x * x ; res = res + sign * pow / fact ; } return res ; } public static void main ( String [ ] args ) { float x = 50 ; int n = 5 ; System . out . println ( ( float ) ( cosXSertiesSum ( x , 5 ) * 1000000 ) / 1000000.00 ) ; } }
Sum of digits written in different bases from 2 to n | Java program to find sum of digits of n in different bases from 2 to n - 1. ; function to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple bases ; Driver Code
class GFG { static int solve ( int n , int base ) { int result = 0 ; while ( n > 0 ) { int remainder = n % base ; result = result + remainder ; n = n / base ; } return result ; } static void printSumsOfDigits ( int n ) { for ( int base = 2 ; base < n ; ++ base ) System . out . print ( solve ( n , base ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 8 ; printSumsOfDigits ( n ) ; } }
Possible two sets from first N natural numbers difference of sums as D | Java program for implementing above approach ; Function returns true if it is possible to split into two sets otherwise returns false ; Driver code
class GFG { static boolean check ( int N , int D ) { int temp = ( N * ( N + 1 ) ) / 2 + D ; return ( temp % 2 == 0 ) ; } static public void main ( String args [ ] ) { int N = 5 ; int M = 7 ; if ( check ( N , M ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }
Minimum digits to remove to make a number Perfect Square | Java program to find required minimum digits need to remove to make a number perfect square ; function to check minimum number of digits should be removed to make this number a perfect square ; size of the string ; our final answer ; to store string which is perfect square . ; We make all possible subsequences ; to check jth bit is set or not . ; we do not consider a number with leading zeros ; convert our temporary string into integer ; checking temp is perfect square or not . ; taking maximum sized string ; print PerfectSquare ; Driver code
import java . io . * ; import java . lang . * ; public class GFG { static int perfectSquare ( String s ) { int n = s . length ( ) ; int ans = - 1 ; String num = " " ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { String str = " " ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( i >> j ) & 1 ) == 1 ) { str += s . charAt ( j ) ; } } if ( str . charAt ( 0 ) != '0' ) { int temp = 0 ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) temp = temp * 10 + ( int ) ( str . charAt ( j ) - '0' ) ; int k = ( int ) Math . sqrt ( temp ) ; if ( k * k == temp ) { if ( ans < ( int ) str . length ( ) ) { ans = ( int ) str . length ( ) ; num = str ; } } } } if ( ans == - 1 ) return ans ; else { System . out . print ( num + " ▁ " ) ; return n - ans ; } } public static void main ( String args [ ] ) { System . out . println ( perfectSquare ( "8314" ) ) ; System . out . println ( perfectSquare ( "753" ) ) ; } }
Lagrange 's four square theorem | Java program for Lagrange 's four square identity ; Prints all the possible combinations 4 numbers whose sum of squares is equal to the given no . ; loops checking the sum of squares ; if sum of four squares equals the given no . ; printing the numbers ; Driver Code ; 74 = 0 * 0 + 0 * 0 + 5 * 5 + 7 * 7 74 = 0 * 0 + 1 * 1 + 3 * 3 + 8 * 8 74 = 0 * 0 + 3 * 3 + 4 * 4 + 7 * 7 74 = 1 * 1 + 1 * 1 + 6 * 6 + 6 * 6 74 = 2 * 2 + 3 * 3 + 5 * 5 + 6 * 6
class GFG { static void printFourSquares ( int a ) { for ( int i = 0 ; i * i <= a ; i ++ ) { for ( int j = i ; j * j <= a ; j ++ ) { for ( int k = j ; k * k <= a ; k ++ ) { for ( int l = k ; l * l <= a ; l ++ ) { if ( i * i + j * j + k * k + l * l == a ) { System . out . print ( a + " ▁ = ▁ " + i + " * " + i + " ▁ + ▁ " + j + " * " + j + " ▁ + ▁ " ) ; System . out . println ( k + " * " + k + " ▁ + ▁ " + l + " * " + l ) ; } } } } } } public static void main ( String [ ] args ) { int a = 74 ; printFourSquares ( a ) ; } }
Hardy | Java program to count all prime factors ; A function to count prime factors of a given number n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; This condition is to handle the case when n is a prime number greater than 2 ; driver function
import java . io . * ; class GFG { static int exactPrimeFactorCount ( int n ) { int count = 0 ; if ( n % 2 == 0 ) { count ++ ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { count ++ ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 51242183 ; System . out . println ( " The ▁ number ▁ of ▁ distinct ▁ " + " prime ▁ factors ▁ is / are ▁ " + exactPrimeFactorCount ( n ) ) ; System . out . println ( " The ▁ value ▁ of ▁ log ( log ( n ) ) " + " ▁ is ▁ " + Math . log ( Math . log ( n ) ) ) ; } }
Number of Digits in a ^ b | Java Program to calculate no . of digits in a ^ b ; function to calculate number of digits in a ^ b ; driver program
class GFG { static int no_of_digit ( int a , int b ) { return ( ( int ) ( b * Math . log10 ( a ) ) + 1 ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 100 ; System . out . print ( " no . ▁ of ▁ digits ▁ = ▁ " + no_of_digit ( a , b ) ) ; } }
Check whether a number is Emirpimes or not | Java code to check whether a number is Emirpimes or not ; Checking whether a number is semi - prime or not ; Increment count of prime numbers ; If number is still greater than 1 , after exiting the for loop add it to the count variable as it indicates the number is a prime number ; Return '1' if count is equal to '2' else return '0' ; Checking whether a number is emirpimes or not ; Number itself is not semiprime . ; Finding reverse of n . ; The definition of emirpimes excludes palindromes , hence we do not check further , if the number entered is a palindrome ; Checking whether the reverse of the semi prime number entered is also a semi prime number or not ; Driver Code
import java . io . * ; class GFG { static boolean checkSemiprime ( 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 ; } static boolean isEmirpimes ( int n ) { if ( checkSemiprime ( n ) == false ) return false ; int r = 0 ; for ( int t = n ; t != 0 ; t = t / n ) r = r * 10 + t % 10 ; if ( r == n ) return false ; return ( checkSemiprime ( r ) ) ; } public static void main ( String [ ] args ) { int n = 15 ; if ( isEmirpimes ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Connell Sequence | Java code to generate first ' n ' terms of Connell Sequence ; Function to generate a fixed number of even or odd terms . The size of r decides whether numbers to be generated even or odd . ; Generating the first ' n ' terms of Connell Sequence ; A dummy 0 is inserted at the beginning for consistency ; Calling function gen ( ) to generate ' k ' number of terms ; Checking if ' n ' terms are already generated ; Removing the previously inserted dummy 0 ; Driver Code
import java . util . * ; class GFG { static Vector < Long > gen ( long n , Vector < Long > r ) { long a = r . get ( r . size ( ) - 1 ) ; a ++ ; for ( int i = 1 ; i <= n ; a += 2 , i ++ ) { r . add ( a ) ; } return r ; } static Vector < Long > conell ( long n ) { Vector < Long > res = new Vector < Long > ( ) ; long k = 1 ; res . add ( 0L ) ; while ( true ) { res = gen ( k , res ) ; k ++ ; int j = res . size ( ) - 1 ; while ( j != n && j + k > n ) { k -- ; } if ( j >= n ) { break ; } } res . remove ( 0 ) ; return res ; } public static void main ( String [ ] args ) { long n = 10 ; System . out . println ( " The ▁ first ▁ " + n + " ▁ terms ▁ are " ) ; Vector < Long > res = conell ( n ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { System . out . print ( res . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; } }