codeparrot/xlcost-text-to-code huggingface dataset:
Midpoint ellipse drawing algorithm | Javascript program for implementing Mid - Point Ellipse Drawing Algorithm ; Initial decision parameter of region 1 ; For region 1 ; Print points based on 4 - way symmetry ; Checking and updating value of decision parameter based on algorithm ; Decision parameter of region 2 ; Plotting points of region 2 ; Print points based on 4 - way symmetry ; Checking and updating parameter value based on algorithm ; To draw a ellipse of major and minor radius 15 , 10 centred at ( 50 , 50 )
function midptellipse ( rx , ry , xc , yc ) { var dx , dy , d1 , d2 , x , y ; x = 0 ; y = ry ; d1 = ( ry * ry ) - ( rx * rx * ry ) + ( 0.25 * rx * rx ) ; dx = 2 * ry * ry * x ; dy = 2 * rx * rx * y ; while ( dx < dy ) { document . write ( " " + ( x + xc ) . toFixed ( 5 ) + " " + ( y + yc ) . toFixed ( 5 ) + " " + " " ) ; document . write ( " " + ( - x + xc ) . toFixed ( 5 ) + " " + ( y + yc ) . toFixed ( 5 ) + " " + " " ) ; document . write ( " " + ( x + xc ) . toFixed ( 5 ) + " " + ( - y + yc ) . toFixed ( 5 ) + " " + " " ) ; document . write ( " " + ( - x + xc ) . toFixed ( 5 ) + " " + ( - y + yc ) . toFixed ( 5 ) + " " + " " ) ; if ( d1 < 0 ) { x ++ ; dx = dx + ( 2 * ry * ry ) ; d1 = d1 + dx + ( ry * ry ) ; } else { x ++ ; y -- ; dx = dx + ( 2 * ry * ry ) ; dy = dy - ( 2 * rx * rx ) ; d1 = d1 + dx - dy + ( ry * ry ) ; } } d2 = ( ( ry * ry ) * ( ( x + 0.5 ) * ( x + 0.5 ) ) ) + ( ( rx * rx ) * ( ( y - 1 ) * ( y - 1 ) ) ) - ( rx * rx * ry * ry ) ; while ( y >= 0 ) { document . write ( " " + ( x + xc ) . toFixed ( 5 ) + " " + ( y + yc ) . toFixed ( 5 ) + " " + " " ) ; document . write ( " " + ( - x + xc ) . toFixed ( 5 ) + " " + ( y + yc ) . toFixed ( 5 ) + " " + " " ) ; document . write ( " " + ( x + xc ) . toFixed ( 5 ) + " " + ( - y + yc ) . toFixed ( 5 ) + " " + " " ) ; document . write ( " " + ( - x + xc ) . toFixed ( 5 ) + " " + ( - y + yc ) . toFixed ( 5 ) + " " + " " ) ; if ( d2 > 0 ) { y -- ; dy = dy - ( 2 * rx * rx ) ; d2 = d2 + ( rx * rx ) - dy ; } else { y -- ; x ++ ; dx = dx + ( 2 * ry * ry ) ; dy = dy - ( 2 * rx * rx ) ; d2 = d2 + dx - dy + ( rx * rx ) ; } } } midptellipse ( 10 , 15 , 50 , 50 ) ;
Program to check if a number is divisible by sum of its digits | Function to check if the given number is divisible by sum of its digits ; Find sum of digits ; Check if sum of digits divides n ; Driver Code
function isDivisible ( n ) { temp = n ; sum = 0 ; while ( n ) { k = n % 10 ; sum += k ; n = parseInt ( n / 10 ) ; } if ( temp % sum == 0 ) return " " ; return " " ; } let n = 123 ; document . write ( isDivisible ( n ) ) ;
Program to check if a number is divisible by sum of its digits | JavaScript implementation of above approach ; Converting integer to String ; Initialising sum to 0 ; Traversing through the String ; Converting character to int ; Comparing number and sum ; Driver Code ; Passing this number to get result function
function getResult ( n ) { let st = ( n ) . toString ( ) ; let sum = 0 ; for ( let i of st . split ( " " ) ) { sum = sum + parseInt ( i ) ; } if ( n % sum == 0 ) return " " ; else return " " ; } let n = 123 ; document . write ( getResult ( n ) + " " ) ;
Find the final X and Y when they are Altering under given condition | Function to get final value of X and Y ; Following the sequence but by replacing minus with modulo ; Step 1 ; Step 2 ; Step 3 ; Otherwise terminate ; Get the initial X and Y values ; Find the result
function alter ( x , y ) { while ( true ) { if ( x == 0 y == 0 ) break ; if ( x >= 2 * y ) x = x % ( 2 * y ) ; else if ( y >= 2 * x ) y = y % ( 2 * x ) ; else break ; } document . write ( " " + x + " " + " " + y ) ; } var x = 12 , y = 5 ; alter ( x , y ) ;
Print all multiplicative primes <= N | Function to return the digit product of n ; Function to print all multiplicative primes <= n ; Create a boolean array " prime [ 0 . . n + 1 ] " . 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 ; If i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; Driver code
function digitProduct ( n ) { let prod = 1 ; while ( n > 0 ) { prod = prod * ( n % 10 ) ; n = Math . floor ( n / 10 ) ; } return prod ; } function printMultiplicativePrimes ( n ) { let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( let i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] && prime [ digitProduct ( i ) ] ) document . write ( i + " " ) ; } } let n = 10 ; printMultiplicativePrimes ( n ) ;
Largest number less than or equal to N / 2 which is coprime to N | Function to calculate gcd of two number ; Function to check if two numbers are coprime or not ; two numbers are coprime if their gcd is 1 ; Function to find largest integer less than or equal to N / 2 and coprime with N ; Check one by one all numbers less than or equal to N / 2 ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } function coPrime ( n1 , n2 ) { if ( gcd ( n1 , n2 ) == 1 ) return true ; else return false ; } function largestCoprime ( N ) { let half = Math . floor ( N / 2 ) ; while ( coPrime ( N , half ) == false ) half -- ; return half ; } let n = 50 ; document . write ( largestCoprime ( n ) ) ;
Largest number less than or equal to N / 2 which is coprime to N | Function to find largest integer less than or equal to N / 2 and is coprime with N ; Handle the case for N = 6 ; Driver Code
function largestCoprime ( N ) { if ( N == 6 ) return 1 ; else if ( N % 4 == 0 ) return ( N / 2 ) - 1 ; else if ( N % 2 == 0 ) return ( N / 2 ) - 2 ; else return ( ( N - 1 ) / 2 ) ; } var n = 50 ; document . write ( largestCoprime ( n ) ) ;
Print all safe primes below N | Function to print first n safe primes ; Initialize all entries of integer array as 1. A value in prime [ i ] will finally be 0 if i is Not a prime , else 1 ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime ; 2 p + 1 ; If 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; Driver code
function printSafePrimes ( n ) { let prime = new Array ( n + 1 ) ; for ( let i = 2 ; i <= n ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == 1 ) { for ( let i = p * 2 ; i <= n ; i += p ) prime [ i ] = 0 ; } } for ( let i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] != 0 ) { let temp = ( 2 * i ) + 1 ; if ( temp <= n && prime [ temp ] != 0 ) prime [ temp ] = 2 ; } } for ( let i = 5 ; i <= n ; i ++ ) if ( prime [ i ] == 2 ) document . write ( i + " " ) ; } let n = 20 ; printSafePrimes ( n ) ;
Minimum multiplications with { 2 , 3 , 7 } to make two numbers equal | Function to find powers of 2 , 3 and 7 in x ; To keep count of each divisor ; To store the result ; Count powers of 2 in x ; Count powers of 3 in x ; Count powers of 7 in x ; Remaining number which is not divisible by 2 , 3 or 7 ; Function to return the minimum number of given operations required to make a and b equal ; a = x * 2 ^ a1 * 3 ^ a2 * 7 ^ a3 va [ 0 ] = a1 va [ 1 ] = a2 va [ 2 ] = a3 va [ 3 ] = x ; Similarly for b ; If a and b cannot be made equal with the given operation . Note that va [ 3 ] and vb [ 3 ] contain remaining numbers after repeated divisions with 2 , 3 and 7. If remaining numbers are not same then we cannot make them equal . ; Minimum number of operations required ; Driver code
function Divisors ( x ) { var c = 0 ; var v = [ ] ; while ( x % 2 == 0 ) { c ++ ; x /= 2 ; } v . push ( c ) ; c = 0 ; while ( x % 3 == 0 ) { c ++ ; x /= 3 ; } v . push ( c ) ; c = 0 ; while ( x % 7 == 0 ) { c ++ ; x /= 7 ; } v . push ( c ) ; v . push ( x ) ; return v ; } function MinOperations ( a , b ) { var va = Divisors ( a ) ; var vb = Divisors ( b ) ; if ( va [ 3 ] != vb [ 3 ] ) { return - 1 ; } var minOperations = Math . abs ( va [ 0 ] - vb [ 0 ] ) + Math . abs ( va [ 1 ] - vb [ 1 ] ) + Math . abs ( va [ 2 ] - vb [ 2 ] ) ; return minOperations ; } var a = 14 , b = 28 ; document . write ( MinOperations ( a , b ) ) ;
Product of N with its largest odd digit | Function to return the largest odd digit in n ; If all digits are even then - 1 will be returned ; Last digit from n ; If current digit is odd and > maxOdd ; Remove last digit ; Return the maximum odd digit ; Function to return the product of n with its largest odd digit ; If there are no odd digits in n ; Product of n with its largest odd digit ; Driver code
function largestOddDigit ( n ) { var maxOdd = - 1 ; while ( n > 0 ) { var digit = n % 10 ; if ( digit % 2 == 1 && digit > maxOdd ) maxOdd = digit ; n = n / 10 ; } return maxOdd ; } function getProduct ( n ) { var maxOdd = largestOddDigit ( n ) ; if ( maxOdd == - 1 ) return - 1 ; return ( n * maxOdd ) ; } var n = 12345 ; document . write ( getProduct ( n ) ) ;
Check if a number is perfect square without finding square root | Program to find if x is a perfect square . ; Check if mid is perfect square ; Mid is small -> go right to increase mid ; Mid is large -> to left to decrease mid ; Driver Code ; Function Call
function isPerfectSquare ( x ) { let left = 1 , right = x ; while ( left <= right ) { let mid = parseInt ( ( left + right ) / 2 ) ; if ( mid * mid == x ) { return true ; } if ( mid * mid < x ) { left = mid + 1 ; } else { right = mid - 1 ; } } return false ; } let x = 2500 ; if ( isPerfectSquare ( x ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of LCM ( 1 , n ) , LCM ( 2 , n ) , LCM ( 3 , n ) , ... , LCM ( n , n ) | javascript implementation of the approach ; Euler totient Function ; Function to return the required LCM sum ; Summation of d * ETF ( d ) where d belongs to set of divisors of n ; Driver code
var n = 1000002 ; var phi = Array ( n + 2 ) . fill ( 0 ) ; var ans = Array ( n + 2 ) . fill ( 0 ) ; function ETF ( ) { for ( i = 1 ; i <= n ; i ++ ) { phi [ i ] = i ; } for ( i = 2 ; i <= n ; i ++ ) { if ( phi [ i ] == i ) { phi [ i ] = i - 1 ; for ( j = 2 * i ; j <= n ; j += i ) { phi [ j ] = ( phi [ j ] * ( i - 1 ) ) / i ; } } } } function LcmSum ( m ) { ETF ( ) ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = i ; j <= n ; j += i ) { ans [ j ] += ( i * phi [ i ] ) ; } } var answer = ans [ m ] ; answer = ( answer + 1 ) * m ; answer = answer / 2 ; return answer ; } var m = 5 ; document . write ( LcmSum ( m ) ) ;
Permutations of string such that no two vowels are adjacent | Factorial of a number ; Function to find c ( n , r ) ; Function to count permutations of string such that no two vowels are adjacent ; Finding the frequencies of the characters ; finding the no . of vowels and consonants in given word ; finding places for the vowels ; ways to fill consonants 6 ! / 2 ! ; ways to put vowels 7 C5 x 5 ! ; Driver code
function factorial ( n ) { let fact = 1 ; for ( let i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } function ncr ( n , r ) { return Math . floor ( factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ) ; } function countWays ( str ) { let freq = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { freq [ i ] = 0 ; } let nvowels = 0 , nconsonants = 0 ; let vplaces , cways , vways ; for ( let i = 0 ; i < str . length ; i ++ ) ++ freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 ) nvowels += freq [ i ] ; else nconsonants += freq [ i ] ; } vplaces = nconsonants + 1 ; cways = factorial ( nconsonants ) ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 && freq [ i ] > 1 ) { cways = Math . floor ( cways / factorial ( freq [ i ] ) ) ; } } vways = ncr ( vplaces , nvowels ) * factorial ( nvowels ) ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 && freq [ i ] > 1 ) { vways = Math . floor ( vways / factorial ( freq [ i ] ) ) ; } } return cways * vways ; } let str = " " ; document . write ( countWays ( str ) ) ;
Pairs from an array that satisfy the given condition | Function to return the number of set bits in n ; Function to return the count of required pairs ; Set bits for first element of the pair ; Set bits for second element of the pair ; Set bits of the resultant number which is the XOR of both the elements of the pair ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver code
function setBits ( n ) { let count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } function countPairs ( a , n ) { let count = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let setbits_x = setBits ( a [ i ] ) ; for ( let j = i + 1 ; j < n ; j ++ ) { let setbits_y = setBits ( a [ j ] ) ; let setbits_xor_xy = setBits ( a [ i ] ^ a [ j ] ) ; if ( setbits_x + setbits_y == setbits_xor_xy ) count ++ ; } } return count ; } let a = [ 2 , 3 , 4 , 5 , 6 ] ; let n = a . length ; document . write ( countPairs ( a , n ) ) ;
Number of array elements derivable from D after performing certain operations | Function to return gcd of a and b ; * Function to Return the number of elements of arr which can be derived from * D by performing ( + A , - A , + B , - B ) ; find the gcd of A and B ; counter stores the number of array elements which can be derived from D ; arr [ i ] can be derived from D only if | arr [ i ] - D | is divisible by gcd of A and B ; Driver Code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function findPossibleDerivables ( arr , n , D , A , B ) { var gcdAB = gcd ( A , B ) ; var counter = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( Math . abs ( arr [ i ] - D ) % gcdAB ) == 0 ) { counter ++ ; } } return counter ; } var arr = [ 1 , 2 , 3 , 4 , 7 , 13 ] ; var n = arr . length ; var D = 5 , A = 4 , B = 2 ; document . write ( findPossibleDerivables ( arr , n , D , A , B ) + " " ) ; var a = [ 1 , 2 , 3 ] ; n = a . length ; D = 6 ; A = 3 ; B = 2 ; document . write ( findPossibleDerivables ( a , n , D , A , B ) ) ;
Find the sum of first N terms of the series 2 * 3 * 5 , 3 * 5 * 7 , 4 * 7 * 9 , ... | Function to return the sum of the first n terms of the given series ; As described in the approach ; Driver Code
function calSum ( n ) { return ( n * ( 2 * n * n * n + 12 * n * n + 25 * n + 21 ) ) / 2 ; } let n = 3 ; document . write ( calSum ( n ) ) ;
Find elements of array using XOR of consecutive elements | Function to find the array elements using XOR of consecutive elements ; Array to store the original elements ; first element a i . e elements [ 0 ] = a ; To get the next elements we have to calculate xor of previous elements with given xor of 2 consecutive elements . e . g . if a ^ b = k1 so to get b xor a both side . b = k1 ^ a ; Printing the original array elements ; Driver Code
function getElements ( a , arr , n ) { let elements = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { elements [ i ] = 0 ; } elements [ 0 ] = a ; for ( let i = 0 ; i < n ; i ++ ) { elements [ i + 1 ] = arr [ i ] ^ elements [ i ] ; } for ( let i = 0 ; i < n + 1 ; i ++ ) document . write ( elements [ i ] + " " ) ; } let arr = [ 13 , 2 , 6 , 1 ] ; let n = arr . length ; let a = 5 ; getElements ( a , arr , n ) ;
Maximum GCD of N integers with given product | Function to find maximum GCD of N integers with product P ; map to store prime factors of P ; prime factorization of P ; traverse all prime factors and multiply its 1 / N power to the result ; Driver code
function maxGCD ( N , P ) { let ans = 1 ; let prime_factors = new Map ( ) ; for ( let i = 2 ; i * i <= P ; i ++ ) { while ( P % i == 0 ) { if ( prime_factors . get ( i ) == null ) prime_factors . set ( i , 1 ) ; else prime_factors . set ( i , ( prime_factors . get ( i ) + 1 ) ) ; P = Math . floor ( P / i ) ; } } if ( P != 1 ) prime_factors [ P ] ++ ; console . log ( prime_factors ) for ( let v of prime_factors ) { console . log ( v ) ans *= Math . pow ( v [ 0 ] , Math . floor ( v [ 1 ] / N ) ) ; } return ans ; } let N = 3 , P = 24 ; document . write ( maxGCD ( N , P ) ) ;
Check if the sum of distinct digits of two integers are equal | Function to return the sum of distinct digits of a number ; Take last digit ; If digit has not been used before ; Set digit as used ; Remove last digit ; Function to check whether the sum of distinct digits of two numbers are equal ; Driver code
function distinctDigitSum ( n ) { var used = Array ( 10 ) . fill ( false ) ; var sum = 0 ; while ( n > 0 ) { var digit = n % 10 ; if ( ! used [ digit ] ) { used [ digit ] = true ; sum += digit ; } n = parseInt ( n / 10 ) ; } return sum ; } function checkSum ( m , n ) { var sumM = distinctDigitSum ( m ) ; var sumN = distinctDigitSum ( n ) ; if ( sumM == sumN ) return " " ; return " " ; } var m = 2452 , n = 9222 ; document . write ( checkSum ( m , n ) ) ;
All pairs whose xor gives unique prime | Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the count of valid pairs ; If xor ( a [ i ] , a [ j ] ) is prime and unique ; 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 countPairs ( a , n ) { let count = 0 ; let m = new Map ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( isPrime ( a [ i ] ^ a [ j ] ) && ! m . has ( a [ i ] ^ a [ j ] ) ) { m . set ( ( a [ i ] ^ a [ j ] ) , 1 ) ; count ++ ; } } } return count ; } let a = [ 10 , 12 , 23 , 45 , 5 , 6 ] ; let n = a . length ; document . write ( countPairs ( a , n ) ) ;
Find nth Hermite number | Utility function to calculate double factorial of a number ; Function to return nth Hermite number ; If n is even then return 0 ; If n is odd ; Calculate double factorial of ( n - 1 ) and multiply it with 2 ^ ( n / 2 ) ; If n / 2 is odd then nth Hermite number will be negative ; Return nth Hermite number ; Driver Code ; Print nth Hermite number
function doubleFactorial ( n ) { var fact = 1 ; for ( var i = 1 ; i <= n ; i = i + 2 ) { fact = fact * i ; } return fact ; } function hermiteNumber ( n ) { if ( n % 2 == 1 ) return 0 ; else { var number = ( Math . pow ( 2 , n / 2 ) ) * doubleFactorial ( n - 1 ) ; if ( ( n / 2 ) % 2 == 1 ) number = number * - 1 ; return number ; } } var n = 6 ; document . write ( hermiteNumber ( n ) ) ;
Sum of integers upto N with given unit digit | Function to return the required sum ; If the unit digit is d ; Driver code
function getSum ( n , d ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( i % 10 == d ) sum += i ; } return sum ; } let n = 30 ; let d = 3 ; document . write ( getSum ( n , d ) ) ;
Split a number into 3 parts such that none of the parts is divisible by 3 | javascript program to split a number into three parts such than none of them is divisible by 3. ; Print x = 1 , y = 1 and z = N - 2 ; Otherwise , prvar x = 1 , y = 2 and z = N - 3 ; Driver code
function printThreeParts ( N ) { if ( N % 3 == 0 ) document . write ( " " + ( N - 2 ) ) ; else document . write ( " " + ( N - 3 ) ) ; } var N = 10 ; printThreeParts ( N ) ;
Minimum absolute difference of a number and its closest prime | Function to check if a number is prime or not ; Function to find the minimum absolute difference between a number and its closest prime ; Variables to store first prime above and below N ; Finding first prime number greater than N ; Finding first prime number less than N ; Variables to store the differences ; Driver code
function isPrime ( N ) { for ( let i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } function getDifference ( N ) { if ( N == 0 ) return 2 ; else if ( N == 1 ) return 1 ; else if ( isPrime ( N ) ) return 0 ; let aboveN = - 1 ; let belowN = - 1 ; let n1 = N + 1 ; while ( true ) { if ( isPrime ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPrime ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } let diff1 = aboveN - N ; let diff2 = N - belowN ; return Math . min ( diff1 , diff2 ) ; } let N = 25 ; document . write ( getDifference ( N ) + " " ) ;
Check if the sum of perfect squares in an array is divisible by x | Function that returns true if the the sum of all the perfect squares of the given array is divisible by x ; If arr [ i ] is a perfect square ; Driver Code
function check ( arr , x , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let y = Math . sqrt ( arr [ i ] ) ; if ( Math . floor ( y ) == Math . ceil ( y ) ) { sum += arr [ i ] ; } } if ( sum % x == 0 ) return true ; else return false ; } let arr = [ 2 , 3 , 4 , 9 , 10 ] ; let n = arr . length ; let x = 13 ; if ( check ( arr , x , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Largest Divisor of a Number not divisible by a perfect square | Javascript Program to find the largest divisor not divisible by any perfect square greater than 1 ; Function to find the largest divisor not divisible by any perfect square greater than 1 ; set to store divisors in descending order ; If the number is divisible by i , then insert it ; Vector to store perfect squares ; Check for each divisor , if it is not divisible by any perfect square , simply return it as the answer . ; Driver code
let MAX = 100000 ; function findLargestDivisor ( n ) { let m = n ; let s = new Set ( ) ; s . add ( 1 ) ; s . add ( n ) ; for ( let i = 2 ; i < Math . floor ( Math . sqrt ( n ) ) + 1 ; i ++ ) { if ( n % i == 0 ) { s . add ( Math . floor ( n / i ) ) ; s . add ( i ) ; while ( m % i == 0 ) m = Math . floor ( m / i ) ; } } if ( m > 1 ) s . add ( m ) ; let l = Array . from ( s ) ; l . sort ( function ( a , b ) { return a - b ; } ) ; l . reverse ( ) ; let vec = [ ] ; for ( let i = 2 ; i <= MAX ; i ++ ) vec . push ( i * i ) ; for ( let d = 0 ; d < l . length ; d ++ ) { let divi = 0 ; for ( let j = 0 ; j < vec . length && vec [ j ] <= l [ d ] ; j ++ ) { if ( l [ d ] % vec [ j ] == 0 ) { divi = 1 ; break ; } } if ( divi == 0 ) return l [ d ] ; } return 0 ; } let n = 12 ; document . write ( findLargestDivisor ( n ) + " " ) ; n = 97 ; document . write ( findLargestDivisor ( n ) + " " ) ;
Find the Number of Maximum Product Quadruples | Returns the number of ways to select r objects out of available n choices ; ncr = ( n * ( n - 1 ) * ( n - 2 ) * ... . . ... ( n - r + 1 ) ) / ( r * ( r - 1 ) * ... * 1 ) ; Returns the number of quadruples having maximum product ; stores the frequency of each element ; remaining_choices denotes the remaining elements to select inorder to form quadruple ; Getting an iterator ; If Frequency of element < remaining choices , select all of these elements , else select only the number of elements required ; Decrement remaining_choices acc to the number of the current elements selected ; if the quadruple is formed stop the algorithm ; Driver Code
function NCR ( n , r ) { let numerator = 1 ; let denominator = 1 ; while ( r > 0 ) { numerator *= n ; denominator *= r ; n -- ; r -- ; } return Math . floor ( numerator / denominator ) ; } function findWays ( arr , n ) { let count = new Map ( ) ; if ( n < 4 ) return 0 ; for ( let i = 0 ; i < n ; i ++ ) { count . set ( arr [ i ] , ( count . get ( arr [ i ] ) == null ? 0 : count . get ( arr [ i ] ) + 1 ) ) ; } let remaining_choices = 4 ; let ans = 1 ; for ( let [ key , value ] of count . entries ( ) ) { let number = key ; let frequency = value ; let toSelect = Math . min ( remaining_choices , frequency ) ; ans = ans * NCR ( frequency , toSelect ) ; remaining_choices -= toSelect ; if ( remaining_choices == 0 ) { break ; } } return ans ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 5 ] ; let n = arr . length ; let maxQuadrupleWays = findWays ( arr , n ) ; document . write ( maxQuadrupleWays ) ;