codeparrot/xlcost-text-to-code huggingface dataset:
Find temperature of missing days using given sum and average | Function for finding the temperature ; Store Day1 - Day2 in diff ; Remaining from s will be Day1 ; Print Day1 and Day2 ; Driver Code ; Functions
function findTemperature ( x , y , s ) { let Day1 , Day2 ; let diff = ( x - y ) * 6 ; Day2 = ( diff + s ) / 2 ; Day1 = s - Day2 ; document . write ( " " + Day1 + " " ) ; document . write ( " " + Day2 + " " ) ; } let x = 5 , y = 10 , s = 40 ; findTemperature ( x , y , s ) ;
Find two numbers whose sum is N and does not contain any digit as K | Javascript program for the above approach ; Function to find two numbers whose sum is N and do not contain any digit as k ; Check every number i and ( n - i ) ; Check if i and n - i doesn 't  contain k in them print i and n-i ; Check if flag is 0 then print - 1 ; Given N and K ; Function call
function freqCount ( str , k ) { var count = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == k ) count ++ ; } return count ; } function findAandB ( n , k ) { var flag = 0 ; for ( var i = 1 ; i < n ; i ++ ) { if ( freqCount ( i . toString ( ) , String . fromCharCode ( k + 48 ) ) == 0 && freqCount ( ( n - i ) . toString ( ) , String . fromCharCode ( k + 48 ) ) == 0 ) { document . write ( " " + i + " " + ( n - i ) + " " ) ; flag = 1 ; break ; } } if ( flag == 0 ) cout + - 1 ; } var N = 100 ; var K = 0 ; findAandB ( N , K ) ;
Find the value of P and modular inverse of Q modulo 998244353 | Function to find the value of P * Q ^ - 1 mod 998244353 ; Loop to find the value until the expo is not zero ; Multiply p with q if expo is odd ; Reduce the value of expo by 2 ; Driver code ; Function call
function calculate ( P , Q ) { let mod = 998244353 , expo ; expo = mod - 2 ; p = 748683265 ; while ( expo != 0 ) { if ( ( expo & 1 ) == 1 ) { P = ( P * Q ) % mod ; } Q = ( Q * Q ) % mod ; expo >>= 1 ; } return p ; } let p = 1 , q = 4 ; document . write ( calculate ( p , q ) ) ;
Find two numbers with given sum and maximum possible LCM | Function that print two numbers with the sum X and maximum possible LCM ; variables to store the result ; If X is odd ; If X is even ; If floor ( X / 2 ) is even ; If floor ( X / 2 ) is odd ; Print the result ; Given Number ; Function call
function maxLCMWithGivenSum ( X ) { let A , B ; if ( X & 1 ) { A = X / 2 ; B = X / 2 + 1 ; } else { if ( ( X / 2 ) % 2 == 0 ) { A = X / 2 - 1 ; B = X / 2 + 1 ; } else { A = X / 2 - 2 ; B = X / 2 + 2 ; } } document . write ( A + " " + B + " " ) ; } let X = 30 ; maxLCMWithGivenSum ( X ) ;
Length of longest subarray whose sum is not divisible by integer K | Function to find the longest subarray with sum is not divisible by k ; left is the index of the leftmost element that is not divisible by k ; right is the index of the rightmost element that is not divisible by k ; sum of the array ; Find the element that is not multiple of k ; left = - 1 means we are finding the leftmost element that is not divisible by k ; Updating the rightmost element ; Update the sum of the array up to the index i ; Check if the sum of the array is not divisible by k , then return the size of array ; All elements of array are divisible by k , then no such subarray possible so return - 1 ; Length of prefix elements that can be removed ; Length of suffix elements that can be removed ; Return the length of subarray after removing the elements which have lesser number of elements ; Driver Code
function MaxSubarrayLength ( arr , n , k ) { let left = - 1 ; let right = 0 ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] % k ) != 0 ) { if ( left == - 1 ) { left = i ; } right = i ; } sum += arr [ i ] ; } if ( ( sum % k ) != 0 ) { return n ; } else if ( left == - 1 ) { return - 1 ; } else { let prefix_length = left + 1 ; let suffix_length = n - right ; return n - Math . min ( prefix_length , suffix_length ) ; } } let arr = [ 6 , 3 , 12 , 15 ] ; let n = arr . length ; let K = 3 ; document . write ( MaxSubarrayLength ( arr , n , K ) ) ;
Minimum steps to convert X to Y by repeated division and multiplication | Javascript implementation to find minimum steps to convert X to Y by repeated division and multiplication ; Check if X is greater than Y then swap the elements ; Check if X equals Y ; Driver code
function solve ( X , Y ) { if ( X > Y ) { let temp = X ; X = Y ; Y = temp ; } if ( X == Y ) document . write ( 0 ) ; else if ( Y % X == 0 ) document . write ( 1 ) ; else document . write ( 2 ) ; } let X = 8 , Y = 13 ; solve ( X , Y ) ;
Count quadruplets ( A , B , C , D ) till N such that sum of square of A and B is equal to that of C and D | Function to count the quadruples ; Counter variable ; Map to store the sum of pair ( a ^ 2 + b ^ 2 ) ; Iterate till N ; Calculate a ^ 2 + b ^ 2 ; Increment the value in map ; Check if this sum was also in a ^ 2 + b ^ 2 ; Return the count ; Given N ; Function Call
function countQuadraples ( N ) { var cnt = 0 ; var m = new Map ( ) ; for ( var a = 1 ; a <= N ; a ++ ) { for ( var b = 1 ; b <= N ; b ++ ) { var x = a * a + b * b ; if ( m . has ( x ) ) m . set ( x , m . get ( x ) + 1 ) else m . set ( x , 1 ) } } for ( var c = 1 ; c <= N ; c ++ ) { for ( var d = 1 ; d <= N ; d ++ ) { var x = c * c + d * d ; if ( m . has ( x ) ) cnt += m . get ( x ) ; } } return cnt ; } var N = 2 ; document . write ( countQuadraples ( N ) )
Count of distinct index pair ( i , j ) such that element sum of First Array is greater | function to find the number of pairs satisfying the given cond . ; Variables used for traversal ; count variable to store the count of possible pairs ; Nested loop to find out the possible pairs ; Check if the given condition is satisfied or not . If yes then increment the count . ; Return the count value ; Size of the arrays ; Initialise the arrays ; Function call that returns the count of possible pairs
function count_pairs ( a , b , N ) { let i , j ; let count = 0 ; for ( i = 0 ; i < ( N - 1 ) ; i ++ ) { for ( j = ( i + 1 ) ; j < N ; j ++ ) { if ( ( a [ i ] + a [ j ] ) > ( b [ i ] + b [ j ] ) ) { count ++ ; } } } return count ; } let N = 5 ; let a = [ 1 , 2 , 3 , 4 , 5 ] ; let b = [ 2 , 5 , 6 , 1 , 9 ] ; document . write ( count_pairs ( a , b , N ) ) ;
Count of distinct index pair ( i , j ) such that element sum of First Array is greater | Function to find the number of pairs . ; Array c [ ] where c [ i ] = a [ i ] - b [ i ] ; Sort the array c ; Initialise answer as 0 ; Iterate from index 0 to n - 1 ; If c [ i ] <= 0 then in the sorted array c [ i ] + c [ pos ] can never greater than 0 where pos < i ; Which is equivalent to c [ j ] >= - c [ i ] + 1 ; Add ( i - pos ) to answer ; Return the answer ; Number of elements in a and b ; Array a ; Array b
function numberOfPairs ( a , b , n ) { let c = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { c [ i ] = a [ i ] - b [ i ] ; } c . sort ( function ( a , b ) { return a - b } ) ; let answer = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( c [ i ] <= 0 ) continue ; let pos = - 1 ; for ( let j = 0 ; j < n ; j ++ ) { if ( c [ i ] + c [ j ] > 0 ) { pos = j ; break ; } } answer += ( i - pos ) ; } return answer ; } let n = 5 ; let a = [ 1 , 2 , 3 , 4 , 5 ] ; let b = [ 2 , 5 , 6 , 1 , 9 ] ; document . write ( numberOfPairs ( a , b , n ) ) ;
Find K for every Array element such that at least K prefixes are â ‰¥ K | Function to find the K - value for every index in the array ; Multiset to store the array in the form of red - black tree ; Iterating over the array ; Inserting the current value in the multiset ; Condition to check if the smallest value in the set is less than it 's size ; Erase the smallest value ; h - index value will be the size of the multiset ; array ; Size of the array ; function call
function print_h_index ( arr , N ) { var ms = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { ms . push ( arr [ i ] ) ; ms . sort ( ( a , b ) => a - b ) if ( ms [ 0 ] < ms . length ) { ms . shift ( ) ; } document . write ( ms . length + " " ) ; } } var arr = [ 9 , 10 , 7 , 5 , 0 , 10 , 2 , 0 ] ; var N = arr . length ; print_h_index ( arr , N ) ;
Prefix Product Array | Function to generate prefix product array ; Update the array with the product of prefixes ; Print the array ; Driver Code
function prefixProduct ( a , n ) { for ( let i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i ] * a [ i - 1 ] ; } for ( let j = 0 ; j < n ; j ++ ) { document . write ( a [ j ] + " " ) ; } return 0 ; } let arr = [ 2 , 4 , 6 , 5 , 10 ] ; let N = arr . length ; prefixProduct ( arr , N ) ;
Count of ways to distribute N items among 3 people with one person receiving maximum | Function to find the number of ways to distribute N items among 3 people ; No distribution possible ; Total number of ways to distribute N items among 3 people ; Store the number of distributions which are not possible ; Count possibilities of two persons receiving the maximum ; If N is divisible by 3 ; Return the final count of ways to distribute ; Driver Code
function countWays ( N ) { if ( N < 4 ) return 0 ; let ans = ( ( N - 1 ) * ( N - 2 ) ) / 2 ; let s = 0 ; for ( let i = 2 ; i <= N - 3 ; i ++ ) { for ( let j = 1 ; j < i ; j ++ ) { if ( N == 2 * i + j ) s ++ ; } } if ( N % 3 == 0 ) s = 3 * s + 1 ; else s = 3 * s ; return ans - s ; } let N = 10 ; document . write ( countWays ( N ) ) ;
Magnanimous Numbers | Function to check if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the number is Magnanimous or not ; Converting the number to string ; Finding length of string ; Number should not be of single digit ; Loop to find all left and right part of the string ; 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 ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isMagnanimous ( N ) { let s = N . toString ( ) ; let l = s . length ; if ( l < 2 ) return false ; for ( let i = 0 ; i < l - 1 ; i ++ ) { let left = s . substring ( 0 , i + 1 ) ; let right = s . substring ( i + 1 ) ; let x = parseInt ( left ) ; let y = parseInt ( right ) ; if ( ! isPrime ( x + y ) ) return false ; } return true ; } let N = 12 ; if ( isMagnanimous ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Honaker Prime Number | Javascript program for above approach ; 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 ; Function to get sum of digits ; Function to check whether the given number is Honaker Prime number or not ; Precompute the prime numbers till 10 ^ 6 ; Given Number ; Function Call
const limit = 10000000 ; let position = Array ( limit + 1 ) . fill ( 0 ) ; function sieve ( ) { position [ 0 ] = - 1 ; position [ 1 ] = - 1 ; let pos = 0 ; for ( let i = 2 ; i <= limit ; i ++ ) { if ( position [ i ] == 0 ) { position [ i ] = ++ pos ; for ( let j = i * 2 ; j <= limit ; j += i ) position [ j ] = - 1 ; } } } function getSum ( n ) { let sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = parseInt ( n / 10 ) ; } return sum ; } function isHonakerPrime ( n ) { let pos = position [ n ] ; if ( pos == - 1 ) return false ; return getSum ( n ) == getSum ( pos ) ; } sieve ( ) ; let N = 121 ; if ( isHonakerPrime ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if Matrix sum is prime or not | Javascript implementation to check if the sum of matrix is prime or not ; Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for to find the sum of the given matrix ; Driver Code
let N = 4 , M = 5 ; function isPrime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function takeSum ( a ) { let s = 0 ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < M ; j ++ ) s += a [ i ] [ j ] ; return s ; } let a = [ [ 1 , 2 , 3 , 4 , 2 ] , [ 0 , 1 , 2 , 3 , 34 ] , [ 0 , 34 , 21 , 12 , 12 ] , [ 1 , 2 , 3 , 6 , 6 ] ] ; let sum = takeSum ( a ) ; if ( isPrime ( sum ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of sum | Function to find the sum ; Calculate sum - series for every natural number and add them ; Driver code
function sumOfSumSeries ( N ) { let sum = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { sum = sum + ( i * ( i + 1 ) ) / 2 ; } return sum ; } let N = 5 ; document . write ( sumOfSumSeries ( N ) ) ;
Sum of sum | Function to find the sum ; Driver code
function sumOfSumSeries ( n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } let N = 5 ; document . write ( sumOfSumSeries ( N ) ) ;
Tetradic Primes | Function to check if the number N having all digits lies in the set ( 0 , 1 , 8 ) ; Function to check if the number N is palindrome ; Function to check if a number N is Tetradic ; Function to generate all primes and checking whether number is Tetradic or not ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all Tetradic prime numbers ; Checking whether the given number is prime Tetradic or not ; Driver code
function isContaindigit ( n ) { while ( n > 0 ) { if ( ! ( n % 10 == 0 n % 10 == 1 n % 10 == 8 ) ) return false ; n = parseInt ( n / 10 , 10 ) ; } return true ; } function ispalindrome ( n ) { let temp = n . toString ( ) ; let l = temp . length ; for ( let i = 0 ; i < parseInt ( l / 2 , 10 ) ; i ++ ) { if ( temp [ i ] != temp [ l - i - 1 ] ) return false ; } return true ; } function isTetradic ( n ) { if ( ispalindrome ( n ) && isContaindigit ( n ) ) return true ; return false ; } function printTetradicPrimesLessThanN ( n ) { let prime = new Array ( n + 1 ) ; prime . fill ( true ) ; let p = 2 ; while ( p * p <= n ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i < n + 1 ; i += p ) prime [ i ] = false ; } p += 1 ; } for ( p = 2 ; p < n + 1 ; p ++ ) { if ( prime [ p ] && isTetradic ( p ) ) document . write ( p + " " ) ; } } let n = 1000 ; printTetradicPrimesLessThanN ( n ) ;
Astonishing Numbers | Function to concatenate two integers into one ; Convert both the integers to string ; Concatenate both strings ; Convert the concatenated string to integer ; return the formed integer ; Function to check if N is a Astonishing number ; Loop to find sum of all integers from i till the sum becomes >= n ; variable to store sum of all integers from i to j and check if sum and concatenation equals n or not ; finding concatenation of i and j ; condition for Astonishing number ; Given Number ; Function Call
function concat ( a , b ) { var s1 = a . toString ( ) ; var s2 = b . toString ( ) ; var s = s1 + s2 ; var c = s ; return c ; } function isAstonishing ( n ) { for ( var i = 1 ; i < n ; i ++ ) { var sum = 0 ; for ( var j = i ; j < n ; j ++ ) { sum += j ; if ( sum == n ) { var concatenation = concat ( i , j ) ; if ( concatenation == n ) { return true ; } } } } return false ; } var n = 429 ; if ( isAstonishing ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Digitally balanced numbers | Function to check if the digits in the number is the same number of digits ; Loop to iterate over the digits of the number N ; Loop to iterate over the map ; Driver Code ; function to check
function checkSame ( n , b ) { var m = { } ; while ( n != 0 ) { var r = n % b ; n = n / b ; if ( r in m ) m [ r ] += 1 else m [ r ] = 1 } var last = - 1 ; for ( var i in m ) { if ( last != - 1 && m [ i ] != last ) { return false ; } else { last = m [ i ] ; } } return true ; } var n = 9 ; var base = 2 ; if ( checkSame ( n , base ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of series formed by difference between product and sum of N natural numbers | Function to calculate the sum upto Nth term ; Stores the sum of the series ; Stores the product of natural numbers upto the current term ; Stores the sum of natural numbers upto the upto current term ; Generate the remaining terms and calculate sum ; Update the sum ; Return the sum ; Driver code
function seriesSum ( n ) { let sum = 0 ; let currProd = 1 ; let currSum = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { currProd *= i ; currSum += i ; sum += currProd - currSum ; } return sum ; } let N = 5 ; document . write ( seriesSum ( N ) + " " ) ;
Count of elements not divisible by any other elements of Array | Function to count the number of elements of array which are not divisible by any other element in the array arr [ ] ; Iterate over the array ; Check if the element is itself or not ; Check for divisibility ; Return the final result ; Given array ; Function Call
function count ( a , n ) { let countElements = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let flag = true ; for ( let j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; if ( a [ i ] % a [ j ] == 0 ) { flag = false ; break ; } } if ( flag == true ) ++ countElements ; } return countElements ; } let arr = [ 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 ] ; let n = arr . length ; document . write ( count ( arr , n ) ) ;
Smallest N digit number divisible by N | Function to find the smallest N - digit number divisible by N ; Return the smallest N - digit number calculated using above formula ; Given N ; Function Call
function smallestNumber ( N ) { return N * Math . ceil ( Math . pow ( 10 , ( N - 1 ) ) / N ) ; } let N = 2 ; document . write ( smallestNumber ( N ) ) ;
Count pairs in an array containing at least one even value | Function to count the pairs in the array such as there is at least one even element in each pair ; Generate all possible pairs and increment then count if the condition is satisfied ; Driver code ; Function call
function CountPairs ( arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] % 2 == 0 arr [ j ] % 2 == 0 ) count ++ ; } } return count ; } let arr = [ 8 , 2 , 3 , 1 , 4 , 2 ] ; let n = arr . length ; document . write ( CountPairs ( arr , n ) ) ;
Count pairs in an array containing at least one even value | Function to count the pairs in the array such as there is at least one even element in each pair ; Store count of even and odd elements ; Check element is even or odd ; Driver Code
function CountPairs ( arr , n ) { let even = 0 , odd = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return ( even * ( even - 1 ) ) / 2 + ( even * odd ) ; } let arr = [ 8 , 2 , 3 , 1 , 4 , 2 ] ; let n = arr . length ; document . write ( CountPairs ( arr , n ) ) ;
Giuga Numbers | Function to check if n is a composite number ; Corner cases ; This is checked to skip middle 5 numbers ; Function to check if N is a Giuga Number ; N should be composite to be a Giuga Number ; Print the number of 2 s that divide n ; N must be odd at this point . So we can skip one element ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number > 2 ; Given Number N ; Function Call
function isComposite ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } function isGiugaNum ( n ) { if ( ! ( isComposite ( n ) ) ) return false ; let N = n ; while ( n % 2 == 0 ) { if ( ( N / 2 - 1 ) % 2 != 0 ) return false ; n = n / 2 ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { if ( ( N / i - 1 ) % i != 0 ) return false ; n = n / i ; } } if ( n > 2 ) if ( ( N / n - 1 ) % n != 0 ) return false ; return true ; } let n = 30 ; if ( isGiugaNum ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Droll Numbers | Function to check droll numbers ; To store sum of even prime factors ; To store sum of odd prime factors ; Add the number of 2 s that divide n in sum_even ; N must be odd at this polet . So we can skip one element ( Note i = i + 2 ) ; While i divides n , prlet i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Condition to check droll number ; Given Number N ; Function Call
function isDroll ( n ) { if ( n == 1 ) return false ; let sum_even = 0 ; let sum_odd = 0 ; while ( n % 2 == 0 ) { sum_even += 2 ; n = n / 2 ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { sum_odd += i ; n = n / i ; } } if ( n > 2 ) sum_odd += n ; return sum_even == sum_odd ; } let n = 72 ; if ( isDroll ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count all pairs of divisors of a number N whose sum is coprime with N | Function to calculate GCD ; Function to count all valid pairs ; Initialize count ; Check if sum of pair and n are coprime ; Return the result ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return ( gcd ( b , a % b ) ) ; } function CountPairs ( n ) { let cnt = 0 ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { let div1 = i ; let div2 = Math . floor ( n / i ) ; let sum = div1 + div2 ; if ( gcd ( sum , n ) == 1 ) cnt += 1 ; } } return cnt ; } let n = 24 ; document . write ( CountPairs ( n ) + " " ) ;
Check if A can be converted to B by reducing with a Prime number | Function to find if it is possible to make A equal to B ; Driver Code ; Function Call
function isPossible ( A , B ) { return ( A - B > 1 ) ; } let A = 10 , B = 4 ; if ( isPossible ( A , B ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximize sum of minimum difference of divisors of nodes in N | Array to store the result at each node ; Function to get minimum difference between the divisors of a number ; Iterate from square root of N to N ; return absolute difference ; DFS function to calculate the maximum sum ; Store the min difference ; Add the maximum of all children to sub [ u ] ; Return maximum sum of node ' u ' to its parent ; Driver code
let sub = new Array ( 100005 ) ; sub . fill ( 0 ) ; function minDivisorDifference ( n ) { let num1 = 0 ; let num2 = 0 ; for ( let i = parseInt ( Math . sqrt ( n ) , 10 ) ; i <= n ; i ++ ) { if ( n % i == 0 ) { num1 = i ; num2 = parseInt ( n / i , 10 ) ; break ; } } return Math . abs ( num1 - num2 ) ; } function dfs ( g , u , par ) { sub [ u ] = minDivisorDifference ( u ) ; let mx = 0 ; for ( let c = 0 ; c < g [ u ] . length ; c ++ ) { if ( g [ u ] != par ) { let ans = dfs ( g , g [ u ] , u ) ; mx = Math . max ( mx , ans ) ; } } sub [ u ] += mx ; return sub [ u ] ; } let g = new Array ( 100005 ) ; for ( let i = 0 ; i < g . length ; i ++ ) g [ i ] = [ ] ; let edges = 6 ; g [ 18 ] . push ( 7 ) ; g [ 7 ] . push ( 18 ) ; g [ 18 ] . push ( 15 ) ; g [ 15 ] . push ( 18 ) ; g [ 15 ] . push ( 2 ) ; g [ 2 ] . push ( 15 ) ; g [ 7 ] . push ( 4 ) ; g [ 4 ] . push ( 7 ) ; g [ 7 ] . push ( 12 ) ; g [ 12 ] . push ( 7 ) ; g [ 12 ] . push ( 9 ) ; g [ 9 ] . push ( 12 ) ; let root = 18 ; document . write ( dfs ( g , root , - 1 ) ) ;
Program to check if N is a Centered Cubic Number | Function to check if the number N is a centered cubic number ; Iterating from 1 ; Infinite loop ; Finding ith_term ; Checking if the number N is a Centered cube number ; If ith_term > N then N is not a Centered cube number ; Incrementing i ; Driver code ; Function call
function isCenteredcube ( N ) { let i = 1 ; while ( true ) { let ith_term = ( 2 * i + 1 ) * ( i * i + i + 1 ) ; if ( ith_term == N ) { return true ; } if ( ith_term > N ) { return false ; } i ++ ; } } let N = 9 ; if ( isCenteredcube ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Product of N terms of a given Geometric series | Function to calculate product of geometric series ; Initialise final product with 1 ; Multiply product with each term stored in a ; Return the final product ; Given first term and common ratio ; Number of terms ; Function Call
function productOfGP ( a , r , n ) { let product = 1 ; for ( let i = 0 ; i < n ; i ++ ) { product = product * a ; a = a * r ; } return product ; } let a = 1 , r = 2 ; let N = 4 ; document . write ( productOfGP ( a , r , N ) ) ;