codeparrot/xlcost-text-to-code huggingface dataset:
Split the array into odd number of segments of odd lengths | Function to check ; Check the result by processing the first & last element and size ; Driver code
function checkArray ( arr , n ) { return ( ( arr [ 0 ] % 2 ) > 0 && ( arr [ n - 1 ] % 2 ) > 0 && ( n % 2 ) > 0 ) ? 1 : 0 ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var n = arr . length ; document . write ( checkArray ( arr , n ) ) ;
Minimum removals to make array sum odd | Function to find minimum removals ; Count odd numbers ; If the counter is odd return 0 otherwise return 1 ; Driver Code
function findCount ( arr , n ) { var countOdd = 0 ; for ( var i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 1 ) countOdd ++ ; if ( countOdd % 2 == 0 ) return 1 ; else return 0 ; } var arr = [ 1 , 2 , 3 , 5 , 1 ] ; var n = arr . length ; document . write ( findCount ( arr , n ) ) ;
Check whether the number can be made perfect square after adding 1 | Function that returns true if x is a perfect square ; Find floating point value of square root of x ; If square root is an integer ; Function that returns true if n is a sunny number ; If ( n + 1 ) is a perfect square ; Driver code
function isPerfectSquare ( x ) { let sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function isSunnyNum ( n ) { if ( isPerfectSquare ( n + 1 ) ) return true ; return false ; } let n = 3 ; if ( isSunnyNum ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count primes that can be expressed as sum of two consecutive primes and 1 | JavaScript implementation of the approach ; To check if a number is prime or not ; To store possible numbers ; Function to return all prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count all possible prime numbers that can be expressed as the sum of two consecutive primes and one ; All possible prime numbers below N ; Driver code
let N = 10005 ; let isprime = new Array ( N ) . fill ( true ) ; let can = new Array ( N ) . fill ( false ) ; function SieveOfEratosthenes ( ) { for ( let p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { for ( let i = p * p ; i < N ; i += p ) isprime [ i ] = false ; } } let primes = new Array ( ) ; for ( let i = 2 ; i < N ; i ++ ) if ( isprime [ i ] ) primes . push ( i ) ; return primes ; } function Prime_Numbers ( n ) { let primes = SieveOfEratosthenes ( ) ; for ( let i = 0 ; i < primes . length - 1 ; i ++ ) if ( primes [ i ] + primes [ i + 1 ] + 1 < N ) can [ primes [ i ] + primes [ i + 1 ] + 1 ] = true ; let ans = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( can [ i ] && isprime [ i ] ) { ans ++ ; } } return ans ; } let n = 50 ; document . write ( Prime_Numbers ( n ) ) ;
Sum of bitwise AND of all subarrays | Function to find the sum of bitwise AND of all subarrays ; variable to store the final sum ; multiplier ; variable to check if counting is on ; variable to store the length of the subarrays ; loop to find the contiguous segments ; updating the multiplier ; returning the sum ; Driver Code
function findAndSum ( arr , n ) { var sum = 0 ; var mul = 1 ; for ( var i = 0 ; i < 30 ; i ++ ) { var count_on = 0 ; var l = 0 ; for ( var j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) if ( count_on ) l ++ ; else { count_on = 1 ; l ++ ; } else if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = 0 ; l = 0 ; } } if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = 0 ; l = 0 ; } mul *= 2 ; } return sum ; } var arr = [ 7 , 1 , 1 , 5 ] ; var n = arr . length ; document . write ( findAndSum ( arr , n ) ) ;
Source to destination in 2 | Function that returns true if it is possible to move from source to the destination with the given moves ; Driver code
function isPossible ( Sx , Sy , Dx , Dy , x , y ) { if ( Math . abs ( Sx - Dx ) % x == 0 && Math . abs ( Sy - Dy ) % y == 0 && ( Math . abs ( Sx - Dx ) / x ) % 2 == ( Math . abs ( Sy - Dy ) / y ) % 2 ) return true ; return false ; } let Sx = 0 , Sy = 0 , Dx = 0 , Dy = 0 ; let x = 3 , y = 4 ; if ( isPossible ( Sx , Sy , Dx , Dy , x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of pairs ( x , y ) in an array such that x < y | Function to return the number of pairs ( x , y ) such that x < y ; To store the number of valid pairs ; If a valid pair is found ; Return the count of valid pairs ; Driver code
function getPairs ( a ) { let count = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { for ( let j = 0 ; j < a . length ; j ++ ) { if ( a [ i ] < a [ j ] ) count ++ ; } } return count ; } let a = [ 2 , 4 , 3 , 1 ] ; document . write ( getPairs ( a ) ) ;
Composite numbers with digit sum 1 | Function that returns true if number n is a composite number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if the eventual digit sum of number nm is 1 ; Loop till the sum is not single digit number ; Intitialize the sum as zero ; Find the sum of digits ; If sum is eventually 1 ; Function to print the required numbers from the given range ; If i is one of the required numbers ; Driver code
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 isDigitSumOne ( nm ) { while ( nm > 9 ) { let sum_digit = 0 ; while ( nm > 0 ) { let digit = nm % 10 ; sum_digit = sum_digit + digit ; nm = Math . floor ( nm / 10 ) ; } nm = sum_digit ; } if ( nm == 1 ) return true ; else return false ; } function printValidNums ( l , r ) { for ( let i = l ; i <= r ; i ++ ) { if ( isComposite ( i ) && isDigitSumOne ( i ) ) document . write ( i + " " ) ; } } let l = 10 , r = 100 ; printValidNums ( l , r ) ;
Determine the count of Leaf nodes in an N | Function to calculate leaf nodes in n - ary tree ; Driver code
function calcNodes ( N , I ) { var result = 0 ; result = I * ( N - 1 ) + 1 ; return result ; } var N = 5 , I = 2 ; document . write ( " " + calcNodes ( N , I ) ) ;
An application on Bertrand 's ballot theorem | Javascript implementation of the approach ; Function to calculate factorial of a number mod 1000000007 ; Factorial of i = factorial of ( i - 1 ) * i ; ; Taking mod along with calculation . ; Function for modular exponentiation ; If p is odd ; If p is even ; Function to return the count of required permutations ; Calculating multiplicative modular inverse for x ! and multiplying with ans ; Calculating multiplicative modular inverse for y ! and multiplying with ans ; Pre - compute factorials
let mod = 1000000007 ; let arr = new Array ( 1000001 ) ; arr . fill ( 0 ) ; function cal_factorial ( ) { arr [ 0 ] = 1 ; for ( let i = 1 ; i <= 1000000 ; i ++ ) { arr [ i ] = ( ( arr [ i - 1 ] * i ) % mod ) ; } } function mod_exponent ( num , p ) { if ( p != 0 ) return 1 ; if ( ( p & 1 ) != 0 ) { return ( ( num % mod ) * ( mod_exponent ( ( num * num ) % mod , parseInt ( p / 2 , 10 ) ) ) % mod ) % mod ; } else return ( mod_exponent ( ( num * num ) % mod , parseInt ( p / 2 , 10 ) ) ) % mod ; } function getCount ( x , y ) { let ans = arr [ x + y - 1 ] ; ans *= 0 * mod_exponent ( arr [ x ] , mod - 2 ) ; ans ++ ; ans %= mod ; ans *= mod_exponent ( arr [ y ] , mod - 2 ) ; ans %= mod ; ans *= ( x - y ) ; ans %= mod ; return ans ; } cal_factorial ( ) ; let x = 3 , y = 1 ; document . write ( getCount ( x , y ) ) ;
Find the values of X and Y in the Given Equations | Function to find the values of X and Y ; base condition ; required answer ; Driver Code
function findValues ( a , b ) { if ( ( a - b ) % 2 == 1 ) { document . write ( " " ) ; return ; } document . write ( ( a - b ) / 2 + " " + ( a + b ) / 2 ) ; } let a = 12 ; let b = 8 ; findValues ( a , b ) ;
Count triplet pairs ( A , B , C ) of points in 2 | Function to return the count of possible triplets ; Insert all the points in a set ; If the mid point exists in the set ; Return the count of valid triplets ; Driver code
function countTriplets ( n , points ) { var pts = new Set ( ) ; var ct = 0 ; for ( var i = 0 ; i < n ; i ++ ) pts . add ( points [ i ] . toString ( ) ) ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) { var x = points [ i ] [ 0 ] + points [ j ] [ 0 ] ; var y = points [ i ] [ 1 ] + points [ j ] [ 1 ] ; if ( x % 2 == 0 && y % 2 == 0 ) if ( pts . has ( [ ( x / 2 ) , ( y / 2 ) ] . toString ( ) ) ) ct ++ ; } return ct ; } var points = [ [ 1 , 1 ] , [ 2 , 2 ] , [ 3 , 3 ] ] ; var n = points . length ; document . write ( countTriplets ( n , points ) )
Concentration of juice after mixing n glasses in equal proportion | Function to return the concentration of the resultant mixture ; Driver Code
function mixtureConcentration ( n , p ) { var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) res += p [ i ] ; res /= n ; return res ; } var arr = [ 0 , 20 , 20 ] ; var n = arr . length ; document . write ( mixtureConcentration ( n , arr ) . toFixed ( 4 ) ) ;
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B | JavaScript implementation of the approach ; Function that returns true if the num contains a and b digits only ; Modular Exponentiation ; Function to return the modular inverse of x modulo MOD ; Function to return the required count of numbers ; Generating factorials of all numbers ; Generating inverse of factorials modulo MOD of all numbers ; Keeping a as largest number ; Iterate over all possible values of s and if it is a valid S then proceed further ; Check for invalid cases in the equation ; Find answer using combinatorics ; Add this result to final answer ; Driver Code
const MAX = ( 5 ) ; let MOD = ( 7 ) ; function check ( num , a , b ) { while ( num > 0 ) { var rem = num % 10 ; num = parseInt ( num / 10 ) ; if ( rem != a && rem != b ) return false ; } return true ; } function power ( x , y ) { var ans = 1 ; while ( y > 0 ) { if ( ( y & 1 ) > 0 ) ans = ( ans * x ) % MOD ; y >>= 1 ; x = ( x * x ) % MOD ; } return ans % MOD ; } function modInverse ( x ) { return power ( x , MOD - 2 ) ; } function countNumbers ( n , a , b ) { var fact = Array ( MAX ) . fill ( 0 ) ; var inv = Array ( MAX ) . fill ( 0 ) ; var ans = 0 ; fact [ 0 ] = 1 ; for ( var i = 1 ; i < MAX ; i ++ ) { fact [ i ] = ( 1 * fact [ i - 1 ] * i ) ; fact [ i ] %= MOD ; } inv [ MAX - 1 ] = modInverse ( fact [ MAX - 1 ] ) ; for ( i = MAX - 2 ; i >= 0 ; i -- ) { inv [ i ] = ( inv [ i + 1 ] * ( i + 1 ) ) ; inv [ i ] %= MOD ; } if ( a < b ) { var x = a ; a = b ; b = x ; } for ( s = n ; s <= 9 * n ; s ++ ) { if ( ! check ( s , a , b ) ) continue ; if ( s < n * b || ( s - n * b ) % ( a - b ) != 0 ) continue ; var numDig = parseInt ( ( ( s - n * b ) / ( a - b ) ) ) ; if ( numDig > n ) continue ; var curr = fact [ parseInt ( n ) ] ; curr = ( curr * inv [ numDig ] ) % MOD ; curr = ( curr * inv [ parseInt ( n - numDig ) ] ) % MOD ; ans = ( ans + curr ) % MOD ; } return ans ; } var n = 3 , a = 1 , b = 3 ; document . write ( countNumbers ( n , a , b ) ) ;
Number of elements with even factors in the given range | Function to count the perfect squares ; Driver code
function countOddSquares ( n , m ) { return Math . pow ( m , 0.5 ) - Math . pow ( n - 1 , 0.5 ) ; } var n = 5 , m = 100 ; document . write ( " " + ( ( m - n + 1 ) - countOddSquares ( n , m ) ) ) ;
Total position where king can reach on a chessboard in exactly M moves | Function to return the number of squares that the king can reach in the given number of moves ; Calculate initial and final coordinates ; Since chessboard is of size 8 X8 so if any coordinate is less than 1 or greater than 8 make it 1 or 8. ; Calculate total positions ; Driver code
function Square ( row , column , moves ) { var a = 0 , b = 0 , c = 0 , d = 0 , total = 0 ; a = row - moves ; b = row + moves ; c = column - moves ; d = column + moves ; if ( a < 1 ) a = 1 ; if ( c < 1 ) c = 1 ; if ( b > 8 ) b = 8 ; if ( d > 8 ) d = 8 ; total = ( b - a + 1 ) * ( d - c + 1 ) - 1 ; return total ; } var R = 4 , C = 5 , M = 2 ; document . write ( Square ( R , C , M ) ) ;
Find M | Function to find the M - th number whosesum till one digit is N ; Driver Code
function findNumber ( n , m ) { var num = ( m - 1 ) * 9 + n ; return num ; } var n = 2 , m = 5 ; document . write ( findNumber ( n , m ) ) ;
Check if a number can be represented as a sum of 2 triangular numbers | Function to check if it is possible or not ; Store all triangular numbers up to N in a Set ; Check if a pair exists ; Driver Code
function checkTriangularSumRepresentation ( n ) { var tri = new Set ( ) ; var i = 1 ; while ( 1 ) { var x = i * parseInt ( ( i + 1 ) / 2 ) ; if ( x >= n ) break ; tri . add ( x ) ; i ++ ; } var ans = false ; tri . forEach ( tm => { if ( tri . has ( n - tm ) ) ans = true } ) ; return ans ; } var n = 24 ; checkTriangularSumRepresentation ( n ) ? document . write ( " " ) : document . write ( " " ) ;
Absolute difference between the first X and last X Digits of N | Function to find the number of digits in the integer ; Function to find the absolute difference ; Store the last x digits in last ; Count the no . of digits in N ; Remove the digits except the first x ; Store the first x digits in first ; Return the absolute difference between the first and last ; driver program
function digitsCount ( n ) { let len = 0 ; while ( n > 0 ) { len ++ ; n = Math . floor ( n / 10 ) ; } return len ; } function absoluteFirstLast ( n , x ) { let i = 0 , mod = 1 ; while ( i < x ) { mod *= 10 ; i ++ ; } let last = n % mod ; let len = digitsCount ( n ) ; while ( len != x ) { n = Math . floor ( n / 10 ) ; len -- ; } let first = n ; return Math . abs ( first - last ) ; } let n = 21546 , x = 2 ; document . write ( absoluteFirstLast ( n , x ) ) ;
Generate minimum sum sequence of integers with even elements greater | Function to print the required sequence ; arr [ ] will hold the sequence sum variable will store the sum of the sequence ; If sum of the sequence is odd ; Print the sequence ; Driver Code
function make_sequence ( N ) { var arr = Array ( N + 1 ) , sum = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 1 ) arr [ i ] = 1 ; else arr [ i ] = 2 ; sum += arr [ i ] ; } if ( sum % 2 == 1 ) arr [ 2 ] = 3 ; for ( var i = 1 ; i <= N ; i ++ ) document . write ( arr [ i ] + " " ) ; } var N = 9 ; make_sequence ( N ) ;
Count Odd and Even numbers in a range from L to R | Return the number of odd numbers in the range [ L , R ] ; if either R or L is odd ; Driver Code
function countOdd ( L , R ) { let N = Math . floor ( ( R - L ) / 2 ) ; if ( R % 2 != 0 L % 2 != 0 ) N += 1 ; return N ; } let L = 3 , R = 7 ; let odds = countOdd ( L , R ) ; let evens = ( R - L + 1 ) - odds ; document . write ( " " + odds + " " ) ; document . write ( " " + evens + " " ) ;
Cost of painting n * m grid | Function to return the minimum cost ; Driver Code
function getMinCost ( n , m ) { let cost = ( n - 1 ) * m + ( m - 1 ) * n ; return cost ; } let n = 4 , m = 5 ; document . write ( getMinCost ( n , m ) ) ;
Minimum operations required to make all the array elements equal | Function to return the minimum number of given operation required to make all the array elements equal ; Check if all the elements from kth index to last are equal ; Finding the 1 st element which is not equal to the kth element ; Driver code
function minOperation ( n , k , a ) { for ( i = k ; i < n ; i ++ ) { if ( a [ i ] != a [ k - 1 ] ) document . write ( - 1 ) ; } for ( i = k - 2 ; i > - 1 ; i -- ) { if ( a [ i ] != a [ k - 1 ] ) document . write ( i + 1 ) ; } } var n = 5 ; var k = 3 ; var a = [ 2 , 1 , 1 , 1 , 1 ] ; minOperation ( n , k , a ) ;
Number of Simple Graph with N Vertices and M Edges | Function to return the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * ... * 1 ] ; Driver Code
function binomialCoeff ( n , k ) { if ( k > n ) return 0 ; var res = 1 ; if ( k > n - k ) k = n - k ; for ( var i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } var N = 5 , M = 1 ; var P = ( N * ( N - 1 ) ) / 2 ; document . write ( binomialCoeff ( P , M ) ) ;
Increasing sequence with given GCD | Function to print the required sequence ; Driver Code
function generateSequence ( n , g ) { for ( var i = 1 ; i <= n ; i ++ ) { document . write ( i * g + " " ) ; } } var n = 6 , g = 5 ; generateSequence ( n , g ) ;
Program to find LCM of two Fibonnaci Numbers | Javascript program to find LCM of Fib ( a ) and Fib ( b ) ; Create an array for memoization ; Function to return the n 'th Fibonacci number using table f[]. ; Base cases ; If fib ( n ) is already computed ; Applying recursive formula Note value n & 1 is 1 if n is odd , else 0. ; Function to return gcd of a and b ; Function to return the LCM of Fib ( a ) and Fib ( a ) ; Driver code
let MAX = 1000 ; let f = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { f [ i ] = 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 = 0 ; if ( ( n & 1 ) != 0 ) k = ( n + 1 ) / 2 ; else k = n / 2 ; if ( ( n & 1 ) != 0 ) f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) ; else f [ n ] = ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function findLCMFibonacci ( a , b ) { return ( fib ( a ) * fib ( b ) ) / fib ( gcd ( a , b ) ) ; } let a = 3 , b = 12 ; document . write ( findLCMFibonacci ( a , b ) ) ;
Check whether XOR of all numbers in a given range is even or odd | Function to check if XOR of all numbers in range [ L , R ] is Even or Odd ; Count odd Numbers in range [ L , R ] ; Check if count of odd Numbers is even or odd ; Driver Code
function isEvenOrOdd ( L , R ) { let oddCount = Math . floor ( ( R - L ) / 2 ) ; if ( R % 2 == 1 L % 2 == 1 ) oddCount ++ ; if ( oddCount % 2 == 0 ) return " " ; else return " " ; } let L = 5 , R = 15 ; document . write ( isEvenOrOdd ( L , R ) ) ;
Count number of trailing zeros in ( 1 ^ 1 ) * ( 2 ^ 2 ) * ( 3 ^ 3 ) * ( 4 ^ 4 ) * . . | Function to return the number of trailing zeros ; To store the number of 2 s and 5 s ; If we get a factor 2 then we have i number of 2 s because the power of the number is raised to i ; If we get a factor 5 then we have i number of 5 s because the power of the number is raised to i ; Take the minimum of them ; Driver code
function trailing_zeros ( N ) { let count_of_two = 0 , count_of_five = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { let val = i ; while ( val % 2 == 0 && val > 0 ) { val = parseInt ( val / 2 ) ; count_of_two += i ; } while ( val % 5 == 0 && val > 0 ) { val = parseInt ( val / 5 ) ; count_of_five += i ; } } let ans = Math . min ( count_of_two , count_of_five ) ; return ans ; } let N = 12 ; document . write ( trailing_zeros ( N ) ) ;
Print numbers such that no two consecutive numbers are co | Javascript implementation of the approach ; Function to generate Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Add the prime numbers to the array b ; Function to return the gcd of a and b ; Function to print the required sequence of integers ; Including the primes in a series of primes which will be later multiplied ; This is done to mark a product as existing ; Maximum number of primes that we consider ; For different interval ; For different starting index of jump ; For storing the numbers ; Checking for occurrence of a product . Also checking for the same prime occurring consecutively ; Including the primes in a series of primes which will be later multiplied ; Driver Code
let limit = 1000000000 let MAX_PRIME = 2000000 let MAX = 1000000 let I_MAX = 50000 let mp = new Map ( ) ; let b = new Array ( MAX ) ; let p = new Array ( MAX ) ; let j = 0 ; let prime = new Array ( MAX_PRIME + 1 ) ; function SieveOfEratosthenes ( n ) { prime . fill ( true ) ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( let p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { b [ j ++ ] = p ; } } } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function printSeries ( n ) { SieveOfEratosthenes ( MAX_PRIME ) ; let i , g , k , l , m , d ; let ar = new Array ( I_MAX + 2 ) ; for ( i = 0 ; i < j ; i ++ ) { if ( ( b [ i ] * b [ i + 1 ] ) > limit ) break ; p [ i ] = b [ i ] ; mp [ b [ i ] * b [ i + 1 ] ] = 1 ; } d = 550 ; let flag = false ; for ( k = 2 ; ( k < d - 1 ) && ! flag ; k ++ ) { for ( m = 2 ; ( m < d ) && ! flag ; m ++ ) { for ( l = m + k ; l < d ; l += k ) { if ( ( ( b [ l ] * b [ l + k ] ) < limit ) && ( l + k ) < d && p [ i - 1 ] != b [ l + k ] && p [ i - 1 ] != b [ l ] && mp [ b [ l ] * b [ l + k ] ] != 1 ) { if ( mp [ p [ i - 1 ] * b [ l ] ] != 1 ) { p [ i ] = b [ l ] ; mp [ p [ i - 1 ] * b [ l ] ] = 1 ; i ++ ; } } if ( i >= I_MAX ) { flag = true ; break ; } } } } for ( i = 0 ; i < n ; i ++ ) ar [ i ] = p [ i ] * p [ i + 1 ] ; for ( i = 0 ; i < n - 1 ; i ++ ) document . write ( ar [ i ] + " " ) ; g = gcd ( ar [ n - 1 ] , ar [ n - 2 ] ) ; document . write ( g * 2 + " " ) ; } let n = 4 ; printSeries ( n ) ;
Print numbers such that no two consecutive numbers are co | Javascript implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print the required sequence ; Store only the required primes ; Base condition ; First integer in the list ; Second integer in the list ; Third integer in the list ; Generate ( N - 1 ) th term ; Generate Nth term ; Modify first term ; Print the sequence ; Driver code
let MAX = 620000 ; let prime = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { prime [ i ] = 0 ; } function Sieve ( ) { for ( let i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { for ( let j = 2 * i ; j < MAX ; j += i ) { prime [ j ] = 1 ; } } } } function printSequence ( n ) { Sieve ( ) ; let v = [ ] ; let u = [ ] ; for ( let i = 13 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { v . push ( i ) ; } } if ( n == 3 ) { document . write ( 6 + " " + 10 + " " + 15 ) ; return ; } let k ; for ( k = 0 ; k < n - 2 ; k ++ ) { if ( k % 3 == 0 ) { u . push ( v [ k ] * 6 ) ; } else if ( k % 3 == 1 ) { u . push ( v [ k ] * 15 ) ; } else { u . push ( v [ k ] * 10 ) ; } } k -- ; u . push ( v [ k ] * 7 ) ; u . push ( 7 * 11 ) ; u [ 0 ] = u [ 0 ] * 11 ; for ( let i = 0 ; i < u . length ; i ++ ) { document . write ( u [ i ] + " " ) ; } } let n = 4 ; printSequence ( n ) ;