codeparrot/xlcost-text-to-code huggingface dataset:
Check if frequency of each digit is less than the digit | Java program to validate a number ; Function to validate number ( Check iffrequency of a digit is less than thedigit itself or not ) ; If current digit of temp is same as i ; if frequency is greater than digit value , return false ; Driver Code
import java . io . * ; public class GFG { static boolean validate ( long n ) { for ( int i = 0 ; i < 10 ; i ++ ) { long temp = n ; int count = 0 ; while ( temp > 0 ) { if ( temp % 10 == i ) count ++ ; if ( count > i ) return false ; temp /= 10 ; } } return true ; } static public void main ( String [ ] args ) { long n = 1552793 ; if ( validate ( n ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
Check if a larger number divisible by 36 | Java program to find if a number is divisible by 36 or not ; Function to check whether a number is divisible by 36 or not ; null number cannot be divisible by 36 ; single digit number other than 0 is not divisible by 36 ; number formed by the last 2 digits ; if number is not divisible by 4 ; number is divisible by 4 calculate sum of digits ; sum of digits is not divisible by 9 ; number is divisible by 4 and 9 hence , number is divisible by 36 ; main function
class IsDivisible { static boolean divisibleBy36 ( String num ) { int l = num . length ( ) ; if ( l == 0 ) return false ; if ( l == 1 && num . charAt ( 0 ) != '0' ) return false ; int two_digit_num = ( num . charAt ( l - 2 ) - '0' ) * 10 + ( num . charAt ( l - 1 ) - '0' ) ; if ( two_digit_num % 4 != 0 ) return false ; int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) sum += ( num . charAt ( i ) - '0' ) ; if ( sum % 9 != 0 ) return false ; return true ; } public static void main ( String [ ] args ) { String num = "92567812197966231384" ; if ( divisibleBy36 ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if a large number is divisible by 8 or not | Java program to find if a number is divisible by 8 or not ; Function to find that number divisible by 8 or not ; Empty string ; If there is single digit ; If there is double digit ; If number formed by last three digits is divisible by 8. ; main function
class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str . charAt ( 0 ) - '0' ) % 8 == 0 ) ; if ( n == 2 ) return ( ( ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 8 == 0 ) ; int last = str . charAt ( n - 1 ) - '0' ; int second_last = str . charAt ( n - 2 ) - '0' ; int third_last = str . charAt ( n - 3 ) - '0' ; return ( ( third_last * 100 + second_last * 10 + last ) % 8 == 0 ) ; } public static void main ( String [ ] args ) { String str = "76952" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Prime points ( Points that split a number into two primes ) | Java program to print all prime points ; Function to count number of digits ; Function to check whether a number is prime or not . Returns 0 if prime else - 1 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to print prime points ; counting digits ; As single and double digit numbers do not have left and right number pairs ; Finding all left and right pairs . Printing the prime points accordingly . Discarding first and last index point ; Calculating left number ; Calculating right number ; Prime point condition ; No prime point found ; Driver Code
import java . io . * ; class GFG { static int countDigits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n / 10 ; } return count ; } static int checkPrime ( int n ) { if ( n <= 1 ) return - 1 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 n % 3 == 0 ) return - 1 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return - 1 ; return 0 ; } static void printPrimePoints ( int n ) { int count = countDigits ( n ) ; if ( count == 1 count == 2 ) { System . out . print ( " - 1" ) ; return ; } boolean found = false ; for ( int i = 1 ; i < ( count - 1 ) ; i ++ ) { int left = n / ( ( int ) Math . pow ( 10 , count - i ) ) ; int right = n % ( ( int ) Math . pow ( 10 , count - i - 1 ) ) ; if ( checkPrime ( left ) == 0 && checkPrime ( right ) == 0 ) { System . out . print ( i + " ▁ " ) ; found = true ; } } if ( found == false ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 2317 ; printPrimePoints ( n ) ; } }
Find ways an Integer can be expressed as sum of n | Java program to count number of ways any given integer x can be expressed as n - th power of unique natural numbers . ; Function to calculate and return the power of any given number ; Function to check power representations recursively ; Initialize number of ways to express x as n - th powers of different natural numbers ; Calling power of ' i ' raised to ' n ' ; Recursively check all greater values of i ; If sum of powers is equal to x then increase the value of result . ; Return the final result ; Driver Code .
class GFG { static int power ( int num , int n ) { if ( n == 0 ) return 1 ; else if ( n % 2 == 0 ) return power ( num , n / 2 ) * power ( num , n / 2 ) ; else return num * power ( num , n / 2 ) * power ( num , n / 2 ) ; } static int checkRecursive ( int x , int n , int curr_num , int curr_sum ) { int results = 0 ; int p = power ( curr_num , n ) ; while ( p + curr_sum < x ) { results += checkRecursive ( x , n , curr_num + 1 , p + curr_sum ) ; curr_num ++ ; p = power ( curr_num , n ) ; } if ( p + curr_sum == x ) results ++ ; return results ; } public static void main ( String [ ] args ) { int x = 10 , n = 2 ; System . out . println ( checkRecursive ( x , n , 1 , 0 ) ) ; } }
Number Theory | Generators of finite cyclic group under addition | A simple Java program to find all generators ; Function to return gcd of a and b ; Print generators of n ; 1 is always a generator ; A number x is generator of GCD is 1 ; Driver program to test above function
class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void printGenerators ( int n ) { System . out . println ( "1 ▁ " ) ; for ( int i = 2 ; i < n ; i ++ ) if ( gcd ( i , n ) == 1 ) System . out . println ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 ; printGenerators ( n ) ; } }
Check if a large number is divisible by 3 or not | Java program to find if a number is divisible by 3 or not ; Function to find that number divisible by 3 or not ; Compute sum of digits ; Check if sum of digits is divisible by 3. ; main function
class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - '0' ) ; return ( digitSum % 3 == 0 ) ; } public static void main ( String [ ] args ) { String str = "1332" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count all perfect divisors of a number | Java code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code
class GFG { static int MAX = 100001 ; static int [ ] perfectDiv = new int [ MAX ] ; static void precomputeCounts ( ) { for ( int i = 1 ; i * i < MAX ; ++ i ) { for ( int j = i * i ; j < MAX ; j += i * i ) ++ perfectDiv [ j ] ; } } static int countPerfectDivisors ( int n ) { return perfectDiv [ n ] ; } public static void main ( String [ ] args ) { precomputeCounts ( ) ; int n = 16 ; System . out . println ( " Total ▁ perfect ▁ divisors ▁ of ▁ " + n + " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; n = 12 ; System . out . println ( " Total ▁ perfect ▁ divisors ▁ of ▁ " + n + " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; } }
Prime Factorization using Sieve O ( log n ) for multiple queries | Java program to find prime factorization of a number n in O ( Log n ) time with precomputation allowed . ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; Driver method ; precalculating Smallest Prime Factor ; calling getFactorization function
import java . util . Vector ; class Test { static final int MAXN = 100001 ; static int spf [ ] = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static Vector < Integer > getFactorization ( int x ) { Vector < Integer > ret = new Vector < > ( ) ; while ( x != 1 ) { ret . add ( spf [ x ] ) ; x = x / spf [ x ] ; } return ret ; } public static void main ( String args [ ] ) { sieve ( ) ; int x = 12246 ; System . out . print ( " prime ▁ factorization ▁ for ▁ " + x + " ▁ : ▁ " ) ; Vector < Integer > p = getFactorization ( x ) ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) System . out . print ( p . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } }
Nearest element with at | Java program to print nearest element with at least one common prime factor . ; Pre - computation of smallest prime divisor of all numbers ; Prime number will have same divisor ; Function to calculate all divisors of input array ; Pre - compute all the divisors of array element by using prime factors ; Traverse all elements , ; For every divisor of current element , find closest element . ; Visit divisor if not visited ; Fetch the index of visited divisor ; Update the divisor index to current index ; Set the minimum distance ; Set the min distance of current index ' i ' to nearest one ; Add 1 as indexing starts from 0 ; Set the min distance of found index ' ind ' ; Add 1 as indexing starts from 0 ; Driver code ; Simple sieve to find smallest prime divisor of number from 2 to MAX ; function to calculate nearest distance of every array elements ; Print the nearest distance having GDC > 1
import java . io . * ; import java . util . * ; class GFG { static int MAX = 100001 ; static int INF = Integer . MAX_VALUE ; static int [ ] primedivisor = new int [ MAX ] ; static int [ ] dist = new int [ MAX ] ; static int [ ] pos = new int [ MAX ] ; static int [ ] divInd = new int [ MAX ] ; static ArrayList < ArrayList < Integer > > divisors = new ArrayList < ArrayList < Integer > > ( ) ; static void sieveOfEratosthenes ( ) { for ( int i = 2 ; i * i < MAX ; ++ i ) { if ( primedivisor [ i ] == 0 ) { for ( int j = i * i ; j < MAX ; j += i ) { primedivisor [ j ] = i ; } } } for ( int i = 1 ; i < MAX ; ++ i ) { if ( primedivisor [ i ] == 0 ) { primedivisor [ i ] = i ; } } } static void findDivisors ( int arr [ ] , int n ) { for ( int i = 0 ; i < MAX ; ++ i ) { pos [ i ] = divInd [ i ] = - 1 ; dist [ i ] = INF ; } for ( int i = 0 ; i < n ; ++ i ) { int num = arr [ i ] ; while ( num > 1 ) { int div = primedivisor [ num ] ; divisors . get ( i ) . add ( div ) ; while ( num % div == 0 ) { num /= div ; } } } } static void nearestGCD ( int arr [ ] , int n ) { findDivisors ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { for ( int div : divisors . get ( i ) ) { if ( divInd [ div ] == - 1 ) { divInd [ div ] = i ; } else { int ind = divInd [ div ] ; divInd [ div ] = i ; if ( dist [ i ] > Math . abs ( ind - i ) ) { dist [ i ] = Math . abs ( ind - i ) ; pos [ i ] = ind + 1 ; } if ( dist [ ind ] > Math . abs ( ind - i ) ) { dist [ ind ] = Math . abs ( ind - i ) ; pos [ ind ] = i + 1 ; } } } } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < MAX ; i ++ ) { divisors . add ( new ArrayList < Integer > ( ) ) ; } sieveOfEratosthenes ( ) ; int arr [ ] = { 2 , 9 , 4 , 3 , 13 } ; int n = arr . length ; nearestGCD ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( pos [ i ] + " ▁ " ) ; } } }
Largest subsequence having GCD greater than 1 | Efficient Java program to find length of the largest subsequence with GCD greater than 1. ; Returns length of the largest subsequence with GCD more than 1. ; Finding the Maximum value in arr [ ] ; Iterate from 2 to maximum possible divisor of all give values ; If we found divisor , increment count ; Driver program to test above
import java . util . Arrays ; class GFG { static int largestGCDSubsequence ( int arr [ ] , int n ) { int ans = 0 ; int maxele = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; ; for ( int i = 2 ; i <= maxele ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( arr [ j ] % i == 0 ) ++ count ; } ans = Math . max ( ans , count ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 2 , 5 , 4 } ; int size = arr . length ; System . out . println ( largestGCDSubsequence ( arr , size ) ) ; } }
Minimum step to reach one | Java program to get minimum step to reach 1 under given constraints ; structure represent one node in queue ; method returns minimum step to reach one ; set is used to visit numbers so that they won 't be pushed in queue again ; loop until we reach to 1 ; if current data value is 1 , return its steps from N ; check curr - 1 , only if it not visited yet ; loop from 2 to Math . sqrt ( value ) for its divisors ; check divisor , only if it is not visited yet if i is divisor of val , then val / i will be its bigger divisor ; Driver code
import java . util . * ; class GFG { static class data { int val ; int steps ; public data ( int val , int steps ) { this . val = val ; this . steps = steps ; } } ; static int minStepToReachOne ( int N ) { Queue < data > q = new LinkedList < > ( ) ; q . add ( new data ( N , 0 ) ) ; HashSet < Integer > st = new HashSet < Integer > ( ) ; while ( ! q . isEmpty ( ) ) { data t = q . peek ( ) ; q . remove ( ) ; if ( t . val == 1 ) return t . steps ; if ( ! st . contains ( t . val - 1 ) ) { q . add ( new data ( t . val - 1 , t . steps + 1 ) ) ; st . add ( t . val - 1 ) ; } for ( int i = 2 ; i * i <= t . val ; i ++ ) { if ( t . val % i == 0 && ! st . contains ( t . val / i ) ) { q . add ( new data ( t . val / i , t . steps + 1 ) ) ; st . add ( t . val / i ) ; } } } return - 1 ; } public static void main ( String [ ] args ) { int N = 17 ; System . out . print ( minStepToReachOne ( N ) + "NEW_LINE"); } }
Queries on the sum of prime factor counts in a range | Java program to find sum prime factors in given range . ; using sieve method to evaluating the prime factor of numbers ; if i is prime ; setting number of prime factor of a prime number . ; Returns sum of counts of prime factors in range from l to r . This function mainly uses count [ ] which is filled by Sieve ( ) ; finding the sum of number of prime factor of numbers in a range . ; Driver code
class GFG { static final int MAX = 1000006 ; static void sieve ( int count [ ] ) { for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( count [ i ] == 0 ) { for ( int j = 2 * i ; j < MAX ; j += i ) count [ j ] ++ ; count [ i ] = 1 ; } } } static int query ( int count [ ] , int l , int r ) { int sum = 0 ; for ( int i = l ; i <= r ; i ++ ) sum += count [ i ] ; return sum ; } public static void main ( String [ ] args ) { int count [ ] = new int [ MAX ] ; sieve ( count ) ; System . out . println ( query ( count , 6 , 10 ) + " ▁ " + query ( count , 1 , 5 ) ) ; } }
Generation of n numbers with given set of factors | Java program to generate n numbers with given factors ; Generate n numbers with factors in factor [ ] ; array of k to store next multiples of given factors ; Prints n numbers int output = 0 ; Next number to print as output ; Find the next smallest multiple ; Printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; incrementing the current value by the respective factor ; Driver code
import java . io . * ; class GFG { static void generateNumbers ( int factor [ ] , int n , int k ) { int next [ ] = new int [ k ] ; for ( int i = 0 ; i < n ; ) { int toincrement = 0 ; for ( int j = 0 ; j < k ; j ++ ) if ( next [ j ] < next [ toincrement ] ) toincrement = j ; if ( output != next [ toincrement ] ) { output = next [ toincrement ] ; System . out . print ( next [ toincrement ] + " ▁ " ) ; i ++ ; } next [ toincrement ] += factor [ toincrement ] ; } } public static void main ( String [ ] args ) { int factor [ ] = { 3 , 5 , 7 } ; int n = 10 ; int k = factor . length ; generateNumbers ( factor , n , k ) ; } }
Reversible numbers | Java program to check if a given number is reversible or not ; Function to check reversible number ; Calculate reverse of n ; Calculate sum of number and its reverse ; Check for reverse number reach digit must be odd ; Driver function
import java . io . * ; class GFG { static void checkReversible ( int n ) { int rev = 0 , rem = 0 ; int flag = n ; while ( flag > 0 ) { rem = flag % 10 ; rev *= 10 ; rev += rem ; flag /= 10 ; } int sum = rev + n ; while ( sum > 0 && ( rem % 2 != 0 ) ) { rem = sum % 10 ; sum /= 10 ; } if ( sum == 0 ) System . out . println ( " Reversible ▁ Number " ) ; else System . out . println ( " Non - Reversible ▁ Number " ) ; } public static void main ( String [ ] args ) { int n = 36 ; checkReversible ( n ) ; } }
Reversible numbers | Java program to find the count of reversible numbers upto a given number n ; Function to calculate the count of reversible number ; Calculate counts of reversible number of 1 to n - digits ; All four possible cases and their formula ; for i of form 2 k ; for i of form 4 k + 3 ; for i of form 4 k + 1 no solution ; Driver function ; count up - to 9 digit numbers ( 1 billion )
import java . io . * ; class GFG { static void countReversible ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { switch ( i % 4 ) { case 0 : case 2 : count += 20 * Math . pow ( 30 , ( i / 2 - 1 ) ) ; break ; case 3 : count += 100 * Math . pow ( 500 , i / 4 ) ; break ; case 1 : break ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int n = 9 ; countReversible ( n ) ; } }
Multiplicative order | Java program to implement multiplicative order ; function for GCD ; Function return smallest + ve integer that holds condition A ^ k ( mod N ) = 1 ; result store power of A that rised to the power N - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; driver program to test above function
import java . io . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int multiplicativeOrder ( int A , int N ) { if ( GCD ( A , N ) != 1 ) return - 1 ; int result = 1 ; int K = 1 ; while ( K < N ) { result = ( result * A ) % N ; if ( result == 1 ) return K ; K ++ ; } return - 1 ; } public static void main ( String args [ ] ) { int A = 4 , N = 7 ; System . out . println ( multiplicativeOrder ( A , N ) ) ; } }
Sum of product of x and y such that floor ( n / x ) = y | Java program to find sum of product of x and y such that n / x = y ( Integer Division ) ; Return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driver Code
import java . io . * ; class GFG { static int sumOfRange ( int a , int b ) { int i = ( a * ( a + 1 ) ) >> 1 ; int j = ( b * ( b + 1 ) ) >> 1 ; return ( i - j ) ; } static int sumofproduct ( int n ) { int sum = 0 ; int root = ( int ) Math . sqrt ( n ) ; for ( int i = 1 ; i <= root ; i ++ ) { int up = n / i ; int low = Math . max ( n / ( i + 1 ) , root ) ; sum += ( i * sumOfRange ( up , low ) ) ; sum += ( i * ( n / i ) ) ; } return sum ; } static public void main ( String [ ] args ) { int n = 10 ; System . out . println ( sumofproduct ( n ) ) ; } }
Primitive root of a prime number n modulo n | Java program to find primitive root of a given number n ; Returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Iterative Function to calculate ( x ^ n ) % p in O ( logy ) ; x = x % p ; 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 ; Utility function to store prime factors of a number ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to find smallest primitive root of n ; Check if n is prime or not ; Find value of Euler Totient function of n Since n is a prime number , the value of Euler Totient function is n - 1 as there are n - 1 relatively prime numbers . ; Find prime factors of phi and store in a set ; Check for every number from 2 to phi ; Iterate through all prime factors of phi . and check if we found a power with value 1 ; Check if r ^ ( ( phi ) / primefactors ) mod n is 1 or not ; If there was no power with value 1. ; If no primitive root found ; Driver code
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 int power ( int x , int y , int p ) { while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } static void findPrimefactors ( HashSet < Integer > s , int n ) { while ( n % 2 == 0 ) { s . add ( 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { s . add ( i ) ; n = n / i ; } } if ( n > 2 ) { s . add ( n ) ; } } static int findPrimitive ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; if ( isPrime ( n ) == false ) { return - 1 ; } int phi = n - 1 ; findPrimefactors ( s , phi ) ; for ( int r = 2 ; r <= phi ; r ++ ) { boolean flag = false ; for ( Integer a : s ) { if ( power ( r , phi / ( a ) , n ) == 1 ) { flag = true ; break ; } } if ( flag == false ) { return r ; } } return - 1 ; } public static void main ( String [ ] args ) { int n = 761 ; System . out . println ( " ▁ Smallest ▁ primitive ▁ root ▁ of ▁ " + n + " ▁ is ▁ " + findPrimitive ( n ) ) ; } }
Minimum number of power terms with sum equal to n | Java program to calculate minimum numberof powers of x to make sum equal to n . ; Return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; Consider n = a * x + b where a = n / x and b = n % x . ; Update count of powers for 1 's added ; Repeat the process for reduced n ; Driver code
class GFG { static int minPower ( int n , int x ) { if ( x == 1 ) return n ; int ans = 0 ; while ( n > 0 ) { ans += ( n % x ) ; n /= x ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 , x = 3 ; System . out . println ( minPower ( n , x ) ) ; } }
Sum of Perrin Numbers | Java program to calculate sum of Perrin Numbers ; function for sum of first n Perrin number . ; if ( n == 0 ) n = 0 ; if ( n == 1 ) n = 1 ; if ( n == 2 ) n = 2 ; calculate k = 5 sum of three previous step . ; Sum remaining numbers ; calculate next term ; Driver code
import java . lang . * ; class GFG { static int calSum ( int n ) { int a = 3 , b = 0 , c = 2 ; return 3 ; return 3 ; return 5 ; int sum = 5 ; while ( n > 2 ) { int d = a + b ; sum += d ; a = b ; b = c ; c = d ; n -- ; } return sum ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . print ( calSum ( n ) ) ; } }