codeparrot/xlcost-text-to-code huggingface dataset:
Count Magic squares in a grid | JavaScript program to count magic squares ;  ; function to check is subgrid is Magic Square ; Elements of grid must contain all numbers from 1 to 9 , sum of all rows , columns and diagonals must be same , i . e . , 15. ; Function to count total Magic square subgrids ; if condition true skip check ; check for magic square subgrid ; return total magic square ; Driver program ; function call to print required answer
var R = 3 ; var C = 4 ; function eqSet ( as , bs ) { if ( as . size !== bs . size ) return false ; for ( var a of as ) if ( ! bs . has ( a ) ) return false ; return true ; } function magic ( a , b , c , d , e , f , g , h , i ) { var s1 = new Set ( [ a , b , c , d , e , f , g , h , i ] ) ; var s2 = new Set ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ) ; if ( eqSet ( s1 , s2 ) && ( a + b + c ) == 15 && ( d + e + f ) == 15 && ( g + h + i ) == 15 && ( a + d + g ) == 15 && ( b + e + h ) == 15 && ( c + f + i ) == 15 && ( a + e + i ) == 15 && ( c + e + g ) == 15 ) return true ; return false ; } function CountMagicSquare ( Grid ) { var ans = 0 ; for ( var i = 0 ; i < R - 2 ; i ++ ) for ( var j = 0 ; j < C - 2 ; j ++ ) { if ( Grid [ i + 1 ] [ j + 1 ] != 5 ) continue ; if ( magic ( Grid [ i ] [ j ] , Grid [ i ] [ j + 1 ] , Grid [ i ] [ j + 2 ] , Grid [ i + 1 ] [ j ] , Grid [ i + 1 ] [ j + 1 ] , Grid [ i + 1 ] [ j + 2 ] , Grid [ i + 2 ] [ j ] , Grid [ i + 2 ] [ j + 1 ] , Grid [ i + 2 ] [ j + 2 ] ) ) ans += 1 ; } return ans ; } var G = [ [ 4 , 3 , 8 , 4 ] , [ 9 , 5 , 1 , 9 ] , [ 2 , 7 , 6 , 2 ] ] ; document . write ( CountMagicSquare ( G ) ) ;
Sum of minimum elements of all subarrays | Function to return required minimum sum ; getting number of element strictly larger than A [ i ] on Left . ; get elements from stack until element greater than A [ i ] found ; getting number of element larger than A [ i ] on Right . ; get elements from stack until element greater or equal to A [ i ] found ; calculating required resultult ; Driver program ; function call to get required resultult
function sumSubarrayMins ( A , n ) { var left = Array ( n ) , right = Array ( n ) ; var s1 = [ ] , s2 = [ ] ; for ( var i = 0 ; i < n ; ++ i ) { var cnt = 1 ; while ( s1 . length != 0 && ( s1 [ s1 . length - 1 ] [ 0 ] ) > A [ i ] ) { cnt += s1 [ s1 . length - 1 ] [ 1 ] ; s1 . pop ( ) ; } s1 . push ( [ A [ i ] , cnt ] ) ; left [ i ] = cnt ; } for ( var i = n - 1 ; i >= 0 ; -- i ) { var cnt = 1 ; while ( s2 . length != 0 && ( s2 [ s2 . length - 1 ] [ 0 ] ) >= A [ i ] ) { cnt += s2 [ s2 . length - 1 ] [ 1 ] ; s2 . pop ( ) ; } s2 . push ( [ A [ i ] , cnt ] ) ; right [ i ] = cnt ; } var result = 0 ; for ( var i = 0 ; i < n ; ++ i ) result = ( result + A [ i ] * left [ i ] * right [ i ] ) ; return result ; } var A = [ 3 , 1 , 2 , 4 ] ; var n = A . length ; document . write ( sumSubarrayMins ( A , n ) ) ;
Minimum and Maximum element of an array which is divisible by a given number k | Function to find the minimum element ; Function to find the maximum element ; Driver code
function getMin ( arr , n , k ) { let res = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) res = Math . min ( res , arr [ i ] ) ; } return res ; } function getMax ( arr , n , k ) { let res = Number . MIN_VALUE ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) res = Math . max ( res , arr [ i ] ) ; } return res ; } let arr = [ 10 , 1230 , 45 , 67 , 1 ] ; let k = 10 ; let n = arr . length ; document . write ( " " + getMin ( arr , n , k ) + " " ) ; document . write ( " " + getMax ( arr , n , k ) ) ;
Print a number containing K digits with digital root D | Function to find a number ; If d is 0 k has to be 1 ; Print k - 1 zeroes ; Driver Code
function printNumberWithDR ( k , d ) { if ( d == 0 && k != 1 ) document . write ( " " ) ; else { document . write ( d ) ; k -- ; while ( k -- > 0 ) document . write ( " " ) ; } } var k = 4 , d = 4 ; printNumberWithDR ( k , d ) ;
Count number of integers less than or equal to N which has exactly 9 divisors | Function to count numbers having exactly 9 divisors ; Sieve array ; initially prime [ i ] = i ; use sieve concept to store the first prime factor of every number ; mark all factors of i ; check for all numbers if they can be expressed in form p * q ; p prime factor ; q prime factor ; if both prime factors are different if p * q <= n and q != ; Check if it can be expressed as p ^ 8 ; Driver Code
function countNumbers ( n ) { let c = 0 ; let limit = parseInt ( Math . sqrt ( n ) , 10 ) ; let prime = new Array ( limit + 1 ) ; prime . fill ( 0 ) ; for ( let i = 1 ; i <= limit ; i ++ ) { prime [ i ] = i ; } for ( let i = 2 ; i * i <= limit ; i ++ ) { if ( prime [ i ] == i ) { for ( let j = i * i ; j <= limit ; j += i ) { if ( prime [ j ] == j ) { prime [ j ] = i ; } } } } for ( let i = 2 ; i <= limit ; i ++ ) { let p = prime [ i ] ; let q = prime [ parseInt ( i / prime [ i ] , 10 ) ] ; if ( p * q == i && q != 1 && p != q ) { c += 1 ; } else if ( prime [ i ] == i ) { if ( Math . pow ( i , 8 ) <= n ) { c += 1 ; } } } return c ; } let n = 1000 ; document . write ( countNumbers ( n ) ) ;
Interprime | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the given number is interprime or not ; Smallest Interprime is 4 So the number less than 4 can not be a Interprime ; Calculate first prime number < n ; Calculate first prime number > n ; Check if prev_prime and next_prime have the same average ; 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 ( var i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function isInterprime ( n ) { if ( n < 4 ) return false ; var prev_prime = n ; var next_prime = n ; while ( ! isPrime ( prev_prime ) ) { prev_prime -- ; } while ( ! isPrime ( next_prime ) ) { next_prime ++ ; } if ( ( prev_prime + next_prime ) == 2 * n ) return true ; else return false ; } var n = 9 ; if ( isInterprime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the unit place digit of sum of N factorials | Function to find the unit 's place digit ; Let us write for cases when N is smaller than or equal to 4. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ) % 10 = 3 else ( N >= 4 ) ; Driver code
function get_unit_digit ( N ) { if ( N == 0 N == 1 ) return 1 ; else if ( N == 2 ) return 3 ; else if ( N == 3 ) return 9 ; return 3 ; } var N = 1 ; for ( N = 0 ; N <= 10 ; N ++ ) document . write ( " " + N + " " + get_unit_digit ( N ) + " " )
Sum of squares of Fibonacci numbers | Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Function to calculate sum of squares of Fibonacci numbers ; Driver Code
let f = new Array ( 1000 ) ; f . fill ( 0 ) ; function fib ( n ) { if ( n == 0 ) { return 0 ; } if ( n == 1 n == 2 ) { return ( f [ n ] = 1 ) ; } if ( f [ n ] > 0 ) { return f [ n ] ; } let k = ( ( n & 1 ) > 0 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( ( n & 1 ) > 0 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } function calculateSumOfSquares ( n ) { return fib ( n ) * fib ( n + 1 ) ; } let n = 6 ; document . write ( " " + calculateSumOfSquares ( n ) ) ;
Number of solutions for the equation x + y + z <= n | Function to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; To store answer ; for values of x ; for values of y ; Maximum possible value of z ; If z value greater than equals to 0 then only it is valid ; Find minimum of temp and z ; Return required answer ; Driver Code
function NumberOfSolutions ( x , y , z , n ) { var ans = 0 ; for ( var i = 0 ; i <= x ; i ++ ) { for ( var j = 0 ; j <= y ; j ++ ) { var temp = n - i - j ; if ( temp >= 0 ) { temp = Math . min ( temp , z ) ; ans += temp + 1 ; } } } return ans ; } var x = 1 , y = 2 , z = 3 , n = 4 ; document . write ( NumberOfSolutions ( x , y , z , n ) ) ;
Program to find the Nth term of series 5 , 12 , 21 , 32 , 45. ... . . | calculate Nth term of series ; Driver code Get N
function nthTerm ( n ) { return Math . pow ( n , 2 ) + 4 * n ; } let N = 4 ; document . write ( nthTerm ( N ) ) ;
Numbers less than N which are product of exactly two distinct prime numbers | Function to check whether a number is a PerfectSquare or not ; Function to check if a number is a product of exactly two distinct primes ; Function to find numbers that are product of exactly two distinct prime numbers . ; Vector to store such numbers ; insert in the vector ; Print all numbers till n from the vector ; Driver function
function isPerfectSquare ( x ) { sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function isProduct ( num ) { var cnt = 0 ; for ( var i = 2 ; cnt < 2 && ( i * i ) <= num ; ++ i ) { while ( num % i == 0 ) { num = parseInt ( num / i ) ; ++ cnt ; } } if ( num > 1 ) ++ cnt ; return cnt == 2 ; } function findNumbers ( N ) { vec = [ ] ; for ( var i = 1 ; i <= N ; i ++ ) { if ( isProduct ( i ) && ! isPerfectSquare ( i ) ) { vec . push ( i ) ; } } for ( var i = 0 ; i < vec . length ; i ++ ) { document . write ( vec [ i ] + " " ) ; } } var N = 30 ; findNumbers ( N ) ;
Program to find the Nth term of the series 3 , 20 , 63 , 144 , 230 , â €¦ â €¦ | calculate Nth term of series ; return final sum ; Driver code
function nthTerm ( n ) { return 2 * Math . pow ( n , 3 ) + Math . pow ( n , 2 ) ; } let N = 3 ; document . write ( nthTerm ( N ) ) ;
Find Nth number of the series 1 , 6 , 15 , 28 , 45 , ... . . | function to return nth term of the series ; Taking n as 4 ; Printing the nth term
function NthTerm ( n ) { var x = ( 2 * n * n ) % 1000000009 ; return ( x - n + 1000000009 ) % 1000000009 ; } var N = 4 ; document . write ( NthTerm ( N ) ) ;
Program to print the Sum of series | calculate Nth term of series ; Get the value of N ; Get the sum of the series
function findSum ( N ) { return ( N * ( N + 1 ) * ( 2 * N - 5 ) + 4 * N ) / 2 ; } var N = 3 ; document . write ( findSum ( N ) ) ;
Program to find the Nth term of series | calculate Nth term of series ; Get the value of N ; Find the Nth term and print it
function nthTerm ( N ) { return ( ( 3 * N * N ) - ( 6 * N ) + 2 ) ; } let N = 3 ; document . write ( nthTerm ( N ) ) ;
Program to find the value of tan ( nÎ ˜ ) | Javascript program to find the value of cos ( n - theta ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; store required answer ; use to toggle sign in sequence . ; calculate numerator ; calculate denominator ; Driver code .
MAX = 16 var nCr = Array . from ( Array ( MAX ) , ( ) => new Array ( MAX ) ) ; function binomial ( ) { for ( var i = 0 ; i < MAX ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } function findTanNTheta ( tanTheta , n ) { ans = 0 , numerator = 0 , denominator = 0 ; toggle = 1 ; for ( var i = 1 ; i <= n ; i += 2 ) { numerator = numerator + nCr [ n ] [ i ] * Math . pow ( tanTheta , i ) * toggle ; toggle = toggle * - 1 ; } denominator = 1 ; toggle = - 1 ; for ( var i = 2 ; i <= n ; i += 2 ) { numerator = numerator + nCr [ n ] [ i ] * Math . pow ( tanTheta , i ) * toggle ; toggle = toggle * - 1 ; } ans = numerator / denominator ; return ans . toFixed ( 5 ) ; } binomial ( ) ; var tanTheta = 0.3 ; var n = 10 ; document . write ( findTanNTheta ( tanTheta , n ) ) ;
Check if any large number is divisible by 19 or not | Function to check if the number is divisible by 19 or not ; Extracting the last digit ; Truncating the number ; Subtracting the five times the last digit from the remaining number ; Return n is divisible by 19 ; Driver code
function isDivisible ( n ) { while ( parseInt ( n / 100 ) > 0 ) { var d = n % 10 ; n = parseInt ( n / 10 ) ; n += d * 2 ; } return ( n % 19 == 0 ) ; } var n = 101156 ; if ( isDivisible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find Sum of the series 1 * 3 + 3 * 5 + ... . | Javascript program to find sum of first n terms ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; number of terms to be included in the sum ; find the Sn
function calculateSum ( n ) { return ( n * ( 4 * n * n + 6 * n - 1 ) / 3 ) ; } let n = 4 ; document . write ( " " + calculateSum ( n ) ) ;
Triplet with no element divisible by 3 and sum N | Function to print a , b and c ; first loop ; check for 1 st number ; second loop ; check for 2 nd number ; third loop ; Check for 3 rd number ; Driver Code
function printCombination ( n ) { for ( let i = 1 ; i < n ; i ++ ) { if ( i % 3 != 0 ) { for ( let j = 1 ; j < n ; j ++ ) { if ( j % 3 != 0 ) { for ( let k = 1 ; k < n ; k ++ ) { if ( k % 3 != 0 && ( i + j + k ) == n ) { document . write ( i + " " + j + " " + k ) ; return ; } } } } } } } let n = 233 ; printCombination ( n ) ;
Program to find the percentage of difference between two numbers | Function to calculate the percentage ; Driver Code . ; Function calling
function percent ( a , b ) { var result = 0 ; result = ( ( b - a ) * 100 ) / a ; return result ; } var a = 20 , b = 25 ; document . write ( percent ( a , b ) + " " ) ;
Numbers with sum of digits equal to the sum of digits of its all prime factor | maximum size of number ; array to store smallest prime factor of number ; array to store sum of digits of a number ; boolean array to check given number is countable for required answer or not . ; prefix array to store answer ; Calculating SPF ( SmallestPrime Factor ) for every number till MAXN . ; 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 ; Function to find sum of digits in a number ; find sum of digits of all numbers up to MAXN ; add sum of digits of least prime factor and n / spf [ n ] ; if it is valid make isValid true ; prefix sum to compute answer ; Driver code ; declaration ; print answer for required range ; print answer for required range
var MAXN = 100005 ; var spf = Array . from ( { length : MAXN } , ( _ , i ) => 0 ) ; var sum_digits = Array . from ( { length : MAXN } , ( _ , i ) => 0 ) ; var isValid = Array . from ( { length : MAXN } , ( _ , i ) => false ) ; var ans = Array . from ( { length : MAXN } , ( _ , i ) => 0 ) ; function Smallest_prime_factor ( ) { for ( i = 1 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( i = 3 ; i * i <= MAXN ; i += 2 ) if ( spf [ i ] == i ) for ( j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } function Digit_Sum ( copy ) { var d = 0 ; while ( copy > 0 ) { d += copy % 10 ; copy = parseInt ( copy / 10 ) ; } return d ; } function Sum_Of_All_Digits ( ) { for ( n = 2 ; n < MAXN ; n ++ ) { sum_digits [ n ] = sum_digits [ parseInt ( n / spf [ n ] ) ] + Digit_Sum ( spf [ n ] ) ; if ( Digit_Sum ( n ) == sum_digits [ n ] ) isValid [ n ] = true ; } for ( n = 2 ; n < MAXN ; n ++ ) { if ( isValid [ n ] ) ans [ n ] = 1 ; ans [ n ] += ans [ n - 1 ] ; } } Smallest_prime_factor ( ) ; Sum_Of_All_Digits ( ) ; var l , r ; l = 2 ; r = 3 ; document . write ( " " + l + " " + r + " " + ( ans [ r ] - ans [ l - 1 ] ) ) ; l = 2 ; r = 10 ; document . write ( " " + l + " " + r + " " + ( ans [ r ] - ans [ l - 1 ] ) ) ;
Count numbers which can be represented as sum of same parity primes | Function to calculate count ; Driver code
function calculate ( ar , size ) { var count = 0 ; for ( i = 0 ; i < size ; i ++ ) if ( ar [ i ] % 2 == 0 && ar [ i ] != 0 && ar [ i ] != 2 ) count ++ ; return count ; } var a = [ 1 , 3 , 4 , 6 ] ; var size = a . length ; document . write ( calculate ( a , size ) ) ;
N | Javascript program to answer queries for N - th prime factor of a number ; 2 - D vector that stores prime factors ; Function to pre - store prime factors of all numbers till 10 ^ 6 ; calculate unique prime factors for every number till 10 ^ 6 ; find prime factors ; store if prime factor ; Function that returns answer for every query ; Function to pre - store unique prime factors ; 1 st query ; 2 nd query ; 3 rd query ; 4 th query
const N = 1000001 ; let v = new Array ( ) ; for ( let i = 0 ; i < N ; i ++ ) { v . push ( new Array ( ) ) } function preprocess ( ) { for ( let i = 1 ; i < N ; i ++ ) { let num = i ; for ( let j = 2 ; j <= Math . sqrt ( num ) ; j ++ ) { if ( num % j == 0 ) { v [ i ] . push ( j ) ; while ( num % j == 0 ) { num = num / j ; } } } if ( num > 2 ) v [ i ] . push ( num ) ; } } function query ( number , n ) { return v [ number ] [ n - 1 ] ; } preprocess ( ) ; let number = 6 , n = 1 ; document . write ( query ( number , n ) + " " ) ; number = 210 , n = 3 ; document . write ( query ( number , n ) + " " ) ; number = 210 , n = 2 ; document . write ( query ( number , n ) + " " ) ; number = 60 , n = 2 ; document . write ( query ( number , n ) + " " ) ;
Program to find HCF ( Highest Common Factor ) of 2 Numbers | Recursive function to return gcd of a and b ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } var a = 98 , b = 56 ; document . write ( " " + a + " " + b + " " + gcd ( a , b ) ) ;
Largest number with maximum trailing nines which is less than N and greater than N | It 's better to use var to handle big integers function to count no. of digits ; function to implement above approach ; if difference between power and n doesn 't exceed d ; loop to build a number from the appropriate no of digits containing only 9 ; if the build number is same as original number ( n ) ; observation ; Driver Code ; variable that stores no of digits in n
function dig ( a ) { var count = 0 ; while ( a > 0 ) { a /= 10 ; count ++ ; } return count ; } function required_number ( num , n , d ) { var i , j , power = 1 , a , flag = 0 ; for ( i = num ; i >= 1 ; i -- ) { power = Math . pow ( 10 , i ) ; a = n % power ; if ( d > a ) { flag = 1 ; break ; } } if ( flag > 0 ) { var t = 0 ; for ( j = 0 ; j < i ; j ++ ) { t += 9 * Math . pow ( 10 , j ) ; } if ( n % power == t ) document . write ( n ) ; else { document . write ( n - ( n % power ) - 1 ) ; } } else document . write ( n ) ; } var n = 1029 , d = 102 ; var num = dig ( n ) ; required_number ( num , n , d ) ;
Egg Dropping Puzzle with 2 Eggs and K Floors | JavaScript program to find optimal number of trials for k floors and 2 eggs . ; Driver Code
function twoEggDrop ( k ) { return Math . ceil ( ( - 1.0 + Math . sqrt ( 1 + 8 * k ) ) / 2.0 ) ; } var k = 100 ; document . write ( twoEggDrop ( k ) ) ;
Program to find the Area and Volume of Icosahedron | Function to find area of Icosahedron ; Formula to calculating area ; Function to find volume of Icosahedron ; Formula to calculating volume ; Driver Code ; Function call to find area of Icosahedron . ; Function call to find volume of Icosahedron .
function findArea ( a ) { let area ; area = 5 * Math . sqrt ( 3 ) * a * a ; return area ; } function findVolume ( a ) { let volume ; volume = ( 5 / 12 ) * ( 3 + Math . sqrt ( 5 ) ) * a * a * a ; return volume ; } let a = 5 ; document . write ( " " + findArea ( a ) . toFixed ( 3 ) + " " ) ; document . write ( " " + findVolume ( a ) . toFixed ( 3 ) ) ;
Total number of ways to place X and Y at n places such that no two X are together | Function to return number of ways ; for n = 1 ; for n = 2 ; iterate to find Fibonacci term ; total number of places
function ways ( n ) { var first = 2 ; var second = 3 ; var res = 0 ; for ( i = 3 ; i <= n ; i ++ ) { res = first + second ; first = second ; second = res ; } return res ; } var n = 7 ; document . write ( " " + ways ( n ) ) ;
Number of digits in N factorial to the power N | Javascript program to find count of digits in N factorial raised to N ; we take sum of logarithms as explained in the approach ; multiply the result with n ; Driver Code
function countDigits ( n ) { let ans = 0 ; for ( let i = 1 ; i <= n ; i ++ ) ans += Math . log10 ( i ) ; ans = ans * n ; return 1 + Math . floor ( ans ) ; } let n = 4 ; document . write ( countDigits ( n ) + " " ) ;
Program to convert centimeter into meter and kilometer | Driver Code ; Converting centimeter into meter and kilometer
let cm , meter , kilometer ; cm = 1000 ; meter = cm / 100.0 ; kilometer = cm / 100000.0 ; document . write ( " " + meter + " " + " " ) ; document . write ( " " + kilometer + " " + " " ) ;
Check if two people starting from different points ever meet | Javascript program to find if two people starting from different positions ever meet or not . ; If speed of a person at a position before other person is smaller , then return false . ; Making sure that x1 is greater ; Checking if relative speed is a factor of relative distance or not ;  ; Driver code
function everMeet ( x1 , x2 , v1 , v2 ) { if ( x1 < x2 && v1 <= v2 ) { return false ; } if ( x1 > x2 && v1 >= v2 ) { return false ; } if ( x1 < x2 ) { swap ( x1 , x2 ) ; swap ( v1 , v2 ) ; } return ( ( x1 - x2 ) % ( v1 - v2 ) == 0 ) ; } function swap ( a , b ) { let t = a ; a = b ; b = t ; } let x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ; if ( everMeet ( x1 , x2 , v1 , v2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count of distinct GCDs among all the non |  ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; calculate largest number in A and mapping A to Mp ; iterate over all possible values of GCD ; variable to check current GCD ; iterate over all multiples of i ; If j is present in A ; calculate gcd of all encountered multiples of i ; current GCD is possible ; return answer ; Input ; Function calling
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function distinctGCDs ( arr , N ) { let M = - 1 , ans = 0 ; var Mp = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { M = Math . max ( M , arr [ i ] ) ; Mp . set ( arr [ i ] , 1 ) ; } for ( let i = 1 ; i <= M ; i ++ ) { let currGcd = 0 ; for ( let j = i ; j <= M ; j += i ) { if ( Mp . has ( j ) ) { currGcd = gcd ( currGcd , j ) ; if ( currGcd == i ) { ans ++ ; break ; } } } } return ans ; } let arr = [ 3 , 11 , 14 , 6 , 12 ] ; let N = arr . length ; document . write ( distinctGCDs ( arr , N ) + ' ' ) ;
Find ceil of a / b without using ceil ( ) function | taking input 1 ; example of perfect division taking input 2
var a = 4 ; var b = 3 ; var val = ( a + b - 1 ) / b ; document . write ( " " + val + " " ) ; a = 6 ; b = 3 ; val = parseInt ( ( a + b - 1 ) / b ) ; document . write ( " " + val ) ;
Sum of range in a series of first odd then even natural numbers | Function that returns sum in the range 1 to x in the sequence 1 3 5 7. ... . N 2 4 6. . . N - 1 ; number of odd numbers ; number of extra even numbers required ; Driver code
function sumTillX ( x , n ) { let odd = Math . ceil ( n / 2.0 ) ; if ( x <= odd ) return x * x ; let even = x - odd ; return ( ( odd * odd ) + ( even * even ) + even ) ; } function rangeSum ( N , L , R ) { return sumTillX ( R , N ) - sumTillX ( L - 1 , N ) ; } let N = 10 , L = 1 , R = 6 ; document . write ( rangeSum ( N , L , R ) ) ;
Twin Prime Numbers between 1 and n | JavaScript program to print all Twin Prime Numbers using Sieve of Eratosthenes ; 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 ; to check for twin prime numbers display th twin prime ; Driver program ; calling the function
function printTwinPrime ( n ) { let prime = [ ] ; for ( let i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( let i = 2 ; i <= n - 2 ; i ++ ) { if ( prime [ i ] == true && prime [ i + 2 ] == true ) document . write ( " " + i + " " + ( i + 2 ) + " " ) ; } } let n = 25 ; printTwinPrime ( n ) ;