codeparrot/xlcost-text-to-code huggingface dataset:
Number of steps to convert to prime factors | Javascript program to count number of steps required to convert an integer array to array of factors . ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate the number of representations ; keep an count of prime factors ; traverse for every element ; count the no of factors ; subtract 1 if Ai is not 1 as the last step wont be taken into count ; call sieve to calculate the factors
let MAX = 1000001 ; let factor = [ ] ; function cal_factor ( ) { factor [ 1 ] = 1 ; for ( let i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( let i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( let i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( let j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } function no_of_representations ( a , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let temp = a [ i ] ; let flag = 0 ; while ( factor [ temp ] != 1 ) { flag = - 1 ; count ++ ; temp = temp / factor [ temp ] ; } count += flag ; } return count ; } cal_factor ( ) ; let a = [ 4 , 4 , 4 ] ; let n = a . length ; document . write ( no_of_representations ( a , n ) ) ;
Subsequences of size three in an array whose sum is divisible by m | Javascript program to find count of subsequences of size three divisible by M . ; Three nested loop to find all the sub sequences of length three in the given array A [ ] . ; checking if the sum of the chosen three number is divisible by m . ; Driver code
function coutSubSeq ( A , N , M ) { let sum = 0 ; let ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { for ( let k = j + 1 ; k < N ; k ++ ) { sum = A [ i ] + A [ j ] + A [ k ] ; if ( sum % M == 0 ) ans ++ ; } } } return ans ; } let M = 3 ; let A = [ 1 , 2 , 4 , 3 ] ; let N = A . length ; document . write ( coutSubSeq ( A , N , M ) ) ;
Subsequences of size three in an array whose sum is divisible by m | javascript program to find count of subsequences of size three divisible by M . ; Storing frequencies of all remainders when divided by M . ; including i and j in the sum rem calculate the remainder required to make the sum divisible by M ; if the required number is less than j , we skip as we have already calculated for that value before . As j here starts with i and rem is less than j . ; if satisfies the first case . ; if satisfies the second case ; if satisfies the third case ; Driver code
function countSubSeq ( A , N , M ) { var ans = 0 ; var h = Array . from ( { length : M } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { A [ i ] = A [ i ] % M ; h [ A [ i ] ] ++ ; } for ( var i = 0 ; i < M ; i ++ ) { for ( var j = i ; j < M ; j ++ ) { var rem = ( M - ( i + j ) % M ) % M ; if ( rem < j ) continue ; if ( i == j && rem == j ) ans += h [ i ] * ( h [ i ] - 1 ) * ( h [ i ] - 2 ) / 6 ; else if ( i == j ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ rem ] / 2 ; else if ( i == rem ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ j ] / 2 ; else if ( rem == j ) ans += h [ j ] * ( h [ j ] - 1 ) * h [ i ] / 2 ; else ans = ans + h [ i ] * h [ j ] * h [ rem ] ; } } return ans ; } var M = 3 ; var A = [ 1 , 2 , 4 , 3 ] ; var N = A . length ; document . write ( countSubSeq ( A , N , M ) ) ;
Find n | utility function ; since first element of the series is 7 , we initialise a variable with 7 ; Using iteration to find nth term ; Driver Code
function findTerm ( n ) { if ( n == 1 ) return n ; else { let term = 7 ; for ( let i = 2 ; i <= n ; i ++ ) term = term * 2 + ( i - 1 ) ; return term ; } } let n = 5 ; document . write ( findTerm ( n ) ) ;
Find n | Returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; One by one subtract counts elements in different blocks ; Driver code
function findNumber ( n ) { n -- ; let i = 1 ; while ( n >= 0 ) { n -= i ; ++ i ; } return ( n + i ) ; } let n = 3 ; document . write ( findNumber ( n ) ) ;
Program to find correlation coefficient | Function that returns correlation coefficient . ; Sum of elements of array X . ; Sum of elements of array Y . ; Sum of X [ i ] * Y [ i ] . ; Sum of square of array elements . ; Use formula for calculating correlation coefficient . ; Driver code ; Find the size of array . ; Function call to correlationCoefficient .
function correlationCoefficient ( X , Y , n ) { let sum_X = 0 , sum_Y = 0 , sum_XY = 0 ; let squareSum_X = 0 , squareSum_Y = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum_X = sum_X + X [ i ] ; sum_Y = sum_Y + Y [ i ] ; sum_XY = sum_XY + X [ i ] * Y [ i ] ; squareSum_X = squareSum_X + X [ i ] * X [ i ] ; squareSum_Y = squareSum_Y + Y [ i ] * Y [ i ] ; } let corr = ( n * sum_XY - sum_X * sum_Y ) / ( Math . sqrt ( ( n * squareSum_X - sum_X * sum_X ) * ( n * squareSum_Y - sum_Y * sum_Y ) ) ) ; return corr ; } let X = [ 15 , 18 , 21 , 24 , 27 ] ; let Y = [ 25 , 25 , 27 , 31 , 32 ] ; let n = X . length ; document . write ( correlationCoefficient ( X , Y , n ) ) ;
Find the number of spectators standing in the stadium at time t | Javascript program to find number of spectators standing at a time ; If the time is less than k then we can print directly t time . ; If the time is n then k spectators are standing . ; Otherwise we calculate the spectators standing . ; Stores the value of n , k and t t is time n & k is the number of specators
function result ( n , k , t ) { if ( t <= k ) document . write ( t ) ; else if ( t <= n ) document . write ( k ) ; else { let temp = t - n ; temp = k - temp ; document . write ( temp ) ; } } let n , k , t ; n = 10 ; k = 5 ; t = 12 ; result ( n , k , t ) ;
Program for weighted mean of natural numbers . | Function to calculate weighted mean . ; Take num array and corresponding weight array and initialize it . ; Calculate the size of array . ; Check the size of both array is equal or not .
function weightedMean ( X , W , n ) { let sum = 0 , numWeight = 0 ; for ( let i = 0 ; i < n ; i ++ ) { numWeight = numWeight + X [ i ] * W [ i ] ; sum = sum + W [ i ] ; } return ( numWeight ) / sum ; } let X = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let W = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let n = X . length ; let m = W . length ; if ( n == m ) document . write ( weightedMean ( X , W , n ) ) ; else document . write ( " " ) ;
Program to find GCD of floating point numbers | Recursive function to return gcd of a and b ; base case ; Driver Function .
function gcd ( a , b ) { if ( a < b ) return gcd ( b , a ) ; if ( Math . abs ( b ) < 0.001 ) return a ; else return ( gcd ( b , a - Math . floor ( a / b ) * b ) ) ; } var a = 1.20 , b = 22.5 ; document . write ( gcd ( a , b ) . toFixed ( 1 ) ) ;
Program for harmonic mean of numbers | Function that returns harmonic mean . ; Declare sum variables and initialize with zero . ; Driver code
function harmonicMean ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum = sum + ( 1 / arr [ i ] ) ; return n / sum ; } let arr = [ 13.5 , 14.5 , 14.8 , 15.2 , 16.1 ] ; let n = arr . length ; document . write ( harmonicMean ( arr , n ) ) ;
Program for harmonic mean of numbers | Function that returns harmonic mean . ; Driver code
function harmonicMean ( arr , freq , n ) { let sum = 0 , frequency_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + ( freq [ i ] / arr [ i ] ) ; frequency_sum = frequency_sum + freq [ i ] ; } return frequency_sum / sum ; } let num = [ 13 , 14 , 15 , 16 , 17 ] ; let freq = [ 2 , 5 , 13 , 7 , 3 ] ; let n = num . length ; document . write ( harmonicMean ( num , freq , n ) ) ;
First collision point of two series | Javascript program to calculate the colliding point of two series ; Iterating through n terms of the first series ; x is i - th term of first series ; d is first element of second series and c is common difference for second series . ; If no term of first series is found ; Driver Code
function point ( a , b , c , d , n ) { let x ; let flag = 0 ; for ( let i = 0 ; i < n ; i ++ ) { x = b + i * a ; if ( ( x - d ) % c == 0 && x - d >= 0 ) { document . write ( x ) ; flag = 1 ; break ; } } if ( flag == 0 ) { document . write ( " " ) ; } } let a = 20 ; let b = 2 ; let c = 9 ; let d = 19 ; let n = 20 ; point ( a , b , c , d , n ) ;
Armstrong Numbers between two integers | Prints Armstrong Numbers in given range ; number of digits calculation ; compute sum of nth power of its digits ; checks if number i is equal to the sum of nth power of its digits ; Driver code
function findArmstrong ( low , high ) { for ( let i = low + 1 ; i < high ; ++ i ) { let x = i ; let n = 0 ; while ( x != 0 ) { x = parseInt ( x / 10 ) ; ++ n ; } let pow_sum = 0 ; x = i ; while ( x != 0 ) { let digit = x % 10 ; pow_sum += parseInt ( Math . pow ( digit , n ) ) ; x = parseInt ( x / 10 ) ; } if ( pow_sum == i ) document . write ( i + " " ) ; } } let num1 = 100 ; let num2 = 400 ; findArmstrong ( num1 , num2 ) ;
Pair with maximum GCD from two arrays | Find the maximum GCD pair with maximum sum ; array to keep a count of existing elements ; first [ i ] and second [ i ] are going to store maximum multiples of i in a [ ] and b [ ] respectively . ; traverse through the first array to mark the elements in cnt ; Find maximum multiple of every number in first array ; Find maximum multiple of every number in second array . We re - initialise cnt [ ] and traverse through the second array to mark the elements in cnt ; if the multiple is present in the second array then store the max of number or the pre - existing element ; traverse for every elements and checks the maximum N that is present in both the arrays ; driver program ; Maximum possible value of elements in both arrays .
function gcdMax ( a , b , n , N ) { let cnt = Array . from ( { length : N } , ( _ , i ) => 0 ) ; let first = Array . from ( { length : N } , ( _ , i ) => 0 ) ; let second = Array . from ( { length : N } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; ++ i ) cnt [ a [ i ] ] = 1 ; for ( let i = 1 ; i < N ; ++ i ) for ( let j = i ; j < N ; j += i ) if ( cnt [ j ] > 0 ) first [ i ] = Math . max ( first [ i ] , j ) ; cnt = Array . from ( { length : N } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; ++ i ) cnt [ b [ i ] ] = 1 ; for ( let i = 1 ; i < N ; ++ i ) for ( let j = i ; j < N ; j += i ) if ( cnt [ j ] > 0 ) second [ i ] = Math . max ( second [ i ] , j ) ; let x ; for ( x = N - 1 ; x >= 0 ; x -- ) if ( first [ x ] > 0 && second [ x ] > 0 ) break ; document . write ( first [ x ] + " " + second [ x ] ) ; } let a = [ 3 , 1 , 4 , 2 , 8 ] ; let b = [ 5 , 2 , 12 , 8 , 3 ] ; let n = a . length ; let N = 20 ; gcdMax ( a , b , n , N ) ;
Pierpont Prime | Javascript program to print Pierpont prime numbers smaller than n . ; Finding all numbers having factor power of 2 and 3 Using sieve ; Storing number of the form 2 ^ i . 3 ^ k + 1. ; Finding prime number using sieve of Eratosthenes . Reusing same array as result of above computations in v . ; Printing n pierpont primes smaller than n ; Driven Program
function printPierpont ( n ) { var arr = Array ( n + 1 ) . fill ( false ) ; var two = 1 , three = 1 ; while ( two + 1 < n ) { arr [ two ] = true ; while ( two * three + 1 < n ) { arr [ three ] = true ; arr [ two * three ] = true ; three *= 3 ; } three = 1 ; two *= 2 ; } var v = [ ] ; for ( var i = 0 ; i < n ; i ++ ) if ( arr [ i ] ) v . push ( i + 1 ) ; arr = Array ( n + 1 ) . fill ( false ) ; for ( var p = 2 ; p * p < n ; p ++ ) { if ( arr [ p ] == false ) for ( var i = p * 2 ; i < n ; i += p ) arr [ i ] = true ; } for ( var i = 0 ; i < v . length ; i ++ ) if ( ! arr [ v [ i ] ] ) document . write ( v [ i ] + " " ) ; } var n = 200 ; printPierpont ( n ) ;
Woodall Number | Javascript program to check if a number is Woodball or not . ; If number is even , return false . ; If x is 1 , return true . ; Add 1 to make x even ; While x is divisible by 2 ; Divide x by 2 ; Count the power ; If at any point power and x became equal , return true . ; Driver Code
function isWoodall ( x ) { if ( x % 2 == 0 ) return false ; if ( x == 1 ) return true ; x ++ ; let p = 0 ; while ( x % 2 == 0 ) { x = x / 2 ; p ++ ; if ( p == x ) return true ; } return false ; } let x = 383 ; if ( isWoodall ( x ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print k numbers where all pairs are divisible by m | function to generate k numbers whose difference is divisible by m ; Using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; If we found k elements which have same remainder . ; If we could not find k elements ; driver program to test the above function
function print_result ( a , n , k , m ) { var v = Array . from ( Array ( m ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < n ; i ++ ) { var rem = a [ i ] % m ; v [ rem ] . push ( a [ i ] ) ; if ( v [ rem ] . length == k ) { for ( var j = 0 ; j < k ; j ++ ) document . write ( v [ rem ] [ j ] + " " ) ; return ; } } document . write ( " " ) ; } var a = [ 1 , 8 , 4 ] ; var n = a . length ; print_result ( a , n , 2 , 3 ) ;
Smallest x such that 1 * n , 2 * n , ... x * n have all digits from 1 to 9 | Returns smallest value x such that 1 * n , 2 * n , 3 * n ... x * n have all digits from 1 to 9 at least once ; taking temporary array and variable . ; iterate till we get all the 10 digits at least once ; checking all the digits ; driver program
function smallestX ( n ) { let temp = Array . from ( { length : 10 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < 10 ; i ++ ) temp [ i ] = 0 ; if ( n == 0 ) return - 1 ; let count = 0 , x = 0 ; for ( x = 1 ; count < 10 ; x ++ ) { let y = x * n ; while ( y > 0 ) { if ( temp [ y % 10 ] == 0 ) { count ++ ; temp [ y % 10 ] = 1 ; } y /= 10 ; } } return x - 1 ; } let n = 5 ; document . write ( smallestX ( n ) ) ;
Find a number x such that sum of x and its digits is equal to given n . | utility function for digit sum ; function for finding x ; iterate from 1 to n . For every no . check if its digit sum with it is equal to n . ; if no such i found return - 1 ; driver program
function digSum ( n ) { let sum = 0 , rem = 0 ; while ( n > 0 ) { rem = n % 10 ; sum += rem ; n = Math . floor ( n / 10 ) ; } return sum ; } function findX ( n ) { for ( let i = 0 ; i <= n ; i ++ ) if ( i + digSum ( i ) == n ) return i ; return - 1 ; } let n = 43 ; document . write ( " " + findX ( n ) ) ;
9 's complement of a decimal number | Javascript program to find 9 's complement of a number. ; Driver code
function complement ( number ) { number = number . split ( ' ' ) for ( let i = 0 ; i < number . length ; i ++ ) { if ( number [ i ] != ' ' ) { number [ i ] = String ( 9 - Number ( number [ i ] ) + 0 ) ; } } number = number . join ( " " ) document . write ( " " + number ) ; } let number = " " ; complement ( number ) ;
Ways to express a number as product of two different factors | To count number of ways in which number expressed as product of two different numbers ; To store count of such pairs ; Counting number of pairs upto sqrt ( n ) - 1 ; To return count of pairs ; Driver Code
function countWays ( n ) { let count = 0 ; for ( let i = 1 ; i * i < n ; i ++ ) if ( n % i == 0 ) count ++ ; return count ; } let n = 12 ; document . write ( countWays ( n ) ) ;
Count divisors of n that have at | function to return true if any digit of m is present in hash [ ] . ; check till last digit ; if number is also present in original number then return true ; if no number matches then return 1 ; Count the no of divisors that have at least 1 digits same ; Store digits present in n in a hash [ ] ; marks that the number is present ; last digit removed ; loop to traverse from 1 to sqrt ( n ) to count divisors ; if i is the factor ; call the function to check if any digits match or not ; if n / i != i then a different number , then check it also ; return the answer ; Driver Code
function isDigitPresent ( m , hash ) { while ( m > 0 ) { if ( hash [ m % 10 ] ) return true ; m = Math . floor ( m / 10 ) ; } return false ; } function countDivisibles ( n ) { let hash = Array . from ( { length : 10 } , ( _ , i ) => 0 ) ; let m = n ; while ( m > 0 ) { hash [ m % 10 ] = true ; m = Math . floor ( m / 10 ) ; } let ans = 0 ; for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( isDigitPresent ( i , hash ) ) ans ++ ; if ( n / i != i ) { if ( isDigitPresent ( n / i , hash ) ) ans ++ ; } } } return ans ; } let n = 15 ; document . write ( countDivisibles ( n ) ) ;
Doolittle Algorithm : LU Decomposition | Javascript Program to decompose a matrix into lower and upper triangular matrix function MAX = 100 ; ; Decomposing matrix into Upper and Lower triangular matrix ; Upper Triangular ; Summation of L ( i , j ) * U ( j , k ) ; Evaluating U ( i , k ) ; Lower Triangular ; Diagonal as 1 ; Summation of L ( k , j ) * U ( j , i ) ; Evaluating L ( k , i ) ; Setw is for displaying nicely ; Displaying the result : ; Lower ; Upper ; Driver code
var s = " " ; function luDecomposition ( mat , n ) { var lower = Array ( n ) . fill ( 0 ) . map ( x => Array ( n ) . fill ( 0 ) ) ; var upper = Array ( n ) . fill ( 0 ) . map ( x => Array ( n ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var k = i ; k < n ; k ++ ) { var sum = 0 ; for ( var j = 0 ; j < i ; j ++ ) sum += ( lower [ i ] [ j ] * upper [ j ] [ k ] ) ; upper [ i ] [ k ] = mat [ i ] [ k ] - sum ; } for ( var k = i ; k < n ; k ++ ) { if ( i == k ) lower [ i ] [ i ] = 1 ; else { var sum = 0 ; for ( var j = 0 ; j < i ; j ++ ) sum += ( lower [ k ] [ j ] * upper [ j ] [ i ] ) ; lower [ k ] [ i ] = parseInt ( ( mat [ k ] [ i ] - sum ) / upper [ i ] [ i ] ) ; } } } document . write ( setw ( 2 ) + " " + setw ( 10 ) + " " ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) document . write ( setw ( 4 ) + lower [ i ] [ j ] + " " ) ; document . write ( setw ( 10 ) ) ; for ( var j = 0 ; j < n ; j ++ ) document . write ( setw ( 4 ) + upper [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } function setw ( noOfSpace ) { var s = " " ; for ( i = 0 ; i < noOfSpace ; i ++ ) s += " " ; return s ; } var mat = [ [ 2 , - 1 , - 2 ] , [ - 4 , 6 , 3 ] , [ - 4 , - 2 , 8 ] ] ; luDecomposition ( mat , 3 ) ;
Divide number into two parts divisible by given numbers | method prints divisible parts if possible , otherwise prints ' Not ▁ possible ' ; creating arrays to store reminder ; looping over all suffix and storing reminder with f ; getting suffix reminder from previous suffix reminder ; looping over all prefix and storing reminder with s ; getting prefix reminder from next prefix reminder ; updating base1 value ; now looping over all reminders to check partition condition ; if both reminders are 0 and digit itself is not 0 , then print result and return ; if we reach here , then string can ' be partitioned under constraints ; Driver Code
function printTwoDivisibleParts ( num , f , s ) { var N = num . length ; var prefixReminder = [ ] var suffixReminder = [ ] suffixReminder [ 0 ] = 0 ; for ( var i = 1 ; i < N ; i ++ ) suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( num [ i - 1 ] - ' ' ) ) % f ; prefixReminder [ N ] = 0 ; var base1 = 1 ; for ( var i = N - 1 ; i >= 0 ; i -- ) { prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( num [ i ] - ' ' ) * base1 ) % s ; base1 = ( base1 * 10 ) % s ; } for ( var i = 0 ; i < N ; i ++ ) { if ( prefixReminder [ i ] == 0 && suffixReminder [ i ] == 0 && num [ i ] != ' ' ) { document . write ( num . substring ( 0 , i ) + " " + num . substring ( i ) ) ; return ; } } document . write ( " " ) ; } var num = " " ; var f = 12345 ; var s = 1024 ; printTwoDivisibleParts ( num , f , s ) ;
Number of subarrays whose minimum and maximum are same | calculate the no of contiguous subarrays which has same minimum and maximum ; stores the answer ; loop to traverse from 0 - n ; start checking subarray from next element ; traverse for finding subarrays ; if the elements are same then we check further and keep a count of same numbers in ' r ' ; the no of elements in between r and i with same elements . ; the no . of subarrays that can be formed between i and r ; again start checking from the next index ; returns answer ; Driver Code
function calculate ( a , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let r = i + 1 ; for ( let j = r ; j < n ; j ++ ) { if ( a [ i ] == a [ j ] ) r += 1 ; else break ; } let d = r - i ; ans += ( d * ( d + 1 ) / 2 ) ; i = r - 1 ; } return ans ; } let a = [ 2 , 4 , 5 , 3 , 3 , 3 ] ; document . write ( calculate ( a , a . length ) ) ;
Count of numbers satisfying m + sum ( m ) + sum ( sum ( m ) ) = N | function that returns sum of digits in a number ; initially sum of digits is 0 ; loop runs till all digits have been extracted ; last digit from backside ; sums up the digits ; the number is reduced to the number removing the last digit ; returns the sum of digits in a number ; function to calculate the count of such occurrences ; counter to calculate the occurrences ; loop to traverse from n - 97 to n ; calls the function to calculate the sum of digits of i ; calls the function to calculate the sum of digits of a ; if the summation is equal to n then increase counter by 1 ; returns the count ; driver program to test the above function ; calls the function to get the answer
function sum ( n ) { var rem = 0 ; var sum_of_digits = 0 ; while ( n > 0 ) { rem = n % 10 ; sum_of_digits += rem ; n = parseInt ( n / 10 ) ; } return sum_of_digits ; } function count ( n ) { var c = 0 ; for ( i = n - 97 ; i <= n ; i ++ ) { var a = sum ( i ) ; var b = sum ( a ) ; if ( ( i + a + b ) == n ) { c += 1 ; } } return c ; } var n = 9939 ; document . write ( count ( n ) ) ;
Check if a number is power of k using base changing method | JavaScript program to check if a number can be raised to k ; loop to change base n to base = k ; Find current digit in base k ; If digit is neither 0 nor 1 ; Make sure that only one 1 is present . ; Driver Code
function isPowerOfK ( n , k ) { let oneSeen = false ; while ( n > 0 ) { let digit = n % k ; if ( digit > 1 ) return false ; if ( digit == 1 ) { if ( oneSeen ) return false ; oneSeen = true ; } n = Math . floor ( n / k ) ; } return true ; } let n = 64 , k = 4 ; if ( isPowerOfK ( n , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if number is palindrome or not in Octal | Javascript program to check if octal representation of a number is prime ; Function to Check no is in octal or not ; Function To check no is palindrome or not ; If number is already in octal , we traverse digits using repeated division with 10. Else we traverse digits using repeated division with 8 ; To store individual digits ; Traversing all digits ; checking if octal no is palindrome ; Driver code
const MAX_DIGITS = 20 ; function isOctal ( n ) { while ( n ) { if ( ( n % 10 ) >= 8 ) return false ; else n = Math . floor ( n / 10 ) ; } return true ; } function isPalindrome ( n ) { var divide = ( isOctal ( n ) == false ) ? 8 : 10 ; var octal = new Array ( MAX_DIGITS ) ; var i = 0 ; while ( n != 0 ) { octal [ i ++ ] = n % divide ; n = Math . floor ( n / divide ) ; } for ( var j = i - 1 , k = 0 ; k <= j ; j -- , k ++ ) if ( octal [ j ] != octal [ k ] ) return false ; return true ; } var n = 97 ; if ( isPalindrome ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find all factorial numbers less than or equal to n | Javascript program to find all factorial numbers smaller than or equal to n . ; Compute next factorial using previous ; Driver code
function printFactorialNums ( n ) { let fact = 1 ; let x = 2 ; while ( fact <= n ) { document . write ( fact + " " ) ; fact = fact * x ; x ++ ; } } let n = 100 ; printFactorialNums ( n ) ;