codeparrot/xlcost-text-to-code huggingface dataset:
Check if a given number divides the sum of the factorials of its digits | Function that returns true if n divides the sum of the factorials of its digits ; To store factorials of digits ; To store sum of the factorials of the digits ; Store copy of the given number ; Store sum of the factorials of the digits ; If it is divisible ; Driver Code
function isPossible ( n ) { var fac = new Array ( 10 ) ; fac [ 0 ] = fac [ 1 ] = 1 ; for ( var i = 2 ; i < 10 ; i ++ ) fac [ i ] = fac [ i - 1 ] * i ; var sum = 0 ; var x = n ; while ( x != 0 ) { sum += fac [ x % 10 ] ; x = parseInt ( x / 10 ) ; } if ( sum % n == 0 ) return true ; return false ; } var n = 19 ; if ( isPossible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the longest subsequence of an array having LCM at most K | Function to find the longest subsequence having LCM less than or equal to K ; Map to store unique elements and their frequencies ; Update the frequencies ; Array to store the count of numbers whom 1 <= X <= K is a multiple of ; Check every unique element ; Find all its multiples <= K ; Store its frequency ; Obtain the number having maximum count ; Condition to check if answer doesn 't exist ; Print the answer ; Driver code
function findSubsequence ( arr , n , k ) { let M = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( M . has ( arr [ i ] ) ) { M . set ( arr [ i ] , M . get ( arr [ i ] ) + 1 ) } else { M . set ( arr [ i ] , 1 ) } } let numCount = new Array ( k + 1 ) ; for ( let i = 0 ; i <= k ; ++ i ) numCount [ i ] = 0 ; for ( let p of M ) { if ( p [ 0 ] <= k ) { for ( let i = 1 ; ; ++ i ) { if ( p [ 0 ] * i > k ) break ; numCount [ p [ 0 ] * i ] += p [ 1 ] ; } } else break ; } let lcm = 0 , length = 0 ; for ( let i = 1 ; i <= k ; ++ i ) { if ( numCount [ i ] > length ) { length = numCount [ i ] ; lcm = i ; } } if ( lcm == 0 ) document . write ( - 1 + " " ) ; else { document . write ( " " + lcm + " " + length + " " ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; ++ i ) if ( lcm % arr [ i ] == 0 ) document . write ( i + " " ) ; } } let k = 14 ; let arr = [ 2 , 3 , 4 , 5 ] ; let n = arr . length ; findSubsequence ( arr , n , k ) ;
Count number of binary strings of length N having only 0 ' s ▁ and ▁ 1' s | Javascript implementation of the approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to count the number of binary strings of length N having only 0 ' s ▁ and ▁ 1' s ; Driver code
mod = 1000000007 function power ( x , y , p ) { var res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function findCount ( N ) { var count = power ( 2 , N , mod ) ; return count ; } var N = 25 ; document . write ( findCount ( N ) ) ;
XOR of all the elements in the given range [ L , R ] | Function to return the most significant bit ; Function to return the required XOR ; Finding the MSB ; Value of the current bit to be added ; To store the final answer ; Loop for case 1 ; Edge case when both the integers lie in the same segment of continuous 1 s ; To store whether parity of count is odd ; Updating the answer if parity is odd ; Updating the number to be added ; Case 2 ; Driver code ; Final answer
function msb ( x ) { let ret = 0 ; while ( ( x >> ( ret + 1 ) ) != 0 ) ret ++ ; return ret ; } function xorRange ( l , r ) { let max_bit = msb ( r ) ; let mul = 2 ; let ans = 0 ; for ( let i = 1 ; i <= max_bit ; i ++ ) { if ( ( parseInt ( l / mul ) * mul ) == ( parseInt ( r / mul ) * mul ) ) { if ( ( ( l & ( 1 << i ) ) != 0 ) && ( r - l + 1 ) % 2 == 1 ) ans += mul ; mul *= 2 ; continue ; } let odd_c = 0 ; if ( ( ( l & ( 1 << i ) ) != 0 ) && l % 2 == 1 ) odd_c = ( odd_c ^ 1 ) ; if ( ( ( r & ( 1 << i ) ) != 0 ) && r % 2 == 0 ) odd_c = ( odd_c ^ 1 ) ; if ( odd_c ) ans += mul ; mul *= 2 ; } let zero_bit_cnt = parseInt ( ( r - l + 1 ) / 2 ) ; if ( l % 2 == 1 && r % 2 == 1 ) zero_bit_cnt ++ ; if ( zero_bit_cnt % 2 == 1 ) ans ++ ; return ans ; } let l = 1 , r = 4 ; document . write ( xorRange ( l , r ) ) ;
XOR of all the elements in the given range [ L , R ] | Function to return the required XOR ; Modulus operator are expensive on most of the computers . n & 3 will be equivalent to n % 4 n % 4 ; If n is a multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Driver code
function computeXOR ( n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; case 3 : return 0 ; } } let l = 1 , r = 4 ; document . write ( computeXOR ( r ) ^ computeXOR ( l - 1 ) ) ;
Find the number of integers from 1 to n which contains digits 0 ' s ▁ and ▁ 1' s only | Function to find the number of integers from 1 to n which contains 0 ' s ▁ and ▁ 1' s only ; If number is greater than n ; Otherwise add count this number and call two functions ; Driver code
function countNumbers ( x , n ) { if ( x > n ) return 0 ; return 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ; } let n = 120 ; document . write ( countNumbers ( 1 , n ) ) ;
Check if factorial of N is divisible by the sum of squares of first N natural numbers | Function to count number of times prime P divide factorial N ; Lengendre Formula ; Function to find count number of times all prime P divide summation ; Formula for summation of square after removing n and constant 6 ; Loop to traverse over all prime P which divide summation ; If Number itself is a Prime Number ; Driver Code
function checkfact ( N , countprime , prime ) { var countfact = 0 ; if ( prime == 2 prime == 3 ) countfact ++ ; var divide = prime ; while ( N / divide != 0 ) { countfact += N / divide ; divide = divide * divide ; } if ( countfact >= countprime ) return true ; else return false ; } function check ( N ) { var sumsquares = ( N + 1 ) * ( 2 * N + 1 ) ; var countprime = 0 ; for ( i = 2 ; i <= Math . sqrt ( sumsquares ) ; i ++ ) { var flag = 0 ; while ( sumsquares % i == 0 ) { flag = 1 ; countprime ++ ; sumsquares /= i ; } if ( flag == 1 ) { if ( ! checkfact ( N - 1 , countprime , i ) ) return false ; countprime = 0 ; } } if ( sumsquares != 1 ) if ( ! checkfact ( N - 1 , 1 , sumsquares ) ) return false ; return true ; } var N = 5 ; if ( check ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count the number of non | Javascript program to count number of non increasing subarrays ; Initialize result ; Initialize length of current non increasing subarray ; Traverse through the array ; If arr [ i + 1 ] is less than or equal to arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver code
function countNonIncreasing ( arr , n ) { var cnt = 0 ; var len = 1 ; for ( var i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] <= arr [ i ] ) len ++ ; else { cnt += parseInt ( ( ( len + 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += parseInt ( ( ( len + 1 ) * len ) / 2 ) ; return cnt ; } var arr = [ 5 , 2 , 3 , 7 , 1 , 1 ] ; var n = arr . length ; document . write ( countNonIncreasing ( arr , n ) ) ;
Minimum array elements to be changed to make Recaman 's sequence | Javascript implementation of the approach ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula ; If arr [ i - 1 ] - i is negative or already exists ; Function that returns minimum changes required ; Set to store first n Recaman numbers ; Generate and store first n Recaman numbers ; push first n Recaman numbers to set ; If current element of the array is present in the set ; Return the remaining number of elements in the set ; Driver code
function recamanGenerator ( arr , n ) { arr [ 0 ] = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { var temp = arr [ i - 1 ] - i ; var j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == temp ) temp < 0 ) { temp = arr [ i - 1 ] + i ; break ; } } arr [ i ] = temp ; } } function recamanArray ( arr , n ) { var s = [ ] ; var recaman = Array ( n ) . fill ( 0 ) ; recamanGenerator ( recaman , n ) ; for ( var i = 0 ; i < n ; i ++ ) s . push ( recaman [ i ] ) ; s . sort ( ( a , b ) => b - a ) for ( var i = 0 ; i < n ; i ++ ) { if ( s . includes ( arr [ i ] ) ) { s . splice ( s . indexOf ( arr [ i ] ) , 1 ) ; } } return s . length ; } var arr = [ 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 ] ; var n = arr . length ; document . write ( recamanArray ( arr , n ) ) ;
Check if product of array containing prime numbers is a perfect square | Function that returns true if the product of all the array elements is a perfect square ; Update the frequencies of all the array elements ; If frequency of some element in the array is odd ; Driver code
function isPerfectSquare ( arr , n ) { let umap = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { umap [ arr [ i ] ] ++ ; if ( umap . has ( arr [ i ] ) ) { umap . set ( arr [ i ] , umap . get ( arr [ i ] ) + 1 ) } else { umap . set ( arr [ i ] , 1 ) } } for ( let itr of umap ) if ( ( itr [ 1 ] ) % 2 == 1 ) return false ; return true ; } let arr = [ 2 , 2 , 7 , 7 ] ; let n = arr . length ; if ( isPerfectSquare ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the good permutation of first N natural numbers | Function to print the good permutation of first N natural numbers ; If n is odd ; Otherwise ; Driver code
function printPermutation ( n ) { if ( n % 2 != 0 ) document . write ( - 1 ) ; else for ( let i = 1 ; i <= Math . floor ( n / 2 ) ; i ++ ) document . write ( ( 2 * i ) + " " + ( ( 2 * i ) - 1 ) + " " ) ; } let n = 4 ; printPermutation ( n ) ;
Minimum number operations required to convert n to m | Set | javascript implementation of the above approach ; Function to find the minimum number of steps ; If n exceeds M ; If N reaches the target ; The minimum of both the states will be the answer ; Driver code
var MAXN = 10000000 ; function minimumSteps ( n , m , a , b ) { if ( n > m ) return MAXN ; if ( n == m ) return 0 ; return Math . min ( 1 + minimumSteps ( n * a , m , a , b ) , 1 + minimumSteps ( n * b , m , a , b ) ) ; } var n = 120 , m = 51840 ; var a = 2 , b = 3 ; document . write ( minimumSteps ( n , m , a , b ) ) ;
Minimum number of given operation required to convert n to m | Function to return the minimum operations required ; Counting all 2 s ; Counting all 3 s ; If q contained only 2 and 3 as the only prime factors then it must be 1 now ; Driver code
function minOperations ( n , m ) { if ( m % n != 0 ) return - 1 ; var minOperations = 0 ; var q = m / n ; while ( q % 2 == 0 ) { q = q / 2 ; minOperations ++ ; } while ( q % 3 == 0 ) { q = q / 3 ; minOperations ++ ; } if ( q == 1 ) return minOperations ; return - 1 ; } var n = 120 , m = 51840 ; document . write ( minOperations ( n , m ) ) ;
Sum of Fibonacci Numbers in a range | Function to return the nth Fibonacci number ; Function to return the required sum ; To store the sum ; Calculate the sum ; Driver code
function fib ( n ) { var phi = ( 1 + Math . sqrt ( 5 ) ) / 2 ; return parseInt ( Math . round ( Math . pow ( phi , n ) / Math . sqrt ( 5 ) ) ) ; } function calculateSum ( l , r ) { var sum = 0 ; for ( i = l ; i <= r ; i ++ ) sum += fib ( i ) ; return sum ; } var l = 4 , r = 8 ; document . write ( calculateSum ( l , r ) ) ;
Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone | Function to find the radius of the sphere ; height and radius cannot be negative ; radius of the sphere ; Driver code
function sphereSide ( h , r ) { if ( h < 0 && r < 0 ) return - 1 ; var R = ( ( h * r * Math . sqrt ( 2 ) ) / ( h + Math . sqrt ( 2 ) * r ) ) / 2 ; return R ; } var h = 5 , r = 6 ; document . write ( sphereSide ( h , r ) . toFixed ( 5 ) ) ;
Find the number of ways to divide number into four parts such that a = c and b = d | Function to find the number of ways to divide N into four parts such that a = c and b = d ; Driver code
function possibleways ( n ) { if ( n % 2 == 1 ) return 0 ; else if ( n % 4 == 0 ) return n / 4 - 1 ; else return n / 4 ; } let n = 20 ; document . write ( possibleways ( n ) ) ;
Count sub | Function to count sub - arrays whose product is divisible by K ; Calculate the product of the current sub - array ; If product of the current sub - array is divisible by K ; Driver code
function countSubarrays ( arr , n , K ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let product = 1 ; for ( let x = i ; x <= j ; x ++ ) product *= arr [ x ] ; if ( product % K == 0 ) count ++ ; } } return count ; } let arr = [ 6 , 2 , 8 ] ; let n = arr . length ; let K = 4 ; document . write ( countSubarrays ( arr , n , K ) ) ;
Count sub | JavaScript implementation for above approach ; Segment tree implemented as an array ; Function to build the segment tree ; Function to query product of sub - array [ l . . r ] in O ( log n ) time ; Function to count sub - arrays whose product is divisible by K ; Query segment tree to find product % k of the sub - array [ i . . j ] ; Driver code ; Build the segment tree
let MAX = 100002 ; let tree = new Array ( 4 * MAX ) ; function build ( node , start , end , arr , k ) { if ( start == end ) { tree [ node ] = ( 1 * arr [ start ] ) % k ; return ; } let mid = ( start + end ) >> 1 ; build ( 2 * node , start , mid , arr , k ) ; build ( 2 * node + 1 , mid + 1 , end , arr , k ) ; tree [ node ] = ( tree [ 2 * node ] * tree [ 2 * node + 1 ] ) % k ; } function query ( node , start , end , l , r , k ) { if ( start > end start > r end < l ) { return 1 ; } if ( start >= l && end <= r ) { return tree [ node ] % k ; } let mid = ( start + end ) >> 1 ; let q1 = query ( 2 * node , start , mid , l , r , k ) ; let q2 = query ( 2 * node + 1 , mid + 1 , end , l , r , k ) ; return ( q1 * q2 ) % k ; } function countSubarrays ( arr , n , k ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let product_mod_k = query ( 1 , 0 , n - 1 , i , j , k ) ; if ( product_mod_k == 0 ) { count ++ ; } } } return count ; } let arr = [ 6 , 2 , 8 ] ; let n = arr . length ; let k = 4 ; build ( 1 , 0 , n - 1 , arr , k ) ; document . write ( countSubarrays ( arr , n , k ) ) ;
Find a pair from the given array with maximum nCr value | Function to print the pair that gives maximum nCr ; This gives the value of N in nCr ; Case 1 : When N is odd ; Case 2 : When N is even ; Driver code
function printMaxValPair ( v , n ) { v . sort ( ( a , b ) => a - b ) var N = v [ n - 1 ] ; if ( N % 2 == 1 ) { var first_maxima = N / 2 ; var second_maxima = first_maxima + 1 ; var ans1 = 3000000000000000000 , ans2 = 3000000000000000000 ; var from_left = - 1 , from_right = - 1 ; var from = - 1 ; for ( var i = 0 ; i < n ; ++ i ) { if ( v [ i ] > first_maxima ) { from = i ; break ; } else { var diff = first_maxima - v [ i ] ; if ( diff < ans1 ) { ans1 = diff ; from_left = v [ i ] ; } } } from_right = v [ from ] ; var diff1 = first_maxima - from_left ; var diff2 = from_right - second_maxima ; if ( diff1 < diff2 ) document . write ( N + " " + from_left ) ; else document . write ( N + " " + from_right ) ; } else { var maxima = parseInt ( N / 2 ) ; var ans1 = 3000000000000000000 ; var R = - 1 ; for ( var i = 0 ; i < n - 1 ; ++ i ) { var diff = Math . abs ( v [ i ] - maxima ) ; if ( diff < ans1 ) { ans1 = diff ; R = v [ i ] ; } } document . write ( N + " " + R ) ; } } var v = [ 1 , 1 , 2 , 3 , 6 , 1 ] ; var n = v . length ; printMaxValPair ( v , n ) ;
Find the number of good permutations | Function to return the count of good permutations ; For m = 0 , ans is 1 ; If k is greater than 1 ; If k is greater than 2 ; If k is greater than 3 ; Driver Code
function Permutations ( n , k ) { var ans = 1 ; if ( k >= 2 ) ans += ( n ) * ( n - 1 ) / 2 ; if ( k >= 3 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * 2 / 6 ; if ( k >= 4 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) * 9 / 24 ; return ans ; } var n = 5 , k = 2 ; document . write ( Permutations ( n , k ) ) ;
Count integers in a range which are divisible by their euler totient value | Function to return a ^ n ; Function to return count of integers that satisfy n % phi ( n ) = 0 ; Driver Code
function power ( a , n ) { if ( n == 0 ) return 1 ; var p = power ( a , parseInt ( n / 2 ) ) ; p = p * p ; if ( n % 2 == 1 ) p = p * a ; return p ; } function countIntegers ( l , r ) { var ans = 0 , i = 1 ; var v = power ( 2 , i ) ; while ( v <= r ) { while ( v <= r ) { if ( v >= l ) ans ++ ; v = v * 3 ; } i ++ ; v = power ( 2 , i ) ; } if ( l == 1 ) ans ++ ; return parseInt ( ans ) ; } var l = 12 , r = 21 ; document . write ( countIntegers ( l , r ) ) ;
Number of pairs from the first N natural numbers whose sum is divisible by K | Function to find the number of pairs from the set of natural numbers up to N whose sum is divisible by K ; Declaring a Hash to store count ; Storing the count of integers with a specific remainder in Hash array ; Check if K is even ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Count of pairs when both the remainders are K / 2 ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Driver code ; Print the count of pairs
function findPairCount ( N , K ) { var count = 0 ; var rem = Array . from ( { length : K } , ( _ , i ) => 0 ) ; rem [ 0 ] = parseInt ( N / K ) ; for ( i = 1 ; i < K ; i ++ ) rem [ i ] = parseInt ( ( N - i ) / K + 1 ) ; if ( K % 2 == 0 ) { count += parseInt ( ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ) ; for ( i = 1 ; i < K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; count += ( rem [ K / 2 ] * ( rem [ K / 2 ] - 1 ) ) / 2 ; } else { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( i = 1 ; i <= K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; } return count ; } var N = 10 , K = 4 ; document . write ( findPairCount ( N , K ) ) ;
Find the sum of all Truncatable primes below N | Javascript implementation of the approach ; To check if a number is prime or not ; Sieve of Eratosthenes function to find all prime numbers ; Function to return the sum of all truncatable primes below n ; To store the required sum ; Check every number below n ; Check from right to left ; If number is not prime at any stage ; Check from left to right ; If number is not prime at any stage ; If flag is still true ; Return the required sum ; Driver code
let N = 1000005 ; let prime = new Array ( N ) ; function sieve ( ) { for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 1 ] = false ; prime [ 0 ] = false ; for ( let i = 2 ; i < N ; i ++ ) { if ( prime [ i ] ) { for ( let j = i * 2 ; j < N ; j += i ) { prime [ j ] = false ; } } } } function sumTruncatablePrimes ( n ) { let sum = 0 ; for ( let i = 2 ; i < n ; i ++ ) { let num = i ; let flag = true ; while ( num > 0 ) { if ( ! prime [ num ] ) { flag = false ; break ; } num = Math . floor ( num / 10 ) ; } num = i ; let power = 10 ; while ( num / power > 0 ) { if ( ! prime [ num % power ] ) { flag = false ; break ; } power *= 10 ; } if ( flag ) { sum += i ; } } return sum ; } let n = 25 ; sieve ( ) ; document . write ( sumTruncatablePrimes ( n ) ) ;
Smallest and Largest N | Function to print the largest and the smallest n - digit perfect squares ; Smallest n - digit perfect square ; Largest n - digit perfect square ; Driver code
function nDigitPerfectSquares ( n ) { document . write ( Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n - 1 ) ) ) , 2 ) + " " ) ; document . write ( Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n ) ) ) - 1 , 2 ) ) ; } var n = 4 ; nDigitPerfectSquares ( n ) ;
Maximum trace possible for any sub | Javascript program for the above approach ; Function to return the maximum trace possible for a sub - matrix of the given matrix ; Calculate the trace for each of the sub - matrix with top left corner at cell ( r , s ) ; Update the maximum trace ; Return the maximum trace ; Driver code
var N = 3 ; function MaxTraceSub ( mat ) { var max_trace = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { var r = i , s = j , trace = 0 ; while ( r < N && s < N ) { trace += mat [ r ] [ s ] ; r ++ ; s ++ ; max_trace = Math . max ( trace , max_trace ) ; } } } return max_trace ; } var mat = [ [ 10 , 2 , 5 ] , [ 6 , 10 , 4 ] , [ 2 , 7 , - 10 ] ] ; document . write ( MaxTraceSub ( mat ) ) ;
Check if matrix can be converted to another matrix by transposing square sub | JavaScript implementation of the approach ; Function that returns true if matrix1 can be converted to matrix2 with the given operation ; Traverse all the diagonals starting at first column ; Traverse in diagonal ; Store the diagonal elements ; Move up ; Sort the elements ; Check if they are same ; Traverse all the diagonals starting at last row ; Traverse in the diagonal ; Store diagonal elements ; Sort all elements ; Check for same ; If every element matches ; Driver code
var n = 3 var m = 3 function check ( a , b ) { for ( var i = 0 ; i < n ; i ++ ) { var v1 = [ ] , v2 = [ ] ; var r = i ; var col = 0 ; while ( r >= 0 && col < m ) { v1 . push ( a [ r ] [ col ] ) ; v2 . push ( b [ r ] [ col ] ) ; r -- ; col ++ ; } v1 . sort ( ) ; v2 . sort ( ) ; for ( var i = 0 ; i < v1 . length ; i ++ ) { if ( v1 [ i ] != v2 [ i ] ) return false ; } } for ( var j = 1 ; j < m ; j ++ ) { var v1 = [ ] , v2 = [ ] ; var r = n - 1 ; var col = j ; while ( r >= 0 && col < m ) { v1 . push ( a [ r ] [ col ] ) ; v2 . push ( b [ r ] [ col ] ) ; r -- ; col ++ ; } v1 . sort ( ) ; v2 . sort ( ) ; for ( var i = 0 ; i < v1 . length ; i ++ ) { if ( v1 [ i ] != v2 [ i ] ) return false ; } } return true ; } var a = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; var b = [ [ 1 , 4 , 7 ] , [ 2 , 5 , 6 ] , [ 3 , 8 , 9 ] ] ; if ( check ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;
Last digit of Product of two Large or Small numbers ( a * b ) | Fthe unction to print last digit of product a * b ; Driver code
function lastDigit ( a , b ) { var lastDig = ( a [ a . length - 1 ] - ' ' ) * ( b [ b . length - 1 ] - ' ' ) ; document . write ( lastDig % 10 ) ; } var a = " " , b = " " ; lastDigit ( a , b ) ;
Smallest and Largest Palindrome with N Digits | Function to print the smallest and largest palindrome with N digits ; Driver Code
function printPalindrome ( n ) { if ( n == 1 ) { document . write ( " " ) ; document . write ( " " ) ; } else { document . write ( " " + ( parseInt ( Math . pow ( 10 , n - 1 ) ) + 1 ) ) ; document . write ( " " + parseInt ( Math . pow ( 10 , n ) - 1 ) ) ; } } var n = 4 ; printPalindrome ( n ) ;
Addition of two numbers without propagating Carry | Function to print sum of 2 numbers without propagating carry ; Reverse a ; Reverse b ; Generate sum Since length of both a and b are same , take any one of them . ; Extract digits from a and b and add ; If sum is single digit ; If sum is not single digit reverse sum ; Extract digits from sum and append to result ; Driver code
function printSum ( a , b ) { var res = 0 ; var temp1 = 0 , temp2 = 0 ; while ( a ) { temp1 = temp1 * 10 + ( a % 10 ) ; a = parseInt ( a / 10 ) ; } a = temp1 ; while ( b ) { temp2 = temp2 * 10 + ( b % 10 ) ; b = parseInt ( b / 10 ) ; } b = temp2 ; while ( a ) { var sum = ( a % 10 + b % 10 ) ; if ( parseInt ( sum / 10 ) == 0 ) res = res * 10 + sum ; else { temp1 = 0 ; while ( sum ) { temp1 = temp1 * 10 + ( sum % 10 ) ; sum = parseInt ( sum / 10 ) ; } sum = temp1 ; while ( sum ) { res = res * 10 + ( sum % 10 ) ; sum = parseInt ( sum / 10 ) ; } } a = parseInt ( a / 10 ) ; b = parseInt ( b / 10 ) ; } return res ; } var a = 7752 , b = 8834 ; document . write ( printSum ( a , b ) ) ;
Number of digits before the decimal point in the division of two numbers | Function to return the number of digits before the decimal in a / b ; Absolute value of a / b ; If result is 0 ; Count number of digits in the result ; Return the required count of digits ; Driver code
function countDigits ( a , b ) { var count = 0 ; var p = Math . abs ( parseInt ( a / b ) ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = parseInt ( p / 10 ) ; } return count ; } var a = 100 ; var b = 10 ; document . write ( countDigits ( a , b ) ) ;