codeparrot/xlcost-text-to-code huggingface dataset:
MÃ ¼ nchhausen Number | power i . ; Function to check out whether the number is Munchhausen Number or not ; Precompute i raised to power i for every i ; The input here is fixed i . e . it will check up to n ; check the integer for Munchhausen Number , if yes then prvar out the number ; Driver code
var pwr ; function isMunchhausen ( n ) { var sum = 0 ; var temp = n ; while ( temp > 0 ) { var index = temp % 10 ; sum = sum + pwr [ index ] ; temp = parseInt ( temp / 10 ) ; } return ( sum == n ) ; } function printMunchhausenNumbers ( n ) { pwr = Array . from ( { length : 10 } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < 10 ; i ++ ) pwr [ i ] = Math . pow ( i , i ) ; for ( var i = 1 ; i <= n ; i ++ ) if ( isMunchhausen ( i ) == true ) document . write ( i + " " ) ; } var n = 10000 ; printMunchhausenNumbers ( n ) ;
K | To compute k - th digit in a ^ b ; Computing a ^ b ; Getting last digit ; Increasing count by 1 ; If current number is required digit ; Remove last digit ; Driver code
function kthdigit ( a , b , k ) { let p = Math . pow ( a , b ) ; let count = 0 ; while ( p > 0 && count < k ) { let rem = p % 10 ; count ++ ; if ( count == k ) return rem ; p = p / 10 ; } return 0 ; } let a = 5 , b = 2 ; let k = 1 ; document . write ( kthdigit ( a , b , k ) ) ;
Recursive sum of digit in n ^ x , where n and x are very large | function to get sum of digits of a number ; function to return sum ; Find sum of digits in n ; Find remainder of exponent ; Driver code
function digSum ( n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } function PowDigSum ( n , x ) { let sum = digSum ( n ) ; let rem = x % 6 ; if ( ( sum == 3 sum == 6 ) && x > 1 ) return 9 ; else if ( x == 1 ) return sum ; else if ( x == 0 ) return 1 ; else if ( rem == 0 ) return digSum ( Math . pow ( sum , 6 ) ) ; else return digSum ( Math . pow ( sum , rem ) ) ; } let n = 33333 ; let x = 332654 ; document . write ( PowDigSum ( n , x ) ) ;
Container with Most Water | Javascript code for Max Water Container ; Calculating the max area ; Driver code
function maxArea ( A , len ) { let l = 0 ; let r = len - 1 ; let area = 0 ; while ( l < r ) { area = Math . max ( area , Math . min ( A [ l ] , A [ r ] ) * ( r - l ) ) ; if ( A [ l ] < A [ r ] ) l += 1 ; else r -= 1 ; } return area ; } let a = [ 1 , 5 , 4 , 3 ] ; let b = [ 3 , 1 , 2 , 4 , 5 ] ; let len1 = a . length ; document . write ( maxArea ( a , len1 ) + " " ) ; let len2 = b . length ; document . write ( maxArea ( b , len2 ) ) ;
Program for Mobius Function | Returns value of mobius ( ) ; Handling 2 separately ; If 2 ^ 2 also divides N ; Check for all other prime factors ; If i divides n ; If i ^ 2 also divides N ; Driver Code
function mobius ( n ) { let p = 0 ; if ( n % 2 == 0 ) { n = parseInt ( n / 2 , 10 ) ; p ++ ; if ( n % 2 == 0 ) return 0 ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { n = parseInt ( n / i , 10 ) ; p ++ ; if ( n % i == 0 ) return 0 ; } } return ( p % 2 == 0 ) ? - 1 : 1 ; } let N = 17 ; document . write ( " " + " " + N + " " + mobius ( N ) + " " ) ; document . write ( " " + " " + 25 + " " + mobius ( 25 ) + " " ) ; document . write ( " " + " " + 6 + " " + mobius ( 6 ) ) ;
Sum of squares of binomial coefficients | function to return product of number from start to end . ; Return the sum of square of binomial coefficient ; Driven Program
function factorial ( start , end ) { let res = 1 ; for ( let i = start ; i <= end ; i ++ ) res *= i ; return res ; } function sumofsquare ( n ) { return parseInt ( factorial ( n + 1 , 2 * n ) / factorial ( 1 , n ) , 10 ) ; } let n = 4 ; document . write ( sumofsquare ( n ) ) ;
Find nth Fibonacci number using Golden ratio | Approximate value of golden ratio ; Fibonacci numbers upto n = 5 ; Function to find nth Fibonacci number ; Fibonacci numbers for n < 6 ; Else start counting from 5 th term ; driver code
let PHI = 1.6180339 ; let f = [ 0 , 1 , 1 , 2 , 3 , 5 ] ; function fib ( n ) { if ( n < 6 ) return f [ n ] ; let t = 5 , fn = 5 ; while ( t < n ) { fn = Math . round ( fn * PHI ) ; t ++ ; } return fn ; } let n = 9 ; document . write ( n + " " + fib ( n ) + " " ) ;
Euler Method for solving differential equation | Consider a differential equation dy / dx = ( x + y + xy ) ; Function for Euler formula ; Iterating till the point at which we need approximation ; Printing approximation ; Initial Values ; Value of x at which we need approximation
function func ( x , y ) { return ( x + y + x * y ) ; } function euler ( x0 , y , h , x ) { let temp = - 0 ; while ( x0 < x ) { temp = y ; y = y + h * func ( x0 , y ) ; x0 = x0 + h ; } document . write ( " " + x + " " + y ) ; } let x0 = 0 ; let y0 = 1 ; let h = 0.025 ; let x = 0.1 ; euler ( x0 , y0 , h , x ) ;
Find x and y satisfying ax + by = n | function to find the solution ; traverse for all possible values ; check if it is satisfying the equation ; Driver code
function solution ( a , b , n ) { for ( let i = 0 ; i * a <= n ; i ++ ) { if ( ( n - ( i * a ) ) % b == 0 ) { document . write ( " " + i + " " + ( n - ( i * a ) ) / b ) ; return ; } } document . write ( " " ) ; } let a = 2 , b = 3 , n = 7 ; solution ( a , b , n ) ;
Sum of Binomial coefficients | Returns value of Binomial Coefficient Sum which is 2 raised to power n . ; Driver Code
function binomialCoeffSum ( n ) { return ( 1 << n ) ; } let n = 4 ; document . write ( binomialCoeffSum ( n ) ) ;
Program to compute division upto n decimal places | JavaScript program to compute division upto n decimal places . ; Base cases ; Since n <= 0 , don 't compute after  the decimal ; Handling negative numbers ; Integral division ; Now one by print digits after dot using school division method . ; Driver code
function precisionCompute ( x , y , n ) { if ( y == 0 ) { document . write ( " " ) ; return ; } if ( x == 0 ) { document . write ( " " ) ; return ; } if ( n <= 0 ) { document . write ( x / y ) ; return ; } if ( ( ( x > 0 ) && ( y < 0 ) ) || ( ( x < 0 ) && ( y > 0 ) ) ) { document . write ( " " ) ; x = x > 0 ? x : - x ; y = y > 0 ? y : - y ; } let d = x / y ; for ( let i = 0 ; i <= n ; i ++ ) { document . write ( d ) ; x = x - ( y * d ) ; if ( x == 0 ) break ; x = x * 10 ; d = x / y ; if ( i == 0 ) document . write ( " " ) ; } } let x = 22 , y = 7 , n = 15 ; precisionCompute ( x , y , n ) ;
Program to determine the quadrant of the cartesian plane | Function to check quadrant ; Driver code ; Function Calling
function quadrant ( x , y ) { if ( x > 0 && y > 0 ) document . write ( " " ) ; else if ( x < 0 && y > 0 ) document . write ( " " ) ; else if ( x < 0 && y < 0 ) document . write ( " " ) ; else if ( x > 0 && y < 0 ) document . write ( " " ) ; else if ( x == 0 && y > 0 ) document . write ( " " ) ; else if ( x == 0 && y < 0 ) document . write ( " " ) ; else if ( y == 0 && x < 0 ) document . write ( " " ) ; else if ( y == 0 && x > 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let x = 1 , y = 1 ; quadrant ( x , y ) ;
Check if a number is Full Prime | function to check digits ; check all digits are prime or not ; check if digits are prime or not ; To check if n is prime or not ; check for all factors ; To check if n is Full Prime ; The order is important here for efficiency ; Driver Code
function checkDigits ( n ) { while ( n > 0 ) { let dig = n % 10 ; if ( dig != 2 && dig != 3 && dig != 5 && dig != 7 ) return false ; n = parseInt ( n / 10 , 10 ) ; } return true ; } function prime ( n ) { if ( n == 1 ) return false ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function isFullPrime ( n ) { return ( checkDigits ( n ) && prime ( n ) ) ; } let n = 53 ; if ( isFullPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Narcissistic number | Javascript program for checking of Narcissistic number ; Traversing through the string ; Since ascii value of numbers starts from 48 so we subtract it from sum ; Converting string to integer ; Comparing number and sum ; Driver Code
function getResult ( st ) { let sum = 0 ; let length = st . length ; for ( let i = 0 ; i < length ; i ++ ) { sum = sum + Math . pow ( st [ i ] - ' ' , length ) ; } let number = parseInt ( st , 10 ) ; if ( number == sum ) return " " ; else return " " ; } let st = " " ; document . write ( getResult ( st ) ) ;
SchrÃ ¶ derâ €“ Hipparchus number | A memoization based optimized Javascript program to find n - th Schroder - Hipparchus number ; Driver code
let MAX = 500 ; function nthSHN ( n , dp ) { if ( n == 1 n == 2 ) return dp [ n ] = 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; return dp [ n ] = ( ( 6 * n - 9 ) * nthSHN ( n - 1 , dp ) - ( n - 3 ) * nthSHN ( n - 2 , dp ) ) / n ; } let n = 6 ; let dp = new Array ( MAX ) ; for ( let i = 0 ; i < dp . length ; i ++ ) dp [ i ] = - 1 ; document . write ( nthSHN ( n , dp ) ) ;
Sum of first n even numbers | function to find sum of first n even numbers ; sum of first n even numbers ; next even number ; required sum ; Driver program to test above
function evenSum ( n ) { let curr = 2 , sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { sum += curr ; curr += 2 ; } return sum ; } let n = 20 ; document . write ( " " + n + " " + evenSum ( n ) ) ;
Sum of first n even numbers | function to find sum of first n even numbers ; required sum ; Driver Code
function evenSum ( n ) { return ( n * ( n + 1 ) ) ; } let n = 20 ; document . write ( " " + n + " " , evenSum ( n ) ) ;
Program to Convert Km / hr to miles / hr and vice versa | Function to convert kmph to mph ; Function to convert mph to kmph ; Driver Code
function kmphTOmph ( kmph ) { return ( 0.6214 * kmph ) ; } function mphTOkmph ( mph ) { return ( 1.60934 * mph ) ; } var kmph = 150 var mph = 100 document . write ( " " + kmphTOmph ( kmph ) + " " ) ; document . write ( " " + mphTOkmph ( mph ) ) ;
Find element in array that divides all array elements | Returns gcd of two numbers . ; Function to return the desired number if exists ; Find GCD of array ; Check if GCD is present in array ; Driver Code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function findNumber ( arr , n ) { let ans = arr [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) ans = gcd ( ans , arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] == ans ) return ans ; return - 1 ; } let arr = [ 2 , 2 , 4 ] ; let n = arr . length ; document . write ( findNumber ( arr , n ) ) ;
Special prime numbers | Javascript program to check whether there exist at least k or not in range [ 2. . n ] ; Generating all the prime numbers from 2 to n . ; If a prime number is Special prime number , then we increments the value of k . ; If at least k Special prime numbers are present , then we return 1. else we return 0 from outside of the outer loop . ; Driver function
let primes = [ ] ; function SieveofEratosthenes ( n ) { let visited = new Array ( n ) ; visited . fill ( false ) ; for ( let i = 2 ; i <= n + 1 ; i ++ ) if ( ! visited [ i ] ) { for ( let j = i * i ; j <= n + 1 ; j += i ) visited [ j ] = true ; primes . push ( i ) ; } } function specialPrimeNumbers ( n , k ) { SieveofEratosthenes ( n ) ; let count = 0 ; for ( let i = 0 ; i < primes . length ; i ++ ) { for ( let j = 0 ; j < i - 1 ; j ++ ) { if ( primes [ j ] + primes [ j + 1 ] + 1 == primes [ i ] ) { count ++ ; break ; } } if ( count == k ) return true ; } return false ; } let n = 27 , k = 2 ; if ( specialPrimeNumbers ( n , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Prime factors of a big number | function to calculate all the prime factors and count of every prime factor ; count the number of times 2 divides ; n = parseInt ( n / 2 ) equivalent to n = n / 2 ; ; if 2 divides it ; check for all the possible numbers that can divide it ; if n at the end is a prime number . ; driver program to test the above function
function factorize ( n ) { var count = 0 ; while ( ( n % 2 ) == 0 ) { count ++ ; } if ( count ) document . write ( 2 + " " + count + " " ) ; for ( var i = 3 ; i <= parseInt ( Math . sqrt ( n ) ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n = parseInt ( n / i ) ; } if ( count != 0 ) document . write ( i + " " + count + " " ) ; } if ( n > 2 ) document . write ( n + " " + 1 + " " ) ; } var n = 1000000000000000000 ; factorize ( n ) ;
Minimum gcd operations to make all array elements one | __gcd function ; Function to count number of moves . ; Counting Number of ones . ; If there is a one ; Find smallest subarray with GCD equals to one . ; to calculate GCD ; Not Possible ; Final answer ; Driver code
function __gcd ( a , b ) { if ( a == 0 ) return b ; return __gcd ( b % a , a ) ; } function minimumMoves ( A , N ) { let one = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( A [ i ] == 1 ) one ++ ; if ( one != 0 ) return N - one ; let minimum = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < N ; i ++ ) { let g = A [ i ] ; for ( let j = i + 1 ; j < N ; j ++ ) { g = __gcd ( A [ j ] , g ) ; if ( g == 1 ) { minimum = Math . min ( minimum , j - i ) ; break ; } } } if ( minimum == Number . MAX_SAFE_INTEGER ) return - 1 ; else return N + minimum - 1 ; } let A = [ 2 , 4 , 3 , 9 ] ; let N = A . length ; document . write ( minimumMoves ( A , N ) ) ;
Given N and Standard Deviation , find N elements | Function to print series of n elements ; If S . D . is 0 then print all elements as 0. ; Print n 0 's ; If S . D . is even ; Print - SD , + SD , - SD , + SD ; If odd ; Convert n to a float integer ; Print one element to be 0 ; Print ( n - 1 ) elements as xi derived from the formula ; Driver Code
function series ( n , d ) { if ( d == 0 ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( " " ) ; document . write ( ) ; return ; } if ( n % 2 == 0 ) { for ( let i = 1 ; i <= n ; i ++ ) { document . write ( Math . pow ( - 1 , i ) * d + " " ) ; } document . write ( ) ; } else { let m = n ; let r = ( m / ( m - 1 ) ) ; let g = ( d * ( Math . sqrt ( r ) ) ) ; document . write ( " " ) ; for ( let i = 1 ; i < n ; i ++ ) { document . write ( Math . pow ( - 1 , i ) * g + " " ) ; } document . write ( ) ; } } let n = 3 , d = 3 ; series ( n , d ) ;
Total no of 1 's in numbers | Javascript code to count the frequency of 1 in numbers less than or equal to the given number . ; Driver Code
function countDigitOne ( n ) { let countr = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let str = i . toString ( ) ; countr += str . split ( " " ) . length - 1 ; } return countr ; } let n = 13 ; document . write ( countDigitOne ( n ) + " " ) ; n = 131 ; document . write ( countDigitOne ( n ) + " " ) ; n = 159 ; document . write ( countDigitOne ( n ) + " " ) ;
GCD of factorials of two numbers | JavaScript program to find GCD of factorial of two numbers . ; Driver program to test above functions
function factorial ( x ) { if ( x <= 1 ) return 1 ; var res = 2 ; for ( i = 3 ; i <= x ; i ++ ) res = res * i ; return res ; } function gcdOfFactorial ( m , n ) { var min = m < n ? m : n ; return factorial ( min ) ; } var m = 5 , n = 9 ; document . write ( gcdOfFactorial ( m , n ) ) ;
Recursive sum of digits of a number is prime or not | Function for recursive digit sum ; Function to check if prime or not the single digit ; Calls function which returns sum till single digit ; Checking prime ; Driver code
function recDigSum ( n ) { if ( n == 0 ) return 0 ; else { if ( n % 9 == 0 ) return 9 ; else return n % 9 ; } } function check ( n ) { n = recDigSum ( n ) ; if ( n == 2 n == 3 n == 5 n == 7 ) document . write ( " " ) ; else document . write ( " " ) ; } let n = 5602 ; check ( n ) ;
Find n | Definition of findNumber function ; Finding x from equation n = x ( x + 1 ) / 2 + 1 ; Base of current block ; Value of n - th element ; Driver code
function findNumber ( n ) { let x = Math . floor ( ( - 1 + Math . sqrt ( 1 + 8 * n - 8 ) ) / 2 ) ; let base = ( x * ( x + 1 ) ) / 2 + 1 ; return n - base + 1 ; } let n = 55 ; document . write ( findNumber ( n ) ) ;
Program for weighted mean of natural numbers . | Returns weighted mean assuming for numbers { 1 , 2 , . . n } and weights { 1 , 2 , . . n } ; Driver Code
function weightedMean ( n ) { return parseInt ( ( 2 * n + 1 ) / 3 , 10 ) ; } let n = 10 ; document . write ( weightedMean ( n ) ) ;
Divide every element of one array by other array elements | Function to calculate the quotient of every element of the array ; Calculate the product of all elements ; To calculate the quotient of every array element ; Driver code
function calculate ( a , b , n , m ) { let mul = 1 ; for ( let i = 0 ; i < m ; i ++ ) if ( b [ i ] != 0 ) mul = mul * b [ i ] ; for ( let i = 0 ; i < n ; i ++ ) { let x = Math . floor ( a [ i ] / mul ) ; document . write ( x + " " ) ; } } let a = [ 5 , 100 , 8 ] ; let b = [ 2 , 3 ] ; let n = a . length ; let m = b . length ; calculate ( a , b , n , m ) ;
Largest power of k in n ! ( factorial ) where k may not be prime | JavaScript program to find the largest power of k that divides n ! ; To find the power of a prime p in factorial N ; calculating Math . floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of k ; Returns largest power of k that divides n ! ; calculating minimum power of all the prime factors of k ; Driver code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function findPowerOfP ( n , p ) { let count = 0 ; let r = p ; while ( r <= n ) { count += Math . floor ( n / r ) ; r = r * p ; } return count ; } function primeFactorsofK ( k ) { let ans = [ ] ; for ( let i = 2 ; k != 1 ; i ++ ) { if ( k % i == 0 ) { let count = 0 ; while ( k % i == 0 ) { k = Math . floor ( k / i ) ; count ++ ; } ans . push ( new pair ( i , count ) ) ; } } return ans ; } function largestPowerOfK ( n , k ) { let vec = [ ] ; vec = primeFactorsofK ( k ) ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < vec . length ; i ++ ) ans = Math . min ( ans , findPowerOfP ( n , vec [ i ] . first ) / vec [ i ] . second ) ; return ans ; } document . write ( largestPowerOfK ( 7 , 2 ) + " " ) ; document . write ( largestPowerOfK ( 10 , 9 ) + " " ) ;
Minimum number of bombs | function to prvar where to shoot ; no . of bombs required ; bomb all the even positions ; bomb all the odd positions ; bomb all the even positions again ; Driver code
function print ( n ) { document . write ( n + parseInt ( n / 2 ) + " " ) ; for ( i = 2 ; i <= n ; i += 2 ) document . write ( i + " " ) ; for ( i = 1 ; i <= n ; i += 2 ) document . write ( i + " " ) ; for ( i = 2 ; i <= n ; i += 2 ) document . write ( i + " " ) ; } var n = 3 ; print ( n ) ;
LCM of digits of a given number | define lcm function ; If at any point LCM become 0. return it ; Driver code
function lcm_fun ( a , b ) { if ( b == 0 ) return a ; return lcm_fun ( b , a % b ) ; } function digitLCM ( n ) { let lcm = 1 ; while ( n > 0 ) { lcm = ( n % 10 * lcm ) / lcm_fun ( n % 10 , lcm ) ; if ( lcm == 0 ) return 0 ; n = parseInt ( n / 10 ) ; } return lcm ; } let n = 397 ; document . write ( digitLCM ( n ) ) ;
Newton Forward And Backward Interpolation | Calculation of u mentioned in formula ; Calculating factorial of given n ; number of values given ; y is used for difference table and y [ 0 ] used for input ; Calculating the backward difference table ; Displaying the backward difference table ; Value to interpolate at ; Initializing u and sum
function u_cal ( u , n ) { var temp = u ; for ( var i = 1 ; i < n ; i ++ ) temp = temp * ( u + i ) ; return temp ; } function fact ( n ) { var f = 1 ; for ( var i = 2 ; i <= n ; i ++ ) f *= i ; return f ; } var n = 5 ; var x = [ 1891 , 1901 , 1911 , 1921 , 1931 ] ; var y = Array ( n ) . fill ( 0.0 ) . map ( x => Array ( n ) . fill ( 0.0 ) ) ; y [ 0 ] [ 0 ] = 46 ; y [ 1 ] [ 0 ] = 66 ; y [ 2 ] [ 0 ] = 81 ; y [ 3 ] [ 0 ] = 93 ; y [ 4 ] [ 0 ] = 101 ; for ( var i = 1 ; i < n ; i ++ ) { for ( var j = n - 1 ; j >= i ; j -- ) y [ j ] [ i ] = y [ j ] [ i - 1 ] - y [ j - 1 ] [ i - 1 ] ; } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) document . write ( y [ i ] [ j ] + " " ) ; document . write ( ' ' ) ; ; } var value = 1925 ; var sum = y [ n - 1 ] [ 0 ] ; var u = ( value - x [ n - 1 ] ) / ( x [ 1 ] - x [ 0 ] ) ; for ( var i = 1 ; i < n ; i ++ ) { sum = sum + ( u_cal ( u , i ) * y [ n - 1 ] [ i ] ) / fact ( i ) ; } document . write ( " " + value + " " + sum ) ;
Happy Numbers | Returns sum of squares of digits of a number n . For example for n = 12 it returns 1 + 4 = 5 ; Returns true if n is Happy number else returns false . ; Keep replacing n with sum of squares of digits until we either reach 1 or we end up in a cycle ; Number is Happy if we reach 1 ; Replace n with sum of squares of digits ; Number is not Happy if we reach 4 ; Driver code
function sumDigitSquare ( n ) { let sq = 0 ; while ( n != 0 ) { let digit = n % 10 ; sq += digit * digit ; n = parseInt ( n / 10 , 10 ) ; } return sq ; } function isHappy ( n ) { while ( true ) { if ( n == 1 ) return true ; n = sumDigitSquare ( n ) ; if ( n == 4 ) return false ; } } let n = 23 ; if ( isHappy ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of all subsets of a set formed by first n natural numbers | javascript program to find sum of all subsets of a set . ; sum of subsets is ( n * ( n + 1 ) / 2 ) * pow ( 2 , n - 1 ) ; Driven Program
function findSumSubsets ( n ) { return ( n * ( n + 1 ) / 2 ) * ( 1 << ( n - 1 ) ) ; } let n = 3 ; document . write ( findSumSubsets ( n ) ) ;
Minimum element whose n | function to find the minimum element ; loop to traverse and store the sum of log ; computes sum ; calculates the elements according to formula . ; returns the minimal element ; initialised array ; computes the size of array ; prints out the minimal element
function findMin ( a , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += Math . log ( a [ i ] ) ; var x = parseInt ( Math . exp ( sum / n ) ) ; return x + 1 ; } var a = [ 3 , 2 , 1 , 4 ] ; var n = a . length ; document . write ( findMin ( a , n ) ) ;
Generate all cyclic permutations of a number | Function to count the total number of digits in a number . ; Function to generate all cyclic permutations of a number ; Following three lines generates a circular pirmutation of a number . ; If all the permutations are checked and we obtain original number exit from loop . ; Driver Program
function countdigits ( N ) { var count = 0 ; while ( N > 0 ) { count ++ ; N = parseInt ( N / 10 ) ; } return count ; } function cyclic ( N ) { var num = N ; var n = countdigits ( N ) ; while ( true ) { document . write ( num + " " ) ; var rem = num % 10 ; var dev = parseInt ( num / 10 ) ; num = parseInt ( ( ( Math . pow ( 10 , n - 1 ) ) * rem + dev ) ) ; if ( num == N ) break ; } } var N = 5674 ; cyclic ( N ) ;
Check whether a number is circular prime or not | 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 number is circular prime or not . ; Count digits . ; Following three lines generate the next circular permutation of a number . We move last digit to first position . ; If all the permutations are checked and we obtain original number exit from loop . ; Driver Code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function checkCircular ( N ) { let count = 0 , temp = N ; while ( temp > 0 ) { count ++ ; temp = parseInt ( temp / 10 , 10 ) ; } let num = N ; while ( isPrime ( num ) ) { let rem = num % 10 ; let div = parseInt ( num / 10 , 10 ) ; num = ( ( Math . pow ( 10 , count - 1 ) ) * rem ) + div ; if ( num == N ) return true ; } return false ; } let N = 1193 ; if ( checkCircular ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;