codeparrot/xlcost-text-to-code huggingface dataset:
Program to find the value of sin ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | Java Program to find the value of sin ( n ? ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; find cosTheta from sinTheta ; store required answer ; use to toggle sign in sequence . ; Driver code
public class GFG { private static final int MAX = 16 ; static long nCr [ ] [ ] = new long [ MAX ] [ MAX ] ; static void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } static double findCosNTheta ( double sinTheta , int n ) { double cosTheta = Math . sqrt ( 1 - sinTheta * sinTheta ) ; double ans = 0 ; long toggle = 1 ; for ( int i = 1 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * Math . pow ( cosTheta , n - i ) * Math . pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans ; } public static void main ( String args [ ] ) { binomial ( ) ; double sinTheta = 0.5 ; int n = 10 ; System . out . println ( findCosNTheta ( sinTheta , n ) ) ; } }
Program to find Nth term of series 9 , 23 , 45 , 75 , 113. . . | Java program to find N - th term of the series : 9 , 23 , 45 , 75 , 113. . . ; calculate Nth term of series ; Driver code ; Get the value of N ; Find the Nth term and print it
class GFG { static int nthTerm ( int N ) { return ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
Program to Find the value of cos ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | Java program to find the value of cos ( n - theta ) ; Function to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of cos ( n - theta ) ; find sinTheta from cosTheta ; to store required answer ; use to toggle sign in sequence . ; Driver code
class GFG { static int MAX = 16 ; static int [ ] [ ] nCr = new int [ MAX ] [ MAX ] ; static void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } static double findCosnTheta ( double cosTheta , int n ) { double sinTheta = Math . sqrt ( 1 - cosTheta * cosTheta ) ; double ans = 0 ; int toggle = 1 ; for ( int i = 0 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * Math . pow ( cosTheta , n - i ) * Math . pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans ; } public static void main ( String [ ] args ) { binomial ( ) ; double cosTheta = 0.5 ; int n = 10 ; System . out . println ( String . format ( " % .5f " , findCosnTheta ( cosTheta , n ) ) ) ; } }
Find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | Java Program to find Sum of first n terms ; Returning the final sum ; Driver code ; no . of terms to find the sum
import java . util . * ; class solution { static int calculateSum ( int n ) { return ( ( int ) Math . pow ( 10 , n + 1 ) * ( 9 * n - 1 ) + 10 ) / ( int ) Math . pow ( 9 , 3 ) - n * ( n + 1 ) / 18 ; } public static void main ( String ar [ ] ) { int n = 3 ; System . out . println ( " Sum = ▁ " + calculateSum ( n ) ) ; } }
Find sum of the series 1 | Java program to find sum of first n terms of the given series ; when n is odd ; when n is not odd ; Driver code ; no . of terms to find the sum
import java . util . * ; class GFG { static int calculateSum ( int n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } public static void main ( String ar [ ] ) { int n = 8 ; System . out . println ( calculateSum ( n ) ) ; } }
Check if a number can be expressed as a ^ b | Set 2 | Java program to check if a number can be expressed as a ^ b . ; Driver code
public class GFG { static boolean isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 16 ; System . out . println ( isPower ( n ) ? " Yes " : " No " ) ; } }
Program to calculate Root Mean Square | Java program to calculate Root Mean Square ; Function that Calculate Root Mean Square ; Calculate square . ; Calculate Mean . ; Calculate Root . ; Driver Code
class GFG { static float rmsValue ( int arr [ ] , int n ) { int square = 0 ; float mean = 0 ; float root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { square += Math . pow ( arr [ i ] , 2 ) ; } mean = ( square / ( float ) ( n ) ) ; root = ( float ) Math . sqrt ( mean ) ; return root ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 4 , 6 , 8 } ; int n = arr . length ; System . out . println ( rmsValue ( arr , n ) ) ; } }
Program to find the quantity after mixture replacement | Java code using above Formula . ; Function to calculate the Remaining amount . ; calculate Right hand Side ( RHS ) . ; calculate Amount left by multiply it with original value . ; Driver Code
import java . io . * ; class GFG { static double Mixture ( int X , int Y , int Z ) { double result1 = 0.0 , result = 0.0 ; result1 = ( ( X - Y ) / ( float ) X ) ; result = Math . pow ( result1 , Z ) ; result = result * X ; return result ; } public static void main ( String [ ] args ) { int X = 10 , Y = 2 , Z = 2 ; System . out . println ( ( float ) Mixture ( X , Y , Z ) + " ▁ litres " ) ; } }
Sum of sum of all subsets of a set formed by first N natural numbers | Java program to find Sum of all subsets of a set formed by first N natural numbers | Set - 2 ; 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 the result ; y must be even now y = y / 2 ; function to find ff ( n ) ; modulo value ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver Code ; function call
class Geeks { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int check ( int n ) { int mod = ( int ) ( 1e9 + 7 ) ; n -- ; int ans = n * n ; if ( ans >= mod ) ans %= mod ; ans += n + 2 ; if ( ans >= mod ) ans %= mod ; ans = ( power ( 2 , n , mod ) % mod * ans % mod ) % mod ; ans = ( ans - 1 + mod ) % mod ; return ans ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( check ( n ) ) ; } }
Program to find LCM of 2 numbers without using GCD | Java program to find LCM of 2 numbers without using GCD ; Function to return LCM of two numbers ; Driver program to test above function
import java . io . * ; import java . lang . * ; class GfG { public static int findLCM ( int a , int b ) { int lar = Math . max ( a , b ) ; int small = Math . min ( a , b ) ; for ( int i = lar ; ; i += lar ) { if ( i % small == 0 ) return i ; } } public static void main ( String [ ] argc ) { int a = 5 , b = 7 ; System . out . println ( " LCM ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + findLCM ( a , b ) ) ; } }
Smarandache | Java program to print the first ' n ' terms of the Smarandache - Wellin Sequence ; Function to collect first ' n ' prime numbers ; List to store first ' n ' primes ; Function to generate Smarandache - Wellin Sequence ; Storing the first ' n ' prime numbers in a list ; Driver Code
class GFG { static void primes ( int n ) { int i = 2 ; int j = 0 ; int [ ] result = new int [ n ] ; int z = 0 ; while ( j < n ) { boolean flag = true ; for ( int item = 2 ; item <= ( int ) ( i * 1 / 2 ) ; item ++ ) if ( i % item == 0 && i != item ) { flag = false ; break ; } if ( flag ) { result [ z ++ ] = i ; j += 1 ; } i += 1 ; } for ( i = 0 ; i < result . length ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) System . out . print ( result [ j ] ) ; System . out . print ( " ▁ " ) ; } } static void smar_wln ( int n ) { primes ( n ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " First ▁ " + n + " ▁ terms ▁ of ▁ the ▁ Sequence ▁ are " ) ; smar_wln ( n ) ; } }
Pentatope number | Java Program to find the nth Pentatope number ; function for Pentatope number ; formula for find Pentatope nth term ; Driver Code
import java . io . * ; class GFG { static int Pentatope_number ( int n ) { return n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 24 ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( n + " th ▁ " + " Pentatope ▁ number ▁ : " + Pentatope_number ( n ) ) ; n = 12 ; System . out . println ( n + " th ▁ " + " Pentatope ▁ number ▁ : " + Pentatope_number ( n ) ) ; } }
Program for Centered Icosahedral Number | Java Program to find nth Centered icosahedral number Java Program to find nth Centered icosahedral number ; Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code
import java . io . * ; class GFG { static int centeredIcosahedralNum ( int n ) { return ( 2 * n + 1 ) * ( 5 * n * n + 5 * n + 3 ) / 3 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( centeredIcosahedralNum ( n ) ) ; n = 12 ; System . out . println ( centeredIcosahedralNum ( n ) ) ; } }
Centered Square Number | Java program to find nth Centered square number ; Function to calculate Centered square number function ; Formula to calculate nth Centered square number ; Driver Code
import java . io . * ; class GFG { static int centered_square_num ( int n ) { return n * n + ( ( n - 1 ) * ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( n + " th ▁ Centered " + " ▁ square ▁ number : ▁ " + centered_square_num ( n ) ) ; } }
Sum of first n natural numbers | java program to find sum series 1 , 3 , 6 , 10 , 15 , 21. . . and then find its sum ; Function to find the sum of series ; Driver code
import java . io . * ; class GFG { static int seriesSum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( seriesSum ( n ) ) ; } }
Dodecagonal number | Java program to find the nth Dodecagonal number ; function for Dodecagonal number ; formula for find Dodecagonal nth term ; Driver Code
import java . util . * ; class GFG { static int Dodecagonal_number ( int n ) { return 5 * n * n - 4 * n ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( Dodecagonal_number ( n ) ) ; n = 12 ; System . out . println ( Dodecagonal_number ( n ) ) ; } }
Arithmetic Number | Java program to check if a number is Arithmetic number or not ; Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; cnt = cnt + 1 ; incrementing power ; Calculating number of divisors If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; return ans ; Total divisors ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driver Program
class GFG { static void SieveOfEratosthenes ( int n , boolean prime [ ] , boolean primesquare [ ] , int a [ ] ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int i = 0 ; i <= ( n * n ) ; i ++ ) primesquare [ i ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } int j = 0 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { a [ j ] = p ; primesquare [ p * p ] = true ; j ++ ; } } } static int countDivisors ( int n ) { if ( n == 1 ) return 1 ; boolean prime [ ] = new boolean [ n + 1 ] , primesquare [ ] = new boolean [ n * n + 1 ] ; SieveOfEratosthenes ( n , prime , primesquare , a ) ; int ans = 1 ; for ( int i = 0 ; ; i ++ ) { if ( a [ i ] * a [ i ] * a [ i ] > n ) break ; int cnt = 1 ; while ( n % a [ i ] == 0 ) { n = n / a [ i ] ; } ans = ans * cnt ; } if ( prime [ n ] ) ans = ans * 2 ; else if ( primesquare [ n ] ) ans = ans * 3 ; else if ( n != 1 ) ans = ans * 4 ; } static int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } static boolean checkArithmetic ( int n ) { int count = countDivisors ( n ) ; int sum = sumofFactors ( n ) ; return ( sum % count == 0 ) ; } public static void main ( String [ ] args ) { int n = 6 ; if ( checkArithmetic ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Finding n | A formula based Java program to find sum of series with cubes of first n natural numbers ; Driver Function
class GFG { static int magicOfSequence ( int N ) { return ( N * ( N + 1 ) / 2 ) + 2 * N ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( magicOfSequence ( N ) ) ; } }
Form a number using corner digits of powers | Java program to find number formed by corner digits of powers . ; Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Storing N raised to power 0 ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code
import java . io . * ; import java . util . * ; public class GFG { static List < Integer > power = new ArrayList < Integer > ( ) ; static void nextPower ( Integer N ) { Integer carry = 0 ; for ( int i = 0 ; i < power . size ( ) ; i ++ ) { Integer prod = ( power . get ( i ) * N ) + carry ; power . set ( i , prod % 10 ) ; carry = prod / 10 ; } while ( carry >= 1 ) { power . add ( carry % 10 ) ; carry = carry / 10 ; } } static void printPowerNumber ( int X , int N ) { power . add ( 1 ) ; List < Integer > res = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i <= X ; i ++ ) { nextPower ( N ) ; res . add ( power . get ( power . size ( ) - 1 ) ) ; res . add ( power . get ( 0 ) ) ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) ) ; } public static void main ( String args [ ] ) { Integer N = 19 , X = 4 ; printPowerNumber ( X , N ) ; } }
First digit in factorial of a number | A Java program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; derive main
class GFG { static int firstDigit ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact = fact * i ; while ( fact % 10 == 0 ) fact = fact / 10 ; } while ( fact >= 10 ) fact = fact / 10 ; return fact ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( firstDigit ( n ) ) ; } }
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | Java program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Code
import java . io . * ; import java . math . * ; class GFG { static int sumofseries ( int n ) { int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) res += ( i ) * ( i + 1 ) * ( i + 2 ) ; return res ; } public static void main ( String [ ] args ) { System . out . println ( sumofseries ( 3 ) ) ; } }
Find N Geometric Means between A and B | java program to illustrate n geometric mean between A and B ; insert function for calculating the means ; Finding the value of R Common ration ; for finding N the Geometric mean between A and B ; Driver code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void printGMeans ( int A , int B , int N ) { float R = ( float ) Math . pow ( ( float ) ( B / A ) , 1.0 / ( float ) ( N + 1 ) ) ; for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( A * Math . pow ( R , i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int A = 3 , B = 81 , N = 2 ; printGMeans ( A , B , N ) ; } }
Numbers having difference with digit sum more than s | Java Program to find number of integer such that integer - digSum > s ; function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; Driver program
import java . io . * ; class GFG { static int digitSum ( long n ) { int digSum = 0 ; while ( n > 0 ) { digSum += n % 10 ; n /= 10 ; } return digSum ; } public static long countInteger ( long n , long s ) { if ( n < s ) return 0 ; for ( long i = s ; i <= Math . min ( n , s + 163 ) ; i ++ ) if ( ( i - digitSum ( i ) ) > s ) return ( n - i + 1 ) ; return 0 ; } public static void main ( String args [ ] ) { long n = 1000 , s = 100 ; System . out . println ( countInteger ( n , s ) ) ; } }
Division without using ' / ' operator | Java program to divide a number by other without using / operator ; Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver program
import java . io . * ; class GFG { static int division ( int num1 , int num2 ) { if ( num1 == 0 ) return 0 ; if ( num2 == 0 ) return Integer . MAX_VALUE ; boolean negResult = false ; if ( num1 < 0 ) { num1 = - num1 ; if ( num2 < 0 ) num2 = - num2 ; else negResult = true ; } else if ( num2 < 0 ) { num2 = - num2 ; negResult = true ; } int quotient = 0 ; while ( num1 >= num2 ) { num1 = num1 - num2 ; quotient ++ ; } if ( negResult ) quotient = - quotient ; return quotient ; } public static void main ( String [ ] args ) { int num1 = 13 , num2 = 2 ; System . out . println ( division ( num1 , num2 ) ) ; } }
Nonagonal number | Java Program find first n nonagonal number . ; Function to find nonagonal number series . ; Driver Code
import java . io . * ; class GFG { static void Nonagonal ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( i * ( 7 * i - 5 ) / 2 ) ; System . out . print ( " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 10 ; Nonagonal ( n ) ; } }
Find n | Java Program to calculate nth term of a series ; func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code
import java . io . * ; class GFG { static int seriesFunc ( int n ) { int sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sumNatural = ( n * ( n + 1 ) / 2 ) ; return ( sumSquare + sumNatural + 1 ) ; } public static void main ( String args [ ] ) { int n = 8 ; System . out . println ( seriesFunc ( n ) ) ; n = 13 ; System . out . println ( seriesFunc ( 13 ) ) ; } }
Program to check Plus Perfect Number | java implementation to check if the number is plus perfect or not ; function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; Driver program
import java . io . * ; class GFG { static boolean checkplusperfect ( int x ) { int temp = x ; int n = 0 ; while ( x != 0 ) { x /= 10 ; n ++ ; } x = temp ; int sum = 0 ; while ( x != 0 ) { sum += Math . pow ( x % 10 , n ) ; x /= 10 ; } return ( sum == temp ) ; } public static void main ( String [ ] args ) { int x = 9474 ; if ( checkplusperfect ( x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Number of distinct subsets of a set | Java program to count number of distinct subsets in an array of distinct numbers ; Returns 2 ^ n ; Driver program to test above function
class GFG { static int subsetCount ( int arr [ ] , int n ) { return 1 << n ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int n = A . length ; System . out . println ( subsetCount ( A , n ) ) ; } }
Program to calculate GST from original and net prices | Java Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver code
import java . io . * ; class GFG { static float Calculate_GST ( float org_cost , float N_price ) { return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; } public static void main ( String [ ] args ) { float org_cost = 100 ; float N_price = 120 ; System . out . print ( " ▁ GST ▁ = ▁ " + Calculate_GST ( org_cost , N_price ) + " % " ) ; } }
Centered hexagonal number | Java Program to find nth centered hexadecimal number ; Function to find centered hexadecimal number ; Formula to calculate nth centered hexadecimal number and return it into main function ; Driver Code
import java . io . * ; class GFG { static int centeredHexagonalNumber ( int n ) { return 3 * n * ( n - 1 ) + 1 ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . print ( n + " th ▁ centered ▁ " + " hexagonal ▁ number : ▁ " ) ; System . out . println ( centeredHexagonalNumber ( n ) ) ; } }
Find the distance covered to collect items at equal distances | Java program to calculate the distance for given problem ; function to calculate the distance ; Driver program
class demo { public static int find_distance ( int n ) { return n * ( 3 * n + 7 ) ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . print ( " Distance ▁ = ▁ " ) ; System . out . println ( find_distance ( n ) ) ; } }
Twin Prime Numbers | JAVA Code for Twin Prime Numbers ; Please refer below post for details of this function https : goo . gl / Wv3fGv ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver program to test above function
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 boolean twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . abs ( n1 - n2 ) == 2 ) ; } public static void main ( String [ ] args ) { int n1 = 11 , n2 = 13 ; if ( twinPrime ( n1 , n2 ) ) System . out . println ( " Twin ▁ Prime " ) ; else System . out . println ( " Not ▁ Twin ▁ Prime " ) ; } }
Sum of the sequence 2 , 22 , 222 , ... ... ... | JAVA Code for Sum of the sequence 2 , 22 , 222 , ... ; function which return the the sum of series ; Driver program
import java . util . * ; class GFG { static double sumOfSeries ( int n ) { return 0.0246 * ( Math . pow ( 10 , n ) - 1 - ( 9 * n ) ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( sumOfSeries ( n ) ) ; } }