codeparrot/xlcost-text-to-code huggingface dataset:
Check whether the exchange is possible or not | Recursive function to return gcd of a and b ; Function that returns true if the exchange is possible ; Find the GCD of the array elements ; If the exchange is possible ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function isPossible ( arr , n , p ) { let gcd = 0 ; for ( let i = 0 ; i < n ; i ++ ) gcd = __gcd ( gcd , arr [ i ] ) ; if ( p % gcd == 0 ) return true ; return false ; } let arr = [ 6 , 9 ] ; let n = arr . length ; let p = 3 ; if ( isPossible ( arr , n , p ) ) document . write ( " " ) ; else document . write ( " " ) ;
Choose two elements from the given array such that their sum is not present in any of the arrays | Function to find the numbers from the given arrays such that their sum is not present in any of the given array ; Find the maximum element from both the arrays ; Driver code
function findNum ( a , n , b , m ) { var x = a . reduce ( function ( a , b ) { return Math . max ( a , b ) ; } ) ; var y = b . reduce ( function ( a , b ) { return Math . max ( a , b ) ; } ) ; document . write ( x + " " + y ) ; } var a = [ 3 , 2 , 2 ] ; var n = a . length ; var b = [ 1 , 5 , 7 , 7 , 9 ] var m = b . length ; findNum ( a , n , b , m ) ;
Generate an array B [ ] from the given array A [ ] which satisfies the given conditions | Utility function to print the array elements ; Function to generate and print the required array ; To switch the ceil and floor function alternatively ; For every element of the array ; If the number is odd then prvar the ceil or floor value after division by 2 ; Use the ceil and floor alternatively ; If arr [ i ] is even then it will be completely divisible by 2 ; Driver code
function printArr ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function generateArr ( arr , n ) { var flip = true ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) { if ( flip ^= true ) document . write ( parseInt ( ( Math . ceil ( arr [ i ] / 2.0 ) ) ) + " " ) ; else document . write ( parseInt ( ( Math . floor ( arr [ i ] / 2.0 ) ) ) + " " ) ; } else { document . write ( arr [ i ] / 2 + " " ) ; } } } var arr = [ 3 , - 5 , - 7 , 9 , 2 , - 2 ] ; var n = arr . length ; generateArr ( arr , n ) ;
Number of K length subsequences with minimum sum | Function for the binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the count of valid subsequences ; Sort the array ; Maximum among the minimum K elements ; Y will store the frequency of num in the minimum K elements ; cntX will store the frequency of num in the complete array ; Driver code
function binomialCoeff ( n , k ) { var C = new Array ( n + 1 ) ; for ( var i = 0 ; i < C . length ; i ++ ) { C [ i ] = new Array ( k + 1 ) ; } var i , j ; 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 cntSubSeq ( arr , n , k ) { arr . sort ( ) ; var num = arr [ k - 1 ] ; var Y = 0 ; for ( var i = k - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == num ) Y += 1 ; } var cntX = Y ; for ( var i = k ; i < n ; i ++ ) { if ( arr [ i ] == num ) cntX += 1 ; } return binomialCoeff ( cntX , Y ) ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; var k = 2 ; document . write ( cntSubSeq ( arr , n , k ) ) ;
Find all even length binary sequences with same sum of first and second half bits | Iterative | Function to convert the number into binary and store the number into an array ; Function to check if the sum of the digits till the mid of the array and the sum of the digits from mid till n is the same , if they are same then print that binary ; Calculating the sum from 0 till mid and store in sum1 ; Calculating the sum from mid till n and store in sum2 ; If sum1 is same as sum2 print the binary ; Function to print sequence ; Creating the array ; Looping over powers of 2 ; Converting the number into binary first ; Checking if the sum of the first half of the array is same as the sum of the next half ; Driver Code
function convertToBinary ( num , a , n ) { let pointer = n - 1 ; while ( num > 0 ) { a [ pointer ] = num % 2 ; num = parseInt ( num / 2 ) ; pointer -- ; } } function checkforsum ( a , n ) { let sum1 = 0 ; let sum2 = 0 ; let mid = parseInt ( n / 2 ) ; for ( let i = 0 ; i < mid ; i ++ ) sum1 = sum1 + a [ i ] ; for ( let j = mid ; j < n ; j ++ ) sum2 = sum2 + a [ j ] ; if ( sum1 == sum2 ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] ) ; document . write ( " " ) ; } } function print_seq ( m ) { let n = ( 2 * m ) ; let a = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { a [ j ] = 0 ; } for ( let i = 0 ; i < Math . pow ( 2 , n ) ; i ++ ) { convertToBinary ( i , a , n ) ; checkforsum ( a , n ) ; } } let m = 2 ; print_seq ( m ) ;
Find out the prime numbers in the form of A + nB or B + nA | Javascript implementation of the above approach ; Utility function to check whether two numbers is co - prime or not ; Utility function to check whether a number is prime or not ; Corner case ; Check from 2 to sqrt ( n ) ; finding the Prime numbers ; Checking whether given numbers are co - prime or not ; To store the N primes ; If ' possible ' is true ; Printing n numbers of prime ; checking the form of a + nb ; Checking the form of b + na ; If ' possible ' is false return - 1 ; Driver Code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function coprime ( a , b ) { if ( __gcd ( a , b ) == 1 ) return true ; else return false ; } function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n == 2 n == 3 ) return true ; for ( let i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function findNumbers ( a , b , n ) { let possible = true ; if ( ! coprime ( a , b ) ) possible = false ; let c1 = 1 ; let c2 = 1 ; let num1 , num2 ; let st = new Set ( ) ; if ( possible ) { while ( st . size != n ) { num1 = a + ( c1 * b ) ; if ( isPrime ( num1 ) ) { st . add ( num1 ) ; } c1 ++ ; num2 = b + ( c2 * a ) ; if ( isPrime ( num2 ) ) { st . add ( num2 ) ; } c2 ++ ; } st = [ ... st ] . sort ( ( a , b ) => a - b ) for ( let i of st ) document . write ( i + " " ) ; } else document . write ( " " ) ; } let a = 3 ; let b = 5 ; let n = 4 ; findNumbers ( a , b , n ) ;
Find the integers that doesnot ends with T1 or T2 when squared and added X | Function to print the elements Not ending with T1 or T2 ; Flag to check if none of the elements Do not end with t1 or t2 ; Traverse through all the elements ; Temporary variable to store the value ; If the last digit is neither t1 nor t2 then ; Print the number ; Set the flag as False ; If none of the elements meets the specification ; Test case 1 ; Call the function ; Test case 2 ; Call the function
function findIntegers ( n , a , x , t1 , t2 ) { let flag = true ; for ( let i = 0 ; i < n ; i ++ ) { let temp = Math . pow ( a [ i ] , 2 ) + x ; if ( temp % 10 != t1 && temp % 10 != t2 ) { document . write ( temp + " " ) ; flag = false ; } } if ( flag ) document . write ( " " ) ; } let N = 4 , X = 10 , T1 = 5 , T2 = 6 ; let a = [ 3 , 1 , 4 , 7 ] ; findIntegers ( N , a , X , T1 , T2 ) ; document . write ( " " ) ; N = 4 , X = 2 , T1 = 5 , T2 = 6 ; let b = [ 2 , 18 , 22 , 8 ] ; findIntegers ( N , b , X , T1 , T2 ) ;
First N terms whose sum of digits is a multiple of 10 | Javascript Program to Find the Unique elements in linked lists ; Function to return the sum of digits of n ; Add last digit to the sum ; Remove last digit ; Function to return the nth term of the required series ; If sum of digit is already a multiple of 10 then append 0 ; To store the minimum digit that must be appended ; Return n after appending the required digit ; Function to print the first n terms of the required series ; Driver code
const TEN = 10 ; function digitSum ( n ) { let sum = 0 ; while ( n > 0 ) { sum += n % TEN ; n = Math . floor ( n / TEN ) ; } return sum ; } function getNthTerm ( n ) { let sum = digitSum ( n ) ; if ( sum % TEN == 0 ) return ( n * TEN ) ; let extra = TEN - ( sum % TEN ) ; return ( ( n * TEN ) + extra ) ; } function firstNTerms ( n ) { for ( let i = 1 ; i <= n ; i ++ ) document . write ( getNthTerm ( i ) + " " ) ; } let n = 10 ; firstNTerms ( n ) ;
Nearest greater number by interchanging the digits | JAVAscript program to find nearest greater value ; Find all the possible permutation of Value A . ; Convert into Integer ; Find the minimum value of A by interchanging the digit of A and store min1 . ; Swap two string character ; Driver code ; Convert integer value into string to find all the permutation of the number ; count = 1 means number greater than B exists
let min1 = Number . MAX_VALUE ; let count = 0 ; function permutation ( str1 , i , n , p ) { if ( i == n ) { let q = parseInt ( str1 ) ; if ( q - p > 0 && q < min1 ) { min1 = q ; count = 1 ; } } else { for ( let j = i ; j <= n ; j ++ ) { str1 = swap ( str1 , i , j ) ; permutation ( str1 , i + 1 , n , p ) ; str1 = swap ( str1 , i , j ) ; } } return min1 ; } function swap ( str , i , j ) { let ch = str . split ( " " ) ; let temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return ( ch ) . join ( " " ) ; } let A = 213 ; let B = 111 ; let str1 = A . toString ( ) ; let len = str1 . length ; let h = permutation ( str1 , 0 , len - 1 , B ) ; if ( count == 1 ) document . write ( h ) ; else document . write ( - 1 ) ;
Alcuin 's Sequence | find the nth term of Alcuin 's sequence ; return the ans ; print first n terms of Alcuin number ; display the number ; Driver code
function Alcuin ( n ) { let _n = n , ans ; ans = Math . round ( ( _n * _n ) / 12 ) - Math . floor ( _n / 4 ) * Math . floor ( ( _n + 2 ) / 4 ) ; return ans ; } function solve ( n ) { let i = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { document . write ( Alcuin ( i ) + " " ) ; } } let n = 15 ; solve ( n ) ;
Find the final sequence of the array after performing given operations | javascript implementation of the approach ; Driver code
function solve ( arr , n ) { var b = Array ( n ) . fill ( 0 ) ; var p = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { b [ p ] = arr [ i -- ] ; if ( i >= 0 ) b [ n - 1 - p ] = arr [ i ] ; p ++ ; } return b ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; var b = solve ( arr , n ) ; document . write ( " " + b . toString ( ) + " " ) ;
Check if the product of every contiguous subsequence is different or not in a number | Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Driver code
function productsDistinct ( N ) { var s = " " ; while ( N ) { s += String . fromCharCode ( N % 10 + ' ' . charCodeAt ( 0 ) ) ; N = parseInt ( N / 10 ) ; } s = s . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; var sz = s . length ; var se = new Set ( ) ; for ( var i = 0 ; i < sz ; i ++ ) { var product = 1 ; for ( var j = i ; j < sz ; j ++ ) { product *= ( s [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; if ( se . has ( product ) ) return false ; else se . add ( product ) ; } } return true ; } var N = 2345 ; if ( productsDistinct ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Number of ways to arrange 2 * N persons on the two sides of a table with X and Y persons on opposite sides | JavaScript implementation for the above approach Function to returns factorial of n ; Function to find nCr ; Function to find the number of ways to arrange 2 * N persons ; Driver code ; Function call
function factorial ( n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } function nCr ( n , r ) { return factorial ( n ) / ( factorial ( n - r ) * factorial ( r ) ) ; } function NumberOfWays ( n , x , y ) { return nCr ( 2 * n - x - y , n - x ) * factorial ( n ) * factorial ( n ) ; } var n = 5 , x = 4 , y = 2 ; document . write ( NumberOfWays ( n , x , y ) ) ;
Find smallest positive number Y such that Bitwise AND of X and Y is Zero | Method to find smallest number Y for a given value of X such that X AND Y is zero ; Convert the number into its binary form ; Case 1 : If all bits are ones , then return the next number ; Case 2 : find the first 0 - bit index and return the Y ; To store the count of set bits ; Driver code
function findSmallestNonZeroY ( A_num ) { let A_binary = ( A_num >>> 0 ) . toString ( 2 ) ; let B = 1 ; let len = A_binary . length ; let no_ones = bitCount ( A_num ) ; if ( len == no_ones ) { return A_num + 1 ; } for ( let i = 0 ; i < len ; i ++ ) { let ch = A_binary [ len - i - 1 ] ; if ( ch == ' ' ) { B = Math . floor ( Math . pow ( 2.0 , i ) ) ; break ; } } return B ; } function bitCount ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let X = findSmallestNonZeroY ( 10 ) ; document . write ( X ) ;
Maximum element in an array such that its previous and next element product is maximum | Function to return the largest element such that its previous and next element product is maximum ; Calculate the product of the previous and the next element for the current element ; Update the maximum product ; If current product is equal to the current maximum product then choose the maximum element ; Driver code
function maxElement ( a , n ) { if ( n < 3 ) return - 1 ; let maxElement = a [ 0 ] ; let maxProd = a [ n - 1 ] * a [ 1 ] ; for ( let i = 1 ; i < n ; i ++ ) { let currProd = a [ i - 1 ] * a [ ( i + 1 ) % n ] ; if ( currProd > maxProd ) { maxProd = currProd ; maxElement = a [ i ] ; } else if ( currProd == maxProd ) { maxElement = Math . max ( maxElement , a [ i ] ) ; } } return maxElement ; } let a = [ 5 , 6 , 4 , 3 , 2 ] ; let n = a . length ; document . write ( maxElement ( a , n ) ) ;
Total ways of choosing X men and Y women from a total of M men and W women | Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of required ways ; Driver Code
function ncr ( n , r ) { let ans = 1 ; for ( let i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans = parseInt ( ans / i ) ; } return ans ; } function totalWays ( X , Y , M , W ) { return ( ncr ( M , X ) * ncr ( W , Y ) ) ; } let X = 4 , Y = 3 , M = 6 , W = 5 ; document . write ( totalWays ( X , Y , M , W ) ) ;
Generate elements of the array following given conditions | Function to generate the required array ; Initialize cnt variable for assigning unique value to prime and its multiples ; When we get a prime for the first time then assign a unique smallest value to it and all of its multiples ; Print the generated array ; Driver code
function specialSieve ( n ) { let cnt = 0 ; let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) prime [ i ] = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( ! prime [ i ] ) { cnt ++ ; for ( let j = i ; j <= n ; j += i ) prime [ j ] = cnt ; } } for ( let i = 2 ; i <= n ; i ++ ) document . write ( prime [ i ] + " " ) ; } let n = 6 ; specialSieve ( n ) ;
Find the node whose sum with X has maximum set bits | Javascript implementation of the approach ; number of set bits ; Function to perform dfs to find the maximum value ; If current set bits value is greater than the current maximum ; If count is equal to the maximum then choose the node with minimum value ; Driver code ; Weights of the node ; Edges of the tree
let maximum = Number . MIN_VALUE , x , ans = Number . MAX_VALUE ; let graph = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { graph [ i ] = [ ] ; } let weight = [ ] ; function __builtin_popcount ( x ) { let c = 0 ; for ( let i = 0 ; i < 60 ; i ++ ) if ( ( ( x >> i ) & 1 ) != 0 ) c ++ ; return c ; } function dfs ( node , parent ) { let a = __builtin_popcount ( weight [ node ] + x ) ; if ( maximum < a ) { maximum = a ; ans = node ; } else if ( maximum == a ) ans = Math . min ( ans , node ) ; for ( let i = 0 ; i < graph [ node ] . length ; i ++ ) { if ( graph [ node ] [ i ] == parent ) continue ; dfs ( graph [ node ] [ i ] , node ) ; } } x = 15 ; weight . push ( 0 ) ; weight . push ( 5 ) ; weight . push ( 10 ) ; ; weight . push ( 11 ) ; ; weight . push ( 8 ) ; weight . push ( 6 ) ; graph [ 1 ] . push ( 2 ) ; graph [ 2 ] . push ( 3 ) ; graph [ 2 ] . push ( 4 ) ; graph [ 1 ] . push ( 5 ) ; dfs ( 1 , 1 ) ; document . write ( ans ) ;
Count Pairs from two arrays with even sum | Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a ; Find the count of odd and even elements in b ; Count the number of pairs ; Return the number of pairs ; Driver code
function count_pairs ( a , b , n , m ) { var odd1 = 0 , even1 = 0 ; var odd2 = 0 , even2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd1 ++ ; else even1 ++ ; } for ( i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) odd2 ++ ; else even2 ++ ; } var pairs = Math . min ( odd1 , odd2 ) + Math . min ( even1 , even2 ) ; return pairs ; } var a = [ 9 , 14 , 6 , 2 , 11 ] ; var b = [ 8 , 4 , 7 , 20 ] ; var n = a . length ; var m = b . length ; document . write ( count_pairs ( a , b , n , m ) ) ;
Find an index such that difference between product of elements before and after it is minimum | Function to find index ; Array to store log values of elements ; Prefix Array to Maintain Sum of log values till index i ; Answer Index ; Find minimum absolute value ; Driver code
function solve ( array , N ) { let Arraynew = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { Arraynew [ i ] = Math . log ( array [ i ] ) ; } let prefixsum = new Array ( N ) ; prefixsum [ 0 ] = Arraynew [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] ; } let answer = 0 ; let minabs = Math . abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) ; for ( let i = 1 ; i < N - 1 ; i ++ ) { let ans1 = Math . abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) ; if ( ans1 < minabs ) { minabs = ans1 ; answer = i ; } } document . write ( " " + answer + " " ) ; } let array = [ 1 , 4 , 12 , 2 , 6 ] ; let N = 5 ; solve ( array , N ) ;
Find the value of N when F ( N ) = f ( a ) + f ( b ) where a + b is the minimum possible and a * b = N | Function to return the value of F ( N ) ; Base cases ; Count the number of times a number if divisible by 2 ; Return the summation ; Driver code
function getValueOfF ( n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; let cnt = 0 ; while ( n % 2 == 0 ) { cnt += 1 ; n = parseInt ( n / 2 , 10 ) ; } return 2 * cnt ; } let n = 20 ; document . write ( getValueOfF ( n ) ) ;
Find the sum of the number of divisors | Javascript code for above given approach ; To store the number of divisors ; Function to find the number of divisors of all numbers in the range 1 to n ; For every number 1 to n ; Increase divisors count for every number ; Function to find the sum of divisors ; To store sum ; Count the divisors ; Driver code ; Function call
let N = 100005 ; let mod = 1000000007 ; let cnt = new Array ( N ) ; cnt . fill ( 0 ) ; function Divisors ( ) { for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 1 ; j * i < N ; j ++ ) { cnt [ i * j ] ++ ; } } } function Sumofdivisors ( A , B , C ) { let sum = 0 ; Divisors ( ) ; for ( let i = 1 ; i <= A ; i ++ ) { for ( let j = 1 ; j <= B ; j ++ ) { for ( let k = 1 ; k <= C ; k ++ ) { let x = i * j * k ; sum += cnt [ x ] ; if ( sum >= mod ) { sum -= mod ; } } } } return sum ; } let A = 5 , B = 6 , C = 7 ; document . write ( Sumofdivisors ( A , B , C ) ) ;
Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver Code
function A_mod_B ( N , a ) { var len = N . length ; var ans = 0 ; for ( var i = 0 ; i < len ; i ++ ) ans = ( ans * 10 + parseInt ( N . charAt ( i ) - ' ' ) ) % a ; return ans % a ; } function findMod ( N ) { var mod = A_mod_B ( N , 4 ) ; var ans = ( 1 + parseInt ( Math . pow ( 2 , mod ) + Math . pow ( 3 , mod ) + Math . pow ( 4 , mod ) ) ) ; return ( ans % 5 ) ; } var N = " " ; document . write ( findMod ( N ) ) ;
Elements greater than the previous and next element in an Array | Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver code
function printElements ( arr , n ) { for ( var i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) document . write ( arr [ i ] + " " ) ; } } var arr = [ 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 ] ; var n = arr . length ; printElements ( arr , n ) ;
Sum of the series Kn + ( K ( n | Recursive Javascript program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code
function exponent ( A , B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; let y ; if ( B % 2 == 0 ) { y = exponent ( A , parseInt ( B / 2 , 10 ) ) ; y = ( y * y ) ; } else { y = A ; y = ( y * exponent ( A , B - 1 ) ) ; } return y ; } function sum ( k , n ) { let sum = exponent ( k , n + 1 ) - exponent ( k - 1 , n + 1 ) ; return sum ; } let n = 3 ; let K = 3 ; document . write ( sum ( K , n ) ) ;
Minimize the cost to split a number | check if a number is prime or not ; run a loop upto square root of x ; Function to return the minimized cost ; If n is prime ; If n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; Every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; Driver code
function isPrime ( x ) { for ( let i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) return 0 ; } return 1 ; } function minimumCost ( n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 1 && isPrime ( n - 2 ) ) return 2 ; if ( n % 2 == 0 ) return 2 ; return 3 ; } let n = 6 ; document . write ( minimumCost ( n ) ) ;
Find amount of water wasted after filling the tank | Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; driver program
function wastedWater ( V , M , N ) { let wasted_amt , amt_per_min , time_to_fill ; amt_per_min = M - N ; time_to_fill = V / amt_per_min ; wasted_amt = N * time_to_fill ; return wasted_amt ; } let V , M , N ; V = 700 ; M = 10 ; N = 3 ; document . write ( wastedWater ( V , M , N ) , " " ) ; V = 1000 ; M = 100 ; N = 50 ; document . write ( wastedWater ( V , M , N ) ) ;
Smallest and Largest N | Function to print the largest and the smallest n - digit perfect cube ; Smallest n - digit perfect cube ; Largest n - digit perfect cube ; Driver code
function nDigitPerfectCubes ( n ) { document . write ( Math . pow ( Math . ceil ( Math . cbrt ( Math . pow ( 10 , ( n - 1 ) ) ) ) , 3 ) + " " ) ; document . write ( Math . pow ( Math . ceil ( Math . cbrt ( Math . pow ( 10 , ( n ) ) ) ) - 1 , 3 ) ) ; } let n = 3 ; nDigitPerfectCubes ( n ) ;
Count numbers which are divisible by all the numbers from 2 to 10 | Function to return the count of numbers from 1 to n which are divisible by all the numbers from 2 to 10 ; Driver code
function countNumbers ( n ) { return ( n / 2520 ) ; } var n = 3000 ; document . write ( Math . round ( countNumbers ( n ) ) ) ;