codeparrot/xlcost-text-to-code huggingface dataset:
Check if a subarray exists with sum greater than the given Array | Function to check whether there exists a subarray whose sum is greater than or equal to sum of given array elements ; Initialize sum with 0 ; Checking possible prefix subarrays . If sum of them is less than or equal to zero , then return 1 ; again reset sum to zero ; Checking possible suffix subarrays . If sum of them is less than or equal to zero , then return 1 ; Otherwise return 0 ; Driver Code
function subarrayPossible ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum <= 0 ) return true ; } sum = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; if ( sum <= 0 ) return true ; } return false ; } let arr = [ 10 , 5 , - 12 , 7 , - 10 , 20 , 30 , - 10 , 50 , 60 ] ; let size = arr . length ; if ( subarrayPossible ( arr , size ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Finding Integreand using Weedle 's Rule | A sample function f ( x ) = 1 / ( 1 + x ^ 2 ) ; Function to find the integral value of f ( x ) with step size h , with initial lower limit and upper limit a and b ; Find step size h ; To store the final sum ; Find sum using Weedle 's Formula ; Return the final sum ; lower limit and upper limit ; Function Call
function y ( x ) { let num = 1 ; let denom = 1.0 + x * x ; return num / denom ; } function WeedleRule ( a , b ) { let h = ( b - a ) / 6 ; let sum = 0 ; sum = sum + ( ( ( 3 * h ) / 10 ) * ( y ( a ) + y ( a + 2 * h ) + 5 * y ( a + h ) + 6 * y ( a + 3 * h ) + y ( a + 4 * h ) + 5 * y ( a + 5 * h ) + y ( a + 6 * h ) ) ) ; return sum . toFixed ( 6 ) ; } let a = 0 , b = 6 ; let num = WeedleRule ( a , b ) ; document . write ( " " + num ) ;
Find the minimum number to be added to N to make it a prime number | Function to check if a given number is a prime or not ; Base cases ; This is checked so that we can skip middle five numbers in below loop ; For all the remaining numbers , check if any number is a factor if the number or not ; If none of the above numbers are the factors for the number , then the given number is prime ; Function to return the smallest number to be added to make a number prime ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; If the number is not a prime , then increment the number by 1 and the counter which stores the number to be added ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function findSmallest ( N ) { if ( N == 0 ) return 2 ; if ( N == 1 ) return 1 ; var prime = N , counter = 0 ; var found = false ; while ( ! found ) { if ( isPrime ( prime ) ) found = true ; else { prime ++ ; counter ++ ; } } return counter ; } var N = 10 ; document . write ( findSmallest ( N ) ) ;
Find the position of the given Prime Number | Javascript program to find the position of the given prime number ; Function to precompute the position of every prime number using Sieve ; 0 and 1 are not prime numbers ; Variable to store the position ; Incrementing the position for every prime number ; Driver code
var limit = 10000000 var position = Array ( limit + 1 ) . fill ( 0 ) ; function sieve ( ) { position [ 0 ] = - 1 , position [ 1 ] = - 1 ; var pos = 0 ; for ( var i = 2 ; i <= limit ; i ++ ) { if ( position [ i ] == 0 ) { position [ i ] = ++ pos ; for ( var j = i * 2 ; j <= limit ; j += i ) position [ j ] = - 1 ; } } } sieve ( ) ; var n = 11 ; document . write ( position [ n ] ) ;
Minimum possible sum of array elements after performing the given operation | Function to find the maximum sum of the sub array ; max_so_far represents the maximum sum found till now and max_ending_here represents the maximum sum ending at a specific index ; Iterating through the array to find the maximum sum of the subarray ; If the maximum sum ending at a specific index becomes less than 0 , then making it equal to 0. ; Function to find the minimum possible sum of the array elements after performing the given operation ; Finding the sum of the array ; Computing the minimum sum of the array ; Driver code
function maxSubArraySum ( a , size ) { var max_so_far = - 1000000000 , max_ending_here = 0 ; for ( var i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } function minPossibleSum ( a , n , x ) { var mxSum = maxSubArraySum ( a , n ) ; var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } sum = sum - mxSum + mxSum / x ; document . write ( sum ) ; } var N = 3 ; var X = 2 ; var A = [ 1 , - 2 , 3 ] ; minPossibleSum ( A , N , X ) ;
Count pairs in array such that one element is reverse of another | Function to reverse the digits of the number ; Loop to iterate till the number is greater than 0 ; Extract the last digit and keep multiplying it by 10 to get the reverse of the number ; Function to find the pairs from the array such that one number is reverse of the other ; Iterate through all pairs ; Increment count if one is the reverse of other ; Driver code
function reverse ( num ) { var rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = parseInt ( num / 10 ) ; } return rev_num ; } function countReverse ( arr , n ) { var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) if ( reverse ( arr [ i ] ) == arr [ j ] ) { res ++ ; } return res ; } var a = [ 16 , 61 , 12 , 21 , 25 ] ; var n = a . length ; document . write ( countReverse ( a , n ) ) ;
Count pairs in array such that one element is reverse of another | Function to reverse the digits of the number ; Loop to iterate till the number is greater than 0 ; Extract the last digit and keep multiplying it by 10 to get the reverse of the number ; Function to find the pairs from the array such that one number is reverse of the other ; Iterate over every element in the array and increase the frequency of the element in hash map ; Iterate over every element in the array ; remove the current element from the hash map by decreasing the frequency to avoid counting when the number is a palindrome or when we visit its reverse ; Increment the count by the frequency of reverse of the number ; Driver code
function reverse ( num ) { let rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = Math . floor ( num / 10 ) ; } return rev_num ; } function countReverse ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . set ( arr [ i ] , 1 ) ; } } let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) - 1 ) ; } else { freq . set ( arr [ i ] , - 1 ) ; } if ( freq . has ( reverse ( arr [ i ] ) ) ) { res += freq . get ( reverse ( arr [ i ] ) ) ; } } return res ; } let a = [ 16 , 61 , 12 , 21 , 25 ] ; let n = a . length ; document . write ( countReverse ( a , n ) ) ;
Sum of i * countDigits ( i ) ^ countDigits ( i ) for all i in range [ L , R ] | Javascript program to find the required sum ; Function to return the required sum ; Iterating for all the number of digits from 1 to 10 ; If the range is valid ; Sum of AP ; Computing the next minimum and maximum numbers by for the ( i + 1 ) - th digit ; Driver code
MOD = 1000000007 function rangeSum ( l , r ) { var a = 1 , b = 9 , res = 0 ; for ( var i = 1 ; i <= 10 ; i ++ ) { var L = Math . max ( l , a ) ; var R = Math . min ( r , b ) ; if ( L <= R ) { var sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += Math . pow ( i , i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } var l = 98 , r = 102 ; document . write ( rangeSum ( l , r ) ) ;
Trial division Algorithm for Prime Factorization | Function to check if a number is a prime number or not ; Initializing with the value 2 from where the number is checked ; Computing the square root of the number N ; While loop till the square root of N ; If any of the numbers between [ 2 , sqrt ( N ) ] is a factor of N Then the number is composite ; If none of the numbers is a factor , then it is a prime number ; Driver code ; To check if a number is a prime or not
function TrialDivision ( N ) { let i = 2 ; let k = Math . ceil ( Math . sqrt ( N ) ) ; while ( i <= k ) { if ( N % i == 0 ) return 0 ; i += 1 ; } return 1 ; } let N = 49 ; let p = TrialDivision ( N ) ; if ( p ) document . write ( " " ) ; else document . write ( " " ) ;
Find if it is possible to choose subarray that it contains exactly K even integers | Function to check if it is possible to choose a subarray that contains exactly K even integers ; Variable to store the count of even numbers ; If we have to select 0 even numbers but there is all odd numbers in the array ; If the count of even numbers is greater than or equal to K then we can select a subarray with exactly K even integers ; If the count of even numbers is less than K then we cannot select any subarray with exactly K even integers ; Driver code
function isPossible ( A , n , k ) { var countOfTwo = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( A [ i ] % 2 == 0 ) { countOfTwo ++ ; } } if ( k == 0 && countOfTwo == n ) document . write ( " " ) ; else if ( countOfTwo >= k ) { document . write ( " " ) ; } else document . write ( " " ) ; } var arr = [ 1 , 2 , 4 , 5 ] ; var K = 2 ; var N = arr . length ; isPossible ( arr , N , K ) ;
Count of pairs ( A , B ) in range 1 to N such that last digit of A is equal to the first digit of B | Function to Count of pairs ( A , B ) in range 1 to N ; count C i , j ; Calculate number of pairs ; Driver code ; Function call
function pairs ( n ) { let c = new Array ( 10 ) ; for ( var i = 0 ; i < c . length ; i ++ ) { c [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < c . length ; i ++ ) { for ( var j = 0 ; j < c . length ; j ++ ) { c [ i ] [ j ] = 0 ; } } let tmp = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( i >= tmp * 10 ) tmp *= 10 ; c [ Math . floor ( i / tmp ) ] [ i % 10 ] ++ ; } let ans = 0 ; for ( let i = 1 ; i < 10 ; i ++ ) for ( let j = 1 ; j < 10 ; j ++ ) ans += c [ i ] [ j ] * c [ j ] [ i ] ; return ans ; } let n = 25 ; document . write ( pairs ( n ) ) ;
Count ways to divide C in two parts and add to A and B to make A strictly greater than B | Function to count the number of ways to divide C into two parts and add to A and B such that A is strictly greater than B ; Minimum value added to A to satisfy the given relation ; Number of different values of A , i . e . , number of ways to divide C ; Driver code
function countWays ( A , B , C ) { var minAddA = Math . max ( 0 , parseInt ( ( C + B - A + 2 ) / 2 ) ) ; var count_ways = Math . max ( C - minAddA + 1 , 0 ) ; return count_ways ; } var A = 3 , B = 5 , C = 5 ; document . write ( countWays ( A , B , C ) ) ;
GCD of elements occurring Fibonacci number of times in an Array | Function to create hash table to check Fibonacci numbers ; Inserting the first two numbers into the hash ; Adding the remaining Fibonacci numbers using the previously added elements ; Function to return the GCD of elements in an array having fibonacci frequency ; Creating the hash ; Map is used to store the frequencies of the elements ; Iterating through the array ; Traverse the map using iterators ; Calculate the gcd of elements having fibonacci frequencies ; Driver code
function createHash ( hash , maxElement ) { let prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { let temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } function gcdFibonacciFreq ( arr , n ) { let hash = new Set ( ) ; createHash ( hash , arr . sort ( ( a , b ) => b - a ) [ 0 ] ) ; let i , j ; let m = new Map ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( m . has ( arr [ i ] ) ) { m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) } else { m . set ( arr [ i ] , 1 ) } } let gcd = 0 ; for ( let it of m ) { if ( hash . has ( it [ 1 ] ) ) { gcd = __gcd ( gcd , it [ 0 ] ) ; } } return gcd ; } function __gcd ( a , b ) { return ( b == 0 ? a : __gcd ( b , a % b ) ) ; } let arr = [ 5 , 3 , 6 , 5 , 6 , 6 , 5 , 5 ] ; let n = arr . length ; document . write ( gcdFibonacciFreq ( arr , n ) ) ;
Program to print the series 1 , 9 , 17 , 33 , 49 , 73 , 97. . . till N terms | Function to print the series ; Generate the ith term and print it ; Driver Code
function printSeries ( N ) { let ith_term = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { ith_term = i % 2 == 0 ? 2 * i * i + 1 : 2 * i * i - 1 ; document . write ( ith_term + " " ) ; } } let N = 7 ; printSeries ( N ) ;
Find Nth term of the series where each term differs by 6 and 2 alternately | Function to find Nth term ; Iterate from 1 till Nth term ; Check if i is even and then add 6 ; Else add 2 ; Print ans ; Driver Code
function findNthTerm ( N ) { let ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { ans = ans + 6 ; } else { ans = ans + 2 ; } } document . write ( ans + " " ) ; } let N = 3 ; findNthTerm ( N ) ;
Find the number of ordered pairs such that a * p + b * q = N , where p and q are primes | JavaScript program to find the number of ordered pairs such that a * p + b * q = N where p and q are primes ; Sieve of erastothenes to store the prime numbers and their frequency in form a * p + b * q ; Performing Sieve of Eratosthenes to find the prime numbers unto 10001 ; Loop to find the number of ordered pairs for every combination of the prime numbers ; Driver code ; Printing the number of ordered pairs for every query
size = 10001 prime = Array ( size ) . fill ( 0 ) ; freq = Array ( size ) . fill ( 0 ) ; function sieve ( a , b ) { prime [ 1 ] = 1 ; for ( var i = 2 ; i * i < size ; i ++ ) { if ( prime [ i ] == 0 ) { for ( var j = i * 2 ; j < size ; j += i ) prime [ j ] = 1 ; } } for ( var p = 1 ; p < size ; p ++ ) { for ( var q = 1 ; q < size ; q ++ ) { if ( prime [ p ] == 0 && prime [ q ] == 0 && a * p + b * q < size ) { freq [ a * p + b * q ] ++ ; } } } } var queries = 2 , a = 1 , b = 2 ; sieve ( a , b ) ; var arr = [ 15 , 25 ] ; for ( var i = 0 ; i < queries ; i ++ ) { document . write ( freq [ arr [ i ] ] + " " ) ; }
Find the Sum of the series 1 / 2 | Function to find the sum of series ; Generate the ith term and add it to the sum if i is even and subtract if i is odd ; Print the sum ; Driver Code
function printSeriesSum ( N ) { let sum = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { if ( i & 1 ) { sum += i / ( i + 1 ) ; } else { sum -= i / ( i + 1 ) ; } } document . write ( sum . toFixed ( 6 ) ) ; } let N = 10 ; printSeriesSum ( N ) ;
Find the Sum of the series 1 + 2 + 9 + 64 + 625 + 7776 . . . till N terms | Function to find the sum of series ; Generate the ith term and add it to the sum ; Print the sum ; Driver Code
function printSeriesSum ( N ) { let sum = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { sum += Math . pow ( i , i - 1 ) ; } document . write ( sum ) ; } let N = 5 ; printSeriesSum ( N ) ;
Find the Sum of the series 1 + 1 / 3 + 1 / 5 + 1 / 7 + ... till N terms | Function to find the sum of the given series ; Initialise the sum to 0 ; Generate the ith term and add it to the sum ; Print the final sum ; Driver Code
function printSumSeries ( N ) { let sum = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { sum += 1.0 / ( 2 * i - 1 ) ; } document . write ( sum . toFixed ( 5 ) ) ; } let N = 6 ; printSumSeries ( N ) ;
Find the Sum of the series 1 , 2 , 3 , 6 , 9 , 18 , 27 , 54 , ... till N terms | Function to find the sum of series ; Flag to find the multiplicating factor . . i . e , by 2 or 3 / 2 ; First term ; If flag is true , multiply by 2 ; If flag is false , multiply by 3 / 2 ; Update the previous element to nextElement ; Print the sum ; Driver Code
function printSeriesSum ( N ) { let sum = 0 ; let a = 1 ; let cnt = 0 ; let flag = true ; sum += a ; while ( cnt < N ) { let nextElement ; if ( flag ) { nextElement = a * 2 ; sum += nextElement ; flag = ! flag ; } else { nextElement = a * 3 / 2 ; sum += nextElement ; flag = ! flag ; } a = nextElement ; cnt ++ ; } document . write ( sum ) ; } let N = 8 ; printSeriesSum ( N ) ;
Program to add two integers of given base | Function to find the sum of two integers of base B ; Padding 0 in front of the number to make both numbers equal ; Condition to check if the strings have lengths mis - match ; Loop to find the find the sum of two integers of base B ; Current Place value for the resultant sum ; Update carry ; Find current digit ; Update sum result ; Driver code ; Function Call
function sumBaseB ( a , b , base_var ) { let len_a , len_b ; len_a = a . length ; len_b = b . length ; let sum , s ; s = " " ; sum = " " ; let diff ; diff = Math . abs ( len_a - len_b ) ; for ( let i = 1 ; i <= diff ; i ++ ) s += " " ; if ( len_a < len_b ) a = s + a ; else b = s + b ; let curr , carry = 0 ; for ( let i = Math . max ( len_a , len_b ) - 1 ; i > - 1 ; i -- ) { curr = carry + ( a [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) + ( b [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; carry = parseInt ( curr / base_var , 10 ) ; curr = curr % base_var ; sum = String . fromCharCode ( curr + ' ' . charCodeAt ( ) ) + sum ; } if ( carry > 0 ) sum = String . fromCharCode ( carry + ' ' . charCodeAt ( ) ) + sum ; return sum ; } let a , b , sum ; let base_var ; a = " " ; b = " " ; base_var = 6 ; sum = sumBaseB ( a , b , base_var ) ; document . write ( sum + " " ) ;
Minimum number of swaps required to make a number divisible by 60 | Function that prlet minimum number of swap operations required ; Condition if more than one zero exist ; Condition if zero_exist ; Computing total sum of all digits ; Condition if zero does not exist or the sum is not divisible by 3 ; Condition to find a digit that is multiple of 2 other than one zero ; Condition if multiple of 2 do not exist ; Condition for zero swaps means the number is already is divisible by 60 ; Condition for only one swap ; Otherwise 2 swaps required ; Driver Code
function MinimumSwapOperations ( s ) { let zero_exist = false ; let multiple_of_2 = false ; let sum = 0 ; let index_of_zero = 0 ; let more_zero = false ; for ( let i = 0 ; i < s . length ; i ++ ) { let val = s [ i ] - ' ' ; if ( zero_exist == true ) more_zero = true ; if ( val == 0 ) { zero_exist = true ; index_of_zero = i ; } sum += val ; } if ( zero_exist == false sum % 3 != 0 ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < s . length ; i ++ ) { let val = s [ i ] - ' ' ; if ( val % 2 == 0 && i != index_of_zero ) multiple_of_2 = true ; } if ( multiple_of_2 == false ) { document . write ( " " ) ; return ; } let last_val = s . charAt [ s . length - 1 ] - ' ' ; let second_last_val = s [ ( s . length - 2 ) ] - ' ' ; if ( last_val == 0 && second_last_val % 2 == 0 ) document . write ( 0 ) ; else if ( ( last_val == 0 && second_last_val % 2 != 0 ) || ( last_val % 2 == 0 && second_last_val == 0 ) ) document . write ( 1 ) ; else if ( more_zero == true && ( last_val == 0 && second_last_val % 2 != 0 ) ) document . write ( 1 ) ; else document . write ( 2 ) ; } let N = " " ; MinimumSwapOperations ( N ) ;
Largest palindrome which is product of two N | Function to check if a number is a Palindrome or not ; Taking the string value of the number ; Loop to check if every i - th character from beginning is equal to every ( N - i ) th char ; Function to find the largest palindrome which is a product of two N digited numbers ; Find lowerBound , upperBound for a given nDigits . for n = 2 ; [ 10 , 99 ] ; Result variables ; Keep p decrementing by 11 ; Find the nearest number divisible by 11 ; Keep decrementing q by 1 ; Update the result if t > r and is a palindrome ; Prleting the readonly result ; Driver Code
function isPalindrome ( x ) { let num = x . toString ( ) ; let result = true ; let i = 0 ; let j = num . length - 1 ; while ( i < j && result ) { result = num [ i ++ ] == num [ j -- ] ; } return result ; } function find ( nDigits ) { let lowerBound = Math . floor ( Math . pow ( 10 , nDigits - 1 ) ) ; let upperBound = ( lowerBound * 10 ) - 1 ; let resultP = 0 , resultQ = 0 , resultR = 0 ; for ( let p = upperBound ; p > lowerBound ; p -= 11 ) { while ( p % 11 != 0 ) { p -- ; } for ( let q = upperBound ; q > lowerBound ; q -- ) { let t = p * q ; if ( t > resultR && isPalindrome ( t ) ) { resultP = p ; resultQ = q ; resultR = t ; break ; } } } document . write ( resultR ) ; } let N = 2 ; find ( N ) ;
Minimize the cost of selecting two numbers whose product is X | Javascript implementation of the above approach ; max_prime [ i ] represents maximum prime number that divides the number i ; min_prime [ i ] represents minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; If i is a prime number , then both minimum and maximum prime numbers that divide the number is the number itself ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to minimize the cost of finding two numbers for every number such that the product of those two is equal to X ; Pre - calculation ; If X == 1 , then there is no way to find N and M . Prlet - 1 ; Case 3 is always valid and cost for that is C + X C for choosing 1 and M = X / 1 ; Case 1 N is prime , first number cost is fixed N is max_prime number divides this number ; If X is prime then the maximum prime number is the number itself . For this case , M becomes 1 and this shouldn 't be considered. ; Find M for this case ; Add cost for the second number also ; Update min_cost , if the cost for prime is minimum ; Case 2 If N is composite For this find the minimum prime number that divides A [ i ] and consider this as M ; Find N for that number ; Check if this number is composite or not if N is prime then there is no way to find any composite number that divides X If N = min_prime [ N ] then N is prime ; Update min_cost , if the cost for the composite is minimum ; Driver Code
let MAX = 1000 ; let max_prime = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; let min_prime = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; function sieve ( n ) { for ( let i = 2 ; i < n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; let j = i + i ; while ( j < n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } function findCost ( A , B , C , X ) { sieve ( MAX ) ; let N , M ; if ( X == 1 ) { return - 1 ; } let min_cost = C + X ; let cost_for_prime = A ; N = max_prime [ X ] ; if ( N != X ) { M = X / N ; cost_for_prime += M ; min_cost = Math . min ( min_cost , cost_for_prime ) ; } M = min_prime [ X ] ; N = X / M ; if ( N != min_prime [ N ] ) { let cost_for_comp = B + M ; min_cost = Math . min ( min_cost , cost_for_comp ) ; } return min_cost ; } let A = 7 , B = 11 , C = 2 , X = 20 ; document . write ( findCost ( A , B , C , X ) ) ;
Count the occurrence of digit K in a given number N using Recursion | Function to count the digit K in the given number N ; Extracting least significant digit ; Driver Code
function countdigits ( n , k ) { if ( n == 0 ) return 0 ; var digit = n % 10 ; if ( digit == k ) return 1 + countdigits ( n / 10 , k ) ; return countdigits ( n / 10 , k ) ; } var n = 1000 ; var k = 0 ; document . write ( countdigits ( n , k ) ) ;
Product of all Subarrays of an Array | Function to find product of all subarrays ; Variable to store the product ; Compute the product while traversing for subarrays ; Printing product of all subarray ; Driver code ; Function call
function product_subarrays ( arr , n ) { var res = 1 ; for ( var i = 0 ; i < n ; i ++ ) { var product = 1 ; for ( var j = i ; j < n ; j ++ ) { product = product * arr [ j ] ; res *= product ; } } document . write ( res ) ; } var arr = [ 10 , 3 , 7 ] ; var n = arr . length ; product_subarrays ( arr , n ) ;