codeparrot/xlcost-text-to-code huggingface dataset:
Median | Function for calculating median ; First we sort the array ; check for even case ; Driver program
function findMedian ( a , n ) { a . sort ( ) ; if ( n % 2 != 0 ) return a [ parseInt ( n / 2 ) ] ; return ( a [ parseInt ( ( n - 1 ) / 2 ) ] + a [ parseInt ( n / 2 ) ] ) / 2.0 ; } let a = [ 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ] ; let n = a . length ; document . write ( " " + findMedian ( a , n ) ) ;
Check if the array has an element which is equal to product of remaining elements | Function to Check if the array has an element which is equal to product of all the remaining elements ; Storing frequency in map ; Calculate the product of all the elements ; If the prod is a perfect square then check if its square root ; exist in the array or not ; Driver code
function CheckArray ( arr , n ) { let prod = 1 ; let freq = [ ] ; for ( let i = 0 ; i < n ; ++ i ) { freq . push ( arr [ i ] ) ; prod *= arr [ i ] ; } let root = Math . floor ( Math . sqrt ( prod ) ) ; if ( root * root == prod ) { if ( freq . includes ( root ) & freq . lastIndexOf ( root ) != ( freq . length ) ) { return true ; } } return false ; } let arr = [ 1 , 2 , 12 , 3 , 2 ] ; let n = arr . length ; if ( CheckArray ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find minimum area of rectangle with given set of coordinates | # function to find minimum area of Rectangle ; creating empty columns ; fill columns with coordinates ; Driver code
function minAreaRect ( points ) { let columns = new Set ( ) ; for ( let point = 0 ; point < points . length ; point ++ ) columns . add ( 40001 * points [ point ] [ 0 ] + points [ point ] [ 1 ] ) ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < points . length ; ++ i ) for ( let j = i + 1 ; j < points . length ; ++ j ) { if ( points [ i ] [ 0 ] != points [ j ] [ 0 ] && points [ i ] [ 1 ] != points [ j ] [ 1 ] ) { if ( columns . has ( 40001 * points [ i ] [ 0 ] + points [ j ] [ 1 ] ) && columns . has ( 40001 * points [ j ] [ 0 ] + points [ i ] [ 1 ] ) ) { ans = Math . min ( ans , Math . abs ( points [ j ] [ 0 ] - points [ i ] [ 0 ] ) * Math . abs ( points [ j ] [ 1 ] - points [ i ] [ 1 ] ) ) ; } } } return ans < Number . MAX_VALUE ? ans : 0 ; } let A = [ [ 1 , 1 ] , [ 1 , 3 ] , [ 3 , 1 ] , [ 3 , 3 ] , [ 2 , 2 ] ] ; document . write ( minAreaRect ( A ) ) ;
Check whether a number has consecutive 0 's in the given base or not | Function to convert N into base K ; Weight of each digit ; Function to check for consecutive 0 ; Flag to check if there are consecutive zero or not ; If there are two consecutive zero then returning False ; We first convert to given base , then check if the converted number has two consecutive 0 s or not ; Driver code
function toK ( N , K ) { let w = 1 ; let s = 0 ; while ( N != 0 ) { let r = N % K ; N = parseInt ( N / K ) ; s = r * w + s ; w *= 10 ; } return s ; } function check ( N ) { let fl = false ; while ( N != 0 ) { let r = N % 10 ; N = parseInt ( N / 10 ) ; if ( fl == true && r == 0 ) return false ; if ( r > 0 ) { fl = false ; continue ; } fl = true ; } return true ; } function hasConsecutiveZeroes ( N , K ) { let z = toK ( N , K ) ; if ( check ( z ) ) document . write ( " " ) ; else document . write ( " " ) ; } let N = 15 ; let K = 8 ; hasConsecutiveZeroes ( N , K ) ;
Sum of every Kâ €™ th prime number in an array | Javascript implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; sum of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; create the sieve
let MAX = 100000 ; let prime = new Array ( MAX + 1 ) . fill ( true ) ; function SieveOfEratosthenes ( ) { prime [ 1 ] = false ; prime [ 0 ] = false ; for ( let p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function SumOfKthPrimes ( arr , n , k ) { let c = 0 ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { sum += arr [ i ] ; c = 0 ; } } } document . write ( sum + " " ) ; } SieveOfEratosthenes ( ) ; let arr = new Array ( 2 , 3 , 5 , 7 , 11 ) ; let n = arr . length ; let k = 2 ; SumOfKthPrimes ( arr , n , k ) ;
Find the super power of a given Number | Javascript for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; Driver program
var MAX = 100000 ; var prime = Array ( 100002 ) . fill ( true ) ; function SieveOfEratosthenes ( ) { for ( var p = 2 ; p * p <= MAX ; p ++ ) if ( prime [ p ] == true ) for ( var i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } function superpower ( n ) { SieveOfEratosthenes ( ) ; var superPower = 0 , factor = 0 ; var i = 2 ; while ( n > 1 && i <= MAX ) { if ( prime [ i ] ) { factor = 0 ; while ( n % i == 0 && n > 1 ) { factor ++ ; n = n / i ; } if ( superPower < factor ) superPower = factor ; } i ++ ; } return superPower ; } var n = 256 ; document . write ( superpower ( n ) ) ;
Count of Prime Nodes of a Singly Linked List | Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Function to check if a number is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find count of prime nodes in a linked list ; If current node is prime ; Update count ; Start with the empty list ; Create the linked list 15 . 5 . 6 . 10 . 17 ; Function call to print require answer
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function countPrime ( head_ref ) { var count = 0 ; var ptr = head_ref ; while ( ptr != null ) { if ( isPrime ( ptr . data ) ) { count ++ ; } ptr = ptr . next ; } return count ; } var head = null ; head = push ( head , 17 ) ; head = push ( head , 10 ) ; head = push ( head , 6 ) ; head = push ( head , 5 ) ; head = push ( head , 15 ) ; document . write ( " " + countPrime ( head ) ) ;
Smallest prime divisor of a number | Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code
function smallestDivisor ( n ) { if ( n % 2 == 0 ) return 2 ; for ( var i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) return i ; } return n ; } var n = 31 ; document . write ( smallestDivisor ( n ) ) ;
Count Number of animals in a zoo from given number of head and legs | Function that calculates Rabbits ; Driver code
function countRabbits ( Heads , Legs ) { var count = 0 ; count = ( Legs ) - 2 * ( Heads ) ; count = count / 2 ; return count ; } var Heads = 100 , Legs = 300 ; var Rabbits = countRabbits ( Heads , Legs ) ; document . write ( " " + Rabbits + " " ) ; document . write ( " " + ( Heads - Rabbits ) ) ;
Program to evaluate the expression ( ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¹ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ Ã ¢ â ‚¬¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¦¡ X + 1 ) ^ 6 + ( ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¹ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ Ã ¢ â ‚¬¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¦¡ X | Function to find the sum ; Driver Code
function calculateSum ( n ) { return 2 * ( Math . pow ( n , 6 ) + 15 * Math . pow ( n , 4 ) + 15 * Math . pow ( n , 2 ) + 1 ) ; } var n = 1.4142 ; document . write ( parseInt ( Math . ceil ( calculateSum ( n ) ) ) ) ;
Find the sum of series 3 , | calculate sum upto N term of series ; Driver code
function Sum_upto_nth_Term ( n ) { return ( 1 - parseInt ( Math . pow ( - 2 , n ) ) ) ; } let N = 5 ; document . write ( Sum_upto_nth_Term ( N ) ) ;
Count numbers whose XOR with N is equal to OR with N | Function to calculate count of numbers with XOR equals OR ; variable to store count of unset bits ; Driver code
function xorEqualsOrCount ( N ) { let count = 0 ; let bit ; while ( N > 0 ) { bit = N % 2 ; if ( bit == 0 ) count ++ ; N = parseInt ( N / 2 ) ; } return Math . pow ( 2 , count ) ; } let N = 7 ; document . write ( xorEqualsOrCount ( N ) ) ;
Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | Function to find the factorial of a number ; Function to compute the sum ; Iterate the loop till n and compute the formula ; Get x and n ; Print output
function fact ( n ) { if ( n == 1 ) return 1 ; return n * fact ( n - 1 ) ; } function sum ( x , n ) { let total = 1.0 ; for ( let i = 1 ; i <= n ; i ++ ) { total = total + ( Math . pow ( x , i ) / fact ( i + 1 ) ) ; } return total . toFixed ( 4 ) ; } let x = 5 ; let n = 4 ; document . write ( " " + sum ( x , n ) ) ;
Find Sum of Series 1 ^ 2 | Function to find sum of series ; If i is even ; If i is odd ; return the result ; Get n ; Find the sum ; Get n ; Find the sum
function sum_of_series ( n ) { var result = 0 ; for ( i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) result = result - parseInt ( Math . pow ( i , 2 ) ) ; else result = result + parseInt ( Math . pow ( i , 2 ) ) ; } return result ; } var n = 3 ; document . write ( sum_of_series ( n ) + " " ) ; n = 10 ; document . write ( sum_of_series ( n ) ) ;
Program to find the sum of the series 23 + 45 + 75 + ... . . upto N terms | calculate Nth term of series ; Get the value of N ; Get the sum of the series
function findSum ( N ) { return ( 2 * N * ( N + 1 ) * ( 4 * N + 17 ) + 54 * N ) / 6 ; } let N = 4 ; document . write ( findSum ( N ) ) ;
Program to find the value of sin ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | Javascript Program to find the value of sin ( n ? ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; find cosTheta from sinTheta ; store required answer ; use to toggle sign in sequence . ; Driver code .
MAX = 16 var nCr = Array . from ( Array ( MAX ) , ( ) => new Array ( MAX ) ) ; function binomial ( ) { for ( var i = 0 ; i < MAX ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } function findCosNTheta ( sinTheta , n ) { var cosTheta = Math . sqrt ( 1 - sinTheta * sinTheta ) ; var ans = 0 ; var toggle = 1 ; for ( var i = 1 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * Math . pow ( cosTheta , n - i ) * Math . pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans . toFixed ( 6 ) ; } binomial ( ) ; var sinTheta = 0.5 ; var n = 10 ; document . write ( findCosNTheta ( sinTheta , n ) ) ;
Program to find Nth term of series 9 , 23 , 45 , 75 , 113. . . | calculate Nth term of series ; get the value of N ; Calculate and print the Nth term
function nthTerm ( N ) { return ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ; } let N = 4 ; document . write ( nthTerm ( N ) ) ;
Program to Find the value of cos ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | Javascript program to find the value of cos ( n - theta ) ; Function to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of cos ( n - theta ) ; find sinTheta from cosTheta ; to store required answer ; use to toggle sign in sequence . ; Driver code
MAX = 16 var nCr = Array . from ( Array ( MAX ) , ( ) => new Array ( MAX ) ) ; function binomial ( ) { for ( var i = 0 ; i < MAX ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } function findCosnTheta ( cosTheta , n ) { var sinTheta = Math . sqrt ( 1 - cosTheta * cosTheta ) ; var ans = 0 ; var toggle = 1 ; for ( var i = 0 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * Math . pow ( cosTheta , n - i ) * Math . pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans . toFixed ( 1 ) ; } binomial ( ) ; var cosTheta = 0.5 ; var n = 10 ; document . write ( findCosnTheta ( cosTheta , n ) ) ;
Find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | Javascript Program to find Sum of first n terms ; Returning the const sum ; no . of terms to find the sum
function calculateSum ( n ) { return ( parseInt ( Math . pow ( 10 , n + 1 ) ) * ( 9 * n - 1 ) + 10 ) / parseInt ( Math . pow ( 9 , 3 ) ) - n * ( n + 1 ) / 18 ; } let n = 3 ; document . write ( " " + calculateSum ( n ) ) ;
Find sum of the series 1 | javascript program to find sum of first n terms of the given series ; when n is odd ; when n is not odd ; no . of terms to find the sum
function calculateSum ( n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } var n = 8 ; document . write ( calculateSum ( n ) ) ;
Check if a number can be expressed as a ^ b | Set 2 | Javascript program to check if a number can be expressed as a ^ b . ; Driver code
function isPower ( a ) { if ( a == 1 ) return true ; for ( let i = 2 ; i * i <= a ; i ++ ) { let val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - Math . floor ( val ) ) < 0.00000001 ) return true ; } return false ; } let n = 16 ; document . write ( isPower ( n ) ? " " : " " ) ;
Program to calculate Root Mean Square | Function that Calculate Root Mean Square ; Calculate square . ; Calculate Mean . ; Calculate Root . ; Driver Code
function rmsValue ( arr , n ) { var square = 0 ; var mean = 0 ; var root = 0 ; for ( i = 0 ; i < n ; i ++ ) { square += Math . pow ( arr [ i ] , 2 ) ; } mean = ( square / ( n ) ) ; root = Math . sqrt ( mean ) ; return root ; } var arr = [ 10 , 4 , 6 , 8 ] ; var n = arr . length ; document . write ( rmsValue ( arr , n ) . toFixed ( 5 ) ) ;
Program to find the quantity after mixture replacement | Function to calculate the Remaining amount . ; calculate Right hand Side ( RHS ) . ; calculate Amount left by multiply it with original value . ; Driver Code
function Mixture ( X , Y , Z ) { var result = 0.0 , result1 = 0.0 ; result1 = ( ( X - Y ) / X ) ; result = Math . pow ( result1 , Z ) ; result = result * X ; return result ; } var X = 10 , Y = 2 , Z = 2 ; document . write ( Mixture ( X , Y , Z ) . toFixed ( 1 ) + " " ) ;
Sum of sum of all subsets of a set formed by first N natural numbers | modulo value ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with the result ; y must be even now y = y >> 1 ; y = y / 2 ; function to find ff ( n ) ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver code ; function call
const mod = ( 1e9 + 7 ) ; function power ( x , y , p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function check ( n ) { n -- ; let ans = n * n ; if ( ans >= mod ) ans %= mod ; ans += n + 2 ; if ( ans >= mod ) ans %= mod ; ans = ( power ( 2 , n , mod ) % mod * ans % mod ) % mod ; ans = ( ans - 1 + mod ) % mod ; return ans ; } let n = 4 ; document . write ( check ( n ) ) ;
Program to find LCM of 2 numbers without using GCD | Function to return LCM of two numbers ; Driver program to test above function
function findLCM ( a , b ) { var lar = Math . max ( a , b ) ; var small = Math . min ( a , b ) ; for ( i = lar ; ; i += lar ) { if ( i % small == 0 ) return i ; } } var a = 5 , b = 7 ; document . write ( " " + a + " " + b + " " + findLCM ( a , b ) ) ;
Smarandache | Function to collect first ' n ' prime numbers ; List to store first ' n ' primes ; Function to generate Smarandache - Wellin Sequence ; Storing the first ' n ' prime numbers in a list ; Driver Code
function primes ( n ) { var i = 2 ; var j = 0 ; var result = Array ( n ) var z = 0 ; while ( j < n ) { var flag = true ; for ( var item = 2 ; item <= parseInt ( i * 1 / 2 ) ; item ++ ) if ( i % item == 0 && i != item ) { flag = false ; break ; } if ( flag ) { result [ z ++ ] = i ; j += 1 ; } i += 1 ; } for ( i = 0 ; i < 5 ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) document . write ( result [ j ] ) ; document . write ( " " ) ; } } function smar_wln ( n ) { primes ( n ) ; } var n = 5 ; document . write ( " " + n + " " ) ; smar_wln ( n ) ;
Pentatope number | function for Pentatope number ; formula for find Pentatope nth term ; Driver Code
function Pentatope_number ( n ) { return n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 24 ; } let n = 7 ; document . write ( n + " " + " " + Pentatope_number ( n ) + " " ) ; n = 12 ; document . write ( n + " " + " " + Pentatope_number ( n ) ) ;
Program for Centered Icosahedral Number | Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code
function centeredIcosahedralNum ( n ) { return parseInt ( ( 2 * n + 1 ) * ( 5 * n * n + 5 * n + 3 ) / 3 ) ; } let n = 10 ; document . write ( centeredIcosahedralNum ( n ) + " " ) ; n = 12 ; document . write ( centeredIcosahedralNum ( n ) ) ;
Sum of first n natural numbers | Function to find the sum of series ; Driver code
function seriesSum ( n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } var n = 4 ; document . write ( seriesSum ( n ) ) ;
Dodecagonal number | function for Dodecagonal number ; formula for find Dodecagonal nth term ; Driver Code
function Dodecagonal_number ( n ) { return 5 * n * n - 4 * n ; } let n = 7 ; document . write ( Dodecagonal_number ( n ) + " " ) ; n = 12 ; document . write ( Dodecagonal_number ( n ) + " " ) ;
Arithmetic Number | Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; cnt = cnt + 1 ; incrementing power ; Calculating number of divisors If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; return ans ; Total divisors ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driven Program
function SieveOfEratosthenes ( n , prime , primesquare , a ) { for ( var i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; for ( var i = 0 ; i <= ( n * n + 1 ) ; i ++ ) primesquare [ i ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } var j = 0 ; for ( var p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { a [ j ] = p ; primesquare [ p * p ] = true ; j ++ ; } } } function countDivisors ( n ) { if ( n == 1 ) return 1 ; var prime = Array ( n + 1 ) . fill ( false ) ; var primesquare = Array ( n * n + 1 ) . fill ( 0 ) ; SieveOfEratosthenes ( n , prime , primesquare , a ) ; var ans = 1 ; for ( var i = 0 ; ; i ++ ) { if ( a [ i ] * a [ i ] * a [ i ] > n ) break ; var cnt = 1 ; while ( n % a [ i ] == 0 ) { n = parseInt ( n / a [ i ] ) ; } ans = ans * cnt ; } if ( prime [ n ] ) ans = ans * 2 ; else if ( primesquare [ n ] ) ans = ans * 3 ; else if ( n != 1 ) ans = ans * 4 ; } function sumofFactors ( n ) { var res = 1 ; for ( var i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { var count = 0 , curr_sum = 1 ; var curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = parseInt ( n / i ) ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } function checkArithmetic ( n ) { var count = countDivisors ( n ) ; var sum = sumofFactors ( n ) ; return ( sum % count == 0 ) ; } var n = 6 ; ( checkArithmetic ( n ) ) ? ( document . write ( " " ) ) : ( document . write ( " " ) ) ;
Finding n | A formula based Java program to find sum of series with cubes of first n natural numbers ; Driver Function
function magicOfSequence ( N ) { return ( N * ( N + 1 ) / 2 ) + 2 * N ; } let N = 6 ; document . write ( magicOfSequence ( N ) ) ;
Form a number using corner digits of powers | Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Storing N raised to power 0 ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code
function nextPower ( N , power ) { var carry = 0 ; for ( var i = 0 ; i < power . length ; i ++ ) { var prod = ( power [ i ] * N ) + carry ; power [ i ] = prod % 10 ; carry = parseInt ( prod / 10 ) ; } while ( carry >= 1 ) { power . push ( carry % 10 ) ; carry = parseInt ( carry / 10 ) ; } return power ; } function printPowerNumber ( X , N ) { var power = [ ] ; power . push ( 1 ) ; var res = [ ] ; for ( var i = 1 ; i <= X ; i ++ ) { power = nextPower ( N , power ) ; res . push ( power [ power . length - 1 ] ) ; res . push ( power [ 0 ] ) ; } for ( var i = 0 ; i < res . length ; i ++ ) document . write ( res [ i ] ) ; } var N = 19 , X = 4 ; printPowerNumber ( X , N ) ;
First digit in factorial of a number | JavaScript program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; Loop for divide the fact until it become the single digit and return the fact ; Driver code
function firstDigit ( n ) { let fact = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { fact = fact * i ; while ( fact % 10 == 0 ) fact = fact / 10 ; } while ( fact >= 10 ) fact = fact / 10 ; return ( Math . round ( fact ) ) ; } let n = 5 ; document . write ( firstDigit ( n ) ) ;