codeparrot/xlcost-text-to-code huggingface dataset:
Find numbers that divide X and Y to produce the same remainder | Function to find the required number as M ; Finding the maximum value among X and Y ; Loop to iterate through maximum value among X and Y . ; If the condition satisfies , then print the value of M ; Driver code
function printModulus ( X , Y ) { var n = Math . max ( X , Y ) ; for ( var i = 1 ; i <= n ; i ++ ) { if ( X % i == Y % i ) document . write ( i + " " ) ; } } X = 10 ; Y = 20 ; printModulus ( X , Y ) ;
Find numbers that divide X and Y to produce the same remainder | Function to print all the possible values of M such that X % M = Y % M ; Finding the absolute difference of X and Y ; Iterating from 1 ; Loop to print all the factors of D ; If i is a factor of d , then print i ; If d / i is a factor of d , then print d / i ; Driver code
function printModulus ( X , Y ) { var d = Math . abs ( X - Y ) ; var i = 1 ; while ( i * i <= d ) { if ( d % i == 0 ) { document . write ( i + " " ) ; if ( d / i != i ) document . write ( parseInt ( d / i ) + " " ) ; } i ++ ; } } var X = 10 ; var Y = 26 ; printModulus ( X , Y ) ;
Check whether a number can be represented as difference of two squares | Function to check whether a number can be represented by the difference of two squares ; Checking if n % 4 = 2 or not ; Driver code
function difSquare ( n ) { if ( n % 4 != 2 ) { return true ; } return false ; } var n = 45 ; if ( difSquare ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count of Fibonacci divisors of a given number | Function to create hash table to check Fibonacci numbers ; Function to count number of divisors of N which are fibonacci numbers ; If divisors are equal , check and count only one ; Otherwise check and count both ; 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 countFibonacciDivisors ( n ) { let hash = new Set ( ) ; createHash ( hash , n ) ; let cnt = 0 ; for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i == i ) && ( hash . has ( n / i ) ) ) cnt ++ ; else { if ( hash . has ( n / i ) ) cnt ++ ; if ( hash . has ( n / ( n / i ) ) ) cnt ++ ; } } } return cnt ; } let n = 12 ; document . write ( countFibonacciDivisors ( n ) ) ;
Count of subtrees in a Binary Tree having XOR value K | A binary tree node ; A utility function to allocate a new node ; Base Case : If node is null , return 0 ; Calculating the XOR of the current subtree var xr = ( root . data ) ; ^ rec ( root . left ) ^ rec ( root . right ) ; ; Increment res if xr is equal to k ; Return the XOR value of the current subtree ; Function to find the required count ; Initialize result variable ' res ' ; Recursively traverse the tree and compute the count ; return the count ' res ' ; Create the binary tree by adding nodes to it
class Node { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } ; var res = 0 ; var k = 0 ; function newNode ( data ) { var newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return newNode ; } function rec ( root ) { if ( root == null ) { return 0 ; } xr ^= rec ( root . left ) ; xr ^= rec ( root . right ) ; if ( xr == k ) { res ++ ; } return xr ; } function findCount ( root , K ) { res = 0 ; k = K ; rec ( root ) ; return res ; } var root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 9 ) ; root . left . left = newNode ( 10 ) ; root . left . right = newNode ( 5 ) ; var K = 5 ; document . write ( findCount ( root , K ) ) ;
Make array elements equal with minimum cost | Function to find the minimum cost required to make array elements equal ; Loop to find the count of odd elements ; Driver Code
function makearrayequal ( arr , n ) { var x = 0 ; for ( i = 0 ; i < n ; i ++ ) { x += ( arr [ i ] & 1 ) ; } document . write ( Math . min ( x , n - x ) ) ; } var arr = [ 4 , 3 , 2 , 1 ] ; var n = arr . length ; makearrayequal ( arr , n ) ;
Count of Prime digits in a Number | Function to find the count of prime digits in a number ; Loop to compute all the digits of the number ; Finding every digit of the given number ; Checking if digit is prime or not Only 2 , 3 , 5 and 7 are prime one - digit number ; Driver code
function countDigit ( n ) { let temp = n , count = 0 ; while ( temp != 0 ) { let d = temp % 10 ; temp = Math . floor ( temp / 10 ) ; if ( d == 2 d == 3 d == 5 d == 7 ) count ++ ; } return count ; } let n = 1234567890 ; document . write ( countDigit ( n ) + " " ) ;
Check if an Array is a permutation of numbers from 1 to N : Set 2 | Function to check if an array represents a permutation or not ; Counting the frequency ; Check if each frequency is 1 only ; Driver code
function permutation ( arr , N ) { var hash = Array ( N + 1 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( var i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] != 1 ) return " " ; } return " " ; } var arr = [ 1 , 1 , 5 , 5 , 3 ] ; var n = arr . length ; document . write ( permutation ( arr , n ) ) ;
Check if the XOR of an array of integers is Even or Odd | Function to check if the XOR of an array of integers is Even or Odd ; Count the number of odd elements ; If count of odd elements is odd , then XOR will be odd ; Else even ; Driver Code ; Function call
function check ( arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) count ++ ; } if ( count & 1 ) return " " ; else return " " ; } let arr = [ 3 , 9 , 12 , 13 , 15 ] ; let n = arr . length ; document . write ( check ( arr , n ) ) ;
Length of Longest Prime Subsequence in an Array | JavaScript program to find the length of Longest Prime Subsequence in an Array ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find the longest subsequence which contain all prime numbers ; Precompute N primes ; Find the length of longest prime subsequence ; Driver code ; Function call
let N = 100005 function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function longestPrimeSubsequence ( arr , n ) { let prime = new Array ( N + 1 ) ; prime . fill ( true ) SieveOfEratosthenes ( prime , N ) ; let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { answer ++ ; } } return answer ; } let arr = [ 3 , 4 , 11 , 2 , 9 , 21 ] ; let n = arr . length document . write ( longestPrimeSubsequence ( arr , n ) + " " ) ;
Check if an Octal number is Even or Odd | Check if the number is odd or even ; Check if the last digit is either '0' , '2' , '4' , or '6' ; Driver code
function even_or_odd ( N ) { var len = N . length ; if ( N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' ) return ( " " ) ; else return ( " " ) ; } var N = " " ; document . write ( even_or_odd ( N ) ) ;
Runge | A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 . ; Count number of iterations using step size or step height h ; Iterate for number of iterations ; Apply Runge Kutta Formulas to find next value of y ; Update next value of y ; Update next value of x ; Driver Code
function dydx ( x , y ) { return ( x + y - 2 ) ; } function rungeKutta ( x0 , y0 , x , h ) { let n = ( ( x - x0 ) / h ) ; let k1 , k2 ; let y = y0 ; for ( let i = 1 ; i <= n ; i ++ ) { k1 = h * dydx ( x0 , y ) ; k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; y = y + ( 1.0 / 6.0 ) * ( k1 + 2 * k2 ) ; x0 = x0 + h ; } return y ; } let x0 = 0 , y = 1 , x = 2 , h = 0.2 ; document . write ( rungeKutta ( x0 , y , x , h ) . toFixed ( 6 ) ) ;
Count of Fibonacci pairs which satisfy the given equation | Javascript program to find the count of Fibonacci pairs ( x , y ) which satisfy the equation Ax + By = N ; Array to store the Fibonacci numbers ; Array to store the number of ordered pairs ; Function to find if a number is a perfect square ; Function that returns 1 if N is non - fibonacci number else 0 ; N is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; Function to store the fibonacci numbers and their frequency in form a * x + b * y ; Storing the Fibonacci numbers ; For loop to find all the possible combinations of the Fibonacci numbers ; Finding the number of ordered pairs ; Driver code ; Find the ordered pair for every query
var size = 10001 var fib = Array ( 100010 ) . fill ( 0 ) ; var freq = Array ( 100010 ) . fill ( 0 ) ; function isPerfectSquare ( x ) { var s = parseInt ( Math . sqrt ( x ) ) ; return ( s * s == x ) ; } function isFibonacci ( n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 1 ; return 0 ; } function compute ( a , b ) { for ( var i = 1 ; i < 100010 ; i ++ ) { fib [ i ] = isFibonacci ( i ) ; } for ( var x = 1 ; x < 100010 ; x ++ ) { for ( var y = 1 ; y < size ; y ++ ) { if ( fib [ x ] == 1 && fib [ y ] == 1 && a * x + b * y < 100010 ) { freq [ a * x + b * y ] ++ ; } } } } var Q = 2 , A = 5 , B = 10 ; compute ( A , B ) ; var arr = [ 50 , 150 ] ; for ( var i = 0 ; i < Q ; i ++ ) { document . write ( freq [ arr [ i ] ] + " " ) ; }
Maximise the sum of two Numbers using at most one swap between them | JavaScript program to maximise the sum of two Numbers using at most one swap between them ; Function to maximize the sum by swapping only one digit ; Store digits of max ( n1 , n2 ) ; Store digits of min ( n1 , n2 ) ; If length of the two numbers are unequal ; Find the most significant number and the most significant index for swapping ; If both numbers are of equal length ; Fetch the index of current maximum digit present in both the arrays ; Compute the difference ; Find the most significant index and the most significant digit to be swapped ; Restore the new numbers ; Display the maximized sum ; Driver Code
let MAX = 100 ; function max_element ( arr , pos ) { let tmp = arr [ 0 ] ; let ind = 0 ; for ( let i = 1 ; i < pos ; i ++ ) { if ( tmp < arr [ i ] ) { tmp = arr [ i ] ; ind = i ; } } return ind ; } function findMaxSum ( n1 , n2 ) { let arr1 = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; let arr2 = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; let l1 = 0 , l2 = 0 ; let max1 = Math . max ( n1 , n2 ) ; let min1 = Math . min ( n1 , n2 ) ; for ( let i = max1 ; i > 0 ; i = Math . floor ( i / 10 ) ) arr1 [ l1 ++ ] = ( i % 10 ) ; for ( let i = min1 ; i > 0 ; i = Math . floor ( i / 10 ) ) arr2 [ l2 ++ ] = ( i % 10 ) ; let f = 0 ; if ( l1 != l2 ) { let index = ( max_element ( arr2 , l2 ) ) ; for ( let i = l1 - 1 ; i > ( l2 - 1 ) ; i -- ) { if ( arr1 [ i ] < arr2 [ index ] ) { let tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index ] ; arr2 [ index ] = tmp ; f = 1 ; break ; } } } if ( f != 1 ) { let index1 = 0 , index2 = 0 ; let diff1 = 0 , diff2 = 0 ; for ( let i = l2 - 1 ; i >= 0 ; i -- ) { index1 = ( max_element ( arr1 , i ) ) ; index2 = ( max_element ( arr2 , i ) ) ; diff1 = ( arr2 [ index2 ] - arr1 [ i ] ) ; diff2 = ( arr1 [ index1 ] - arr2 [ i ] ) ; if ( diff1 > 0 diff2 > 0 ) { if ( diff1 > diff2 ) { let tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index2 ] ; arr2 [ index2 ] = tmp ; break ; } else if ( diff2 > diff1 ) { let tmp = arr1 [ index1 ] ; arr1 [ index1 ] = arr2 [ i ] ; arr2 [ i ] = tmp ; break ; } else if ( diff1 == diff2 ) { if ( index1 <= index2 ) { let tmp = arr1 [ index1 ] ; arr1 [ index1 ] = arr2 [ i ] ; arr2 [ i ] = tmp ; break ; } else if ( index2 <= index1 ) { let tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index2 ] ; arr2 [ index2 ] = tmp ; break ; } } } } } let f_n1 = 0 , f_n2 = 0 ; for ( let i = l1 - 1 ; i >= 0 ; i -- ) { f_n1 = ( f_n1 * 10 ) + arr1 [ i ] ; f_n2 = ( f_n2 * 10 ) + arr2 [ i ] ; } document . write ( f_n1 + f_n2 ) ; } let N1 = 9987 ; let N2 = 123 ; findMaxSum ( N1 , N2 ) ;
Check if a number is divisible by 47 or not | Function to check if the number is divisible by 47 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting fourteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 47 or not ; Driver Code
function isDivisible ( n ) { let d ; while ( Math . floor ( n / 100 ) > 0 ) { d = n % 10 ; n = Math . floor ( n / 10 ) ; n = Math . abs ( n - ( d * 14 ) ) ; } return ( n % 47 == 0 ) ; } let N = 59173 ; if ( isDivisible ( N ) != 0 ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a HexaDecimal number is Even or Odd | Check if the number is odd or even ; check if the last digit is either '0' , '2' , '4' , '6' , '8' , ' A ' ( = 10 ) , ' C ' ( = 12 ) or ' E ' ( = 14 ) ; Driver Code
function even_or_odd ( N ) { let len = N . length ; if ( N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' N [ len - 1 ] == ' ' ) return ( " " ) ; else return ( " " ) ; } let N = " " ; document . write ( even_or_odd ( N ) ) ;
Check if a number is divisible by 31 or not | Function to check if the number is divisible by 31 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting three times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 31 or not ; Driver Code
function isDivisible ( n ) { let d ; while ( Math . floor ( n / 100 ) > 0 ) { d = n % 10 ; n = Math . floor ( n / 10 ) ; n = Math . abs ( n - ( d * 3 ) ) ; } return ( n % 31 == 0 ) ; } let N = 1922 ; if ( isDivisible ( N ) != 0 ) document . write ( " " ) ; else document . write ( " " ) ;
Check if the number is divisible 43 or not | Function to check if the number is divisible by 43 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; adding thirteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 43 or not ; Driver Code
function isDivisible ( n ) { let d ; while ( parseInt ( n / 100 ) > 0 ) { d = n % 10 ; n = parseInt ( n / 10 ) n = Math . abs ( n + ( d * 13 ) ) ; } return ( n % 43 == 0 ) ; } let N = 2795 ; if ( isDivisible ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the next Non | Function to check if a number is perfect square ; Function to check if a number is Fibinacci Number ; N is Fibinacci if either ( 5 * N * N + 4 ) , ( 5 * N * N - 4 ) or both is a perferct square ; Function to find the next Non - Fibinacci Number ; Case 1 If N <= 3 , then 4 will be next Non - Fibinacci Number ; Case 2 If N + 1 is Fibinacci , then N + 2 will be next Non - Fibinacci Number ; If N + 1 is Non - Fibinacci , then N + 2 will be next Non - Fibinacci Number ; Driver code
function isPerfectSquare ( x ) { var s = parseInt ( Math . sqrt ( x ) ) ; return ( s * s == x ) ; } function isFibonacci ( N ) { return isPerfectSquare ( 5 * N * N + 4 ) || isPerfectSquare ( 5 * N * N - 4 ) ; } function nextNonFibonacci ( N ) { if ( N <= 3 ) return 4 ; if ( isFibonacci ( N + 1 ) ) return N + 2 ; else return N + 1 ; } var N = 3 ; document . write ( nextNonFibonacci ( N ) + " " ) ; N = 5 ; document . write ( nextNonFibonacci ( N ) + " " ) ; N = 7 ; document . write ( nextNonFibonacci ( N ) + " " ) ;
Represent K ^ N as the sum of exactly N numbers | Function to print N numbers whose sum is a power of K ; Printing K ^ 1 ; Loop to print the difference of powers from K ^ 2 ; Driver code
function print ( n , k ) { document . write ( k + " " ) ; for ( var i = 2 ; i <= n ; i ++ ) { var x = Math . pow ( k , i ) - Math . pow ( k , i - 1 ) ; document . write ( x + " " ) ; } } var N = 3 , K = 4 ; print ( N , K ) ;
Check if the given number is divisible by 71 or not | Function to check if the number is divisible by 71 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting seven times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 71 or not ; Driver Code
function isDivisible ( n ) { let d ; while ( Math . floor ( n / 100 ) <= 0 ) { d = n % 10 ; n = Math . floor ( n / 10 ) ; n = Math . abs ( n - ( d * 7 ) ) ; } return ( n % 71 == 0 ) ; } let N = 5041 ; if ( isDivisible ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of prime digits of a Number which divides the number | Function to find the number of digits in number which divides the number and is also a prime number ; Only 2 , 3 , 5 and 7 are prime one - digit number ; Loop to compute all the digits of the number untill it is not equal to the zero ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n and is prime too ; Driven Code
function countDigit ( n ) { var prime = Array ( 10 ) . fill ( false ) ; prime [ 2 ] = prime [ 3 ] = true ; prime [ 5 ] = prime [ 7 ] = true ; var temp = n , count = 0 ; while ( temp != 0 ) { var d = temp % 10 ; temp = parseInt ( temp / 10 ) ; if ( d > 0 && n % d == 0 && prime [ d ] ) count ++ ; } return count ; } n = 1032 ; document . write ( countDigit ( n ) ) ;
Bitwise Operations on Digits of a Number | Javascript implementation of the approach ; Function to find the digits ; Function to Find OR of all digits of a number ; Find OR of all digits ; return OR of digits ; Function to Find AND of all digits of a number ; Find AND of all digits ; return AND of digits ; Function to Find XOR of all digits of a number ; Find XOR of all digits ; return XOR of digits ; Driver code ; Find and store all digits ; Find XOR of digits ; Find OR of digits ; Find AND of digits ; Driver Code
let digit = [ ] ; function findDigits ( n ) { let count = 0 ; while ( n != 0 ) { digit [ count ] = n % 10 ; n = n / 10 ; ++ count ; } return count ; } function OR_of_Digits ( n , count ) { let ans = 0 ; for ( let i = 0 ; i < count ; i ++ ) { ans = ans | digit [ i ] ; } return ans ; } function AND_of_Digits ( n , count ) { let ans = 0 ; for ( let i = 0 ; i < count ; i ++ ) { ans = ans & digit [ i ] ; } return ans ; } function XOR_of_Digits ( n , count ) { let ans = 0 ; for ( let i = 0 ; i < count ; i ++ ) { ans = ans ^ digit [ i ] ; } return ans ; } function bitwise_operation ( N ) { let countOfDigit = findDigits ( N ) ; document . write ( " " + XOR_of_Digits ( N , countOfDigit ) + " " ) ; document . write ( " " + OR_of_Digits ( N , countOfDigit ) + " " ) ; document . write ( " " + AND_of_Digits ( N , countOfDigit ) + " " ) ; } let N = 123456 ; bitwise_operation ( N ) ;
Length of longest subarray with product greater than or equal to 0 | Function that count the length of longest subarray with product greater than or equals to zero ; If product is greater than zero , return array size ; Traverse the array and if any negative element found then update the length of longest subarray with the length of left and right subarray ; Driver Code
function maxLength ( arr , N ) { var product = 1 , len = 0 ; for ( var i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } if ( product >= 0 ) { return N ; } for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { len = Math . max ( len , Math . max ( N - i - 1 , i ) ) ; } } return len ; } var arr = [ - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 ] ; var N = arr . length ; document . write ( maxLength ( arr , N ) ) ;
Print a pair of numbers with the given Sum and Product | Prints roots of quadratic equation ax * 2 + bx + c = 0 ; calculating the sq root value for b * b - 4 * a * c ; Finding the roots ; Check if the roots are valid or not ; Finding the roots ; Check if the roots are valid or not ; when d < 0 ; No such pair exists in this case ; Driver code
function findRoots ( b , c ) { var a = 1 ; var d = b * b - 4 * a * c ; var sqrt_val = Math . sqrt ( Math . abs ( d ) ) ; if ( d > 0 ) { var x = - b + sqrt_val ; var y = - b - sqrt_val ; var root1 = ( x ) / ( 2 * a ) ; var root2 = ( y ) / ( 2 * a ) ; if ( root1 + root2 == - 1 * b && root1 * root2 == c ) document . write ( root1 + " " + root2 ) ; else document . write ( - 1 ) ; } else if ( d == 0 ) { var root = - b / ( 2 * a ) ; if ( root + root == - 1 * b && root * root == c ) document . write ( root + " " + root ) ; else document . write ( - 1 ) ; } else { document . write ( - 1 ) ; } document . write ( " " ) ; } var S = 5 , P = 6 ; findRoots ( - S , P ) ; S = 5 , P = 9 ; findRoots ( - S , P ) ;
Print N numbers such that their product is a Perfect Cube | Function to find the N numbers such that their product is a perfect cube ; Loop to traverse each number from 1 to N ; Print the cube of i as the ith term of the output ; Driver Code ; Function Call
function findNumbers ( N ) { let i = 1 ; while ( i <= N ) { document . write ( ( i * i * i ) + " " ) ; i ++ ; } } let N = 4 ; findNumbers ( N ) ;
Sum of all proper divisors from 1 to N | Utility function to find sum of all proper divisor of number up to N ; Loop to iterate over all the numbers from 1 to N ; Find all divisors of i and add them ; Subtracting ' i ' so that the number itself is not included ; Driver Code
function properDivisorSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; ++ i ) { for ( let j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } sum = sum - i ; } return sum ; } let n = 4 ; document . write ( properDivisorSum ( n ) + " " ) ; n = 5 ; document . write ( properDivisorSum ( n ) + " " ) ;
Sum of all proper divisors from 1 to N | Utility function to find sum of all proper divisor of number up to N ; Loop to find the proper divisor of every number from 1 to N ; Driver Code
function properDivisorSum ( n ) { var sum = 0 ; for ( var i = 1 ; i <= n ; ++ i ) sum += parseInt ( n / i ) * i ; return sum - n * ( ( n + 1 ) / 2 ) ; } var n = 4 ; document . write ( properDivisorSum ( n ) + " " ) ; n = 5 ; document . write ( properDivisorSum ( n ) + " " ) ;
Minimum change in given value so that it lies in all given Ranges | Function to find the minimum difference in the number D such that D is inside of every range ; Loop to find the common range out of the given array of ranges . ; Storing the start and end index ; Sorting the range ; Finding the maximum starting value of common segment ; Finding the minimum ending value of common segment ; If there is no common segment ; If the given number is between the computed common range . ; Finding the minimum distance ; Driver Code ; Given array of ranges
function findMinimumOperation ( n , d , arrays ) { var cnt = 0 ; var first = Number . MIN_VALUE , end = Number . MAX_VALUE ; while ( n > 0 ) { var arr = [ arrays [ cnt ] [ 0 ] , arrays [ cnt ] [ 1 ] ] ; arr . sort ( ( a , b ) => a - b ) ; first = Math . max ( first , arr [ 0 ] ) ; end = Math . min ( end , arr [ 1 ] ) ; cnt ++ ; n -- ; } if ( first > end ) document . write ( " " ) ; else { if ( d >= first && d <= end ) { document . write ( " " ) ; } else document . write ( Math . min ( Math . abs ( first - d ) , Math . abs ( d - end ) ) ) ; } } var n = 3 , d = 3 ; var arrays = [ [ 0 , 7 ] , [ 2 , 14 ] , [ 4 , 6 ] ] ; findMinimumOperation ( n , d , arrays ) ;