codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Factors of a Number using Prime Factorization | Using SieveOfEratosthenes to find smallest prime factor of all the numbers . For example , if N is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7 ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries in it as false . ; Initializing smallest factor equal to 2 for all the even numbers ; For odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " . ; Function to find sum of all prime factors ; Declaring array to store smallest prime factor of i at i - th index ; Filling values in s [ ] using sieve ; Current prime factor of N ; Power of current prime factor ; N is now N / s [ N ] . If new N als has smallest prime factor as currFactor , increment power ; Update current prime factor as s [ N ] and initializing power of factor as 1. ; Driver code
function sieveOfEratosthenes ( N , s ) { let prime = new Array ( N + 1 ) ; for ( let i = 0 ; i < N + 1 ; i ++ ) prime [ i ] = false ; for ( let i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( let i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( let j = i ; j * i <= N ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } function findSum ( N ) { let s = new Array ( N + 1 ) ; let ans = 1 ; sieveOfEratosthenes ( N , s ) ; let currFactor = s [ N ] ; let power = 1 ; while ( N > 1 ) { N = Math . floor ( N / s [ N ] ) ; if ( currFactor == s [ N ] ) { power ++ ; continue ; } let sum = 0 ; for ( let i = 0 ; i <= power ; i ++ ) sum += Math . pow ( currFactor , i ) ; ans *= sum ; currFactor = s [ N ] ; power = 1 ; } return ans ; } let n = 12 ; document . write ( " " ) ; document . write ( findSum ( n ) ) ;
Find Multiples of 2 or 3 or 5 less than or equal to N | Bit count function ; Function to count number of multiples of 2 or 3 or 5 less than or equal to N ; As we have to check divisibility by three numbers , So we can implement bit masking ; we check whether jth bit is set or not , if jth bit is set , simply multiply to prod ; check for set bit ; check multiple of product ; Driver code
function count_setbits ( N ) { var cnt = 0 ; while ( N > 0 ) { cnt += ( N & 1 ) ; N = N >> 1 ; } return cnt ; } function countMultiples ( n ) { var multiple = [ 2 , 3 , 5 ] ; var count = 0 , mask = parseInt ( Math . pow ( 2 , 3 ) ) ; for ( i = 1 ; i < mask ; i ++ ) { var prod = 1 ; for ( j = 0 ; j < 3 ; j ++ ) { if ( ( i & 1 << j ) > 0 ) prod = prod * multiple [ j ] ; } if ( count_setbits ( i ) % 2 == 1 ) count = count + parseInt ( n / prod ) ; else count = count - parseInt ( n / prod ) ; } return count ; } var n = 10 ; document . write ( countMultiples ( n ) ) ;
Minimum value of N such that xor from 1 to N is equal to K | Function to find the value of N ; variable to store the result ; handling case for '0' ; handling case for '1' ; when number is completely divided by 4 then minimum ' x ' will be ' k ' ; when number divided by 4 gives 3 as remainder then minimum ' x ' will be ' k - 1' ; else it is not possible to get k for any value of x ; let the given number be 7
function findN ( k ) { var ans ; if ( k == 0 ) ans = 3 ; if ( k == 1 ) ans = 1 ; else if ( k % 4 == 0 ) ans = k ; else if ( k % 4 == 3 ) ans = k - 1 ; else ans = - 1 ; return ans ; } var k = 7 ; var res = findN ( k ) ; if ( res == - 1 ) document . write ( " " ) ; else document . write ( res ) ;
Permutations to arrange N persons around a circular table | Function to find no . of permutations ; Driver Code
function Circular ( n ) { var Result = 1 ; while ( n > 0 ) { Result = Result * n ; n -- ; } return Result ; } var n = 4 ; document . write ( Circular ( n - 1 ) ) ;
Minimum time required to complete a work by N persons together | Function to calculate the time ; Driver Code
function calTime ( arr , n ) { var work = 0 ; for ( i = 0 ; i < n ; i ++ ) work += 1 / arr [ i ] ; return 1 / work ; } var arr = [ 6.0 , 3.0 , 4.0 ] ; var n = arr . length ; document . write ( calTime ( arr , n ) . toFixed ( 5 ) + " " ) ;
Find the largest twins in given range | Function to find twins ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the largest twin in range ; Driver code
function printTwins ( low , high ) { let prime = new Array ( high + 1 ) ; let twin = false ; for ( let i = 0 ; i < high + 1 ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( let p = 2 ; p <= Math . floor ( Math . sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= high ; i += p ) prime [ i ] = false ; } } for ( let i = high ; i >= low ; i -- ) { if ( prime [ i ] && ( i - 2 >= low && prime [ i - 2 ] == true ) ) { document . write ( " " + ( i - 2 ) + " " + ( i ) + " " + " " ) ; twin = true ; break ; } } if ( twin == false ) document . write ( " " ) ; } printTwins ( 10 , 100 ) ;
Complement of a number with any base b | Function to find ( b - 1 ) 's complement ; Calculate number of digits in the given number ; Largest digit in the number system with base b ; Largest number in the number system with base b ; return Complement ; Function to find b 's complement ; b ' s ▁ complement ▁ = ▁ ( b - 1 ) ' s complement + 1 ; Driver code
function prevComplement ( n , b ) { var maxDigit , maxNum = 0 , digits = 0 , num = n ; while ( n != 0 ) { digits ++ ; n = parseInt ( n / 10 ) ; } maxDigit = b - 1 ; while ( ( digits -- ) > 0 ) { maxNum = maxNum * 10 + maxDigit ; } return maxNum - num ; } function complement ( n , b ) { return prevComplement ( n , b ) + 1 ; } document . write ( prevComplement ( 25 , 7 ) + " " ) ; document . write ( complement ( 25 , 7 ) ) ;
Minimum positive integer value possible of X for given A and B in X = P * A + Q * B | utility function to calculate gcd ; Driver Program
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } var a = 2 ; var b = 4 ; document . write ( gcd ( a , b ) ) ;
Count elements in the given range which have maximum number of divisors | Function to count the elements with maximum number of divisors ; To store number of divisors ; Initialise with zero ; To store the maximum number of divisors ; To store required answer ; Find the first divisible number ; Count number of divisors ; Find number of elements with maximum number of divisors ; Driver code
function MaximumDivisors ( X , Y ) { let arr = new Array ( Y - X + 1 ) ; for ( let i = 0 ; i < arr . length ; i ++ ) arr [ i ] = 0 ; let mx = 0 ; let cnt = 0 ; for ( let i = 1 ; i * i <= Y ; i ++ ) { let sq = i * i ; let first_divisible ; if ( Math . floor ( X / i ) * i >= X ) first_divisible = Math . floor ( X / i ) * i ; else first_divisible = ( Math . floor ( X / i ) + 1 ) * i ; for ( let j = first_divisible ; j <= Y ; j += i ) { if ( j < sq ) continue ; else if ( j == sq ) arr [ j - X ] ++ ; else arr [ j - X ] += 2 ; } } for ( let i = X ; i <= Y ; i ++ ) { if ( arr [ i - X ] > mx ) { cnt = 1 ; mx = arr [ i - X ] ; } else if ( arr [ i - X ] == mx ) cnt ++ ; } return cnt ; } let X = 1 , Y = 10 ; document . write ( MaximumDivisors ( X , Y ) ) ;
Find First element in AP which is multiple of given prime | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; function to find nearest element in common ; base conditions ; driver program ; module both A and D ; function call
function power ( x , y , p ) { let res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function NearestElement ( A , D , P ) { if ( A == 0 ) return 0 ; else if ( D == 0 ) return - 1 ; else { let X = power ( D , P - 2 , P ) ; return ( X * ( P - A ) ) % P ; } } let A = 4 , D = 9 , P = 11 ; A %= P ; D %= P ; document . write ( NearestElement ( A , D , P ) ) ;
Cunningham chain | Function to print Cunningham chain of the first kind ; Iterate till all elements are printed ; check prime or not ; Driver Code
function print_C ( p0 ) { let p1 = 0 ; let i = 0 ; let x ; let flag ; let k ; while ( 1 ) { flag = 1 ; x = Math . pow ( 2 , i ) ; p1 = x * p0 + ( x - 1 ) ; for ( let k = 2 ; k < p1 ; k ++ ) { if ( p1 % k == 0 ) { flag = 0 ; break ; } } if ( flag == 0 ) break ; document . write ( p1 + " " ) ; i ++ ; } } let p0 = 2 ; print_C ( p0 ) ;
Count pairs with Bitwise AND as ODD number | Function to count number of odd pairs ; variable for counting odd pairs ; find all pairs ; find AND operation check odd or even ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair
function findOddPair ( A , N ) { var i , j ; var oddPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] & A [ j ] ) % 2 != 0 ) oddPair ++ ; } } return oddPair ; } var a = [ 5 , 1 , 3 , 2 ] ; var n = a . length ; document . write ( findOddPair ( a , n ) ) ;
Sudo Placement [ 1.7 ] | Greatest Digital Root | Function to return dig - sum ; Function to prvar the Digital Roots ; store the largest digital roots ; Iterate till sqrt ( n ) ; if i is a factor ; get the digit sum of both factors i and n / i ; if digit sum is greater then previous maximum ; if digit sum is greater then previous maximum ; if digit sum is same as then previous maximum , then check for larger divisor ; if digit sum is same as then previous maximum , then check for larger divisor ; Prvar the digital roots ; Driver Code ; Function call to prvar digital roots
function summ ( n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } function printDigitalRoot ( n ) { var maxi = 1 ; var dig = 1 ; for ( i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { var d1 = summ ( n / i ) ; var d2 = summ ( i ) ; if ( d1 > maxi ) { dig = n / i ; maxi = d1 ; } if ( d2 > maxi ) { dig = i ; maxi = d2 ; } if ( d1 == maxi ) { if ( dig < ( n / i ) ) { dig = n / i ; maxi = d1 ; } } if ( d2 == maxi ) { if ( dig < i ) { dig = i ; maxi = d2 ; } } } } document . write ( dig + " " + maxi ) ; } var n = 10 ; printDigitalRoot ( n ) ;
Sum of all elements up to Nth row in a Pascal triangle | Function to find sum of all elements upto nth row . ; Initialize sum with 0 ; Calculate 2 ^ n ; Driver Code
function calculateSum ( n ) { sum = 0 ; sum = 1 << n ; return ( sum - 1 ) ; } let n = 10 ; document . write ( " " + calculateSum ( n ) ) ;
Divide two integers without using multiplication , division and mod operator | Set2 | Returns the quotient of dividend / divisor . ; Calculate sign of divisor i . e . , sign will be negative only if either one of them is negative otherwise it will be positive ; Remove signs of dividend and divisor ; Zero division Exception . ; Using Formula derived above . ; Driver Code
function Divide ( a , b ) { var dividend = a ; var divisor = b ; var sign = ( dividend < 0 ) ^ ( divisor < 0 ) ? - 1 : 1 ; dividend = Math . abs ( dividend ) ; divisor = Math . abs ( divisor ) ; if ( divisor == 0 ) { document . write ( " " ) ; return ; } if ( dividend == 0 ) { document . write ( a + " " + b + " " + 0 + " " ) ; return ; } if ( divisor == 1 ) { System . out . println ( a + " " + b + " " + sign * dividend + " " ) ; return ; } document . write ( a + " " + b + " " + Math . floor ( sign * ( Math . exp ( Math . log ( dividend ) - Math . log ( divisor ) ) ) ) + " " ) ; } var a = 10 , b = 5 ; Divide ( a , b ) ; a = 49 ; b = - 7 ; Divide ( a , b ) ;
Represent the fraction of two numbers in the string format | Function to return the required fraction in string format ; If the numerator is zero , answer is 0 ; If any one ( out of numerator and denominator ) is - ve , sign of resultant answer - ve . ; Calculate the absolute part ( before decimal point ) . ; Output string to store the answer ; Append sign ; Append the initial part ; If completely divisible , return answer . ; Initialize Remainder ; Position at which fraction starts repeating if it exists ; If this remainder is already seen , then there exists a repeating fraction . ; Index to insert parentheses ; Calculate quotient , append it to result and calculate next remainder ; If repeating fraction exists , insert parentheses . ; Return result . ; Drivers Code
function calculateFraction ( num , den ) { if ( num == 0 ) return " " ; var sign = ( num < 0 ) ^ ( den < 0 ) ? - 1 : 1 ; num = Math . abs ( num ) ; den = Math . abs ( den ) ; var initial = parseInt ( num / den ) ; var res = [ ] ; if ( sign == - 1 ) res . push ( " " ) ; res . push ( initial . toString ( ) ) ; if ( num % den == 0 ) return res ; res . push ( " " ) ; var rem = num % den ; var mp = new Map ( ) ; var index ; var repeating = false ; while ( rem > 0 && ! repeating ) { if ( mp . has ( rem ) ) { index = mp . get ( rem ) ; repeating = true ; break ; } else mp . set ( rem , res . length ) ; rem = rem * 10 ; var temp = parseInt ( rem / den ) ; res . push ( temp . toString ( ) ) ; rem = rem % den ; } if ( repeating ) { res . push ( " " ) ; res . splice ( index , 0 , " " ) ; } return res . join ( ' ' ) ; } var num = 50 , den = 22 ; document . write ( calculateFraction ( num , den ) + " " ) ; num = - 1 , den = 2 ; document . write ( calculateFraction ( num , den ) ) ;
Check if the n | Return if the nth term is even or odd . ; If a is even ; If b is even ; If b is odd ; If a is odd ; If b is odd ; If b is eve ; Driver code
function findNature ( a , b , n ) { if ( n == 0 ) return ( a & 1 ) == 1 ? true : false ; if ( n == 1 ) return ( b & 1 ) == 1 ? true : false ; if ( ( a & 1 ) == 0 ) { if ( ( b & 1 ) == 0 ) return false ; else return ( n % 3 != 0 ) ; } else { if ( ( b & 1 ) == 0 ) return ( ( n - 1 ) % 3 != 0 ) ; else return ( ( n + 1 ) % 3 != 0 ) ; } } let a = 2 , b = 4 ; let n = 3 ; if ( findNature ( a , b , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if mirror image of a number is same if displayed in seven segment display | Return " Yes " , if the mirror image of number is same as the given number Else return " No " ; Checking if the number contain only 0 , 1 , 8. ; Checking if the number is palindrome or not . ; If corresponding index is not equal . ; Driver code
function checkEqual ( S ) { for ( var i = 0 ; i < S . length ; i ++ ) { if ( S [ i ] != ' ' && S [ i ] != ' ' && S [ i ] != ' ' ) { return " " ; } } var start = 0 , end = S . length - 1 ; while ( start < end ) { if ( S [ start ] != S [ end ] ) { return " " ; } start ++ ; end -- ; } return " " ; } var S = " " ; document . write ( checkEqual ( S ) ) ;
Check whether a given number is Polydivisible or Not | function to check polydivisible number ; digit extraction of input number ; store the digits in an array ; n contains first i digits ; n should be divisible by i ; Driver code
function check_polydivisible ( n ) { let N = n ; let digit = [ ] ; while ( n > 0 ) { digit . push ( n % 10 ) ; n = parseInt ( n / 10 , 10 ) ; } digit . reverse ( ) ; let flag = true ; n = digit [ 0 ] ; for ( let i = 1 ; i < digit . length ; i ++ ) { n = n * 10 + digit [ i ] ; if ( n % ( i + 1 ) != 0 ) { flag = false ; break ; } } if ( flag ) document . write ( N + " " + " " ) ; else document . write ( N + " " ) ; } let n = 345654 ; check_polydivisible ( n ) ;
Check if given number is a power of d where d is a power of 2 | Javascript program to find if a number is power of d where d is power of 2 ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is a multiple of log2 ( d ) then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver code
function Log2n ( n ) { return ( n > 1 ) ? 1 + Log2n ( n / 2 ) : 0 ; } function isPowerOfd ( n , d ) { var count = 0 ; if ( n > 0 && ( n & ( n - 1 ) ) == 0 ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % ( Log2n ( d ) ) == 0 ) ; } return false ; } var n = 64 , d = 8 ; if ( isPowerOfd ( n , d ) ) document . write ( n + " " + d ) ; else document . write ( n + " " + d ) ;
Total nodes traversed in Euler Tour Tree | Javascript program to check the number of nodes in Euler Tour tree . ; Adjacency list representation of tree ; Function to add edges to tree ; Program to check if calculated Value is equal to 2 * size - 1 ; push out - degree of each node ; Driver Code ; Constructing 1 st tree from example ; Out_deg [ node [ i ] ] is equal to adj [ i ] . Count ; clear previous stored tree ; Constructing 2 nd tree from example ; Out_deg [ node [ i ] ] is equal to adj [ i ] . Count
var MAX = 1001 ; var adj = Array . from ( Array ( MAX ) , ( ) => Array ( ) ) ; function add_edge ( u , v ) { adj [ u ] . push ( v ) ; } function checkTotalNumberofNodes ( actualAnswer , size ) { var calculatedAnswer = size ; for ( var i = 1 ; i <= size ; i ++ ) calculatedAnswer += adj [ i ] . length ; if ( actualAnswer == calculatedAnswer ) document . write ( " " + calculatedAnswer + " " ) ; else document . write ( " " + " " ) ; } for ( var i = 0 ; i < MAX ; i ++ ) adj [ i ] = [ ] ; var N = 8 ; add_edge ( 1 , 2 ) ; add_edge ( 1 , 3 ) ; add_edge ( 2 , 4 ) ; add_edge ( 2 , 5 ) ; add_edge ( 3 , 6 ) ; add_edge ( 3 , 7 ) ; add_edge ( 6 , 8 ) ; checkTotalNumberofNodes ( 2 * N - 1 , N ) ; for ( var i = 1 ; i <= N ; i ++ ) adj [ i ] = [ ] N = 9 ; add_edge ( 1 , 2 ) ; add_edge ( 1 , 3 ) ; add_edge ( 2 , 4 ) ; add_edge ( 2 , 5 ) ; add_edge ( 2 , 6 ) ; add_edge ( 3 , 9 ) ; add_edge ( 5 , 7 ) ; add_edge ( 5 , 8 ) ; checkTotalNumberofNodes ( 2 * N - 1 , N ) ;
Octahedral Number | Function to find octahedral number ; Formula to calculate nth octahedral number and return it into main function . ; Drivers Code ; print result
function octahedral_num ( n ) { return n * ( 2 * n * n + 1 ) / 3 ; } let n = 5 ; document . write ( n + " " ) ; document . write ( octahedral_num ( n ) ) ;
Centered tetrahedral number | Function to find centered Centered tetrahedral number ; Formula to calculate nth Centered tetrahedral number and return it into main function . ; Driver Code
function centeredTetrahedralNumber ( n ) { return ( 2 * n + 1 ) * ( n * n + n + 3 ) / 3 ; } var n = 6 ; document . write ( centeredTetrahedralNumber ( n ) ) ;
Swapping four variables without temporary variable | Javascript program to swap 4 variables without using temporary variable . ; swapping a and b variables ; swapping b and c variables ; swapping c and d variables ; initialising variables ; Function call
function swap ( a , b , c , d ) { a = a + b ; b = a - b ; a = a - b ; b = b + c ; c = b - c ; b = b - c ; c = c + d ; d = c - d ; c = c - d ; document . write ( " " + " " ) ; document . write ( " " + a + " " ) ; document . write ( " " + b + " " ) ; document . write ( " " + c + " " ) ; document . write ( " " + d ) ; } let a = 1 ; let b = 2 ; let c = 3 ; let d = 4 ; document . write ( " " + " " ) ; document . write ( " " + a + " " ) ; document . write ( " " + b + " " ) ; document . write ( " " + c + " " ) ; document . write ( " " + d + " " ) ; document . write ( " " + " " ) ; swap ( a , b , c , d ) ;
Sum of first n natural numbers | Function to find the sum of series ; Driver code
function seriesSum ( n ) { var sum = 0 ; for ( i = 1 ; i <= n ; i ++ ) sum += i * ( ( i + 1 ) / 2 ) ; return sum ; } var n = 4 ; document . write ( seriesSum ( n ) ) ;
Centrosymmetric Matrix | Javascript Program to check whether given matrix is centrosymmetric or not . ; Finding the middle row of the matrix ; for each row upto middle row . ; If each element and its corresponding element is not equal then return false . ; Driver Code
let N = 3 function checkCentrosymmetricted ( n , m ) { let mid_row ; if ( n & 1 ) mid_row = Math . floor ( n / 2 ) + 1 ; else mid_row = n / 2 ; for ( let i = 0 ; i < mid_row ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( m [ i ] [ j ] != m [ n - i - 1 ] [ n - j - 1 ] ) return false ; } } return true ; } let n = 3 ; let m = [ [ 1 , 3 , 5 ] , [ 6 , 8 , 6 ] , [ 5 , 3 , 1 ] ] ; ( checkCentrosymmetricted ( n , m ) ? ( document . write ( " " ) ) : document . write ( " " ) ) ;
Centered triangular number | function for Centered Triangular number ; formula for find Centered Triangular number nth term ; For 3 rd Centered Triangular number ; For 12 th Centered Triangular number
function Centered_Triangular_num ( n ) { return ( 3 * n * n + 3 * n + 2 ) / 2 ; } var n = 3 ; document . write ( Centered_Triangular_num ( n ) + " " ) ; n = 12 ; document . write ( Centered_Triangular_num ( n ) + " " ) ;
Array with GCD of any of its subset belongs to the given array | Function to return gcd of a and b ; Function to find gcd of array of numbers ; Function to generate the array with required constraints . ; computing GCD of the given set ; Solution exists if GCD of array is equal to the minimum element of the array ; Printing the built array ; Taking in the input and initializing the set STL set in cpp has a property that it maintains the elements in sorted order , thus we do not need to sort them externally ; Calling the computing function .
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function findGCD ( arr , n ) { var result = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) result = gcd ( arr [ i ] , result ) ; return result ; } function compute ( arr , n ) { var answer = new Array ( ) ; var GCD_of_array = findGCD ( arr , n ) ; if ( GCD_of_array == arr [ 0 ] ) { answer . add ( arr [ 0 ] ) ; for ( i = 1 ; i < n ; i ++ ) { answer . add ( arr [ 0 ] ) ; answer . add ( arr [ i ] ) ; } for ( var i = 0 ; i < answer . length ; i ++ ) document . write ( answer [ i ] + " " ) ; } else document . write ( " " + " " ) ; } var n = 3 ; var input = [ 2 , 5 , 6 , 7 , 11 ] ; compute ( input , n ) ;
Combinatorics on ordered trees | Function returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate the number of trees with exactly k leaves . ; Function to calculate total number of nodes of degree d in these trees . ; Function to calculate the number of trees in which the root has degree r . ; Number of trees having 3 edges and exactly 2 leaves ; Number of nodes of degree 3 in a tree having 4 edges ; Number of trees having 3 edges where root has degree 2
function binomialCoeff ( n , k ) { let C = new Array ( n + 1 ) ; let i , j ; for ( i = 0 ; i <= n ; i ++ ) { C [ i ] = new Array ( k + 1 ) ; for ( j = 0 ; j <= k ; j ++ ) { C [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } function k_Leaves ( n , k ) { let ans = parseInt ( ( binomialCoeff ( n , k ) * binomialCoeff ( n , k - 1 ) ) / n , 10 ) ; document . write ( " " + " " + " " + ans + " " ) ; return 0 ; } function numberOfNodes ( n , d ) { let ans = binomialCoeff ( 2 * n - 1 - d , n - 1 ) ; document . write ( " " + " " + " " + ans + " " ) ; return 0 ; } function rootDegreeR ( n , r ) { let ans = r * binomialCoeff ( 2 * n - 1 - r , n - 1 ) ; ans = parseInt ( ans / n , 10 ) ; document . write ( " " + " " + " " + ans + " " ) ; return 0 ; } k_Leaves ( 3 , 2 ) ; numberOfNodes ( 3 , 1 ) ; rootDegreeR ( 3 , 2 ) ;
Repeated Unit Divisibility | To find least value of k ; To check n is coprime or not ; to store R ( k ) mod n and 10 ^ k mod n value ; Driver Code
function repUnitValue ( n ) { if ( n % 2 == 0 n % 5 == 0 ) return 0 ; let rem = 1 ; let power = 1 ; let k = 1 ; while ( rem % n != 0 ) { k ++ ; power = power * 10 % n ; rem = ( rem + power ) % n ; } return k ; } let n = 13 ; document . write ( repUnitValue ( n ) ) ;