codeparrot/xlcost-text-to-code huggingface dataset:
Boyer Moore Algorithm for Pattern Searching | Java Program for Bad Character Heuristic of Boyer Moore String Matching Algorithm ; A utility function to get maximum of two integers ; The preprocessing function for Boyer Moore 's  bad character heuristic ; Initialize all occurrences as - 1 ; Fill the actual value of last occurrence of a character ( indeces of table are ascii and values are index of occurence ) ; A pattern searching function that uses Bad Character Heuristic of Boyer Moore Algorithm ; Fill the bad character array by calling the preprocessing function badCharHeuristic ( ) for given pattern ; s is shift of the pattern with respect to text ; there are n - m + 1 potential allignments ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at current shift , then index j will become - 1 after the above loop ; Shift the pattern so that the next character in text aligns with the last occurrence of it in pattern . The condition s + m < n is necessary for the case when pattern occurs at the end of text txt [ s + m ] is character after the pattern in text ; Shift the pattern so that the bad character in text aligns with the last occurrence of it in pattern . The max function is used to make sure that we get a positive shift . We may get a negative shift if the last occurrence of bad character in pattern is on the right side of the current character . ; Driver program to test above function
class AWQ { static int NO_OF_CHARS = 256 ; static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static void badCharHeuristic ( char [ ] str , int size , int badchar [ ] ) { for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) badchar [ i ] = - 1 ; for ( i = 0 ; i < size ; i ++ ) badchar [ ( int ) str [ i ] ] = i ; } static void search ( char txt [ ] , char pat [ ] ) { int m = pat . length ; int n = txt . length ; int badchar [ ] = new int [ NO_OF_CHARS ] ; badCharHeuristic ( pat , m , badchar ) ; int s = 0 ; while ( s <= ( n - m ) ) { int j = m - 1 ; while ( j >= 0 && pat [ j ] == txt [ s + j ] ) j -- ; if ( j < 0 ) { System . out . println ( " Patterns ▁ occur ▁ at ▁ shift ▁ = ▁ " + s ) ; s += ( s + m < n ) ? m - badchar [ txt [ s + m ] ] : 1 ; } else s += max ( 1 , j - badchar [ txt [ s + j ] ] ) ; } } public static void main ( String [ ] args ) { char txt [ ] = " ABAAABCD " . toCharArray ( ) ; char pat [ ] = " ABC " . toCharArray ( ) ; search ( txt , pat ) ; } }
Anagram Substring Search ( Or Search for all permutations ) | Java program to search all anagrams of a pattern in a text ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of pat [ ] in txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Driver program to test above function
public class GFG { static final int MAX = 256 ; static boolean compare ( char arr1 [ ] , char arr2 [ ] ) { for ( int i = 0 ; i < MAX ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static void search ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; char [ ] countP = new char [ MAX ] ; char [ ] countTW = new char [ MAX ] ; for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat . charAt ( i ) ] ) ++ ; ( countTW [ txt . charAt ( i ) ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) System . out . println ( " Found ▁ at ▁ Index ▁ " + ( i - M ) ) ; ( countTW [ txt . charAt ( i ) ] ) ++ ; countTW [ txt . charAt ( i - M ) ] -- ; } if ( compare ( countP , countTW ) ) System . out . println ( " Found ▁ at ▁ Index ▁ " + ( N - M ) ) ; } public static void main ( String args [ ] ) { String txt = " BACDGABCDA " ; String pat = " ABCD " ; search ( pat , txt ) ; } }
Manacher 's Algorithm | Java program to implement Manacher 's Algorithm  ; Position count ; LPS Length Array ; centerPosition ; centerRightPosition ; currentRightPosition ; currentLeftPosition ; Uncomment it to print LPS Length array printf ( " % d ▁ % d ▁ " , L [ 0 ] , L [ 1 ] ) ; ; get currentLeftPosition iMirror for currentRightPosition i ; If currentRightPosition i is within centerRightPosition R ; Attempt to expand palindrome centered at currentRightPosition i . Here for odd positions , we compare characters and if match then increment LPS Length by ONE . If even position , we just increment LPS by ONE without any character comparison ; Track maxLPSLength ; If palindrome centered at currentRightPosition i expand beyond centerRightPosition R , adjust centerPosition C based on expanded palindrome . ; Uncomment it to print LPS Length array printf ( " % d ▁ " , L [ i ] ) ; ; Driver Code
import java . util . * ; class GFG { static void findLongestPalindromicString ( String text ) { int N = text . length ( ) ; if ( N == 0 ) return ; N = 2 * N + 1 ; int [ ] L = new int [ N + 1 ] ; L [ 0 ] = 0 ; L [ 1 ] = 1 ; int C = 1 ; int R = 2 ; int i = 0 ; int iMirror ; int maxLPSLength = 0 ; int maxLPSCenterPosition = 0 ; int start = - 1 ; int end = - 1 ; int diff = - 1 ; for ( i = 2 ; i < N ; i ++ ) { iMirror = 2 * C - i ; L [ i ] = 0 ; diff = R - i ; if ( diff > 0 ) L [ i ] = Math . min ( L [ iMirror ] , diff ) ; while ( ( ( i + L [ i ] ) + 1 < N && ( i - L [ i ] ) > 0 ) && ( ( ( i + L [ i ] + 1 ) % 2 == 0 ) || ( text . charAt ( ( i + L [ i ] + 1 ) / 2 ) == text . charAt ( ( i - L [ i ] - 1 ) / 2 ) ) ) ) { L [ i ] ++ ; } if ( L [ i ] > maxLPSLength ) { maxLPSLength = L [ i ] ; maxLPSCenterPosition = i ; } if ( i + L [ i ] > R ) { C = i ; R = i + L [ i ] ; } } start = ( maxLPSCenterPosition - maxLPSLength ) / 2 ; end = start + maxLPSLength - 1 ; System . out . printf ( " LPS ▁ of ▁ string ▁ is ▁ % s ▁ : ▁ " , text ) ; for ( i = start ; i <= end ; i ++ ) System . out . print ( text . charAt ( i ) ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { String text = " babcbabcbaccba " ; findLongestPalindromicString ( text ) ; text = " abaaba " ; findLongestPalindromicString ( text ) ; text = " abababa " ; findLongestPalindromicString ( text ) ; text = " abcbabcbabcba " ; findLongestPalindromicString ( text ) ; text = " forgeeksskeegfor " ; findLongestPalindromicString ( text ) ; text = " caba " ; findLongestPalindromicString ( text ) ; text = " abacdfgdcaba " ; findLongestPalindromicString ( text ) ; text = " abacdfgdcabba " ; findLongestPalindromicString ( text ) ; text = " abacdedcaba " ; findLongestPalindromicString ( text ) ; } }
Print all possible strings that can be made by placing spaces | Java program to print permutations of a given string with spaces ; Function recursively prints the strings having space pattern i and j are indices in ' String ▁ str ' and ' buf [ ] ' respectively ; Either put the character ; Or put a space followed by next character ; Function creates buf [ ] to store individual output string and uses printPatternUtil ( ) to print all permutations ; Buffer to hold the string containing spaces 2 n - 1 characters and 1 string terminator ; Copy the first character as it is , since it will be always at first position ; Driver program
import java . io . * ; class Permutation { static void printPatternUtil ( String str , char buf [ ] , int i , int j , int n ) { if ( i == n ) { buf [ j ] = ' \0' ; System . out . println ( buf ) ; return ; } buf [ j ] = str . charAt ( i ) ; printPatternUtil ( str , buf , i + 1 , j + 1 , n ) ; buf [ j ] = ' ▁ ' ; buf [ j + 1 ] = str . charAt ( i ) ; printPatternUtil ( str , buf , i + 1 , j + 2 , n ) ; } static void printPattern ( String str ) { int len = str . length ( ) ; char [ ] buf = new char [ 2 * len ] ; buf [ 0 ] = str . charAt ( 0 ) ; printPatternUtil ( str , buf , 1 , 1 , len ) ; } public static void main ( String [ ] args ) { String str = " ABCD " ; printPattern ( str ) ; } }
Sudoku | Backtracking | Java program for above approach ; N is the size of the 2D matrix N * N ; A utility function to print grid ; Check whether it will be legal to assign num to the given row , col ; Check if we find the same num in the similar row , we return false ; Check if we find the same num in the similar column , we return false ; Check if we find the same num in the particular 3 * 3 matrix , we return false ; Takes a partially filled - in grid and attempts to assign values to all unassigned locations in such a way to meet the requirements for Sudoku solution ( non - duplication across rows , columns , and boxes ) ; if we have reached the 8 th row and 9 th column ( 0 indexed matrix ) , we are returning true to avoid further backtracking ; Check if column value becomes 9 , we move to next row and column start from 0 ; Check if the current position of the grid already contains value > 0 , we iterate for next column ; Check if it is safe to place the num ( 1 - 9 ) in the given row , col -> we move to next column ; assigning the num in the current ( row , col ) position of the grid and assuming our assined num in the position is correct ; Checking for next possibility with next column ; removing the assigned num , since our assumption was wrong , and we go for next assumption with diff num value ; Driver Code
public class Suduko { static int N = 9 ; static void print ( int [ ] [ ] grid ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( grid [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static boolean isSafe ( int [ ] [ ] grid , int row , int col , int num ) { for ( int x = 0 ; x <= 8 ; x ++ ) if ( grid [ row ] [ x ] == num ) return false ; for ( int x = 0 ; x <= 8 ; x ++ ) if ( grid [ x ] [ col ] == num ) return false ; int startRow = row - row % 3 , startCol = col - col % 3 ; for ( int i = 0 ; i < 3 ; i ++ ) for ( int j = 0 ; j < 3 ; j ++ ) if ( grid [ i + startRow ] [ j + startCol ] == num ) return false ; return true ; } static boolean solveSuduko ( int grid [ ] [ ] , int row , int col ) { if ( row == N - 1 && col == N ) return true ; if ( col == N ) { row ++ ; col = 0 ; } if ( grid [ row ] [ col ] != 0 ) return solveSuduko ( grid , row , col + 1 ) ; for ( int num = 1 ; num < 10 ; num ++ ) { if ( isSafe ( grid , row , col , num ) ) { grid [ row ] [ col ] = num ; if ( solveSuduko ( grid , row , col + 1 ) ) return true ; } grid [ row ] [ col ] = 0 ; } return false ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 3 , 0 , 6 , 5 , 0 , 8 , 4 , 0 , 0 } , { 5 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 8 , 7 , 0 , 0 , 0 , 0 , 3 , 1 } , { 0 , 0 , 3 , 0 , 1 , 0 , 0 , 8 , 0 } , { 9 , 0 , 0 , 8 , 6 , 3 , 0 , 0 , 5 } , { 0 , 5 , 0 , 0 , 9 , 0 , 6 , 0 , 0 } , { 1 , 3 , 0 , 0 , 0 , 0 , 2 , 5 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 4 } , { 0 , 0 , 5 , 2 , 0 , 6 , 3 , 0 , 0 } } ; if ( solveSuduko ( grid , 0 , 0 ) ) print ( grid ) ; else System . out . println ( " No ▁ Solution ▁ exists " ) ; } }
Median of two sorted arrays of same size | A Simple Merge based O ( n ) solution to find median of two sorted arrays ; function to calculate median ; Since there are 2 n elements , median will be average of elements at index n - 1 and n in the array obtained after merging ar1 and ar2 ; Below is to handle case where all elements of ar1 [ ] are smaller than smallest ( or first ) element of ar2 [ ] ; Below is to handle case where all elements of ar2 [ ] are smaller than smallest ( or first ) element of ar1 [ ] ; equals sign because if two arrays have some common elements ; Store the prev median ; Store the prev median ; Driver program to test above function
class Main { static int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) { int i = 0 ; int j = 0 ; int count ; int m1 = - 1 , m2 = - 1 ; for ( count = 0 ; count <= n ; count ++ ) { if ( i == n ) { m1 = m2 ; m2 = ar2 [ 0 ] ; break ; } else if ( j == n ) { m1 = m2 ; m2 = ar1 [ 0 ] ; break ; } if ( ar1 [ i ] <= ar2 [ j ] ) { m1 = m2 ; m2 = ar1 [ i ] ; i ++ ; } else { m1 = m2 ; m2 = ar2 [ j ] ; j ++ ; } } return ( m1 + m2 ) / 2 ; } public static void main ( String [ ] args ) { int ar1 [ ] = { 1 , 12 , 15 , 26 , 38 } ; int ar2 [ ] = { 2 , 13 , 17 , 30 , 45 } ; int n1 = ar1 . length ; int n2 = ar2 . length ; if ( n1 == n2 ) System . out . println ( " Median ▁ is ▁ " + getMedian ( ar1 , ar2 , n1 ) ) ; else System . out . println ( " arrays ▁ are ▁ of ▁ unequal ▁ size " ) ; } }
Median of two sorted arrays of same size | A Java program to divide and conquer based efficient solution to find median of two sorted arrays of same size . ; This function returns median of ar1 [ ] and ar2 [ ] . Assumptions in this function : Both ar1 [ ] and ar2 [ ] are sorted arrays Both have n elements ; get the median of the first array ; get the median of the second array ; If medians are equal then return either m1 or m2 ; if m1 < m2 then median must exist in ar1 [ m1 ... . ] and ar2 [ ... . m2 ] ; if m1 > m2 then median must exist in ar1 [ ... . m1 ] and ar2 [ m2 ... ] ; Function to get median of a sorted array ; Driver code
import java . util . * ; class GfG { static int getMedian ( int [ ] a , int [ ] b , int startA , int startB , int endA , int endB ) { if ( endA - startA == 1 ) { return ( Math . max ( a [ startA ] , b [ startB ] ) + Math . min ( a [ endA ] , b [ endB ] ) ) / 2 ; } int m1 = median ( a , startA , endA ) ; int m2 = median ( b , startB , endB ) ; if ( m1 == m2 ) { return m1 ; } else if ( m1 < m2 ) { return getMedian ( a , b , ( endA + startA + 1 ) / 2 , startB , endA , ( endB + startB + 1 ) / 2 ) ; } else { return getMedian ( a , b , startA , ( endB + startB + 1 ) / 2 , ( endA + startA + 1 ) / 2 , endB ) ; } } static int median ( int [ ] arr , int start , int end ) { int n = end - start + 1 ; if ( n % 2 == 0 ) { return ( arr [ start + ( n / 2 ) ] + arr [ start + ( n / 2 - 1 ) ] ) / 2 ; } else { return arr [ start + n / 2 ] ; } } public static void main ( String [ ] args ) { int ar1 [ ] = { 1 , 2 , 3 , 6 } ; int ar2 [ ] = { 4 , 6 , 8 , 10 } ; int n1 = ar1 . length ; int n2 = ar2 . length ; if ( n1 != n2 ) { System . out . println ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ " + " of ▁ unequal ▁ size " ) ; } else if ( n1 == 0 ) { System . out . println ( " Arrays ▁ are ▁ empty . " ) ; } else if ( n1 == 1 ) { System . out . println ( ( ar1 [ 0 ] + ar2 [ 0 ] ) / 2 ) ; } else { System . out . println ( " Median ▁ is ▁ " + getMedian ( ar1 , ar2 , 0 , 0 , ar1 . length - 1 , ar2 . length - 1 ) ) ; } } }
Median of two sorted arrays of same size |  ; This function returns median of ar1 [ ] and ar2 [ ] . Assumptions in this function : Both ar1 [ ] and ar2 [ ] are sorted arrays Both have n elements ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) { int j = 0 ; int i = n - 1 ; while ( ar1 [ i ] > ar2 [ j ] && j < n && i > - 1 ) { int temp = ar1 [ i ] ; ar1 [ i ] = ar2 [ j ] ; ar2 [ j ] = temp ; i -- ; j ++ ; } Arrays . sort ( ar1 ) ; Arrays . sort ( ar2 ) ; return ( ar1 [ n - 1 ] + ar2 [ 0 ] ) / 2 ; } public static void main ( String [ ] args ) { int ar1 [ ] = { 1 , 12 , 15 , 26 , 38 } ; int ar2 [ ] = { 2 , 13 , 17 , 30 , 45 } ; int n1 = 5 ; int n2 = 5 ; if ( n1 == n2 ) System . out . println ( " Median ▁ is ▁ " + getMedian ( ar1 , ar2 , n1 ) ) ; else System . out . println ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ of ▁ unequal ▁ size " ) ; } }
How to check if two given line segments intersect ? | Java program to check if two given line segments intersect ; Given three colinear points p , q , r , the function checks if point q lies on line segment ' pr ' ; To find orientation of ordered triplet ( p , q , r ) . The function returns following values 0 -- > p , q and r are colinear 1 -- > Clockwise 2 -- > Counterclockwise ; www . geeksforgeeks . org / orientation - 3 - ordered - points / See https : for details of below formula . ; colinear ; clock or counterclock wise ; The main function that returns true if line segment ' p1q1' and ' p2q2' intersect . ; Find the four orientations needed for general and special cases ; General case ; Special Cases p1 , q1 and p2 are colinear and p2 lies on segment p1q1 ; p1 , q1 and q2 are colinear and q2 lies on segment p1q1 ; p2 , q2 and p1 are colinear and p1 lies on segment p2q2 ; p2 , q2 and q1 are colinear and q1 lies on segment p2q2 ; Doesn 't fall in any of the above cases ; Driver code
class GFG { static class Point { int x ; int y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static boolean onSegment ( Point p , Point q , Point r ) { if ( q . x <= Math . max ( p . x , r . x ) && q . x >= Math . min ( p . x , r . x ) && q . y <= Math . max ( p . y , r . y ) && q . y >= Math . min ( p . y , r . y ) ) return true ; return false ; } static int orientation ( Point p , Point q , Point r ) { int val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } static boolean doIntersect ( Point p1 , Point q1 , Point p2 , Point q2 ) { int o1 = orientation ( p1 , q1 , p2 ) ; int o2 = orientation ( p1 , q1 , q2 ) ; int o3 = orientation ( p2 , q2 , p1 ) ; int o4 = orientation ( p2 , q2 , q1 ) ; if ( o1 != o2 && o3 != o4 ) return true ; if ( o1 == 0 && onSegment ( p1 , p2 , q1 ) ) return true ; if ( o2 == 0 && onSegment ( p1 , q2 , q1 ) ) return true ; if ( o3 == 0 && onSegment ( p2 , p1 , q2 ) ) return true ; if ( o4 == 0 && onSegment ( p2 , q1 , q2 ) ) return true ; return false ; } public static void main ( String [ ] args ) { Point p1 = new Point ( 1 , 1 ) ; Point q1 = new Point ( 10 , 1 ) ; Point p2 = new Point ( 1 , 2 ) ; Point q2 = new Point ( 10 , 2 ) ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; p1 = new Point ( 10 , 1 ) ; q1 = new Point ( 0 , 10 ) ; p2 = new Point ( 0 , 0 ) ; q2 = new Point ( 10 , 10 ) ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; p1 = new Point ( - 5 , - 5 ) ; q1 = new Point ( 0 , 0 ) ; p2 = new Point ( 1 , 1 ) ; q2 = new Point ( 10 , 10 ) ; ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check whether a given point lies inside a triangle or not | JAVA Code for Check whether a given point lies inside a triangle or not ; A utility function to calculate area of triangle formed by ( x1 , y1 ) ( x2 , y2 ) and ( x3 , y3 ) ; A function to check whether point P ( x , y ) lies inside the triangle formed by A ( x1 , y1 ) , B ( x2 , y2 ) and C ( x3 , y3 ) ; Calculate area of triangle ABC ; Calculate area of triangle PBC ; Calculate area of triangle PAC ; Calculate area of triangle PAB ; Check if sum of A1 , A2 and A3 is same as A ; Driver program to test above function ; Let us check whether the point P ( 10 , 15 ) lies inside the triangle formed by A ( 0 , 0 ) , B ( 20 , 0 ) and C ( 10 , 30 )
import java . util . * ; class GFG { static double area ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return Math . abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) ; } static boolean isInside ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x , int y ) { double A = area ( x1 , y1 , x2 , y2 , x3 , y3 ) ; double A1 = area ( x , y , x2 , y2 , x3 , y3 ) ; double A2 = area ( x1 , y1 , x , y , x3 , y3 ) ; double A3 = area ( x1 , y1 , x2 , y2 , x , y ) ; return ( A == A1 + A2 + A3 ) ; } public static void main ( String [ ] args ) { if ( isInside ( 0 , 0 , 20 , 0 , 10 , 30 , 10 , 15 ) ) System . out . println ( " Inside " ) ; else System . out . println ( " Not ▁ Inside " ) ; } }
Lucky Numbers | Java program to check Lucky Number ; Returns 1 if n is a lucky no . ohterwise returns 0 ; variable next_position is just for readability of the program we can remove it and use n only ; calculate next position of input no ; driver program
import java . io . * ; class GFG { public static int counter = 2 ; static boolean isLucky ( int n ) { int next_position = n ; if ( counter > n ) return true ; if ( n % counter == 0 ) return false ; next_position -= next_position / counter ; counter ++ ; return isLucky ( next_position ) ; } public static void main ( String [ ] args ) { int x = 5 ; if ( isLucky ( x ) ) System . out . println ( x + " ▁ is ▁ a ▁ lucky ▁ no . " ) ; else System . out . println ( x + " ▁ is ▁ not ▁ a ▁ lucky ▁ no . " ) ; } }
Write you own Power without using multiplication ( * ) and division ( / ) operators |  ; Works only if a >= 0 and b >= 0 ; driver program to test above function
import java . io . * ; class GFG { static int pow ( int a , int b ) { if ( b == 0 ) return 1 ; int answer = a ; int increment = a ; int i , j ; for ( i = 1 ; i < b ; i ++ ) { for ( j = 1 ; j < a ; j ++ ) { answer += increment ; } increment = answer ; } return answer ; } public static void main ( String [ ] args ) { System . out . println ( pow ( 5 , 3 ) ) ; } }
Write you own Power without using multiplication ( * ) and division ( / ) operators |  ; A recursive function to get x * y ; A recursive function to get a ^ b Works only if a >= 0 and b >= 0 ; driver program to test above functions
import java . io . * ; class GFG { static int multiply ( int x , int y ) { if ( y > 0 ) return ( x + multiply ( x , y - 1 ) ) ; else return 0 ; } static int pow ( int a , int b ) { if ( b > 0 ) return multiply ( a , pow ( a , b - 1 ) ) ; else return 1 ; } public static void main ( String [ ] args ) { System . out . println ( pow ( 5 , 3 ) ) ; } }
Count numbers that don 't contain 3 | Java program to count numbers that not contain 3 ; Function that returns count of numbers which are in range from 1 to n and not contain 3 as a digit ; Base cases ( Assuming n is not negative ) ; Calculate 10 ^ ( d - 1 ) ( 10 raise to the power d - 1 ) where d is number of digits in n . po will be 100 for n = 578 ; find the most significant digit ( msd is 5 for 578 ) ; For 578 , total will be 4 * count ( 10 ^ 2 - 1 ) + 4 + count ( 78 ) ; For 35 , total will be equal to count ( 29 ) ; Driver program
import java . io . * ; class GFG { static int count ( int n ) { if ( n < 3 ) return n ; if ( n >= 3 && n < 10 ) return n - 1 ; int po = 1 ; while ( n / po > 9 ) po = po * 10 ; int msd = n / po ; if ( msd != 3 ) return count ( msd ) * count ( po - 1 ) + count ( msd ) + count ( n % po ) ; else return count ( msd * po - 1 ) ; } public static void main ( String [ ] args ) { int n = 578 ; System . out . println ( count ( n ) ) ; } }
Number which has the maximum number of distinct prime factors in the range M to N | Java program to print the Number which has the maximum number of distinct prime factors of numbers in range m to n ; Function to return the maximum number ; array to store the number of distinct primes ; true if index ' i ' is a prime ; initializing the number of factors to 0 and ; Used in Sieve ; condition works only when ' i ' is prime , hence for factors of all prime number , the prime status is changed to false ; Number is prime ; number of factor of a prime number is 1 ; incrementing factorCount all the factors of i ; and changing prime status to false ; Initialize the max and num ; Gets the maximum number ; Gets the maximum number ; Driver code ; Calling function
import java . io . * ; class GFG { static int maximumNumberDistinctPrimeRange ( int m , int n ) { long factorCount [ ] = new long [ n + 1 ] ; boolean prime [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { factorCount [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] == true ) { factorCount [ i ] = 1 ; for ( int j = i * 2 ; j <= n ; j += i ) { factorCount [ j ] ++ ; prime [ j ] = false ; } } } int max = ( int ) factorCount [ m ] ; int num = m ; for ( int i = m ; i <= n ; i ++ ) { if ( factorCount [ i ] > max ) { max = ( int ) factorCount [ i ] ; num = i ; } } return num ; } public static void main ( String [ ] args ) { int m = 4 , n = 6 ; System . out . println ( maximumNumberDistinctPrimeRange ( m , n ) ) ; } }