codeparrot/xlcost-text-to-code huggingface dataset:
First N natural can be divided into two sets with given difference and co | Javascript code to determine whether numbers 1 to N can be divided into two sets such that absolute difference between sum of these two sets is M and these two sum are co - prime ; function that returns boolean value on the basis of whether it is possible to divide 1 to N numbers into two sets that satisfy given conditions . ; initializing total sum of 1 to n numbers ; since ( 1 ) total_sum = sum_s1 + sum_s2 and ( 2 ) m = sum_s1 - sum_s2 assuming sum_s1 > sum_s2 . solving these 2 equations to get sum_s1 and sum_s2 ; total_sum = sum_s1 + sum_s2 and therefore ; if total sum is less than the absolute difference then there is no way we can split n numbers into two sets so return false ; check if these two sums are integers and they add up to total sum and also if their absolute difference is m . ; Now if two sum are co - prime then return true , else return false . ; if two sums don 't add up to total  sum or if their absolute difference  is not m, then there is no way to  split n numbers, hence return false ; Driver code ; function call to determine answer
function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function isSplittable ( n , m ) { let total_sum = parseInt ( ( n * ( n + 1 ) ) / 2 ) ; let sum_s1 = parseInt ( ( total_sum + m ) / 2 ) ; let sum_s2 = total_sum - sum_s1 ; if ( total_sum < m ) return false ; if ( sum_s1 + sum_s2 == total_sum && sum_s1 - sum_s2 == m ) return ( __gcd ( sum_s1 , sum_s2 ) == 1 ) ; return false ; } let n = 5 ; let m = 7 ; if ( isSplittable ( n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Making zero array by decrementing pairs of adjacent | Javascript implementation of the above approach ; Converting array element into number ; Check if divisible by 11 ; Driver code
function isPossibleToZero ( a , n ) { let num = 0 ; for ( let i = 0 ; i < n ; i ++ ) num = num * 10 + a [ i ] ; return ( num % 11 == 0 ) ; } let arr = [ 0 , 1 , 1 , 0 ] ; let n = arr . length ; if ( isPossibleToZero ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Blum Integer | Function to cheek if number is Blum Integer ; To store prime numbers from 2 to n ; If prime [ i ] is not changed , then it is a prime ; Update all multiples of p ; To check if the given odd integer is Blum Integer or not ; Checking the factors are of 4 t + 3 form or not ; Give odd integer greater than 20
function isBlumInteger ( n ) { let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i < n ; i ++ ) prime [ i ] = true ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( let j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } for ( let i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] ) { if ( ( n % i == 0 ) && ( ( i - 3 ) % 4 ) == 0 ) { let q = parseInt ( n / i , 10 ) ; return ( q != i && prime [ q ] && ( q - 3 ) % 4 == 0 ) ; } } } return false ; } let n = 249 ; if ( isBlumInteger ( n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Program to calculate value of nCr | Javascript program To calculate The Value Of nCr ; Returns factorial of n ; Driver code
function nCr ( n , r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } var n = 5 , r = 3 ; document . write ( nCr ( n , r ) ) ;
Program to print the sum of the given nth term | function to calculate sum of series ; Sum of n terms is n ^ 2 ; Driver Code
function summingSeries ( n ) { return Math . pow ( n , 2 ) ; } let n = 100 ; document . write ( " " + summingSeries ( n ) + " " ) ;
Brahmagupta Fibonacci Identity | JavaScript program to verify Brahmagupta Fibonacci identity ; represent the product as sum of 2 squares ; check identity criteria ; 1 ^ 2 + 2 ^ 2 ; 3 ^ 2 + 4 ^ 2 ; express product of sum of 2 squares as sum of ( sum of 2 squares )
function find_sum_of_two_squares ( a , b ) { let ab = a * b ; for ( let i = 0 ; i * i <= ab ; i ++ ) { for ( let j = i ; i * i + j * j <= ab ; j ++ ) { if ( i * i + j * j == ab ) document . write ( i + " " + j + " " + ab + " " ) ; } } } let a = 1 * 1 + 2 * 2 ; let b = 3 * 3 + 4 * 4 ; document . write ( " " + " " + " " ) ; find_sum_of_two_squares ( a , b ) ;
Tetrahedral Numbers | Function to find Tetrahedral Number ; Driver code
function tetrahedralNumber ( n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } let n = 5 ; document . write ( tetrahedralNumber ( n ) ) ;
Euler 's Four Square Identity | function to check euler four square identity ; loops checking the sum of squares ; sum of 2 squares ; sum of 3 squares ; sum of 4 squares ; product of 2 numbers represented as sum of four squares i , j , k , l ; product of 2 numbers a and b represented as sum of four squares i , j , k , l ; given numbers can be represented as sum of 4 squares By euler 's  four square identity  product also can be  represented as sum  of 4 squares
function check_euler_four_square_identity ( a , b , ab ) { let s = 0 ; for ( let i = 0 ; i * i <= ab ; i ++ ) { s = i * i ; for ( let j = i ; j * j <= ab ; j ++ ) { s = j * j + i * i ; for ( let k = j ; k * k <= ab ; k ++ ) { s = k * k + j * j + i * i ; for ( let l = k ; l * l <= ab ; l ++ ) { s = l * l + k * k + j * j + i * i ; if ( s == ab ) { document . write ( " " + i + " " ) ; document . write ( " " + j + " " ) ; document . write ( " " + k + " " ) ; document . write ( " " + l + " " ) ; document . write ( " " + a + " " + b ) ; document . write ( " " + " " + " " + " " ) ; document . write ( ab + " " ) ; document . write ( i + " " + i + " " ) ; document . write ( j + " " + j + " " ) ; document . write ( k + " " + k + " " ) ; document . write ( l + " " + l + " " ) ; document . write ( " " ) ; } } } } } } let ab = a * b ; check_euler_four_square_identity ( a , b , ab ) ;
Number of solutions to Modular Equations | Returns the number of divisors of ( A - B ) greater than B ; If N is divisible by i ; Count only the divisors greater than B ; Checking if a divisor isnt counted twice ; Utility function to calculate number of all possible values of X for which the modular equation holds true ; If A = B there are infinitely many solutions to equation or we say X can take infinitely many values > A . We return - 1 in this case ; If A < B , there are no possible values of X satisfying the equation ; The last case is when A > B , here we calculate the number of divisors of ( A - B ) , which are greater than B ; Wrapper function for numberOfPossibleWaysUtil ( ) ; If infinitely many solutions available ; Driver code
function calculateDivisors ( A , B ) { let N = ( A - B ) ; let noOfDivisors = 0 ; for ( let i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( ( N % i ) == 0 ) { if ( i > B ) noOfDivisors ++ ; if ( ( N / i ) != i && ( N / i ) > B ) noOfDivisors ++ ; } } return noOfDivisors ; } function numberOfPossibleWaysUtil ( A , B ) { if ( A == B ) return - 1 ; if ( A < B ) return 0 ; let noOfDivisors = 0 ; noOfDivisors = calculateDivisors ( A , B ) ; return noOfDivisors ; } function numberOfPossibleWays ( A , B ) { let noOfSolutions = numberOfPossibleWaysUtil ( A , B ) ; if ( noOfSolutions == - 1 ) { document . write ( " " + A + " " + B + " " + " " + " " + A + " " ) ; } else { document . write ( " " + A + " " + B + " " + noOfSolutions + " " + " " ) ; } } let A = 26 , B = 2 ; numberOfPossibleWays ( A , B ) ; A = 21 , B = 5 ; numberOfPossibleWays ( A , B ) ;
Perfect power ( 1 , 4 , 8 , 9 , 16 , 25 , 27 , ... ) | Function that keeps all the odd power numbers upto n ; We need exclude perfect squares . ; Return sum of odd and even powers . ; Driver Code
function powerNumbers ( n ) { let v = new Set ( ) ; for ( let i = 2 ; i * i * i <= n ; i ++ ) { let j = i * i ; while ( j * i <= n ) { j *= i ; let s = parseInt ( Math . sqrt ( j ) , 10 ) ; if ( s * s != j ) v . add ( j ) ; } } return v . size + parseInt ( Math . sqrt ( n ) , 10 ) ; } document . write ( powerNumbers ( 50 ) ) ;
Variance and standard | Function for calculating mean ; Calculating sum ; Returning mean ; Function for calculating variance ; subtracting mean from elements ; a [ i ] [ j ] = fabs ( a [ i ] [ j ] ) ; squaring each terms ; taking sum ; declaring and initializing matrix ; for mean ; for variance ; for standard deviation ; displaying variance and deviation
function mean ( a , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) sum += a [ i ] [ j ] ; return sum / ( n * n ) ; } function variance ( a , n , m ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { a [ i ] [ j ] -= m ; a [ i ] [ j ] *= a [ i ] [ j ] ; } } for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) sum += a [ i ] [ j ] ; return sum / ( n * n ) ; } let mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; let m = mean ( mat , 3 ) ; let varr = variance ( mat , 3 , m ) ; let dev = Math . sqrt ( varr ) ; document . write ( " " + Math . floor ( m ) + " " ) ; document . write ( " " + Math . floor ( varr ) + " " ) ; document . write ( " " + Math . floor ( dev ) + " " ) ;
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | function to calculate sum of series ; Driver Code
function sumofseries ( n ) { return ( n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 4 ) ; } document . write ( sumofseries ( 3 ) ) ;
Largest number in [ 2 , 3 , . . n ] which is co | Returns true if i is co - prime with numbers in set [ 2 , 3 , ... m ] ; Running the loop till square root of n to reduce the time complexity from n ; Find the minimum of square root of n and m to run the loop until the smaller one ; Check from 2 to min ( m , sqrt ( n ) ) ; Function to find the largest number less than n which is Co - prime with all numbers from 2 to m ; Iterating from n to m + 1 to find the number ; checking every number for the given conditions ; The first number which satisfy the conditions is the answer ; If there is no number which satisfy the conditions , then print number does not exist . ; Driver Code
function isValid ( i , m ) { let sq_i = Math . sqrt ( i ) ; let sq = Math . min ( m , sq_i ) ; for ( let j = 2 ; j <= sq ; j ++ ) if ( i % j == 0 ) return false ; return true ; } function findLargestNum ( n , m ) { for ( let i = n ; i > m ; i -- ) { if ( isValid ( i , m ) ) { document . write ( i ) ; return ; } } document . write ( " " ) ; } let n = 16 , m = 3 ; findLargestNum ( n , m ) ;
Check whether a given matrix is orthogonal or not | Function to check orthogonalilty ; Find transpose ; Find product of a [ ] [ ] and its transpose ; Since we are multiplying transpose of itself . We use ; Check if product is identity matrix ; Driver code
function isOrthogonal ( a , m , n ) { if ( m != n ) return false ; let trans = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { trans [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) trans [ i ] [ j ] = a [ j ] [ i ] ; } let prod = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { prod [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { let sum = 0 ; for ( let k = 0 ; k < n ; k ++ ) { sum = sum + ( a [ i ] [ k ] * a [ j ] [ k ] ) ; } prod [ i ] [ j ] = sum ; } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( i != j && prod [ i ] [ j ] != 0 ) return false ; if ( i == j && prod [ i ] [ j ] != 1 ) return false ; } } return true ; } let a = [ [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 1 ] ] ; if ( isOrthogonal ( a , 3 , 3 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if given number is perfect square | JavaScript program to find if x is a perfect square . ; Find floating point value of square root of x . ; if product of square root is equal , then return T / F ; Driver code
function isPerfectSquare ( x ) { if ( x >= 0 ) { let sr = Math . sqrt ( x ) ; return ( ( sr * sr ) == x ) ; } return false ; } let x = 2500 ; if ( isPerfectSquare ( x ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to print GP ( Geometric Progression ) | function to print GP ; starting number ; Common ratio ; N th term to be find
function printGP ( a , r , n ) { let curr_term ; for ( let i = 0 ; i < n ; i ++ ) { curr_term = a * Math . pow ( r , i ) ; document . write ( curr_term + " " ) ; } } let a = 2 ; let r = 3 ; let n = 5 ; printGP ( a , r , n ) ;
HCF of array of fractions ( or rational numbers ) | hcf of two number ; find hcf of numerator series ; return hcf of numerator ; find lcm of denominator series ; ans contains LCM of arr [ 0 ] [ 1 ] , . . arr [ i ] [ 1 ] ; return lcm of denominator ; Core Function ; found hcf of numerator ; found lcm of denominator ; return result ; Driver code ; Initialize the every row with size 2 ( 1 for numeratorand 2 for denominator ) ; function for calculate the result ; print the result
function gcd ( a , b ) { if ( a % b == 0 ) return b ; else return ( gcd ( b , a % b ) ) ; } function findHcf ( arr , size ) { let ans = arr [ 0 ] [ 0 ] ; for ( let i = 1 ; i < size ; i ++ ) ans = gcd ( ans , arr [ i ] [ 0 ] ) ; return ( ans ) ; } function findLcm ( arr , size ) { let ans = arr [ 0 ] [ 1 ] ; for ( let i = 1 ; i < size ; i ++ ) ans = Math . floor ( ( ( arr [ i ] [ 1 ] * ans ) ) / ( gcd ( arr [ i ] [ 1 ] , ans ) ) ) ; return ( ans ) ; } function hcfOfFraction ( arr , size ) { let hcf_of_num = findHcf ( arr , size ) ; let lcm_of_deno = findLcm ( arr , size ) ; let result = new Array ( 2 ) ; result [ 0 ] = hcf_of_num ; result [ 1 ] = lcm_of_deno ; for ( let i = result [ 0 ] / 2 ; i > 1 ; i -- ) { if ( ( result [ 1 ] % i == 0 ) && ( result [ 0 ] % i == 0 ) ) { result [ 1 ] /= i ; result [ 0 ] /= i ; } } return ( result ) ; } let size = 4 ; let arr = new Array ( size ) ; for ( let i = 0 ; i < size ; i ++ ) arr [ i ] = new Array ( 2 ) ; arr [ 0 ] [ 0 ] = 9 ; arr [ 0 ] [ 1 ] = 10 ; arr [ 1 ] [ 0 ] = 12 ; arr [ 1 ] [ 1 ] = 25 ; arr [ 2 ] [ 0 ] = 18 ; arr [ 2 ] [ 1 ] = 35 ; arr [ 3 ] [ 0 ] = 21 ; arr [ 3 ] [ 1 ] = 40 ; let result = hcfOfFraction ( arr , size ) ; document . write ( result [ 0 ] + " " + result [ 1 ] + " " ) ;
Space efficient iterative method to Fibonacci number | Get second MSB ; Consectutively set all the bits ; Returns the second MSB ; Multiply function ; Function to calculate F [ ] [ ] raise to the power n ; Base case ; Take 2D array to store number 's ; Run loop till MSB > 0 ; To return fibonacci number ; Given n
function getMSB ( n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( ( n + 1 ) >> 2 ) ; } function multiply ( F , M ) { let x = F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ; let y = F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ; let z = F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ; let w = F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ; F [ 0 ] [ 0 ] = x ; F [ 0 ] [ 1 ] = y ; F [ 1 ] [ 0 ] = z ; F [ 1 ] [ 1 ] = w ; } function power ( F , n ) { if ( n == 0 n == 1 ) return ; let M = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; for ( let m = getMSB ( n ) ; m > 0 ; m = m >> 1 ) { multiply ( F , F ) ; if ( ( n & m ) > 0 ) { multiply ( F , M ) ; } } } function fib ( n ) { let F = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; if ( n == 0 ) return 0 ; power ( F , n - 1 ) ; return F [ 0 ] [ 0 ] ; } let n = 6 ; document . write ( fib ( n ) ) ;
Stern | Javascript program to print Brocot Sequence ; loop to create sequence ; adding sum of considered element and it 's precedent ; adding next considered element ; printing sequence . . ; Driver code ; adding first two element in the sequence
function SternSequenceFunc ( BrocotSequence , n ) { for ( var i = 1 ; BrocotSequence . length < n ; i ++ ) { var considered_element = BrocotSequence [ i ] ; var precedent = BrocotSequence [ i - 1 ] ; BrocotSequence . push ( considered_element + precedent ) ; BrocotSequence . push ( considered_element ) ; } for ( var i = 0 ; i < 15 ; ++ i ) document . write ( BrocotSequence [ i ] + " " ) ; } var n = 15 ; var BrocotSequence = [ ] ; BrocotSequence . push ( 1 ) ; BrocotSequence . push ( 1 ) ; SternSequenceFunc ( BrocotSequence , n ) ;
Counting numbers whose difference from reverse is a product of k | javascript program to Count the numbers within a given range in which when you subtract a number from its reverse , the difference is a product of k ; function to check if the number and its reverse have their absolute difference divisible by k ; reverse the number ; Driver code
function isRevDiffDivisible ( x , k ) { var n = x ; var m = 0 ; var flag ; while ( x > 0 ) { m = m * 10 + x % 10 ; x = parseInt ( x / 10 ) ; } return ( Math . abs ( n - m ) % k == 0 ) ; } function countNumbers ( l , r , k ) { var count = 0 ; for ( i = l ; i <= r ; i ++ ) if ( isRevDiffDivisible ( i , k ) ) count ++ ; return count ; } var l = 35 , r = 45 , k = 5 ; document . write ( countNumbers ( l , r , k ) ) ;
Check for Amicable Pair | Function to calculate sum of all proper divisors of a given number ; Sum of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add both ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Returns true if x and y are Amicable else false . ; Driver Code
function divSum ( n ) { let result = 0 ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + 1 ) ; } function areAmicable ( x , y ) { if ( divSum ( x ) != y ) return false ; return ( divSum ( y ) == x ) ; } let x = 220 ; let y = 284 ; if ( areAmicable ( x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to print non square numbers | Function to check perfect square ; function to print all non square number ; variable which stores the count ; not perfect square ; Driver code
function isPerfectSquare ( n ) { if ( n < 0 ) return false ; let root = Math . round ( ( Math . sqrt ( n ) ) ) ; return n == root * root ; } function printnonsquare ( n ) { let count = 0 ; for ( let i = 1 ; count < n ; ++ i ) { if ( ! isPerfectSquare ( i ) ) { document . write ( i + " " ) ; count ++ ; } } } let n = 10 ; printnonsquare ( n ) ;
Program to print non square numbers | Returns n - th non - square number . ; loop to print non squares below n ; Driver Code
function nonsquare ( n ) { return n + parseInt ( 0.5 + Math . sqrt ( n ) ) ; } function printNonSquare ( n ) { for ( let i = 1 ; i <= n ; i ++ ) document . write ( nonsquare ( i ) + " " ) ; } let n = 10 ; printNonSquare ( n ) ;
Ludic Numbers | Returns a list containing all Ludic numbers smaller than or equal to n . ; ludics list contain all the ludic numbers ; Here we have to start with index 1 and will remove nothing from the list ; Here first item should be included in the list and the deletion is referred by this first item in the loop . ; remove_index variable is used to store the next index which we want to delete ; removing the next item ; remove_index is updated so that we get the next index for deletion ; Driver code
function getLudic ( n ) { let ludics = [ ] ; for ( let i = 1 ; i <= n ; i ++ ) ludics . push ( i ) ; for ( let index = 1 ; index < ludics . length ; index ++ ) { let first_ludic = ludics [ index ] ; let remove_index = index + first_ludic ; while ( remove_index < ludics . length ) { ludics . splice ( remove_index , 1 ) ; remove_index = remove_index + first_ludic - 1 ; } } return ludics ; } let n = 25 ; document . write ( getLudic ( n ) ) ;
Program to compare two fractions | Get max of the two fractions ; Declare nume1 and nume2 for get the value of first numerator and second numerator ; Compute ad - bc ; Driver Code
function maxFraction ( first , sec ) { a = first [ 0 ] ; b = first [ 1 ] c = sec [ 0 ] ; d = sec [ 1 ] Y = a * d - b * c return ( Y > 0 ) ? first : sec ; } first = [ 3 , 2 ] ; sec = [ 3 , 4 ] ; res = maxFraction ( first , sec ) ; document . write ( res [ 0 ] + " " + res [ 1 ] ) ;
Find the nearest odd and even perfect squares of odd and even array elements respectively | Function to find the nearest even and odd perfect squares for even and odd array elements ; Traverse the array ; Calculate square root of current array element ; If both are of same parity ; Otherwise ; Driver code Given array
function nearestPerfectSquare ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let sr = Math . floor ( Math . sqrt ( arr [ i ] ) ) ; if ( ( sr & 1 ) == ( arr [ i ] & 1 ) ) document . write ( ( sr * sr ) + " " ) ; else { sr ++ ; document . write ( ( sr * sr ) + " " ) ; } } } let arr = [ 6 , 3 , 2 , 15 ] ; let N = arr . length ; nearestPerfectSquare ( arr , N ) ;
Program to check if N is a Pentagonal Number | Function to determine if N is pentagonal or not . ; Substitute values of i in the formula . ; Driver code
function isPentagonal ( N ) { var i = 1 , M ; do { M = ( 3 * i * i - i ) / 2 ; i += 1 ; } while ( M < N ) ; return ( M == N ) ; } var N = 12 ; if ( isPentagonal ( N ) ) document . write ( N + " " ) ; else document . write ( N + " " ) ;
Sum of fourth powers of the first n natural numbers | Return the sum of forth power of first n natural numbers ; Driven Program
function fourthPowerSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum = sum + ( i * i * i * i ) ; return sum ; } let n = 6 ; document . write ( fourthPowerSum ( n ) ) ;
Sum of fourth powers of the first n natural numbers | Return the sum of forth power of first n natural numbers ; Driver Code
function fourthPowerSum ( n ) { return ( ( 6 * n * n * n * n * n ) + ( 15 * n * n * n * n ) + ( 10 * n * n * n ) - n ) / 30 ; } var n = 6 ; document . write ( fourthPowerSum ( n ) ) ;
Find unit digit of x raised to power y | find unit digit ; Get last digit of x ; Last cyclic modular value ; here we simply return the unit digit or the power of a number ; Driver code ; get unit digit number here we pass the unit digit of x and the last cyclicity number that is y % 4
function unitnumber ( x , y ) { x = x % 10 ; if ( y != 0 ) y = y % 4 + 4 ; return ( ( parseInt ( Math . pow ( x , y ) ) ) % 10 ) ; } let x = 133 ; let y = 5 ; document . write ( unitnumber ( x , y ) ) ;
Aliquot sum | Function to calculate sum of all proper divisors ; Driver code
function aliquotSum ( n ) { let sum = 0 ; for ( let i = 1 ; i < n ; i ++ ) if ( n % i == 0 ) sum += i ; return sum ; } let n = 12 ; document . write ( aliquotSum ( n ) ) ;
Multiplication of two complex numbers given as strings | javascript program to multiply two complex numbers given as strings . ; Spiting the real and imaginary parts of the given complex strings based on ' + ' and ' i ' symbols . ; Storing the real part of complex string a ; Storing the imaginary part of complex string a ; Storing the real part of complex string b ; Storing the imaginary part of complex string b ; Returns the product . ; Driver function
function complexNumberMultiply ( a , b ) { var x = a . split ( ' ' ) ; var y = b . split ( ' ' ) ; var a_real = parseInt ( x [ 0 ] ) ; var a_img = parseInt ( x [ 1 ] ) ; var b_real = parseInt ( y [ 0 ] ) ; var b_img = parseInt ( y [ 1 ] ) ; return ( a_real * b_real - a_img * b_img ) + " " + ( a_real * b_img + a_img * b_real ) + " " ; } var str1 = " " ; var str2 = " " ; document . write ( complexNumberMultiply ( str1 , str2 ) ) ;
Average of Squares of Natural Numbers | Function to get the average ; Driver Code
function AvgofSquareN ( n ) { return ( ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; } var n = 2 ; document . write ( AvgofSquareN ( n ) ) ;
Program to implement Simpson 's 3/8 rule | Given function to be integrated ; Function to perform calculations ; Calculates value till integral limit ; Driver Function
function func ( x ) { return ( 1 / ( 1 + x * x ) ) ; } function calculate ( lower_limit , upper_limit , interval_limit ) { let value ; let interval_size = ( upper_limit - lower_limit ) / interval_limit ; let sum = func ( lower_limit ) + func ( upper_limit ) ; for ( let i = 1 ; i < interval_limit ; i ++ ) { if ( i % 3 == 0 ) sum = sum + 2 * func ( lower_limit + i * interval_size ) ; else sum = sum + 3 * func ( lower_limit + i * interval_size ) ; } return ( 3 * interval_size / 8 ) * sum ; } let interval_limit = 10 ; let lower_limit = 1 ; let upper_limit = 10 ; let integral_res = calculate ( lower_limit , upper_limit , interval_limit ) ; document . write ( integral_res ) ;