codeparrot/xlcost-text-to-code huggingface dataset:
Number of digits in the product of two numbers | function to count number of digits in the product of two numbers ; if either of the number is 0 , then product will be 0 ; required count of digits ; Driver program to test above
function countDigits ( a , b ) { if ( a == 0 b == 0 ) return 1 ; return Math . floor ( Math . log10 ( Math . abs ( a ) ) + Math . log10 ( Math . abs ( b ) ) ) + 1 ; } let a = 33 ; let b = - 24 ; document . write ( countDigits ( a , b ) ) ;
Distributing M items in a circle of size N starting from K | n == > Size of circle m == > Number of items k == > Initial position ; n - k + 1 is number of positions before we reach beginning of circle . If m is less than this value , then we can simply return ( m - 1 ) th position ; Let us compute remaining items before we reach beginning . ; We compute m % n to skip all complete rounds . If we reach end , we return n else we return m % n ; Driver code
function lastPosition ( n , m , k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; } let n = 5 ; let m = 8 ; let k = 2 ; document . write ( lastPosition ( n , m , k ) ) ;
An interesting solution to get all prime numbers smaller than n | Javascript program to prints prime numbers smaller than n ; Compute factorials and apply Wilson 's  theorem. ; Driver code
function primesInRange ( n ) { let fact = 1 ; for ( let k = 2 ; k < n ; k ++ ) { fact = fact * ( k - 1 ) ; if ( ( fact + 1 ) % k == 0 ) document . write ( ( k + " " ) ) ; } } let n = 15 ; primesInRange ( n ) ;
A product array puzzle | Set 2 ( O ( 1 ) Space ) | epsilon value to maintain precision ; to hold sum of all values ; output product for each index anti log to find original product value ; Driver code
var EPS = 1e-9 ; function productPuzzle ( a , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += Math . log10 ( a [ i ] ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( parseInt ( ( EPS + Math . pow ( 10.00 , sum - Math . log10 ( a [ i ] ) ) ) ) + " " ) ; } var a = [ 10 , 3 , 5 , 6 , 2 ] ; var n = a . length ; document . write ( " " ) ; productPuzzle ( a , n ) ;
Change all even bits in a number to 0 | Returns modified number with all even bits 0. ; To store sum of bits at even positions . ; To store bits to shift ; One by one put all even bits to end ; If current last bit is set , add it to ans ; Next shift position ; Driver code
function changeEvenBits ( n ) { let to_subtract = 0 ; let m = 0 ; for ( x = n ; x ; x >>= 2 ) { if ( x & 1 ) to_subtract += ( 1 << m ) ; m += 2 ; } return n - to_subtract ; } n = 30 ; document . write ( changeEvenBits ( n ) ) ;
Find the number closest to n and divisible by m | function to find the number closest to n and divisible by m ; find the quotient ; 1 st possible closest number ; 2 nd possible closest number ; if true , then n1 is the required closest number ; else n2 is the required closest number ; Driver Code
function closestNumber ( n , m ) { let q = parseInt ( n / m ) ; let n1 = m * q ; let n2 = ( n * m ) > 0 ? ( m * ( q + 1 ) ) : ( m * ( q - 1 ) ) ; if ( Math . abs ( n - n1 ) < Math . abs ( n - n2 ) ) return n1 ; return n2 ; } let n = 13 ; let m = 4 ; document . write ( closestNumber ( n , m ) + " " ) ; n = - 15 ; m = 6 ; document . write ( closestNumber ( n , m ) + " " ) ; n = 0 ; m = 8 ; document . write ( closestNumber ( n , m ) + " " ) ; n = 18 ; m = - 7 ; document . write ( closestNumber ( n , m ) + " " ) ;
Check if a given number is Pronic | function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Printing Pronic Numbers upto 200
function checkPronic ( x ) { for ( var i = 0 ; i <= parseInt ( Math . sqrt ( x ) ) ; i ++ ) if ( x == i * ( i + 1 ) ) return true ; return false ; } for ( var i = 0 ; i <= 200 ; i ++ ) if ( checkPronic ( i ) ) document . write ( i + " " ) ;
Find minimum sum of factors of number | To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver Code
function findMinSum ( num ) { let sum = 0 ; for ( let i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { sum += i ; num /= i ; } } sum += num ; return sum ; } let num = 12 ; document . write ( findMinSum ( num ) ) ;
Minimum number with digits as 4 and 7 only and given sum | Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code
function findMin ( sum ) { var a = 0 , b = 0 ; while ( sum > 0 ) { if ( sum % 7 == 0 ) { b ++ ; sum -= 7 ; } else if ( sum % 4 == 0 ) { a ++ ; sum -= 4 ; } else { a ++ ; sum -= 4 ; } } if ( sum < 0 ) { document . write ( " " ) ; return ; } for ( i = 0 ; i < a ; i ++ ) document . write ( " " ) ; for ( i = 0 ; i < b ; i ++ ) document . write ( " " ) ; document . write ( ) ; } findMin ( 15 ) ;
Add minimum number to an array so that the sum becomes even | Function to find out minimum number ; Count odd number of terms in array ; driver program
function minNum ( arr , n ) { let odd = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 != 0 ) odd += 1 ; return ( ( odd % 2 ) != 0 ) ? 1 : 2 ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; let n = arr . length ; document . write ( minNum ( arr , n ) ) ;
Find maximum number that can be formed using digits of a given number | Function to print the maximum number ; hashed array to store count of digits ; Converting given number to string ; Updating the count array ; result is to store the final number ; Traversing the count array to calculate the maximum number ; return the result ; Driver program to test above function
function printMaxNum ( num ) { let count = new Array ( 10 ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } let str = num . toString ( ) ; for ( let i = 0 ; i < str . length ; i ++ ) count [ str [ i ] - ' ' ] ++ ; let result = 0 , multiplier = 1 ; for ( let i = 0 ; i <= 9 ; i ++ ) { while ( count [ i ] > 0 ) { result = result + ( i * multiplier ) ; count [ i ] -- ; multiplier = multiplier * 10 ; } } return result ; } let num = 38293367 ; document . write ( printMaxNum ( num ) ) ;
Form the largest number using at most one swap operation | function to form the largest number by applying atmost one swap operation ; for the rightmost digit , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element up to the left element ; if ' num [ i ] ' is less than the greatest digit encountered so far ; else ; there is no greater right digit for ' num [ i ] ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and break ; performing the required swap operation ; required largest number ; Driver program to test above
function largestNumber ( num ) { var n = num . length ; var rightMax = Array ( n ) , right ; rightMax [ n - 1 ] = - 1 ; right = n - 1 ; for ( var i = n - 2 ; i >= 0 ; i -- ) { if ( num [ i ] < num [ right ] ) rightMax [ i ] = right ; else { rightMax [ i ] = - 1 ; right = i ; } } for ( var i = 0 ; i < n ; i ++ ) { if ( rightMax [ i ] != - 1 ) { var tmp = num [ i ] ; num [ i ] = num [ rightMax [ i ] ] ; num [ rightMax [ i ] ] = tmp break ; } } return num . join ( ' ' ) ; } var num = " " . split ( ' ' ) ; document . write ( " " + largestNumber ( num ) ) ;
Binomial Random Variables | function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; driver program
function nCr ( n , r ) { if ( r > n / 2 ) r = n - r ; let answer = 1 ; for ( let i = 1 ; i <= r ; i ++ ) { answer *= ( n - r + i ) ; answer /= i ; } return answer ; } function binomialProbability ( n , k , p ) { return nCr ( n , k ) * Math . pow ( p , k ) * Math . pow ( 1 - p , n - k ) ; } let n = 10 ; let k = 5 ; let p = 1.0 / 3 ; let probability = binomialProbability ( n , k , p ) ; document . write ( " " + k ) ; document . write ( " " + n ) ; document . write ( " " + p ) ; document . write ( " " + probability ) ;
Find pair with maximum GCD in an array | function to find GCD of pair with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least 2 numbers , it is a GCD of at least 1 pair ; Array in which pair with max GCD is to be found ; Size of array
function findMaxGCD ( arr , n ) { var high = 0 ; for ( var i = 0 ; i < n ; i ++ ) high = Math . max ( high , arr [ i ] ) ; var divisors = Array . from ( { length : high + 1 } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 1 ; j <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % j == 0 ) { divisors [ j ] ++ ; if ( j != arr [ i ] / j ) divisors [ arr [ i ] / j ] ++ ; } } } for ( var i = high ; i >= 1 ; i -- ) if ( divisors [ i ] > 1 ) return i ; return 1 ; } var arr = [ 1 , 2 , 4 , 8 , 8 , 12 ] ; var n = arr . length ; document . write ( findMaxGCD ( arr , n ) ) ;
Find pair with maximum GCD in an array | function to find GCD of pair with max GCD in the array ; Calculating MAX in array ; Maintaining count array ; Variable to store the multiples of a number ; Iterating from MAX to 1 GCD is always between MAX and 1 The first GCD found will be the highest as we are decrementing the potential GCD ; Iterating from current potential GCD till it is less than MAX ; A multiple found ; Incrementing potential GCD by itself To check i , 2 i , 3 i ... . ; 2 multiples found , max GCD found ; Array in which pair with max GCD is to be found ; Size of array
function findMaxGCD ( arr , n ) { var high = 0 ; for ( let i = 0 ; i < n ; i ++ ) high = Math . max ( high , arr [ i ] ) ; var count = Array ( high + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; var counter = 0 ; for ( let i = high ; i >= 1 ; i -- ) { var j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 1 ; } var arr = [ 1 , 2 , 4 , 8 , 8 , 12 ] ; var n = arr . length ; document . write ( findMaxGCD ( arr , n ) ) ;
Evil Number | Returns number of 1 s from the binary number ; Counting 1 s ; Check if number is evil or not ; Converting n to binary form ; Calculating remainder ; Storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver code
function count_one ( n ) { let c_one = 0 ; while ( n != 0 ) { let rem = n % 10 ; if ( rem == 1 ) c_one = c_one + 1 ; n = parseInt ( n / 10 , 10 ) ; } return c_one ; } function checkEvil ( n ) { let i = 0 , bin = 0 , n_one = 0 ; while ( n != 0 ) { let r = n % 2 ; bin = bin + r * ( Math . pow ( 10 , i ) ) ; n = parseInt ( n / 2 , 10 ) ; } n_one = count_one ( bin ) ; if ( n_one % 2 == 0 ) return 1 ; else return 0 ; } let check , num ; num = 32 ; check = checkEvil ( num ) ; if ( check == 1 ) document . write ( num + " " ) ; else document . write ( num + " " ) ;
Count number of pairs ( A <= N , B <= N ) such that gcd ( A , B ) is B | returns number of valid pairs ; initialize k ; loop till imin <= n ; Initialize result ; max i with given k floor ( n / k ) ; adding k * ( number of i with floor ( n / i ) = k to ans ; set imin = imax + 1 and k = n / imin ; Driver function
function CountPairs ( n ) { let k = n ; let imin = 1 ; let ans = 0 ; while ( imin <= n ) { let imax = Math . floor ( n / k ) ; ans += k * ( imax - imin + 1 ) ; imin = imax + 1 ; k = Math . floor ( n / imin ) ; } return ans ; } document . write ( CountPairs ( 1 ) + " " ) ; document . write ( CountPairs ( 2 ) + " " ) ; document . write ( CountPairs ( 3 ) + " " ) ;
Sum of all proper divisors of natural numbers in an array | JavaScript program to find sum of proper divisors for every element in an array . ; To store prime factors and their powers ; Fills factors such that factors [ i ] is a vector of pairs containing prime factors ( of i ) and their powers . Also sets values in isPrime [ ] ; To check if a number is prime ; If i is prime , then update its powers in all multiples of it . ; Returns sum of proper divisors of num using factors [ ] ; Applying above discussed formula for every array element ; Driver code
let MAX = 100001 ; class pair { constructor ( f , s ) { this . F = f ; this . S = s ; } } let factors = new Array ( MAX ) ; function sieveOfEratothenese ( ) { let isPrime = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( let i = 2 ; i < MAX ; i ++ ) { if ( isPrime [ i ] ) { for ( let j = i ; j < MAX ; j += i ) { let k , l ; isPrime [ j ] = false ; for ( k = j , l = 0 ; k % i == 0 ; l ++ , k = Math . floor ( k / i ) ) ; factors [ j ] . push ( new pair ( i , l ) ) ; } } } } function sumOfProperDivisors ( num ) { let mul = 1 ; for ( let i = 0 ; i < factors [ num ] . length ; i ++ ) mul *= Math . floor ( ( Math . pow ( factors [ num ] [ i ] . F , factors [ num ] [ i ] . S + 1 ) - 1 ) / ( factors [ num ] [ i ] . F - 1 ) ) ; return mul - num ; } for ( let i = 0 ; i < MAX ; i ++ ) factors [ i ] = [ ] ; sieveOfEratothenese ( ) ; let arr = [ 8 , 13 , 24 , 36 , 59 , 75 , 91 ] ; for ( let i = 0 ; i < arr . length ; i ++ ) document . write ( sumOfProperDivisors ( arr [ i ] ) + " " ) ; document . write ( " " ) ;
Finding power of prime number p in n ! | Returns power of p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; Driver function
function PowerOFPINnfactorial ( n , p ) { let ans = 0 ; let temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } document . write ( PowerOFPINnfactorial ( 4 , 2 ) ) ;
Program for Decimal to Binary Conversion | Function to return the binary equivalent of decimal value N ; To store the binary number ; Count used to store exponent value ; Driver code
function decimalToBinary ( N ) { var B_Number = 0 ; var cnt = 0 ; while ( N != 0 ) { var rem = N % 2 ; var c = Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N = parseInt ( N / 2 ) ; cnt ++ ; } return B_Number ; } var N = 17 ; document . write ( decimalToBinary ( N ) ) ;
Program for Binary To Decimal Conversion | Function to convert binary to decimal ; Initializing base value to 1 , i . e 2 ^ 0 ; Driver program to test above function
function binaryToDecimal ( n ) { let num = n ; let dec_value = 0 ; let base = 1 ; let temp = num ; while ( temp ) { let last_digit = temp % 10 ; temp = Math . floor ( temp / 10 ) ; dec_value += last_digit * base ; base = base * 2 ; } return dec_value ; } let num = 10101001 ; document . write ( binaryToDecimal ( num ) + " " ) ;
Calculating Factorials using Stirling Approximation | Function for calculating factorial ; value of natural e ; evaluating factorial using stirling approximation ; Driver Code
function stirlingFactorial ( n ) { if ( n == 1 ) return 1 ; let z ; let e = 2.71 ; z = Math . sqrt ( 2 * 3.14 * n ) * Math . pow ( ( n / e ) , n ) ; return Math . floor ( z ) ; } document . write ( stirlingFactorial ( 1 ) + " " ) ; document . write ( stirlingFactorial ( 2 ) + " " ) ; document . write ( stirlingFactorial ( 3 ) + " " ) ; document . write ( stirlingFactorial ( 4 ) + " " ) ; document . write ( stirlingFactorial ( 5 ) + " " ) ; document . write ( stirlingFactorial ( 6 ) + " " ) ; document . write ( stirlingFactorial ( 7 ) + " " ) ;
Count pairs with Odd XOR | A function will return number of pair whose XOR is odd ; To store count of odd and even numbers ; Increase even if number is even otherwise increase odd ; Return number of pairs ; Driver program to test countXorPair ( )
function countXorPair ( arr , n ) { let odd = 0 , even = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return odd * even ; } let arr = [ 1 , 2 , 3 ] ; document . write ( countXorPair ( arr , arr . length ) ) ;
Lychrel Number Implementation | Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; driver program
var MAX_ITERATIONS = 20 ; function isLychrel ( number ) { for ( var i = 0 ; i < MAX_ITERATIONS ; i ++ ) { number = number + reverse ( number ) ; if ( isPalindrome ( number ) ) return false ; } return true ; } function isPalindrome ( number ) { return number == reverse ( number ) ; } function reverse ( number ) { var reverse = 0 ; while ( number > 0 ) { var remainder = number % 10 ; reverse = ( reverse * 10 ) + remainder ; number = parseInt ( number / 10 ) ; } return reverse ; } var number = 295 ; document . write ( number + " " + isLychrel ( number ) ) ;
Rectangular ( or Pronic ) Numbers | Returns n - th rectangular number ; Driver code
function findRectNum ( n ) { return n * ( n + 1 ) ; } var n = 6 ; document . write ( findRectNum ( n ) ) ;
Program for Muller Method | JavaScript Program to find root of a function , f ( x ) ; Function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver main function
const MAX_ITERATIONS = 10000 ; function f ( x ) { return 1 * Math . pow ( x , 3 ) + 2 * x * x + 10 * x - 20 ; } function Muller ( a , b , c ) { let i ; let res ; for ( i = 0 ; ; ++ i ) { let f1 = f ( a ) ; let f2 = f ( b ) ; let f3 = f ( c ) ; let d1 = f1 - f3 ; let d2 = f2 - f3 ; let h1 = a - c ; let h2 = b - c ; let a0 = f3 ; let a1 = ( ( ( d2 * Math . pow ( h1 , 2 ) ) - ( d1 * Math . pow ( h2 , 2 ) ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; let a2 = ( ( ( d1 * h2 ) - ( d2 * h1 ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; let x = ( ( - 2 * a0 ) / ( a1 + Math . abs ( Math . sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; let y = ( ( - 2 * a0 ) / ( a1 - Math . abs ( Math . sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; if ( x >= y ) res = x + c ; else res = y + c ; let m = res * 100 ; let n = c * 100 ; m = Math . floor ( m ) ; n = Math . floor ( n ) ; if ( m == n ) break ; a = b ; b = c ; c = res ; if ( i > MAX_ITERATIONS ) { document . write ( " " + " " ) ; break ; } } if ( i <= MAX_ITERATIONS ) document . write ( " " + res . toFixed ( 4 ) ) ; } let a = 0 , b = 1 , c = 2 ; Muller ( a , b , c ) ;
Finding n | Prints n - th number where each digit is a prime number ; Finding the length of n - th number ; Count of numbers with len - 1 digits ; Count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; Finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else prvar the ith digit and break ; Driver method
function nthprimedigitsnumber ( n ) { var len = 1 ; var prev_count = 0 ; while ( true ) { var curr_count = ( prev_count + Math . pow ( 4 , len ) ) ; if ( prev_count < n && curr_count >= n ) break ; len ++ ; prev_count = curr_count ; } for ( var i = 1 ; i <= len ; i ++ ) { for ( var j = 1 ; j <= 4 ; j ++ ) { if ( prev_count + Math . pow ( 4 , len - i ) < n ) prev_count += Math . pow ( 4 , len - i ) ; else { if ( j == 1 ) document . write ( " " ) ; else if ( j == 2 ) document . write ( " " ) ; else if ( j == 3 ) document . write ( " " ) ; else if ( j == 4 ) document . write ( " " ) ; break ; } } } document . write ( ' ' ) ; } nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ;
Find if a number is divisible by every number in a list | Function which check is a number divided with every element in list or not ; driver program
function findNoIsDivisibleOrNot ( a , n , l ) { for ( let i = 0 ; i < l ; i ++ ) { if ( a [ i ] % n != 0 ) return false ; } return true ; } let a = [ 14 , 12 , 4 , 18 ] ; let n = 2 ; let l = a . length ; if ( findNoIsDivisibleOrNot ( a , n , l ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find a range of composite numbers of given length | Method to find factorial of given number ; To print range of length n having all composite integers ; Driver Code
function factorial ( n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } function printRange ( n ) { let a = factorial ( n + 2 ) + 2 ; let b = a + n - 1 ; document . write ( ` ${ a } ${ b } ` ) ; return 0 ; } let n = 3 ; printRange ( n ) ;
Find minimum value to assign all array elements so that array product becomes greater | Javascript program to find minimum value that can be assigned to along elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val let val = 0 ; where double is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code
function findMinValue ( arr , n ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) { val += ( Math . log ( ( arr [ i ] ) ) ) ; } let left = arr [ 0 ] , right = arr [ n - 1 ] ; let ans = 0 ; while ( left <= right ) { let mid = Math . floor ( ( left + right ) / 2 ) ; let temp = n * ( Math . log ( ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else { left = mid + 1 ; } } return ans ; } let arr = [ 4 , 2 , 1 , 10 , 6 ] ; let n = arr . length ; document . write ( findMinValue ( arr , n ) ) ;
Find the sum of all the terms in the n | function to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; Driver program to test above
function sumOfTermsInNthRow ( n ) { let sum = n * ( 2 * Math . pow ( n , 2 ) + 1 ) ; return sum ; } let n = 4 ; document . write ( " " + sumOfTermsInNthRow ( n ) ) ;
First digit in product of an array of numbers | Javascript implementation to find first digit of a single number ; Keep dividing by 10 until it is greater than equal to 10 ; Driver Code
function firstDigit ( x ) { while ( x >= 10 ) x = x / 10 ; return Math . floor ( x ) ; } document . write ( firstDigit ( 12345 ) + " " ) ; document . write ( firstDigit ( 5432 ) ) ;
Find the occurrences of digit d in the range [ 0. . n ] | Javascript program to count appearances of a digit ' d ' in range from [ 0. . n ] ; Initialize result ; Count appearances in numbers starting from d . ; When the last digit is equal to d ; When the first digit is equal to d then ; increment result as well as number ; In case of reverse of number such as 12 and 21 ; Driver code
function getOccurence ( n , d ) { let result = 0 ; let itr = d ; while ( itr <= n ) { if ( itr % 10 == d ) result ++ ; if ( itr != 0 && Math . floor ( itr / 10 ) == d ) { result ++ ; itr ++ ; } else if ( Math . floor ( itr / 10 ) == d - 1 ) itr = itr + ( 10 - d ) ; else itr = itr + 10 ; } return result ; } let n = 11 ; let d = 1 ; document . write ( getOccurence ( n , d ) ) ;
Numbers having Unique ( or Distinct ) digits | Function to print unique numbers ; Iterate from l to r ; Convert the no . to string ; Convert string to set using stl ; Output if condition satisfies ; Input of the lower and higher limits ; Function Call
function printUnique ( l , r ) { for ( let i = l ; i <= r ; i ++ ) { let s = String ( i ) ; let uniDigits = new Set ( ) ; for ( let c of s . split ( " " ) ) uniDigits . add ( c ) ; if ( s . length == uniDigits . size ) { document . write ( i + " " ) ; } } } let l = 1 , r = 20 ; printUnique ( l , r ) ;