codeparrot/xlcost-text-to-code huggingface dataset:
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | JavaScript program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Code
function sumofseries ( n ) { let res = 0 ; for ( let i = 1 ; i <= n ; i ++ ) res += ( i ) * ( i + 1 ) * ( i + 2 ) ; return res ; } document . write ( sumofseries ( 3 ) ) ;
Find N Geometric Means between A and B | insert function for calculating the means ; Finding the value of R Common ration ; for finding N the Geometric mean between A and B ; Driver Code
function printGMeans ( A , B , N ) { let R = Math . pow ( ( B / A ) , 1.0 / ( N + 1 ) ) ; for ( let i = 1 ; i <= N ; i ++ ) document . write ( A * Math . pow ( R , i ) + " " ) ; } let A = 3 , B = 81 , N = 2 ; printGMeans ( A , B , N ) ;
Numbers having difference with digit sum more than s | function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; Driver Code
function digitSum ( n ) { let digSum = 0 ; while ( n > 0 ) { digSum += n % 10 ; n /= 10 ; } return digSum ; } function countInteger ( n , s ) { if ( n < s ) return 0 ; for ( let i = s ; i <= Math . min ( n , s + 163 ) ; i ++ ) if ( ( i - digitSum ( i ) ) > s ) return ( n - i + 1 ) ; return 0 ; } let n = 1000 , s = 100 ; document . write ( countInteger ( n , s ) ) ;
Division without using ' / ' operator | Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver program
function division ( num1 , num2 ) { if ( num1 == 0 ) return 0 ; if ( num2 == 0 ) return Number . MAX_VALUE ; ; let negResult = false ; if ( num1 < 0 ) { num1 = - num1 ; if ( num2 < 0 ) num2 = - num2 ; else negResult = true ; } else if ( num2 < 0 ) { num2 = - num2 ; negResult = true ; } let quotient = 0 ; while ( num1 >= num2 ) { num1 = num1 - num2 ; quotient ++ ; } if ( negResult ) quotient = - quotient ; return quotient ; } let num1 = 13 , num2 = 2 ; document . write ( division ( num1 , num2 ) ) ;
Nonagonal number | Function to find nonagonal number series . ; Driver Code
function Nonagonal ( n ) { for ( let i = 1 ; i <= n ; i ++ ) { document . write ( parseInt ( i * ( 7 * i - 5 ) / 2 ) + " " ) ; } } let n = 10 ; Nonagonal ( n ) ;
Find n | func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver code
function seriesFunc ( n ) { let sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; let sumNatural = ( n * ( n + 1 ) / 2 ) ; return ( sumSquare + sumNatural + 1 ) ; } let n = 8 ; document . write ( seriesFunc ( n ) + " " ) ; n = 13 ; document . write ( seriesFunc ( 13 ) ) ;
Program to check Plus Perfect Number | Function to check plus perfect number ; Calculating number of digits ; Calculating plus perfect number ; Checking whether number is plus perfect or not ; Driver Code
function checkplusperfect ( x ) { let temp = x ; let n = 0 ; while ( x != 0 ) { x = parseInt ( x / 10 ) ; n ++ ; } x = temp ; let sum = 0 ; while ( x != 0 ) { sum += Math . pow ( x % 10 , n ) ; x = parseInt ( x / 10 ) ; } return ( sum == temp ) ; } let x = 9474 ; if ( checkplusperfect ( x ) ) document . write ( " " ) ; else document . write ( " " ) ;
Number of distinct subsets of a set | Returns 2 ^ n ; Driver code
function subsetCount ( arr , n ) { return 1 << n ; } let A = [ 1 , 2 , 3 ] ; let n = A . length ; document . write ( subsetCount ( A , n ) ) ;
Program to calculate GST from original and net prices | javascript Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver code
function Calculate_GST ( org_cost , N_price ) { return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; } var org_cost = 100 ; var N_price = 120 ; document . write ( " " + Calculate_GST ( org_cost , N_price ) + " " ) ;
Centered hexagonal number | Function to find centered hexadecimal number . ; Formula to calculate nth centered hexadecimal number and return it into main function . ; Driver Code
function centeredHexagonalNumber ( n ) { return 3 * n * ( n - 1 ) + 1 ; } let n = 10 ; document . write ( n + " " ) ; document . write ( centeredHexagonalNumber ( n ) ) ;
Find the distance covered to collect items at equal distances | function to calculate the distance ; Driver code
function find_distance ( n ) { return n * ( 3 * n + 7 ) ; } let n = 5 ; document . write ( " " ) ; document . write ( find_distance ( n ) ) ;
Twin Prime Numbers | Please refer below post for details of this function https : goo . gl / Wv3fGv ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver program to test above function
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 twinPrime ( n1 , n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . abs ( n1 - n2 ) == 2 ) ; } let n1 = 11 , n2 = 13 ; if ( twinPrime ( n1 , n2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of the sequence 2 , 22 , 222 , ... ... ... | function which return the the sum of series ; Driver code
function sumOfSeries ( n ) { return 0.0246 * ( Math . pow ( 10 , n ) - 1 - ( 9 * n ) ) ; } let n = 3 ; document . write ( sumOfSeries ( n ) ) ;
Find sum of even index binomial coefficients | Returns value of even indexed Binomial Coefficient Sum which is 2 raised to power n - 1. ; Driver code
function evenbinomialCoeffSum ( n ) { return ( 1 << ( n - 1 ) ) ; } let n = 4 ; document . write ( evenbinomialCoeffSum ( n ) ) ;
Program to print triangular number series till n | Function to find triangular number ; For each iteration increase j by 1 and add it into k ; Increasing j by 1 ; Add value of j into k and update k ; Driven Function
function triangular_series ( n ) { let i , j = 1 , k = 1 ; for ( i = 1 ; i <= n ; i ++ ) { document . write ( k + " " ) ; j = j + 1 ; k = k + j ; } } let n = 5 ; triangular_series ( n ) ;
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + Ã ¢ â ‚¬¦ Ã ¢ â ‚¬¦ + ( 1 + 3 + 5 + 7 + Ã ¢ â ‚¬¦ + ( 2 n | function to find the sum of the given series ; required sum ; Driver Code
function sumOfTheSeries ( n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } let n = 5 ; document . write ( " " + sumOfTheSeries ( n ) ) ;
Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | Function to find sum of given series ; Driver code
function sumOfSeries ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = 1 ; j <= i ; j ++ ) sum += j ; return sum ; } let n = 10 ; document . write ( sumOfSeries ( n ) ) ;
Number of triangles after N moves | function to calculate number of triangles in Nth step ; Driver code
function numberOfTriangles ( n ) { var ans = 2 * ( Math . pow ( 3 , n ) ) - 1 ; return ans ; } var n = 2 ; document . write ( numberOfTriangles ( n ) ) ;
NicomachusÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¾¢ s Theorem ( Sum of k | Return the sum of kth group of positive odd integer . ; Driven Code
function kthgroupsum ( k ) { return k * k * k ; } let k = 3 ; document . write ( kthgroupsum ( k ) ) ;
Find x , y , z that satisfy 2 / n = 1 / x + 1 / y + 1 / z | function to find x y and z that satisfy given equation . ; driver program to test the above function
function printXYZ ( n ) { if ( n == 1 ) document . write ( - 1 ) ; else document . write ( " " + n + " " + ( n + 1 ) + " " + n * ( n + 1 ) ) ; } let n = 7 ; printXYZ ( n ) ;
Find n | Function to find nth term ; Driver code
function term ( n ) { return parseInt ( n * ( n + 1 ) / 2 ) ; } let n = 4 ; document . write ( term ( n ) ) ;
Find Harmonic mean using Arithmetic mean and Geometric mean | Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver Code
function compute ( a , b ) { var AM = ( a + b ) / 2 ; var GM = Math . sqrt ( a * b ) ; var HM = ( GM * GM ) / AM ; return HM ; } var a = 5 ; var b = 15 ; var HM = compute ( a , b ) document . write ( " " + a + " " + b + " " + HM . toFixed ( 3 ) ) ;
Find n | Returns n - th element of the series ; driver program to test the above function
function series ( n ) { return ( 8 * n * n ) + 1 ; } let n = 5 ; document . write ( series ( n ) ) ;
Check if a number is divisible by all prime divisors of another number | Javascript program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver program to test above functions
function gcd ( a , b ) { return b == 0 ? a : gcd ( b , a % b ) ; } function isDivisible ( x , y ) { if ( y == 1 ) return true ; var z = gcd ( x , y ) ; if ( z == 1 ) return false ; return isDivisible ( x , y / z ) ; } var x = 18 , y = 12 ; if ( isDivisible ( x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find Sum of a Series a ^ 1 / 1 ! + a ^ 2 / 2 ! + a ^ 3 / 3 ! + a ^ 4 / 4 ! + Ã ¢ â ‚¬¦ Ã ¢ â ‚¬¦ . + a ^ n / n ! | function to calculate sum of given series ; multiply ( a / i ) to previous term ; store result in res ; Driver Function
function sumOfSeries ( a , num ) { let res = 0 , prev = 1 ; for ( let i = 1 ; i <= num ; i ++ ) { prev *= ( a / i ) ; res = res + prev ; } return ( res ) ; } let n = 5 , a = 2 ; document . write ( sumOfSeries ( a , n ) ) ;
Program for Celsius To Fahrenheit conversion | function to convert Celsius scale to Fahrenheit scale ; driver code
function Cel_To_Fah ( n ) { return ( ( n * 9.0 / 5.0 ) + 32.0 ) ; } let n = 20.0 ; document . write ( Cel_To_Fah ( n ) ) ;
Series with largest GCD and sum equals to n | function to generate and print the sequence ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) d , ; computes the last element of the sequence n - s . ; prints the last element ; Driver Code
function print_sequence ( n , k ) { let b = parseInt ( n / ( k * ( k + 1 ) / 2 ) ) ; if ( b == 0 ) { document . write ( - 1 ) ; } else { let r = 1 ; for ( let x = 1 ; x * x <= n ; x ++ ) { if ( n % x != 0 ) continue ; if ( x <= b && x > r ) r = x ; if ( n / x <= b && n / x > r ) r = n / x ; } for ( let i = 1 ; i < k ; i ++ ) document . write ( r * i + " " ) ; let res = n - ( r * ( k * ( k - 1 ) / 2 ) ) ; document . write ( res + " " ) ; } } let n = 24 ; let k = 4 ; print_sequence ( n , k ) ; n = 24 ; k = 5 ; print_sequence ( n , k ) ; n = 6 ; k = 4 ; print_sequence ( n , k ) ;
Number of compositions of a natural number | javascript program to find the total number of compositions of a natural number ; Return 2 raised to power ( n - 1 ) ; Driver Code
function countCompositions ( n ) { return 1 << ( n - 1 ) ; } var n = 4 ; document . write ( countCompositions ( n ) ) ;
Program to count digits in an integer ( 4 Different Methods ) | Recursive Javascript program to count number of digits in a number ; Driver code
function countDigit ( n ) { if ( n / 10 == 0 ) return 1 ; return 1 + countDigit ( parseInt ( n / 10 ) ) ; } var n = 345289467 ; document . write ( " " + countDigit ( n ) ) ;
Tribonacci Numbers | Javascript program to print first n Tribonacci numbers . ; driver function
function printTrib ( n ) { let dp = Array . from ( { length : n } , ( _ , i ) => 0 ) ; dp [ 0 ] = dp [ 1 ] = 0 ; dp [ 2 ] = 1 ; for ( let i = 3 ; i < n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] ; for ( let i = 0 ; i < n ; i ++ ) document . write ( dp [ i ] + " " ) ; } let n = 10 ; printTrib ( n ) ;
Tribonacci Numbers | javascript Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code
function multiply ( T , M ) { var a , b , c , d , e , f , g , h , i ; a = T [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 0 ] ; b = T [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 1 ] ; c = T [ 0 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 2 ] ; d = T [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 0 ] ; e = T [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 1 ] ; f = T [ 1 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 2 ] ; g = T [ 2 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 0 ] ; h = T [ 2 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 1 ] ; i = T [ 2 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 2 ] ; T [ 0 ] [ 0 ] = a ; T [ 0 ] [ 1 ] = b ; T [ 0 ] [ 2 ] = c ; T [ 1 ] [ 0 ] = d ; T [ 1 ] [ 1 ] = e ; T [ 1 ] [ 2 ] = f ; T [ 2 ] [ 0 ] = g ; T [ 2 ] [ 1 ] = h ; T [ 2 ] [ 2 ] = i ; } function power ( T , n ) { if ( n == 0 n == 1 ) return ; var M = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; power ( T , parseInt ( n / 2 ) ) ; multiply ( T , T ) ; if ( n % 2 != 0 ) multiply ( T , M ) ; } function tribonacci ( n ) { var T = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; if ( n == 0 n == 1 ) return 0 ; else power ( T , n - 2 ) ; return T [ 0 ] [ 0 ] ; } var n = 10 ; for ( var i = 0 ; i < n ; i ++ ) document . write ( tribonacci ( i ) + " " ) ; document . write ( ' ' ) ;
Geometric mean ( Two Methods ) | function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . . + log ( n ) ) / n ) and return the value to main function . ; Driver function ; function call
function geometricMean ( arr , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum = sum + Math . log ( arr [ i ] ) ; sum = sum / n ; return Math . exp ( sum ) ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ; var n = arr . length ; document . write ( geometricMean ( arr , n ) . toFixed ( 5 ) ) ;
Smallest number k such that the product of digits of k is equal to n | function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to ' k ' ; required smallest number ; Driver program to test above
function smallestNumber ( n ) { if ( n >= 0 && n <= 9 ) { return n ; } let digits = [ ] ; for ( let i = 9 ; i >= 2 && n > 1 ; i -- ) { while ( n % i == 0 ) { digits . push ( i ) ; n = Math . floor ( n / i ) ; } } if ( n != 1 ) { return - 1 ; } let k = 0 ; while ( digits . length != 0 ) { k = k * 10 + digits [ digits . length - 1 ] ; digits . pop ( ) ; } return k ; } let n = 100 ; document . write ( smallestNumber ( n ) ) ;
Check if a number is magic ( Recursive sum of digits is 1 ) | JavaScript program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
function isMagic ( n ) { var sum = 0 ; while ( n > 0 sum > 9 ) { if ( n = 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum = 1 ) ; } var n = 1234 ; if ( isMagic ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number is magic ( Recursive sum of digits is 1 ) | Accepting sample input ; Condition to check Magic number
var x = 1234 ; if ( x % 9 == 1 ) document . write ( " " ) ; else document . write ( " " ) ;
Sylvester 's sequence | Javascript program to print terms of Sylvester 's sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driver Code
let N = 1000000007 ; function printSequence ( n ) { let a = 1 ; let ans = 2 ; for ( let i = 1 ; i <= n ; i ++ ) { document . write ( ans + " " ) ; ans = ( ( a % N ) * ( ans % N ) ) % N ; a = ans ; ans = ( ans + 1 ) % N ; } } let n = 6 ; printSequence ( n ) ;
Program to find sum of first n natural numbers | Returns sum of first n natural numbers ; Driver code
function findSum ( n ) { let sum = 0 ; for ( let x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } let n = 5 ; document . write ( findSum ( n ) ) ;
Hailstone Numbers | JavaScript program to generate hailstone numbers and calculate steps required to reduce them to 1 ; function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver Code ; Function to generate Hailstone Numbers ; Output : Number of Steps
let c = 0 ; function HailstoneNumbers ( N ) { document . write ( N + " " ) ; if ( N == 1 && c == 0 ) { return c ; } else if ( N == 1 && c != 0 ) { c ++ ; return c ; } else if ( N % 2 == 0 ) { c ++ ; HailstoneNumbers ( N / 2 ) ; } else if ( N % 2 != 0 ) { c ++ ; HailstoneNumbers ( 3 * N + 1 ) ; } return c ; } let N = 7 ; let x ; x = HailstoneNumbers ( N ) ; document . write ( " " ) ; document . write ( " " + x ) ;
Find m | Function to return mth summation ; base case ; driver program
function SUM ( n , m ) { if ( m == 1 ) return ( n * ( n + 1 ) / 2 ) ; let sum = SUM ( n , m - 1 ) ; return ( sum * ( sum + 1 ) / 2 ) ; } let n = 5 ; let m = 3 ; document . write ( " " + n + " " + m + " " + SUM ( n , m ) ) ;
Count number of digits after decimal on dividing a number | JavaScript program to count digits after dot when a number is divided by another . ; let ans = 0 ; Initialize result ; calculating remainder ; if this remainder appeared before then the numbers are irrational and would not converge to a solution the digits after decimal will be infinite ; Driver code
function count ( x , y ) { let m = new Map ( ) ; while ( x % y != 0 ) { x = x % y ; ans ++ ; if ( m . has ( x ) ) return - 1 ; m . set ( x , 1 ) ; x = x * 10 ; } return ans ; } let res = count ( 1 , 2 ) ; if ( ( res == - 1 ) ) document . write ( " " + " " ) ; else document . write ( res + " " ) ; res = count ( 5 , 3 ) ; if ( ( res == - 1 ) ) document . write ( " " + " " ) ; else document . write ( res + " " ) ; res = count ( 3 , 5 ) ; if ( ( res == - 1 ) ) document . write ( " " + " " ) ; else document . write ( res + " " ) ;
Find smallest number n such that n XOR n + 1 equals to given k . | function to return the required n ; if k is of form 2 ^ i - 1 ; driver program
function xorCalc ( k ) { if ( k == 1 ) return 2 ; if ( ( ( k + 1 ) & k ) == 0 ) return parseInt ( k / 2 ) ; return 1 ; } var k = 31 ; document . write ( xorCalc ( k ) ) ;
Find n | javascript program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append 7 and move to parent ; Reverse res and return . ; Driver code
function findNthNo ( n ) { res = " " ; while ( n >= 1 ) { if ( ( n & 1 ) == 1 ) { res = res + " " ; n = ( n - 1 ) / 2 ; } else { res = res + " " ; n = parseInt ( ( n - 2 ) / 2 ) ; } } return res . split ( " " ) . reverse ( ) . join ( " " ) ; } var n = 13 ; document . write ( findNthNo ( n ) ) ;
Total number of divisors for a given number | program for finding no . of divisors ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; driver program
function divCount ( n ) { var hash = Array ( n + 1 ) . fill ( true ) ; for ( var p = 2 ; p * p < n ; p ++ ) if ( hash [ p ] == true ) for ( var i = p * 2 ; i < n ; i += p ) hash [ i ] = false ; var total = 1 ; for ( var p = 2 ; p <= n ; p ++ ) { if ( hash [ p ] ) { var count = 0 ; if ( n % p == 0 ) { while ( n % p == 0 ) { n = parseInt ( n / p ) ; count ++ ; } total = total * ( count + 1 ) ; } } } return total ; } var n = 24 ; document . write ( divCount ( n ) ) ;
Maximum number of unique prime factors | Javascript program to find maximum number of prime factors for a number in range [ 1 , N ] ; Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of unique prime factors . ; Return maximum element in arr [ ] ; driver program
function getMax ( Arr ) { let max = Arr [ 0 ] ; for ( let i = 1 ; i < Arr . length ; i ++ ) if ( Arr [ i ] > max ) max = Arr [ i ] ; return max ; } function maxPrimefactorNum ( N ) { let arr = new Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) for ( let j = 2 * i ; j <= N ; j += i ) arr [ j ] ++ ; arr [ i ] = 1 ; } return getMax ( arr ) ; } let N = 40 ; document . write ( maxPrimefactorNum ( N ) ) ;
Decimal to binary conversion without using arithmetic operators | function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Driver program to test above
function decToBin ( n ) { if ( n == 0 ) return " " ; var bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? ' ' : ' ' ) + bin ; n >>= 1 ; } return bin ; } var n = 38 ; document . write ( decToBin ( n ) ) ;
Sum of array elements that is first continuously increasing then decreasing | Efficient Javascript method to find sum of the elements of array that is halfway increasing and then halfway decreassing ; Driver code
function arraySum ( arr , n ) { let x = ( n + 1 ) / 2 ; return ( arr [ 0 ] - 1 ) * n + x * x ; } let arr = [ 10 , 11 , 12 , 13 , 12 , 11 , 10 ] ; let n = arr . length ; document . write ( arraySum ( arr , n ) ) ;
Balance pans using given weights that are powers of a number | method returns true if balancing of scale is possible ; baseForm vector will store T 's  representation on base a  in reverse order ; convert T to representation on base a ; make first digit of representation as 0 ; loop over base representation of T ; if any digit is not 0 , 1 , ( a - 1 ) or a then balancing is not possible ; if digit is a or ( a - 1 ) then increase left index ' s ▁ count / ▁ ( case , ▁ when ▁ this ▁ weight ▁ is ▁ transferred ▁ to ▁ T ' s side ) ; if representation is processed then balancing is possible ; Driver code
function isBalancePossible ( T , a ) { let baseForm = [ ] ; let s = 0 ; while ( T > 0 ) { baseForm . push ( T % a ) ; T = Math . floor ( T / a ) ; s ++ ; } baseForm . push ( 0 ) ; for ( let i = 0 ; i < s ; i ++ ) { if ( baseForm [ i ] != 0 && baseForm [ i ] != 1 && baseForm [ i ] != ( a - 1 ) && baseForm [ i ] != a ) { return false ; } if ( baseForm [ i ] == a || baseForm [ i ] == ( a - 1 ) ) { baseForm . splice ( i + 1 , 0 , baseForm [ i + 1 ] + 1 ) ; } } return true ; } let T = 11 ; let a = 4 ; let balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) { document . write ( " " ) ; } else { document . write ( " " ) ; }