codeparrot/xlcost-text-to-code huggingface dataset:
Smallest multiple of 3 which consists of three given non | Function to return the minimum number divisible by 3 formed by the given digits ; Sort the given array in ascending ; Check if any single digit is divisible by 3 ; Check if any two digit number formed by the given digits is divisible by 3 starting from the minimum ; Generate the two digit number ; If none of the above is true , we can form three digit number by taking a [ 0 ] three times . ; Driver code
function printSmallest ( a ) { a . sort ( function ( a , b ) { return a - b } ) ; let i , j , num ; for ( i = 0 ; i < 3 ; i ++ ) { if ( a [ i ] % 3 == 0 ) { return a [ i ] ; } } for ( i = 0 ; i < 3 ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { num = ( a [ i ] * 10 ) + a [ j ] ; if ( num % 3 == 0 ) { return num ; } } } return a [ 0 ] * 100 + a [ 0 ] * 10 + a [ 0 ] ; } let arr = [ 7 , 7 , 1 ] ; document . write ( printSmallest ( arr ) ) ;
Print matrix after applying increment operations in M ranges | Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Size of the matrix ; Queries
function updateMatrix ( n , q , mat ) { let i , j ; for ( i = 0 ; i < q . length ; i ++ ) { let X1 = q [ i ] [ 0 ] ; let Y1 = q [ i ] [ 1 ] ; let X2 = q [ i ] [ 2 ] ; let Y2 = q [ i ] [ 3 ] ; mat [ X1 ] [ Y1 ] ++ ; if ( Y2 + 1 < n ) mat [ X2 ] [ Y2 + 1 ] -- ; else if ( X2 + 1 < n ) mat [ X2 + 1 ] [ 0 ] -- ; } let sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { sum += mat [ i ] [ j ] ; document . write ( sum + " " ) ; } document . write ( " " ) ; } } let n = 5 ; let mat = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { mat [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { mat [ i ] [ j ] = 0 ; } } let q = [ [ 0 , 0 , 1 , 2 ] , [ 1 , 2 , 3 , 4 ] , [ 1 , 4 , 3 , 4 ] ] ; updateMatrix ( n , q , mat ) ;
Replace the maximum element in the array by coefficient of range | Utility function to print the contents of the array ; Function to replace the maximum element from the array with the coefficient of range of the array ; Maximum element from the array ; Minimum element from the arra ; Calculate the coefficient of range for the array ; Assuming all the array elements are distinc Replace the maximum element with the coefficient of range of the array ; Print the updated array ; Driver code
function printArr ( arr , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( arr [ n - 1 ] . toFixed ( 6 ) ) ; } function replaceMax ( arr , n ) { let max = arr [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } let min = arr [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } let range = max - min ; let coeffOfRange = range / ( max + min ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == max ) { arr [ i ] = coeffOfRange ; break ; } } printArr ( arr , n ) ; } let arr = [ 15 , 16 , 10 , 9 , 6 , 7 , 17 ] ; let n = arr . length ; replaceMax ( arr , n ) ;
Divide the two given numbers by their common divisors | print the numbers after dividing them by their common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; divide A and B by their common factors
function divide ( a , b ) { for ( let i = 2 ; i <= Math . min ( a , b ) ; i ++ ) { while ( a % i == 0 && b % i == 0 ) { a = a / i ; b = b / i ; } } document . write ( " " + a + " " + b + " " ) ; } let A = 10 , B = 15 ; divide ( A , B ) ;
Divide the two given numbers by their common divisors | Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function commDiv ( a , b ) { let n = gcd ( a , b ) ; a = parseInt ( a / n , 10 ) ; b = parseInt ( b / n , 10 ) ; document . write ( " " + a + " " + b ) ; } let a = 10 , b = 15 ; commDiv ( a , b ) ;
Minimum absolute difference between N and a power of 2 | Function to return the minimum difference between N and a power of 2 ; Power of 2 closest to n on its left ; Power of 2 closest to n on its right ; Return the minimum abs difference ; Driver code
function minAbsDiff ( n ) { let left = 1 << ( Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ) ; let right = left * 2 ; return Math . min ( ( n - left ) , ( right - n ) ) ; } let n = 15 ; document . write ( minAbsDiff ( n ) ) ;
Find probability that a player wins when probabilities of hitting the target are given | Function to return the probability of the winner ; Driver Code ; Will print 9 digits after the decimal point
function find_probability ( p , q , r , s ) { var t = ( 1 - p / q ) * ( 1 - r / s ) ; var ans = ( p / q ) / ( 1 - t ) ; return ans ; } var p = 1 , q = 2 , r = 1 , s = 2 ; document . write ( find_probability ( p , q , r , s ) . toFixed ( 9 ) ) ;
Represent n as the sum of exactly k powers of two | Set 2 | Function to print k numbers which are powers of two and whose sum is equal to n ; Initialising the sum with k ; Initialising an array A with k elements and filling all elements with 1 ; Iterating A [ ] from k - 1 to 0 ; Update sum and A [ i ] till sum + A [ i ] is less than equal to n ; Impossible to find the combination ; Possible solution is stored in A [ ] ; Driver Code
function FindAllElements ( n , k ) { let sum = k ; let A = new Array ( k ) . fill ( 1 ) ; for ( let i = k - 1 ; i >= 0 ; -- i ) { while ( sum + A [ i ] <= n ) { sum += A [ i ] ; A [ i ] *= 2 ; } } if ( sum != n ) { document . write ( " " ) ; } else { for ( let i = 0 ; i < k ; ++ i ) document . write ( A [ i ] + " " ) ; } } let n = 12 ; let k = 6 ; FindAllElements ( n , k ) ;
Check whether a + b = c or not after removing all zeroes from a , b and c | Function to remove zeroes ; Initialize result to zero holds the Result after removing zeroes from no ; Initialize variable d to 1 that holds digits of no ; Loop while n is greater then zero ; Check if n mod 10 is not equal to zero ; store the result by removing zeroes And increment d by 10 ; Go to the next digit ; Return the result ; Function to check if sum is true after Removing all zeroes . ; Call removeZero ( ) for both sides and check whether they are equal After removing zeroes . ; Driver Code
function removeZero ( n ) { let res = 0 ; let d = 1 ; while ( n > 0 ) { if ( n % 10 != 0 ) { res += ( n % 10 ) * d ; d *= 10 ; } n = Math . floor ( n / 10 ) ; } return res ; } function isEqual ( a , b ) { if ( removeZero ( a ) + removeZero ( b ) == removeZero ( a + b ) ) return true ; return false ; } let a = 105 , b = 106 ; if ( isEqual ( a , b ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
A Sum Array Puzzle | JavaScript implementation of above approach ; Allocate memory for temporary arrays leftSum [ ] , rightSum [ ] and Sum [ ] ; Left most element of left array is always 0 ; Right most element of right array is always 0 ; Construct the left array ; Construct the right array ; Construct the sum array using left [ ] and right [ ] ; print the sum array ; Driver Code
function sumArray ( arr , n ) { let leftSum = new Array ( n ) ; let rightSum = new Array ( n ) ; let Sum = new Array ( n ) ; let i = 0 , j = 0 ; leftSum [ 0 ] = 0 ; rightSum [ n - 1 ] = 0 ; for ( i = 1 ; i < n ; i ++ ) leftSum [ i ] = arr [ i - 1 ] + leftSum [ i - 1 ] ; for ( j = n - 2 ; j >= 0 ; j -- ) rightSum [ j ] = arr [ j + 1 ] + rightSum [ j + 1 ] ; for ( i = 0 ; i < n ; i ++ ) Sum [ i ] = leftSum [ i ] + rightSum [ i ] ; for ( i = 0 ; i < n ; i ++ ) document . write ( Sum [ i ] + " " ) ; } let arr = [ 3 , 6 , 4 , 8 , 9 ] ; let n = arr . length ; sumArray ( arr , n ) ;
Find minimum x such that ( x % k ) * ( x / k ) == n | Set | This function gives the required answer ; Iterate for all the factors ; Check if i is a factor ; Consider i to be A and n / i to be B ; Consider i to be B and n / i to be A ; Driver Code
function minimumX ( n , k ) { let mini = Number . MAX_VALUE ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { let fir = i ; let sec = parseInt ( n / i , 10 ) ; let num1 = fir * k + sec ; let res = parseInt ( ( num1 / k ) , 10 ) * ( num1 % k ) ; if ( res == n ) mini = Math . min ( num1 , mini ) ; let num2 = sec * k + fir ; res = parseInt ( ( num2 / k ) , 10 ) * ( num2 % k ) ; if ( res == n ) mini = Math . min ( num2 , mini ) ; } } return mini ; } let n = 4 , k = 6 ; document . write ( minimumX ( n , k ) + " " ) ; n = 5 , k = 5 ; document . write ( minimumX ( n , k ) ) ;
Find minimum x such that ( x % k ) * ( x / k ) == n | This function gives the required answer ; Iterate over all possible remainders ; it must divide n ; Driver code to test above function
function minimumX ( n , k ) { let ans = Number . MAX_VALUE ; for ( let rem = k - 1 ; rem > 0 ; rem -- ) { if ( n % rem == 0 ) ans = Math . min ( ans , rem + ( n / rem ) * k ) ; } return ans ; } let n = 4 , k = 6 ; document . write ( minimumX ( n , k ) + " " ) ; n = 5 , k = 5 ; document . write ( minimumX ( n , k ) ) ;
Find nth Hermite number | Function to return nth Hermite number ; Base condition ; Driver Code ; Print nth Hermite number
function getHermiteNumber ( n ) { if ( n == 0 ) return 1 ; else if ( n == 1 ) return 1 ; else return - 2 * ( n - 1 ) * getHermiteNumber ( n - 2 ) ; } let n = 6 ; document . write ( getHermiteNumber ( n ) ) ;
Find numbers a and b that satisfy the given conditions | Function to print the required numbers ; Suppose b = n and we want a % b = 0 and also ( a / b ) < n so a = b * ( n - 1 ) ; Special case if n = 1 we get a = 0 so ( a * b ) < n ; If no pair satisfies the conditions ; Driver code
function find ( n ) { let b = n ; let a = b * ( n - 1 ) ; if ( a * b > n && a / b < n ) { document . write ( " " + a + " " + b ) ; } else document . write ( - 1 ) ; } let n = 10 ; find ( n ) ;
Closest perfect square and its distance | Function to check if a number is perfect square or not ; Function to find the closest perfect square taking minimum steps to reach from a number ; Variables to store first perfect square number above and below N ; Finding first perfect square number greater than N ; Finding first perfect square number less than N ; Variables to store the differences ; Driver code
function isPerfect ( N ) { if ( ( Math . sqrt ( N ) - Math . floor ( Math . sqrt ( N ) ) ) != 0 ) return false ; return true ; } function getClosestPerfectSquare ( N ) { if ( isPerfect ( N ) ) { document . write ( N + " " + " " + " " ) ; return ; } let aboveN = - 1 , belowN = - 1 ; let n1 ; n1 = N + 1 ; while ( true ) { if ( isPerfect ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPerfect ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } let diff1 = aboveN - N ; let diff2 = N - belowN ; if ( diff1 > diff2 ) document . write ( belowN + " " + diff2 ) ; else document . write ( aboveN + " " + diff1 ) ; } let N = 1500 ; getClosestPerfectSquare ( N ) ;
Fraction | Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's  simplest form ; Driver Code
function gcd ( a , b ) { if ( a === 0 ) return b ; return gcd ( b % a , a ) ; } function lowest ( den3 , num3 ) { var common_factor = gcd ( num3 , den3 ) ; den3 = parseInt ( den3 / common_factor ) ; num3 = parseInt ( num3 / common_factor ) ; return [ den3 , num3 ] ; } function addFraction ( num1 , den1 , num2 , den2 , num3 , den3 ) { den3 = gcd ( den1 , den2 ) ; den3 = ( den1 * den2 ) / den3 ; num3 = num1 * ( den3 / den1 ) + num2 * ( den3 / den2 ) ; return lowest ( den3 , num3 ) ; } var num1 = 1 , den1 = 500 , num2 = 2 , den2 = 1500 , den3 , num3 ; var [ den3 , num3 ] = addFraction ( num1 , den1 , num2 , den2 , num3 , den3 ) ; document . write ( num1 + " " + den1 + " " + num2 + " " + den2 + " " + num3 + " " + den3 + " " ) ;
Largest Divisor of a Number not divisible by a perfect square | Function to find the largest divisor not divisible by any perfect square greater than 1 ; If the number is divisible by i * i , then remove one i ; Now all squares are removed from n ; Driver Code
function findLargestDivisor ( n ) { for ( let i = 2 ; i < Math . sqrt ( n ) + 1 ; i ++ ) { while ( n % ( i * i ) == 0 ) { n = n / i ; } } return n ; } let n = 12 ; document . write ( findLargestDivisor ( n ) + " " ) ; n = 97 ; document . write ( findLargestDivisor ( n ) + " " ) ;
Arithmetic Progression | Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression ; Sort array ; After sorting , difference between consecutive elements must be same . ; Driven Program
function compare ( a , b ) { if ( a < b ) { return - 1 ; } else if ( a > b ) { return 1 ; } else { return 0 ; } } function checkIsAP ( arr , n ) { if ( n == 1 ) return true ; arr . sort ( compare ) ; let d = arr [ 1 ] - arr [ 0 ] ; for ( let i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } let arr = [ 20 , 15 , 5 , 0 , 10 ] ; let n = arr . length ; ( checkIsAP ( arr , n ) ) ? document . write ( " " ) : document . write ( " " ) ;
Check if a number is Triperfect Number | Returns true if n is Triperfect ; To store sum of divisors . Adding 1 and n since they are divisors of n . ; Find all divisors and add them ; If sum of divisors is equal to 3 * n , then n is a Triperfect number ; Driver Code
function isTriPerfect ( n ) { let sum = 1 + n ; let i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) { if ( parseInt ( n / i , 10 ) == i ) sum = sum + i ; else sum = sum + i + parseInt ( n / i , 10 ) ; } i += 1 ; } if ( sum == 3 * n & n != 1 ) return true ; else return false ; } let n = 120 ; if ( isTriPerfect ( n ) ) document . write ( n + " " ) ;
Sum of first N natural numbers which are divisible by X or Y | Function to calculate the sum of numbers divisible by X or Y ; Driver code
function sum ( N , X , Y ) { var S1 , S2 , S3 ; S1 = ( parseInt ( N / X ) ) * ( 2 * X + parseInt ( N / X - 1 ) * X ) / 2 ; S2 = ( parseInt ( N / Y ) ) * ( 2 * Y + parseInt ( N / Y - 1 ) * Y ) / 2 ; S3 = ( parseInt ( N / ( X * Y ) ) ) * ( 2 * ( X * Y ) + parseInt ( N / ( X * Y ) - 1 ) * ( X * Y ) ) / 2 ; return S1 + S2 - S3 ; } var N = 14 ; var X = 3 , Y = 5 ; document . write ( sum ( N , X , Y ) ) ;
Count numbers from range whose prime factors are only 2 and 3 | Function to count the number within a range whose prime factors are only 2 and 3 ; Start with 2 so that 1 doesn 't get counted ; While num is divisible by 2 , divide it by 2 ; While num is divisible by 3 , divide it by 3 ; If num got reduced to 1 then it has only 2 and 3 as prime factors ; Driver code
function findTwoThreePrime ( l , r ) { if ( l == 1 ) l ++ ; let count = 0 ; for ( let i = l ; i <= r ; i ++ ) { let num = i ; while ( num % 2 == 0 ) num = parseInt ( num / 2 , 10 ) ; while ( num % 3 == 0 ) num = parseInt ( num / 3 , 10 ) ; if ( num == 1 ) count ++ ; } return count ; } let l = 1 , r = 10 ; document . write ( findTwoThreePrime ( l , r ) ) ;
Maximum number with same digit factorial product | Function to return the required number ; Count the frequency of each digit ; 4 ! can be expressed as 2 ! * 2 ! * 3 ! ; 6 ! can be expressed as 5 ! * 3 ! ; 8 ! can be expressed as 7 ! * 2 ! * 2 ! * 2 ! ; 9 ! can be expressed as 7 ! * 3 ! * 3 ! * 2 ! ; To store the required number ; If number has only either 1 and 0 as its digits ; Generate the greatest number possible ; Driver code
function getNumber ( s ) { let number_of_digits = s . length ; let freq = new Array ( 10 ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < number_of_digits ; i ++ ) { if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) { freq [ s [ i ] . charCodeAt ( ) - 48 ] += 1 ; } if ( s [ i ] == ' ' ) { freq [ 2 ] += 2 ; freq [ 3 ] ++ ; } if ( s [ i ] == ' ' ) { freq [ 5 ] ++ ; freq [ 3 ] ++ ; } if ( s [ i ] == ' ' ) { freq [ 7 ] ++ ; freq [ 2 ] += 3 ; } if ( s [ i ] == ' ' ) { freq [ 7 ] ++ ; freq [ 3 ] += 2 ; freq [ 2 ] ++ ; } } let t = " " ; if ( freq [ 1 ] == number_of_digits || freq [ 0 ] == number_of_digits || ( freq [ 0 ] + freq [ 1 ] ) == number_of_digits ) { return s ; } else { for ( let i = 9 ; i >= 2 ; i -- ) { let ctr = freq [ i ] ; while ( ( ctr -- ) > 0 ) { t += String . fromCharCode ( i + 48 ) ; } } return t ; } } let s = " " ; document . write ( getNumber ( s ) ) ;
Program to find first N Iccanobif Numbers | Iterative function to reverse digits of num ; Function to print first N Icanobif Numbers ; Initialize first , second numbers ; Print first two numbers ; Reversing digit of previous two terms and adding them ; Driver Code
function reversDigits ( num ) { let rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = parseInt ( num / 10 , 10 ) ; } return rev_num ; } function icanobifNumbers ( N ) { let first = 0 , second = 1 ; if ( N == 1 ) document . write ( first ) ; else if ( N == 2 ) document . write ( first + " " + second ) ; else { document . write ( first + " " + second + " " ) ; for ( let i = 3 ; i <= N ; i ++ ) { let x = reversDigits ( first ) ; let y = reversDigits ( second ) ; document . write ( x + y + " " ) ; let temp = second ; second = x + y ; first = temp ; } } } let N = 12 ; icanobifNumbers ( N ) ;
Add N digits to A such that it is divisible by B after each addition | Javascript implementation of the approach ; Try all digits from ( 0 to 9 ) ; Fails in the first move itself ; Add ( n - 1 ) 0 's ; Driver Code
function addNDigits ( a , b , n ) { let num = a ; for ( let i = 0 ; i <= 9 ; i ++ ) { let tmp = a * 10 + i ; if ( tmp % b == 0 ) { a = tmp ; break ; } } if ( num == a ) return - 1 ; for ( let j = 0 ; j < n - 1 ; j ++ ) a *= 10 ; return a ; } let a = 5 , b = 3 , n = 3 ; document . write ( addNDigits ( a , b , n ) ) ;
Count number of triplets ( a , b , c ) such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n | function to ind number of Triplets 1 <= a <= b <= c <= n , Such that a ^ 2 + b ^ 2 = c ^ 2 ; to store required answer ; run nested loops for first two numbers . ; third number ; check if third number is perfect square and less than n ; Driver code ; function call
function Triplets ( n ) { var ans = 0 ; for ( let i = 1 ; i <= n ; ++ i ) { for ( let j = i ; j <= n ; ++ j ) { var x = i * i + j * j ; var y = parseInt ( Math . sqrt ( x ) ) ; if ( y * y == x && y <= n ) ++ ans ; } } return ans ; } var n = 10 ; document . write ( Triplets ( n ) ) ;
Sum of the digits of a number N written in all bases from 2 to N / 2 | Function to calculate the sum of the digits of n in the given base ; Sum of digits ; Digit of n in the given base ; Add the digit ; Function to calculate the sum of digits of n in bases from 2 to n / 2 ; to store digit sum in all bases ; function call for multiple bases ; Driver program
function solve ( n , base ) { var sum = 0 ; while ( n > 0 ) { var remainder = n % base ; sum += remainder ; n = parseInt ( n / base ) ; } return sum ; } function SumsOfDigits ( n ) { var sum = 0 ; for ( base = 2 ; base <= n / 2 ; ++ base ) sum += solve ( n , base ) ; document . write ( sum ) ; } var n = 8 ; SumsOfDigits ( n ) ;
Largest number in an array that is not a perfect cube | Function to check if a number is perfect cube number or not ; takes the sqrt of the number ; checks if it is a perfect cube number ; Function to find the largest non perfect cube number in the array ; stores the maximum of all perfect cube numbers ; Traverse all elements in the array ; store the maximum if current element is a non perfect cube ; Driver Code
function checkPerfectcube ( n ) { let d = Math . cbrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } function largestNonPerfectcubeNumber ( a , n ) { let maxi = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! checkPerfectcube ( a [ i ] ) ) maxi = Math . max ( a [ i ] , maxi ) ; } return maxi ; } let a = [ 16 , 64 , 25 , 2 , 3 , 10 ] ; let n = a . length ; document . write ( largestNonPerfectcubeNumber ( a , n ) ) ;
Check if N can be represented as sum of integers chosen from set { A , B } | Function to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; Base condition ; If x is already visited ; Set x as possible ; Recursive call ; Driver code
function checkIfPossibleRec ( x , a , b , isPossible , n ) { if ( x > n ) return ; if ( isPossible [ x ] ) return ; isPossible [ x ] = true ; checkIfPossibleRec ( x + a , a , b , isPossible , n ) ; checkIfPossibleRec ( x + b , a , b , isPossible , n ) ; } function checkPossible ( n , a , b ) { var isPossible = Array ( n + 1 ) . fill ( false ) ; checkIfPossibleRec ( 0 , a , b , isPossible , n ) ; return isPossible [ n ] ; } var a = 3 , b = 7 , n = 8 ; if ( checkPossible ( a , b , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of all odd natural numbers in range L and R | Function to return the sum of all odd natural numbers ; Function to return the sum of all odd numbers in range L and R ; Driver Code
function sumOdd ( n ) { terms = ( n + 1 ) / 2 ; sum = terms * terms ; return sum ; } function suminRange ( l , r ) { return sumOdd ( r ) - sumOdd ( l - 1 ) ; } let l = 2 ; let r = 5 ; document . write ( " " + suminRange ( l , r ) ) ;
Sum of common divisors of two numbers A and B | Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Find the sum of divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver program to run the case
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function sumcommDiv ( a , b ) { var n = gcd ( a , b ) ; var sum = 0 ; for ( var i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum += i ; else sum += ( n / i ) + i ; } } return sum ; } var a = 10 , b = 15 ; document . write ( " " + sumcommDiv ( a , b ) ) ;
Check if a number is formed by Concatenation of 1 , 14 or 144 only | Function to check if a number is formed by Concatenation of 1 , 14 or 144 only ; check for each possible digit if given number consist other then 1 , 14 , 144 print NO else print YES ; Driver Code
function checkNumber ( N ) { let temp = N ; while ( temp > 0 ) { if ( temp % 1000 == 144 ) temp = parseInt ( temp / 1000 , 10 ) ; else if ( temp % 100 == 14 ) temp = parseInt ( temp / 100 , 10 ) ; else if ( temp % 10 == 1 ) temp = parseInt ( temp / 10 , 10 ) ; else { return " " ; } } return " " ; } let N = 1414 ; document . write ( checkNumber ( N ) ) ;
Fibonacci problem ( Value of Fib ( N ) * Fib ( N ) | Javascript implementation of the approach ; Driver code
function getResult ( n ) { if ( ( n & 1 ) > 0 ) return 1 ; return - 1 ; } let n = 3 ; document . write ( getResult ( n ) ) ;
Find two numbers with sum and product both same as N | Function to return the smallest string ; Not possible ; find a and b ; Driver Code
function findAandB ( N ) { let val = N * N - 4.0 * N ; if ( val < 0 ) { document . write ( " " ) ; return ; } let a = ( N + Math . sqrt ( val ) ) / 2.0 ; let b = ( N - Math . sqrt ( val ) ) / 2.0 ; document . write ( " " + a . toFixed ( 4 ) + " " ) ; document . write ( " " + b . toFixed ( 5 ) ) ; } let N = 69.0 ; findAandB ( N ) ;
Find minimum operations needed to make an Array beautiful | Function to find minimum operations required to make array beautiful ; counting consecutive zeros . ; check that start and end are same ; check is zero and one are equal ; Driver program
function minOperations ( A , n ) { if ( ( n & 1 ) > 0 ) return - 1 ; let zeros = 0 , consZeros = 0 , ones = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( A [ i ] == 0 ) zeros ++ ; else ones ++ ; if ( i + 1 < n ) { if ( A [ i ] == 0 && A [ i + 1 ] == 0 ) consZeros ++ ; } } if ( A [ 0 ] == A [ n - 1 ] && A [ 0 ] == 0 ) consZeros ++ ; if ( zeros == ones ) return consZeros ; else return - 1 ; } let A = [ 1 , 1 , 0 , 0 ] ; let n = A . length ; document . write ( minOperations ( A , n ) ) ;