codeparrot/xlcost-text-to-code huggingface dataset:
Sum of the first N terms of the series 2 , 6 , 12 , 20 , 30. ... | Function to calculate the sum ; number of terms to be included in the sum ; find the Sn
function calculateSum ( n ) { return n * ( n + 1 ) / 2 + n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ; } let n = 3 ; document . write ( " " + calculateSum ( n ) ) ;
Program to find the Nth term of the series 0 , 5 , 14 , 27 , 44 , ... ... . . | Calculate Nth term of series ; Driver code
function nthTerm ( n ) { return 2 * Math . pow ( n , 2 ) - n - 1 ; } let N = 4 ; document . write ( nthTerm ( N ) ) ;
Program to find the Nth term of the series 0 , 5 , 18 , 39 , 67 , 105 , 150 , 203 , ... | calculate Nth term of series ; driver code declaration of number of terms
function nthTerm ( n ) { return 4 * Math . pow ( n , 2 ) - 7 * n + 3 ; } let N = 4 ; document . write ( nthTerm ( N ) ) ;
Check whether a given Number is Power | Javascript program to find whether a number is power - isolated or not ; for 2 as prime factor ; for odd prime factor ; calculate product of powers and prime factors ; check result for power - isolation ; Driver code
function checkIfPowerIsolated ( num ) { let input = num ; let count = 0 ; let factor = new Array ( 0 ) ; if ( num % 2 == 0 ) { while ( num % 2 == 0 ) { ++ count ; num /= 2 ; } factor [ 2 ] = count ; } for ( let i = 3 ; i * i <= num ; i += 2 ) { count = 0 ; while ( num % i == 0 ) { ++ count ; num /= i ; } if ( count > 0 ) factor [ i ] = count ; } if ( num > 1 ) factor [ num ] = 1 ; let product = 1 ; for ( let i = 0 ; i < num + 1 ; i ++ ) { if ( factor [ i ] > 0 ) product = product * factor [ i ] * i ; } if ( product == input ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } checkIfPowerIsolated ( 12 ) ; checkIfPowerIsolated ( 18 ) ; checkIfPowerIsolated ( 35 ) ;
Program to find the Nth term of the series 3 , 7 , 13 , 21 , 31. ... . | Function to calculate sum ; Return Nth term ; declaration of number of terms ; Get the Nth term
function getNthTerm ( N ) { return ( Math . pow ( N , 2 ) + N + 1 ) ; } let N = 11 ; document . write ( getNthTerm ( N ) ) ;
Sum of the numbers upto N that are divisible by 2 or 5 | Function to find the sum ; sum2 is sum of numbers divisible by 2 ; sum5 is sum of number divisible by 5 ; sum10 of numbers divisible by 2 and 5 ; Driver code
function findSum ( n ) { var sum2 , sum5 , sum10 ; sum2 = parseInt ( ( parseInt ( n / 2 ) * ( 4 + ( parseInt ( n / 2 ) - 1 ) * 2 ) ) / 2 ) ; sum5 = parseInt ( ( parseInt ( n / 5 ) * ( 10 + ( parseInt ( n / 5 ) - 1 ) * 5 ) ) / 2 ) ; sum10 = parseInt ( ( parseInt ( n / 10 ) * ( 20 + ( parseInt ( n / 10 ) - 1 ) * 10 ) ) / 2 ) ; return sum2 + sum5 - sum10 ; } var n = 5 ; document . write ( findSum ( n ) ) ;
Find four factors of N with maximum product and sum equal to N | Set 3 | Function to find primes ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find factors ; run a loop upto square root of that number ; if the n is perfect square ; otherwise push it 's two divisors ; sort the divisors ; Function to find max product ; To store factors of ' n ' ; find factors ; if it is divisible by 4. ; if it is prime ; otherwise answer will be possible ; include last third factor ; nested loop to find other two factors ; Driver code ; function call
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 factors ( N , v ) { for ( let i = 2 ; i < N ; i ++ ) { for ( let j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( i / j == j ) v [ i ] . push ( j ) ; else { v [ i ] . push ( j ) ; v [ i ] . push ( i / j ) ; } } } v . sort ( ( a , b ) => a - b ) ; } } function product ( n ) { let v = new Array ( ) ; for ( let i = 0 ; i < n + 100 ; i ++ ) { v . push ( new Array ( ) ) } factors ( n + 100 , v ) ; if ( n % 4 == 0 ) { let x = n / 4 ; x *= x ; return x * x ; } else { if ( isPrime [ n ] ) return - 1 ; else { let ans = - 1 ; if ( v [ n ] . length > 2 ) { let fac = v [ n ] [ v [ n ] . length - 3 ] ; for ( let i = v [ n ] . length - 1 ; i >= 0 ; i -- ) { for ( let j = v [ n ] . length - 1 ; j >= 0 ; j -- ) { if ( ( fac * 2 ) + ( v [ n ] [ j ] + v [ n ] [ i ] ) == n ) ans = Math . max ( ans , fac * fac * v [ n ] [ j ] * v [ n ] [ i ] ) ; } } return ans ; } } } } let n = 24 ; document . write ( product ( n ) ) ;
Ratio of mth and nth terms of an A . P . with given ratio of sums | function to calculate ratio of mth and nth term ; ratio will be tm / tn = ( 2 * m - 1 ) / ( 2 * n - 1 ) ; Driver code
function CalculateRatio ( m , n ) { return ( 2 * m - 1 ) / ( 2 * n - 1 ) ; } let m = 6 , n = 2 ; document . write ( CalculateRatio ( m , n ) ) ;
Find the sum of n terms of the series 1 , 8 , 27 , 64 ... . | Function to calculate the sum ; Return total sum ; Driver code
function calculateSum ( n ) { return parseInt ( Math . pow ( n * ( n + 1 ) / 2 , 2 ) ) ; } var n = 4 ; document . write ( calculateSum ( n ) ) ;
Sum of Digits in a ^ n till a single digit | This function finds single digit sum of n . ; Returns single digit sum of a ^ n . We use modular exponentiation technique . ; Driver code
function digSum ( n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } function powerDigitSum ( a , n ) { let res = 1 ; while ( n ) { if ( n % 2 == 1 ) { res = res * digSum ( a ) ; res = digSum ( res ) ; } a = digSum ( digSum ( a ) * digSum ( a ) ) ; n /= 2 ; } return res ; } let a = 9 , n = 4 ; document . write ( powerDigitSum ( a , n ) ) ;
Program to find total number of edges in a Complete Graph | Function to find the total number of edges in a complete graph with N vertices ; Driver Code
function totEdge ( n ) { var result = 0 ; result = ( n * ( n - 1 ) ) / 2 ; return result ; } var n = 6 ; document . write ( totEdge ( n ) ) ;
Find minimum number of Log value needed to calculate Log upto N | Javascript program to find number of log values needed to calculate all the log values from 1 to N ; In this vector prime [ i ] will store true if prime [ i ] is prime , else store false ; Using sieve of Eratosthenes to find all prime upto N ; Function to find number of log values needed to calculate all the log values from 1 to N ; calculate primes upto N ; Driver code
MAX = 1000005 var prime = Array ( MAX ) . fill ( true ) ; function sieve ( N ) { prime [ 0 ] = prime [ 1 ] = false ; for ( var i = 2 ; i <= N ; i ++ ) { if ( prime [ i ] ) { for ( var j = 2 ; i * j <= N ; j ++ ) prime [ i * j ] = false ; } } } function countLogNeeded ( N ) { var count = 0 ; sieve ( N ) ; for ( var i = 1 ; i <= N ; i ++ ) { if ( prime [ i ] ) count ++ ; } return count ; } var N = 6 ; document . write ( countLogNeeded ( N ) ) ;
Program to find the count of coins of each type from the given ratio | function to calculate coin . ; Converting each of them in rupees . As we are given totalRupees = 1800 ; Driver code
function coin ( totalRupees , X , Y , Z ) { var one = 0 , fifty = 0 , twentyfive = 0 , result = 0 , total = 0 ; one = X * 1 ; fifty = ( ( Y * 1 ) / 2.0 ) ; twentyfive = ( ( Z * 1 ) / 4.0 ) ; total = one + fifty + twentyfive ; result = ( ( totalRupees ) / total ) ; return result ; } var totalRupees = 1800 ; var X = 1 , Y = 2 , Z = 4 ; var Rupees = coin ( totalRupees , X , Y , Z ) ; document . write ( " " + Rupees * 1 + " " ) ; document . write ( " " + Rupees * 2 + " " ) ; document . write ( " " + Rupees * 4 + " " ) ;
Find the sum of series 0. X + 0. XX + 0. XXX + ... upto k terms | function which return the sum of series ; Driver code
function sumOfSeries ( x , k ) { var y = ( ( ( x ) / 81 ) * ( 9 * k - 1 + Math . pow ( 10 , ( - 1 ) * k ) ) ) ; return y ; } var x = 9 ; var k = 20 ; document . write ( sumOfSeries ( x , k ) ) ;
Find four factors of N with maximum product and sum equal to N | Set | Function to find factors and to print those four factors ; push all the factors in the container ; number of factors ; Initial maximum ; hash - array to mark the pairs ; form all the pair sums ; if the pair sum is less than n ; push in another container ; mark the sum with the elements formed ; mark in the map that v . get ( i ) + v . get ( j ) is present ; new size of all the pair sums ; iterate for all pair sum ; the required part ; if the required part is also present in pair sum ; find the elements with which the first pair is formed ; find the elements with which the second pair is formed ; check for previous maximum ; Driver code
function findfactors ( n ) { let mpp = new Map ( ) ; let v = [ ] ; let v1 = [ ] ; for ( let i = 1 ; i <= Math . floor ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { v . push ( i ) ; if ( i != Math . floor ( n / i ) && i != 1 ) v . push ( Math . floor ( n / i ) ) ; } } let s = v . length ; let maxi = - 1 ; let mp1_first = new Array ( n + 5 ) , mp1_second = new Array ( n + 5 ) ; for ( let i = 0 ; i < s ; i ++ ) { for ( let j = i ; j < s ; j ++ ) { if ( v [ i ] + v [ j ] < n ) { v1 . push ( v [ i ] + v [ j ] ) ; mp1_first [ v [ i ] + v [ j ] ] = v [ i ] ; mp1_second [ v [ i ] + v [ j ] ] = v [ j ] ; mpp . set ( v [ i ] + v [ j ] , 1 ) ; } } } s = v1 . length ; for ( let i = 0 ; i < s ; i ++ ) { let el = n - ( v1 [ i ] ) ; if ( mpp . get ( el ) != null ) { let a = mp1_first [ v1 [ i ] ] ; let b = mp1_second [ v1 [ i ] ] ; let c = mp1_first [ n - v1 [ i ] ] ; let d = mp1_second [ n - v1 [ i ] ] ; maxi = Math . max ( a * b * c * d , maxi ) ; } } if ( maxi == - 1 ) document . write ( " " ) ; else { document . write ( " " + " " + maxi ) ; } } let n = 50 ; findfactors ( n ) ;
Maximize the product of four factors of a Number | Declare the ArrayList of factors for storing the ; function to find out the factors of a number ; Loop until the i reaches the sqrt ( n ) ; Check if i is a factor of n ; if both the factors are same we only push one factor ; factor1 is pushed ; factor2 is pushed ; Function to find the maximum product ; Initialize the product with - 1 ; Find the sum of factors and store it in s ; Compare whether it is equal to the n ; product of factors ; Check whether we have a better p now if yes update ; Driver Code ; initializes the List with the divisors of n ; prints out the maximised product .
let factors = [ ] ; function findFactors ( n ) { for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i ) == i ) { factors . push ( i ) ; } else { factors . push ( n / i ) ; factors . push ( i ) ; } } } } function findProduct ( n ) { let product = - 1 ; let si = factors . length ; for ( let i = 0 ; i < si ; i ++ ) { for ( let j = 0 ; j < si ; j ++ ) { for ( let k = 0 ; k < si ; k ++ ) { for ( let l = 0 ; l < si ; l ++ ) { let s = factors [ i ] + factors [ j ] + factors [ k ] + factors [ l ] ; if ( s == n ) { let p = factors [ i ] * factors [ j ] * factors [ k ] * factors [ l ] ; if ( p > product ) { product = p ; } } } } } } return product ; } let n = 10 ; findFactors ( n ) ; document . write ( findProduct ( n ) ) ;
Maximize the product of four factors of a Number | For calculation of a ^ b ; Function to check ; every odd and number less than 3. ; Every number divisible by 4. ; Every number divisible by 6. ; Every number divisible by 10. ; For every even number which is not divisible by above values . ; Driver code
function modExp ( a , b ) { let result = 1 ; while ( b > 0 ) { if ( b == 1 ) result = result * a ; a = a * a ; b = Math . floor ( b / 2 ) ; } return result ; } function check ( num ) { if ( num == 1 num < 3 ) return - 1 ; else if ( num % 4 == 0 ) return modExp ( Math . floor ( num / 4 ) , 4 ) ; else if ( num % 6 == 0 ) return modExp ( Math . floor ( num / 3 ) , 2 ) * modExp ( Math . floor ( num / 6 ) , 2 ) ; else if ( num % 10 == 0 ) return modExp ( Math . floor ( num / 5 ) , 2 ) * Math . floor ( num / 10 ) * Math . floor ( num / 2 ) ; else return - 1 ; } let num = 10 ; document . write ( check ( num ) ) ;
Check if any large number is divisible by 17 or not | Function to check if the number is divisible by 17 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 17 ; Driver code
function isDivisible ( n ) { while ( Math . floor ( n / 100 ) > 0 ) { let d = n % 10 ; n = Math . floor ( n / 10 ) ; n -= d * 5 ; } return ( n % 17 == 0 ) ; } let n = 19877658 ; if ( isDivisible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum number of elements to be removed to make XOR maximum | Javascript implementation to find minimum number of elements to remove to get maximum XOR value ; First n in the below condition is for the case where n is 0 ; Function to find minimum number of elements to be removed . ; Driver code ; print minimum number of elements to be removed
function nextPowerOf2 ( n ) { let count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } function removeElement ( n ) { if ( n == 1 n == 2 ) return 0 ; let a = nextPowerOf2 ( n ) ; if ( n == a n == a - 1 ) return 1 ; else if ( n == a - 2 ) return 0 ; else if ( n % 2 == 0 ) return 1 ; else return 2 ; } let n = 5 ; document . write ( removeElement ( n ) ) ;
Program to find Length of Bridge using Speed and Length of Train | Function to calculate the length of bridge . ; Assuming the input variables
function bridge_length ( trainLength , Speed , Time ) { return ( ( Time * Speed ) - trainLength ) ; } var trainLength = 120 ; var Speed = 30 ; var Time = 18 ; document . write ( " " + bridge_length ( trainLength , Speed , Time ) + " " ) ;
Program to find sum of the given sequence | Javascript program to find the sum of the given sequence ; function to find moudulo inverse under 10 ^ 9 + 7 ; Function to find the sum of the given sequence ; Driver code
var MOD = 100000007 ; function modInv ( x ) { var n = MOD - 2 ; var result = 1 ; while ( n ) { if ( n & 1 ) result = result * x % MOD ; x = x * x % MOD ; n = n / 2 ; } return result ; } function getSum ( n , k ) { var ans = 1 ; for ( var i = n + 1 ; i > n - k ; i -- ) ans = ans * i % MOD ; ans = ans * modInv ( k + 1 ) % MOD ; return ans ; } var n = 3 , k = 2 ; document . write ( getSum ( n , k ) ) ;
Triplet with no element divisible by 3 and sum N | Function to print a , b and c ; Check if n - 2 is divisible by 3 or not ; Driver Code
function printCombination ( n ) { document . write ( 1 + " " ) ; if ( ( n - 2 ) % 3 == 0 ) document . write ( 2 + " " + ( n - 3 ) ) ; else document . write ( 1 + " " + ( n - 2 ) ) ; } let n = 233 ; printCombination ( n ) ;
Pairs with GCD equal to one in the given range | Function to print all pairs ; check if even ; We can print all adjacent pairs for ( i = l ; i < r ; i += 2 ) { document . write ( " { " + i + " , ▁ " + i + 1 + " ] , ▁ " ) ; } ; Driver Code
function checkPairs ( l , r ) { if ( ( l - r ) % 2 == 0 ) return false ; return true ; } var l = 1 , r = 8 ; if ( checkPairs ( l , r ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number with even number of digits is palindrome or not | Function to check if the number is palindrome ; if divisible by 11 then true ; if not divisible by 11 ; Driver code
function isPalindrome ( n ) { if ( n % 11 == 0 ) { return true ; } return false ; } document . write ( isPalindrome ( 123321 ) ? " " : " " ) ;
Count number of triplets with product equal to given number with duplicates allowed | The target value for which we have to find the solution ; This variable contains the total count of triplets found ; Loop from the first to the third last integer in the list ; Check if arr [ i ] is a factor of target or not . If not , skip to the next element ; Check if the pair ( arr [ i ] , arr [ j ] ) can be a part of triplet whose product is equal to the target ; Find the remaining element of the triplet ; If element is found . increment the total count of the triplets
var target = 93 ; var arr = [ 1 , 31 , 3 , 1 , 93 , 3 , 31 , 1 , 93 ] ; var length = arr . length ; var totalCount = 0 ; for ( var i = 0 ; i < length - 2 ; i ++ ) { if ( target % arr [ i ] == 0 ) { for ( var j = i + 1 ; j < length - 1 ; j ++ ) { if ( target % ( arr [ i ] * arr [ j ] ) == 0 ) { var toFind = target / ( arr [ i ] * arr [ j ] ) ; for ( var k = j + 1 ; k < length ; k ++ ) { if ( arr [ k ] == toFind ) { totalCount ++ ; } } } } } } document . write ( " " + totalCount ) ;
Number of Permutations such that no Three Terms forms Increasing Subsequence | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
function binomialCoeff ( n , k ) { 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 ; } function catalan ( n ) { var c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } var n = 3 ; document . write ( catalan ( n ) ) ;
Fascinating Number | function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not
function isFascinating ( num ) { let freq = new Array ( 10 ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 ; } let val = " " + num + num * 2 + num * 3 ; for ( let i = 0 ; i < val . length ; i ++ ) { let digit = val [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( freq [ digit ] > 0 && digit != 0 ) return false ; else freq [ digit ] ++ ; } for ( let i = 1 ; i < freq . length ; i ++ ) { if ( freq [ i ] == 0 ) return false ; } return true ; } let num = 192 ; if ( num < 100 ) document . write ( " " ) ; else { let ans = isFascinating ( num ) ; if ( ans ) document . write ( " " ) ; else document . write ( " " ) ; }
Count ways to distribute m items among n people | function used to generate binomial coefficient time complexity O ( m ) ; helper function for generating no of ways to distribute m mangoes amongst n people ; not enough mangoes to be distributed ; ways -> ( n + m - 1 ) C ( n - 1 ) ; m represents number of mangoes n represents number of people
function binomial_coefficient ( n , m ) { let res = 1 ; if ( m > n - m ) m = n - m ; for ( let i = 0 ; i < m ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function calculate_ways ( m , n ) { if ( m < n ) return 0 ; let ways = binomial_coefficient ( n + m - 1 , n - 1 ) ; return ways ; } let m = 7 , n = 5 ; let result = calculate_ways ( m , n ) ; document . write ( result ) ;
Queries to count the number of unordered co | Javascript program to find number of unordered coprime pairs of integers from 1 to N ; to store euler 's  totient function ; to store required answer ; Computes and prints totient of all numbers smaller than or equal to N . ; Initialise the phi [ ] with 1 ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; function to compute number coprime pairs ; function call to compute euler totient function ; prefix sum of all euler totient function values ; function call
let N = 100005 ; let phi = new Array ( N ) ; let S = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { phi [ i ] = 0 ; S [ i ] = 0 ; } function computeTotient ( ) { for ( let i = 1 ; i < N ; i ++ ) phi [ i ] = i ; for ( let p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( let i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } function CoPrimes ( ) { computeTotient ( ) ; for ( let i = 1 ; i < N ; i ++ ) S [ i ] = S [ i - 1 ] + phi [ i ] ; } CoPrimes ( ) ; let q = [ 3 , 4 ] ; let n = q . length ; for ( let i = 0 ; i < n ; i ++ ) document . write ( " " + " " + q [ i ] + " " + S [ q [ i ] ] + " " ) ;
N | Function to return the decimal value of a binary number ; Initializing base value to 1 , i . e 2 ^ 0 ; find the binary representation of the N - th number in sequence ; base case ; answer string ; add n - 1 1 's ; add 0 ; add n 1 's at end ; Driver Code
function binaryToDecimal ( n ) { let num = n ; let dec_value = 0 ; let base = 1 ; let len = num . length ; for ( let i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == ' ' ) dec_value += base ; base = base * 2 ; } return dec_value ; } function numberSequence ( n ) { if ( n == 1 ) return 1 ; let s = " " ; for ( let i = 1 ; i < n ; i ++ ) s += ' ' ; s += ' ' ; for ( let i = 1 ; i <= n ; i ++ ) s += ' ' ; let num = binaryToDecimal ( s ) ; return num ; } let n = 4 ; document . write ( numberSequence ( n ) ) ;
N | Function to find the N - th term ; calculates the N - th term ; Driver Code
function numberSequence ( n ) { let num = Math . pow ( 4 , n ) - Math . pow ( 2 , n ) - 1 ; return num ; } let n = 4 ; document . write ( numberSequence ( n ) ) ;
Alternate Primes till N | * Function for checking number is prime or not ; if flag = 0 then number is prime and return 1 otherwise return 0 ; Function for printing alternate prime number ; counter is initialize with 0 ; looping through 2 to n - 1 ; function calling along with if condition ; if counter is multiple of 2 then only print prime number ; Driver code ; Function calling
function prime ( num ) { var i , flag = 0 ; for ( i = 2 ; i <= num / 2 ; i ++ ) { if ( num % i == 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) return 1 ; else return 0 ; } function print_alternate_prime ( n ) { var counter = 0 ; for ( num = 2 ; num < n ; num ++ ) { if ( prime ( num ) == 1 ) { if ( counter % 2 == 0 ) document . write ( num + " " ) ; counter ++ ; } } } var n = 15 ; document . write ( " " + " " + " " + n + " " ) ; print_alternate_prime ( n ) ;
Alternate Primes till N | Javascript program to print all primes smaller than or equal to n 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 ; Print all prime numbers ; for next prime to get printed ; Driver Code
function SieveOfEratosthenes ( n ) { let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i < prime . length ; 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 ; } } let flag = true ; for ( let p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { if ( flag ) { document . write ( p + " " ) ; flag = false ; } else { flag = true ; } } } } let n = 15 ; document . write ( " " + " " + " " + n + " " ) ; SieveOfEratosthenes ( n ) ;
Find maximum among x ^ ( y ^ 2 ) or y ^ ( x ^ 2 ) where x and y are given | Function to find maximum ; Case 1 ; Case 2 ; Driver Code
function findGreater ( x , y ) { if ( x > y ) { return false ; } else { return true ; } } var x = 4 ; var y = 9 ; if ( findGreater ( x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;