codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all odd factors of numbers in the range [ l , r ] | Javascript implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code
var MAX = 100001 ; prefix = Array ( MAX ) . fill ( 0 ) function sieve_modified ( ) { for ( var i = 1 ; i < MAX ; i += 2 ) { for ( var j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( var i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } function sumOddFactors ( L , R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } sieve_modified ( ) ; var l = 6 , r = 10 ; document . write ( sumOddFactors ( l , r ) ) ;
Number of ways in which an item returns back to its initial position in N swaps in array of size K | Javascript program to find the number of ways in which an item returns back to its initial position in N swaps in an array of size K ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return the number of ways ; Base case ; Recursive case F ( n ) = ( k - 1 ) ^ ( n - 1 ) - F ( n - 1 ) . ; Driver code ; Function calling
let mod = 1000000007 ; function power ( x , y ) { let p = mod ; let res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } function solve ( n , k ) { if ( n == 1 ) return 0 ; return ( power ( ( k - 1 ) , n - 1 ) % mod - solve ( n - 1 , k ) + mod ) % mod ; } let n = 4 , k = 5 ; document . write ( solve ( n , k ) ) ;
XOR of a submatrix queries | Javascript implementation of the approach ; Function to pre - compute the xor ; Left to right prefix xor for each row ; Top to bottom prefix xor for each column ; Function to process the queries x1 , x2 , y1 , y2 represent the positions of the top - left and bottom right corners ; To store the xor values ; Finding the values we need to xor with value at ( x2 , y2 ) in prefix - xor matrix ; Return the required prefix xor ; Driver code ; To store pre - computed xor ; Pre - computing xor ; Queries
var n = 3 ; function preComputeXor ( arr , prefix_xor ) { for ( i = 0 ; i < n ; i ++ ) for ( j = 0 ; j < n ; j ++ ) { if ( j == 0 ) prefix_xor [ i ] [ j ] = arr [ i ] [ j ] ; else prefix_xor [ i ] [ j ] = ( prefix_xor [ i ] [ j - 1 ] ^ arr [ i ] [ j ] ) ; } for ( i = 0 ; i < n ; i ++ ) for ( j = 1 ; j < n ; j ++ ) prefix_xor [ j ] [ i ] = ( prefix_xor [ j - 1 ] [ i ] ^ prefix_xor [ j ] [ i ] ) ; } function ansQuerie ( prefix_xor , x1 , y1 , x2 , y2 ) { var xor_1 = 0 , xor_2 = 0 , xor_3 = 0 ; if ( x1 != 0 ) xor_1 = prefix_xor [ x1 - 1 ] [ y2 ] ; if ( y1 != 0 ) xor_2 = prefix_xor [ x2 ] [ y1 - 1 ] ; if ( x1 != 0 && y1 != 0 ) xor_3 = prefix_xor [ x1 - 1 ] [ y1 - 1 ] ; return ( ( prefix_xor [ x2 ] [ y2 ] ^ xor_1 ) ^ ( xor_2 ^ xor_3 ) ) ; } var arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; var prefix_xor = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) prefix_xor [ i ] = Array ( n ) . fill ( 0 ) ; preComputeXor ( arr , prefix_xor ) ; document . write ( ansQuerie ( prefix_xor , 1 , 1 , 2 , 2 ) + " " ) ; document . write ( ansQuerie ( prefix_xor , 1 , 2 , 2 , 2 ) ) ;
Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1 | Function that returns true if the eventual digit sum of number nm is 1 ; if reminder will 1 then eventual sum is 1 ; Function to print the required numbers less than n ; If it is the required perfect cube ; Driver code
function isDigitSumOne ( nm ) { if ( nm % 9 == 1 ) return true ; else return false ; } function printValidNums ( n ) { let cbrt_n = Math . ceil ( Math . pow ( n , 1 / 3 ) ) ; for ( let i = 1 ; i <= cbrt_n ; i ++ ) { let cube = Math . pow ( i , 3 ) ; if ( cube >= 1 && cube <= n && isDigitSumOne ( cube ) ) document . write ( cube + " " ) ; } } let n = 1000 ; printValidNums ( n ) ;
Count the number of rhombi possible inside a rectangle of given size | Function to return the count of rhombi possible ; All possible diagonal lengths ; Update rhombi possible with the current diagonal lengths ; Return the total count of rhombi possible ; Driver code
function countRhombi ( h , w ) { let ct = 0 ; for ( let i = 2 ; i <= h ; i += 2 ) for ( let j = 2 ; j <= w ; j += 2 ) ct += ( h - i + 1 ) * ( w - j + 1 ) ; return ct ; } let h = 2 , w = 2 ; document . write ( countRhombi ( h , w ) ) ;
Program to calculate the area between two Concentric Circles | Function to find nth concentric hexagon number ; Declare value of pi ; Calculate area of outer circle ; Calculate area of inner circle ; Difference in areas ; Driver code
function calculateArea ( x , y ) { var pi = 3.1415926536 ; var arx = pi * x * x ; var ary = pi * y * y ; return arx - ary ; } var x = 2 ; var y = 1 ; document . write ( calculateArea ( x , y ) ) ;
Find the winner by adding Pairwise difference of elements in the array until Possible | Function to calculate gcd ; Function to return the winner of the game ; To store the gcd of the original array ; To store the maximum element from the original array ; If number of moves are odd ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function getWinner ( arr , n ) { let gcd = arr [ 0 ] ; let maxEle = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { gcd = __gcd ( gcd , arr [ i ] ) ; maxEle = Math . max ( maxEle , arr [ i ] ) ; } let totalMoves = parseInt ( maxEle / gcd , 10 ) - n ; if ( totalMoves % 2 == 1 ) return ' ' ; return ' ' ; } let arr = [ 5 , 6 , 7 ] ; let n = arr . length ; document . write ( getWinner ( arr , n ) ) ;
Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | Function to return the count of required pairs ; Number which will give the max value for ( ( n % i ) % j ) % n ; To store the maximum possible value of ( ( n % i ) % j ) % n ; To store the count of possible pairs ; Check all possible pairs ; Calculating the value of ( ( n % i ) % j ) % n ; If value is equal to maximum ; Return the number of possible pairs ; Driver code
function countPairs ( n ) { let num = ( parseInt ( n / 2 , 10 ) + 1 ) ; let max = n % num ; let count = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { let val = ( ( n % i ) % j ) % n ; if ( val == max ) count ++ ; } } return count ; } let n = 5 ; document . write ( countPairs ( n ) ) ;
Program to check if a number is divisible by any of its digits | Function to check if given number is divisible by any of its digits ; Check if any of digit divides n ; Check if K divides N ; Driver Code
function isDivisible ( n ) { temp = n ; while ( n ) { k = n % 10 ; if ( temp % k == 0 ) return " " ; n = Math . floor ( n / 10 ) ; } return " " ; } let n = 9876543 ; document . write ( isDivisible ( n ) ) ;
Program to find sum of harmonic series | Function to return sum of harmonic series ; Driver code
function sum ( n ) { let i , s = 0.0 ; for ( i = 1 ; i <= n ; i ++ ) s = s + 1 / i ; return s ; } let n = 5 ; document . write ( " " + sum ( n ) ) ;
Check if a number can be expressed as sum two abundant numbers | javascript program to check if number n is expressed as sum of two abundant numbers ; Function to return all abundant numbers This function will be helpful for multiple queries ; To store abundant numbers ; to store sum of the divisors include 1 in the sum ; if j is proper divisor ; if i is not a perfect square ; if sum is greater than i then i is a abundant number ; Check if number n is expressed as sum of two abundant numbers ; if both i and n - i are abundant numbers ; can not be expressed ; Driver code
var N = 100005 ; function ABUNDANT ( ) { var v = new Set ( ) ; var i , j ; for ( i = 1 ; i < N ; i ++ ) { var sum = 1 ; for ( j = 2 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { sum += j ; if ( parseInt ( i / j ) != j ) sum += parseInt ( i / j ) ; } } if ( sum > i ) v . add ( i ) ; } return v ; } function SumOfAbundant ( n ) { var v = new Set ( ) ; v = ABUNDANT ( ) ; var i ; for ( i = 1 ; i <= n ; i ++ ) { if ( v . has ( i ) && v . has ( n - i ) ) { document . write ( i + ' ' + ( n - i ) ) return ; } } document . write ( - 1 ) ; } var n = 24 ; SumOfAbundant ( n ) ;
Find nth term of the series 5 2 13 41 | function to calculate nth term of the series ; to store the nth term of series ; if n is even number ; if n is odd number ; return nth term ; Driver code
function nthTermOfTheSeries ( n ) { let nthTerm ; if ( n % 2 == 0 ) nthTerm = Math . pow ( n - 1 , 2 ) + n ; else nthTerm = Math . pow ( n + 1 , 2 ) + n ; return nthTerm ; } let n ; n = 8 ; document . write ( nthTermOfTheSeries ( n ) + " " ) ; n = 12 ; document . write ( nthTermOfTheSeries ( n ) + " " ) ; n = 102 ; document . write ( nthTermOfTheSeries ( n ) + " " ) ; n = 999 ; document . write ( nthTermOfTheSeries ( n ) + " " ) ; n = 9999 ; document . write ( nthTermOfTheSeries ( n ) + " " ) ;
Find cost price from given selling price and profit or loss percentage | Function to calculate cost price with profit ; required formula to calculate CP with profit ; Function to calculate cost price with loss ; required formula to calculate CP with loss ; Driver code
function CPwithProfit ( sellingPrice , profit ) { var costPrice ; costPrice = ( sellingPrice * 100 ) / ( 100 + profit ) ; return costPrice ; } function CPwithLoss ( sellingPrice , loss ) { var costPrice ; costPrice = ( sellingPrice * 100 ) / ( 100 - loss ) ; return costPrice ; } var SP , profit , loss ; SP = 1020 ; profit = 20 ; document . write ( " " + CPwithProfit ( SP , profit ) + " " ) ; SP = 900 ; loss = 10 ; document . write ( " " + CPwithLoss ( SP , loss ) + " " ) ; SP = 42039 ; profit = 8 ; document . write ( " " + CPwithProfit ( SP , profit ) + " " ) ;
Find the n | Function to return the nth string in the required sequence ; Length of the resultant string ; Relative Index ; Initial string of length len consists of all a 's since the list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Utility reverse function ; Driver code
function obtain_str ( n ) { let len = Math . floor ( ( Math . log ( n + 1 ) / Math . log ( 2 ) ) ) ; let rel_ind = n + 1 - Math . pow ( 2 , len ) ; let i = 0 ; let str = [ ] ; for ( i = 0 ; i < len ; i ++ ) { str . push ( ' ' ) ; } i = 0 ; while ( rel_ind > 0 ) { if ( rel_ind % 2 == 1 ) str [ i ] = ' ' ; rel_ind = parseInt ( rel_ind / 2 , 10 ) ; i ++ ; } return reverse ( str . join ( " " ) ) ; } function reverse ( input ) { let a = input . split ( ' ' ) ; let l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } let n = 11 ; document . write ( obtain_str ( n ) ) ;
Program to find the Nth term of the series 0 , 3 / 1 , 8 / 3 , 15 / 5. . ... ... | Function to return the nth term of the given series ; nth term ; Driver code
function Nthterm ( n ) { let numerator = Math . pow ( n , 2 ) - 1 ; let denomenator = 2 * n - 3 ; document . write ( numerator + " " + denomenator ) ; } let n = 3 ; Nthterm ( n ) ;
Sum of each element raised to ( prime | Function to return the required sum ; Driver code
function getSum ( arr , p ) { return arr . length ; } let arr = [ 5 , 6 , 8 ] ; let p = 7 ; document . write ( getSum ( arr , p ) ) ;
Count numbers upto N which are both perfect square and perfect cube | Function to return required count ; Driver Code ; function call to print required answer
function SquareCube ( N ) { let cnt = 0 , i = 1 ; while ( Math . floor ( Math . pow ( i , 6 ) ) <= N ) { ++ cnt ; ++ i ; } return cnt ; } let N = 100000 ; document . write ( SquareCube ( N ) ) ;
Sum of integers upto N with given unit digit | Function to return the required sum ; Driver code
function getSum ( n , d ) { let sum = 0 ; while ( d <= n ) { sum += d ; d += 10 ; } return sum ; } let n = 30 ; let d = 3 ; document . write ( getSum ( n , d ) ) ;
Summing the sum series | Function to calculate twice of sum of first N natural numbers ; Function to calculate the terms of summing of sum series ; Driver Code
function sum ( N ) { let MOD = 1000000007 ; let val = N * ( N + 1 ) ; val = val % MOD ; return val ; } function sumX ( N , M , K ) { let MOD = 1000000007 ; for ( let i = 0 ; i < M ; i ++ ) { N = sum ( K + N ) ; } N = N % MOD ; return N ; } let N = 1 ; let M = 2 ; let K = 3 ; document . write ( sumX ( N , M , K ) ) ;
Mode | Function that sort input array a [ ] and calculate mode and median using counting sort . ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Driver code
function printMode ( a , n ) { let max = Number . MIN_VALUE ; for ( let i = 0 ; i < a . length ; i ++ ) { max = Math . max ( max , a [ i ] ) ; } let t = max + 1 ; let count = new Array ( t ) ; for ( let i = 0 ; i < t ; i ++ ) count [ i ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; let mode = 0 ; let k = count [ 0 ] ; for ( let i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } document . write ( " " + mode ) ; } let a = [ 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 ] ; let n = a . length ; printMode ( a , n ) ;
Harmonic Progression | JavaScript program to check if a given array can form harmonic progression ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . , first Sort the reciprocal array , then check difference between consecutive elements ; series to check whether it is in H . P ; Checking a series is in H . P or not
function checkIsHP ( arr ) { let n = arr . length ; if ( n == 1 ) { return true ; } let rec = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { rec . push ( ( 1 / arr [ i ] ) ) ; } rec . sort ( ( a , b ) => a - b ) ; let d = ( rec [ 1 ] ) - ( rec [ 0 ] ) ; for ( let i = 2 ; i < n ; i ++ ) { if ( rec [ i ] - rec [ i - 1 ] != d ) { return false ; } } return true ; } let arr = [ 1 / 5 , 1 / 10 , 1 / 15 , 1 / 20 , 1 / 25 ] ; if ( checkIsHP ( arr ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Arithmetic Mean | Prints N arithmetic means between A and B . ; Calculate common difference ( d ) ; For finding N the arithmetic mean between A and B ; Driver code
function printAMeans ( A , B , N ) { let d = ( B - A ) / ( N + 1 ) ; for ( let i = 1 ; i <= N ; i ++ ) document . write ( ( A + i * d ) + " " ) ; } let A = 20 , B = 32 , N = 5 ; printAMeans ( A , B , N ) ;
Prime Factor | A function to print all prime factors of a given number n ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver code
function primeFactors ( n ) { while ( n % 2 == 0 ) { document . write ( 2 + " " ) ; n = Math . floor ( n / 2 ) ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { document . write ( i + " " ) ; n = Math . floor ( n / i ) ; } } if ( n > 2 ) document . write ( n + " " ) ; } let n = 315 ; primeFactors ( n ) ;
Time taken by two persons to meet on a circular track | Function to return the time when both the persons will meet at the starting point ; Time to cover 1 round by both ; Finding LCM to get the meeting point ; Function to return the time when both the persons will meet for the first time ; Driver Code ; Calling function
function startingPoint ( Length , Speed1 , Speed2 ) { var result1 = 0 , result2 = 0 ; var time1 = Length / Speed1 ; var time2 = Length / Speed2 ; result1 = __gcd ( time1 , time2 ) ; result2 = time1 * time2 / ( result1 ) ; return result2 ; } function __gcd ( a , b ) { if ( b == 0 ) { return a ; } return __gcd ( b , a % b ) ; } function firstTime ( Length , Speed1 , Speed2 ) { var result = 0 ; var relativeSpeed = Math . abs ( Speed1 - Speed2 ) ; result = ( Length / relativeSpeed ) ; return result ; } var L = 30 , S1 = 5 , S2 = 2 ; var first_Time = firstTime ( L , S1 , S2 ) ; var starting_Povar = startingPoint ( L , S1 , S2 ) ; document . write ( " " + first_Time + " " ) ; document . write ( " " + starting_Povar + " " ) ;
Check if the array has an element which is equal to product of remaining elements | Function to Check if the array has an element which is equal to product of all the remaining elements ; Calculate the product of all the elements ; Return true if any such element is found ; If no element is found ; Driver code
function CheckArray ( arr , n ) { let prod = 1 ; for ( let i = 0 ; i < n ; ++ i ) prod *= arr [ i ] ; for ( let i = 0 ; i < n ; ++ i ) if ( arr [ i ] == prod / arr [ i ] ) return true ; return false ; } let arr = [ 1 , 2 , 12 , 3 , 2 ] ; let n = arr . length ; if ( CheckArray ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of common divisors of two numbers A and B | print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; print the sum of common factors
function sum ( a , b ) { var sum = 0 ; for ( var i = 1 ; i <= Math . min ( a , b ) ; i ++ ) if ( a % i == 0 && b % i == 0 ) sum += i ; return sum ; } var A = 10 , B = 15 ; document . write ( " " + sum ( A , B ) + " " ) ;
Minimum number of cuts required to make circle segments equal sized | Recursive function to return gcd of two nos ; Function to find the minimum number of additional cuts required to make circle segments are equal sized ; Sort the array ; Initial gcd value ; Including the last segment ; Driver code
function findgcd ( a , b ) { if ( b == 0 ) return a ; return findgcd ( b , a % b ) ; } function minimumCuts ( a , n ) { a . sort ( ) ; var gcd = a [ 1 ] - a [ 0 ] ; var s = gcd ; for ( i = 2 ; i < n ; i ++ ) { gcd = findgcd ( gcd , a [ i ] - a [ i - 1 ] ) ; s += a [ i ] - a [ i - 1 ] ; } if ( 360 - s > 0 ) gcd = findgcd ( gcd , 360 - s ) ; return ( 360 / gcd ) - n ; } var arr = [ 30 , 60 , 180 ] ; var n = arr . length ; document . write ( minimumCuts ( arr , n ) ) ;
Find a number that divides maximum array elements | JavaScript program to find a number that divides maximum array elements ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; Function to find a number that divides maximum array elements ; precalculating Smallest Prime Factor ; Hash to store frequency of each divisors ; Traverse the array and get spf of each element ; calling getFactorization function ; Returns Set view ; Driver program
let MAXN = 100001 ; let spf = new Array ( MAXN ) ; for ( let i = 0 ; i < MAXN ; i ++ ) { spf [ i ] = 0 ; } function sieve ( ) { spf [ 1 ] = 1 ; for ( let i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( let i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( let i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( let j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } function getFactorization ( x ) { let ret = [ ] ; while ( x != 1 ) { let temp = spf [ x ] ; ret . push ( temp ) ; while ( x % temp == 0 ) x = Math . floor ( x / temp ) ; } return ret ; } function maxElement ( A , n ) { sieve ( ) ; let m = new Map ( ) ; for ( let j = 0 ; j < n ; ++ j ) { let p = getFactorization ( A [ j ] ) ; for ( let i = 0 ; i < p . length ; i ++ ) m . set ( p [ i ] , m . get ( p [ i ] ) == null ? 0 : m . get ( p [ i ] ) + 1 ) ; } let cnt = 0 , ans = 10000000 ; for ( let [ key , value ] of m . entries ( ) ) { if ( value >= cnt ) { cnt = value ; if ( ans > key ) ans = key ; else ans = ans ; } } return ans ; } let A = [ 2 , 5 , 10 ] ; let n = A . length ; document . write ( maxElement ( A , n ) ) ;
Find Selling Price from given Profit Percentage and Cost | Function to calculate the Selling Price ; Decimal Equivalent of Profit Percentage ; Find the Selling Price ; return the calculated Selling Price ; Get the CP and Profit % ; Printing the returned value
function SellingPrice ( CP , PP ) { var P_decimal = 1 + ( PP / 100 ) ; var res = P_decimal * CP ; return res . toFixed ( 1 ) ; } var C = 720 , P = 13 ; document . write ( SellingPrice ( C , P ) ) ;
Product of all the Composite Numbers in an array | Function that returns the the product of all composite numbers ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find the product of all composite numbers in the arr [ ] ; Driver code
function compositeProduct ( arr , n ) { let max_val = arr . sort ( ( A , B ) => B - A ) [ 0 ] ; let prime = new Array ( max_val + 1 ) . fill ( true ) ; prime [ 0 ] = true ; prime [ 1 ] = true ; for ( let p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } let product = 1 ; for ( let i = 0 ; i < n ; i ++ ) if ( ! prime [ arr [ i ] ] ) { product *= arr [ i ] ; } return product ; } let arr = new Array ( 2 , 3 , 4 , 5 , 6 , 7 ) ; let n = arr . length ; document . write ( compositeProduct ( arr , n ) ) ;
Primality test for the sum of digits at odd places of a number | Function that return sum of the digits at odd places ; Function that returns true if the number is prime else false ; Corner cases ; This condition is checked so that we can skip middle five numbers in the below loop ; Driver code ; Get the sum of the digits at odd places
function sum_odd ( n ) { let sum = 0 , pos = 1 ; while ( n ) { if ( pos % 2 == 1 ) sum += n % 10 ; n = Math . floor ( n / 10 ) ; pos ++ ; } return sum ; } function check_prime ( 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 ; } let n = 223 ; let sum = sum_odd ( n ) ; if ( check_prime ( sum ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Find amount to be added to achieve target ratio in a given mixture | Javascript program to find amount of water to be added to achieve given target ratio . ; Driver code
function findAmount ( X , W , Y ) { return ( X * ( Y - W ) ) / ( 100 - Y ) ; } let X = 100 , W = 50 , Y = 60 ; document . write ( " " + findAmount ( X , W , Y ) . toFixed ( 2 ) ) ;
Check if a number is a Mystery Number | Finds reverse of given num x . ; if found print the pair , return
function reverseNum ( x ) { let s = x . toString ( ) ; let str = " " ; for ( let i = s . length - 1 ; i >= 0 ; i -- ) { str = str + s [ i ] ; } let rev = parseInt ( str ) ; return rev ; } function isMysteryNumber ( n ) { for ( let i = 1 ; i <= Math . floor ( n / 2 ) ; i ++ ) { let j = reverseNum ( i ) ; if ( i + j == n ) { document . write ( i + " " + j + " " ) ; return true ; } } document . write ( " " ) ; return false ; } let n = 121 ; isMysteryNumber ( n ) ;
Replace every element of the array by product of all other elements | Javascript program to Replace every element by the product of all other elements ; Calculate the product of all the elements ; Replace every element product of all other elements ; Driver Code ; Print the modified array .
function ReplaceElements ( arr , n ) { let prod = 1 ; for ( let i = 0 ; i < n ; ++ i ) { prod *= arr [ i ] ; } for ( let i = 0 ; i < n ; ++ i ) { arr [ i ] = parseInt ( prod / arr [ i ] , 10 ) ; } } let arr = [ 2 , 3 , 3 , 5 , 7 ] ; let n = arr . length ; ReplaceElements ( arr , n ) ; for ( let i = 0 ; i < n ; ++ i ) { document . write ( arr [ i ] + " " ) ; }