codeparrot/xlcost-text-to-code huggingface dataset:
Print all proper fractions with denominators less than equal to N | Function to print all proper functions ; If the numerator and denominator are coprime ; Driver code
const printFractions = ( n ) => { for ( var i = 1 ; i < n ; i ++ ) { for ( var j = i + 1 ; j <= n ; j ++ ) { if ( __gcd ( i , j ) == 1 ) { let a = ` ${ i } ` ; let b = ` ${ j } ` ; document . write ( ` ${ a } ${ b } ` ) } } } } const __gcd = ( a , b ) => { if ( b == 0 ) { return a ; } else { return __gcd ( b , a % b ) ; } } let n = 3 ; printFractions ( n ) ;
Maximum number of objects that can be created as per given conditions | Function for finding the maximum number of objects from N type - 1 and M type - 2 items ; storing minimum of N and M ; storing maximum number of objects from given items ; Driver Code
function numberOfObjects ( N , M ) { let initial = Math . min ( N , M ) ; let final = Math . floor ( ( N + M ) / 3 ) ; return Math . min ( initial , final ) ; } let N = 8 ; let M = 7 ; document . write ( numberOfObjects ( N , M ) + " " ) ;
Square root of a number by Repeated Subtraction method | Function to return the square root of the given number ; Subtract n - th odd number ; Return the result ; Driver Code
function SquareRoot ( num ) { let count = 0 ; for ( let n = 1 ; n <= num ; n += 2 ) { num = num - n ; count += 1 ; if ( num == 0 ) break ; } return count ; } let N = 81 ; document . write ( SquareRoot ( N ) ) ;
Maximize count of distinct elements possible in an Array from the given operation | Function to find the gcd of two numbers ; Function to calculate and return the count of maximum possible distinct elements in the array ; Find the maximum element ; Base Case ; Finding the gcd of first two element ; Calculate Gcd of the array ; Return the total count of distinct elements ; Driver Code
function gcd ( x , y ) { if ( x == 0 ) return y ; return gcd ( y % x , x ) ; } function findDistinct ( arr , n ) { let maximum = Math . max ( ... arr ) ; if ( n == 1 ) return 1 ; if ( n == 2 ) { return ( maximum / gcd ( arr [ 0 ] , arr [ 1 ] ) ) ; } let k = gcd ( arr [ 0 ] , arr [ 1 ] ) ; for ( let i = 2 ; i < n ; i ++ ) { k = gcd ( k , arr [ i ] ) ; } return ( maximum / k ) ; } let arr = [ 1 , 2 , 3 , 5 ] ; let n = arr . length ; document . write ( findDistinct ( arr , n ) ) ;
Smallest N digit number with none of its digits as its divisor | Function to calculate power ; Function to check if the N - digit number satisfies the given condition or not ; Getting the last digit ; Every number is divisible by 1 and dividing a number by 0 isn 't possible. Thus  numbers with 0 as a digit  must be discarded. ; If any digit divides the number , return false ; If no digit divides the number , return true ; ; Function to find the smallest number not divisible by any of its digits . ; Get the lower range of n digit number ; Get the high range of n digit number ; check all the N - digit numbers ; If the first number to satify the constraints is found print it , set the flag value and break out of the loop . ; If no such digit found , return - 1 as per problem statement ; Driver Code
function power ( a , b ) { if ( b == 0 ) return 1 ; if ( b == 1 ) return a ; let tmp = power ( a , Math . floor ( b / 2 ) ) ; let result = tmp * tmp ; if ( b % 2 == 1 ) result *= a ; return result ; } function check ( n ) { let temp = n ; while ( temp > 0 ) { let last_digit = temp % 10 ; if ( last_digit == 0 last_digit == 1 ) return false ; if ( n % last_digit == 0 ) return false ; temp = Math . floor ( temp / 10 ) ; } return true ; } function solve ( n ) { let L = power ( 10 , n - 1 ) ; let R = power ( 10 , n ) - 1 ; let flag = 0 ; for ( let i = L ; i <= R ; i ++ ) { let answer = check ( i ) ; if ( answer === true ) { document . write ( i + " " ) ; flag ++ ; break ; } } if ( flag === 0 ) document . write ( " " + " " ) ; } let N = 4 ; solve ( N ) ;
Cost of creating smallest subsequence with sum of difference between adjacent elements maximum |  ; initializing cost = 0 ; to store the removed element ; this will store the sum of the subsequence ; checking all the element of the vector ; storing the value of arr [ i ] in temp variable ; if the situation like arr [ i - 1 ] < arr [ i ] < arr [ i + 1 ] or arr [ i - 1 ] > arr [ i ] > arr [ i + 1 ] occur remove arr [ i ] i . e , temp from sequence ; insert the element in the set removedElements ; storing the value of arr [ i ] in temp ; taking the element not in removedElements ; adding the value of elements of subsequence ; if we have to remove the element then we need to add the cost associated with the element ; printing the sum of the subsequence with minimum length possible ; printing the cost incurred in creating subsequence ; Driver code ; calling the function
function costOfSubsequence ( N , arr , costArray ) { let i , temp ; let cost = 0 ; let removedElements = new Set ( ) ; let ans = 0 ; for ( i = 1 ; i < ( N - 1 ) ; i ++ ) { temp = arr [ i ] ; if ( ( ( arr [ i - 1 ] < temp ) && ( temp < arr [ i + 1 ] ) ) || ( ( arr [ i - 1 ] > temp ) && ( temp > arr [ i + 1 ] ) ) ) { removedElements . add ( temp ) ; } } for ( i = 0 ; i < ( N ) ; i ++ ) { temp = arr [ i ] ; if ( ! removedElements . has ( temp ) ) { ans += arr [ i ] ; } else { cost += costArray [ i ] ; } } document . write ( ans + " " ) ; document . write ( cost ) ; } let N ; N = 4 ; let arr = [ 1 , 3 , 4 , 2 ] ; let costArray = [ 0 , 1 , 0 , 0 ] ; costOfSubsequence ( N , arr , costArray ) ;
Count of natural numbers in range [ L , R ] which are relatively prime with N | Javascript code to count of natural numbers in range [ L , R ] which are relatively prime with N ; Container of all the primes up to sqrt ( n ) ; Function to calculate prime factors of n ; Run the sieve of Eratosthenes ; 0 ( false ) means prime , 1 ( true ) means not prime ; No even number is prime except for 2 ; All the multiples of each each prime numbers are non - prime ; Get all the primes in prime vector ; Count the number of numbers up to m which are divisible by given prime numbers ; Run from i = 000. . 0 to i = 111. . 1 or check all possible subsets of the array ; If the number of set bits is odd , then add to the number of multiples ; Function calculates all number not greater than ' m ' which are relatively prime with n . ; If square of the prime number is greater than ' n ' , it can ' t ▁ ▁ be ▁ a ▁ factor ▁ of ▁ ' n ' ; If prime is a factor of n then increment count ; Driver code
let maxN = 100000000 ; let prime = [ ] ; function sieve ( n ) { let check = new Array ( 1000007 ) ; for ( let i = 0 ; i < 1000007 ; i ++ ) check [ i ] = false ; let i , j ; check [ 0 ] = false ; check [ 1 ] = true ; check [ 2 ] = false ; for ( i = 4 ; i <= n ; i += 2 ) check [ i ] = true ; for ( i = 3 ; i * i <= n ; i += 2 ) if ( ! check [ i ] ) { for ( j = i * i ; j <= n ; j += 2 * i ) check [ j ] = true ; } prime . push ( 2 ) ; for ( i = 3 ; i <= n ; i += 2 ) if ( ! check [ i ] ) prime . push ( i ) ; return ; } function countSetBits ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function count ( a , n , m ) { let parity = new Array ( 3 ) ; for ( let i = 0 ; i < 3 ; i ++ ) parity [ i ] = 0 ; for ( let i = 1 ; i < ( 1 << n ) ; i ++ ) { let mult = 1 ; for ( let j = 0 ; j < n ; j ++ ) if ( ( i & ( 1 << j ) ) != 0 ) mult *= a [ j ] ; parity [ countSetBits ( i ) & 1 ] += ( m / mult ) ; } return parity [ 1 ] - parity [ 0 ] ; } function countRelPrime ( n , m ) { let a = new Array ( 20 ) ; let i = 0 , j = 0 ; let pz = prime . length ; while ( n != 1 && i < pz ) { if ( prime [ i ] * prime [ i ] > n ) break ; if ( n % prime [ i ] == 0 ) { a [ j ] = prime [ i ] ; j ++ ; } while ( n % prime [ i ] == 0 ) n = Math . floor ( n / prime [ i ] ) ; i ++ ; } if ( n != 1 ) { a [ j ] = n ; j ++ ; } return m - count ( a , j , m ) ; } function countRelPrimeInRange ( n , l , r ) { sieve ( Math . floor ( Math . sqrt ( maxN ) ) ) ; let result = countRelPrime ( n , r ) - countRelPrime ( n , l - 1 ) ; document . write ( result ) ; } let N = 7 , L = 3 , R = 9 ; countRelPrimeInRange ( N , L , R ) ;
XOR of a subarray ( range of elements ) | Set 2 | Function to find XOR in a range from L to R ; Compute xor from arr [ 0 ] to arr [ i ] ; process every query in constant time ; if L == 0 ; Driver Code ; query [ ]
function find_Xor ( arr , query , N , Q ) { for ( var i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i - 1 ] ; } var ans = 0 ; for ( var i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 0 ) ans = arr [ query [ i ] [ 1 ] ] ; else ans = arr [ query [ i ] [ 0 ] - 1 ] ^ arr [ query [ i ] [ 1 ] ] ; document . write ( ans + " " ) ; } } var arr = [ 3 , 2 , 4 , 5 , 1 , 1 , 5 , 3 ] ; var N = 8 ; var Q = 2 ; var query = [ [ 1 , 4 ] , [ 3 , 7 ] ] ; find_Xor ( arr , query , N , Q ) ;
Generate an alternate increasing and decreasing Array | Function that returns generated array ; Dynamically allocate array ; START = 0 , END = N ; iterate over array ; if Str [ i ] == ' I ' assign arr [ i ] as START and increment START ; if str [ i ] == ' D ' assign arr [ i ] as END and decrement END ; assign A [ N ] as START ; return starting address of array A ; Driver Program
function DiStirngMatch ( Str ) { var N = Str . length ; var arr = Array ( N ) . fill ( 0 ) ; var START = 0 , END = N ; for ( var i = 0 ; i < N ; i ++ ) { if ( Str [ i ] == ' ' ) arr [ i ] = START ++ ; if ( Str [ i ] == ' ' ) arr [ i ] = END -- ; } arr [ N ] = START ; return arr ; } var Str = " " ; var N = Str . length ; var ptr = DiStirngMatch ( Str ) ; for ( var i = 0 ; i <= N ; i ++ ) document . write ( ptr [ i ] + " " ) ;
Count of square free divisors of a given number | The function to check if a number is prime or not ; If the number is even then its not prime ; Stores the count of distinct prime factors ; Print the number of square - free divisors
function IsPrime ( i ) { if ( i % 2 == 0 && i != 2 ) return false ; else { for ( j = 3 ; j <= Math . sqrt ( i ) ; j += 2 ) { if ( i % j == 0 ) return false ; } return true ; } } var c = 0 ; var N = 72 ; for ( i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( IsPrime ( i ) ) { if ( N % i == 0 ) { c ++ ; if ( IsPrime ( N / i ) && i != ( N / i ) ) c ++ ; } } } document . write ( Math . pow ( 2 , c ) - 1 ) ;
Generate Array whose difference of each element with its left yields the given Array | Function to find the sequence ; initializing 1 st element ; Creating sequence in terms of x ; Finding min element ; Finding value of x ; Creating original sequence ; Output original sequence ; Driver function
function find_seq ( arr , m , n ) { let b = new Array ( n ) ; let x = 0 ; b [ 0 ] = x ; for ( let i = 0 ; i < n - 1 ; i ++ ) { b [ i + 1 ] = x + arr [ i ] + b [ i ] ; } let mn = n ; for ( let i = 0 ; i < n ; i ++ ) { mn = Math . min ( mn , b [ i ] ) ; } x = 1 - mn ; for ( let i = 0 ; i < n ; i ++ ) { b [ i ] += x ; } for ( let i = 0 ; i < n ; i ++ ) { document . write ( b [ i ] + " " ) ; } document . write ( " " ) ; } let N = 3 ; let arr = [ - 2 , 1 ] ; let M = arr . length ; find_seq ( arr , M , N ) ;
Check whether count of odd and even factors of a number are equal | Function to check condition ; Initialize even_count and od_count ; Loop runs till square root ; Driver Code
function isEqualFactors ( N ) { let ev_count = 0 , od_count = 0 ; for ( let i = 1 ; i <= Math . sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } else { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; if ( ( N / i ) % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } } } if ( ev_count == od_count ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let N = 10 ; isEqualFactors ( N ) ;
Check whether count of odd and even factors of a number are equal | Function to check condition ; Driver Code
function isEqualFactors ( N ) { if ( ( N % 2 == 0 ) && ( N % 4 != 0 ) ) document . write ( " " ) ; else document . write ( " " ) ; } var N = 10 ; isEqualFactors ( N ) ; N = 125 ; isEqualFactors ( N ) ;
Count of all values of N in [ L , R ] such that count of primes upto N is also prime | Function to count the number of crazy primes in the given range [ L , R ] ; Stores all primes ; Stores count of primes ; Stores if frequency of primes is a prime or not upto each index ; Sieve of Eratosthenes ; Count primes ; If i is a prime ; Stores frequency of primes ; If the frequency of primes is a prime ; Increase count of required numbers ; Return the required count ; Given range ; Function call
function count_crazy_primes ( L , R ) { let prime = Array . from ( { length : R + 1 } , ( _ , i ) => 0 ) ; let countPrime = Array . from ( { length : R + 1 } , ( _ , i ) => - 1 ) ; let freqPrime = Array . from ( { length : R + 1 } , ( _ , i ) => 0 ) ; prime [ 0 ] = 1 ; prime [ 1 ] = 1 ; for ( let p = 2 ; p * p <= R ; p ++ ) { if ( prime [ p ] == 0 ) { for ( let i = p * p ; i <= R ; i += p ) prime [ i ] = 1 ; } } for ( let i = 1 ; i <= R ; i ++ ) { countPrime [ i ] = countPrime [ i - 1 ] ; if ( prime [ i ] != 0 ) { countPrime [ i ] ++ ; } } for ( let i = 1 ; i <= R ; i ++ ) { freqPrime [ i ] = freqPrime [ i - 1 ] ; if ( ! prime [ countPrime [ i ] ] ) { freqPrime [ i ] ++ ; } } return ( freqPrime [ R ] - freqPrime [ L - 1 ] ) ; } let L = 4 , R = 12 ; document . write ( count_crazy_primes ( L , R ) ) ;
Largest N digit number in Base B | Function to print the largest N - digit numbers of base b ; Find the largest N digit number in base b using the formula B ^ N - 1 ; Print the largest number ; Given Number and Base ; Function Call
function findNumbers ( n , b ) { let largest = Math . pow ( b , n ) - 1 ; document . write ( largest ) ; } let N = 2 , B = 5 ; findNumbers ( N , B ) ;
Generate an Array such with elements maximized through swapping bits | Function to generate the maximized array elements ; Traverse the array ; Iterate to count set and unset bits ; Count of unset bits ; Count of set bits ; Bitwise right shift ; Shifting all 1 ' s ▁ to ▁ MSB ▁ ▁ and ▁ 0' s to LSB ; Driver Code
function maximizedArray ( arr , N ) { let num , i = 0 ; for ( let l = N ; l > 0 ; l -- ) { num = arr [ i ] ; let one = 0 ; let zero = 0 ; while ( num != 0 ) { if ( num % 2 == 0 ) { zero ++ ; } else { one ++ ; } num = num >> 1 ; } for ( let j = zero ; j < ( one + zero ) ; j ++ ) { num += ( 1 << j ) ; } document . write ( num ) ; i ++ ; if ( N > 0 ) document . write ( " " ) ; } } let arr = [ 8 , 15 , 9 , 10 , 14 ] ; let N = arr . length ; maximizedArray ( arr , N ) ;
Find the ratio of LCM to GCD of a given Array | Function to calculate and return GCD of the given array ; Initialise GCD ; Once GCD is 1 , it will always be 1 with all other elements ; Return GCD ; Function to calculate and return LCM of the given array ; Initialise LCM ; LCM of two numbers is evaluated as [ ( a * b ) / gcd ( a , b ) ] ; Return LCM ; Function to prvar the ratio of LCM to GCD of the given array ; Driver Code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function findGCD ( arr , n ) { var gcd = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { gcd = __gcd ( arr [ i ] , gcd ) ; if ( gcd == 1 ) { return 1 ; } } return gcd ; } function findLCM ( arr , n ) { var lcm = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { lcm = ( ( ( arr [ i ] * lcm ) ) / ( __gcd ( arr [ i ] , lcm ) ) ) ; } return lcm ; } function findRatio ( arr , n ) { var gcd = findGCD ( arr , n ) ; var lcm = findLCM ( arr , n ) ; document . write ( ( lcm / gcd ) ) ; document . write ( " " ) ; } var arr = [ 6 , 12 , 36 ] ; var N = 3 ; findRatio ( arr , N ) ;
Minimum operations required to make all Array elements divisible by K | Function to find the minimum number of moves required to update the array such that each of its element is divisible by K ; Initialize Map data structure ; Iterate for all the elements of the array ; Calculate the value to be added ; Check if the value equals to 0 then simply continue ; Check if the value to be added is present in the map ; Update the answer ; Print the required result We need to add 1 to maxX because we cant ignore the first move where initially X = 0 and we need to increase it by 1 to make some changes in array ; Driver code
function compute ( a , N , K ) { let eqVal = new Map ( ) ; let maxX = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let val = a [ i ] % K ; val = ( val == 0 ? 0 : K - val ) ; if ( val == 0 ) continue ; if ( eqVal . has ( val ) ) { let numVal = eqVal . get ( val ) ; maxX = Math . max ( maxX , val + ( K * numVal ) ) ; eqVal . set ( val , eqVal . get ( val ) + 1 ) ; } else { eqVal . set ( val , 1 ) ; maxX = Math . max ( maxX , val ) ; } } document . write ( maxX == 0 ? 0 : maxX + 1 ) ; } let K = 3 ; let a = [ 1 , 2 , 2 , 18 ] ; let N = a . length ; compute ( a , N , K ) ;
Array sum after replacing all occurrences of X by Y for Q queries | Function that print the sum of the array for Q queries ; Stores the frequencies of array elements ; Calculate the sum of the initial array and store the frequency of each element in map ; Iterate for all the queries ; Store query values ; Decrement the sum accordingly ; Increment the sum accordingly ; Set count of Y [ i ] ; Reset count of X [ i ] ; Print the sum ; Driver Code ; Function call
function sumOfTheArrayForQuery ( A , N , X , Y , Q ) { var sum = 0 ; var count = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( count . has ( A [ i ] ) ) count . set ( A [ i ] , count . get ( A [ i ] ) + 1 ) else count . set ( A [ i ] , 1 ) } for ( var i = 0 ; i < Q ; i ++ ) { var x = X [ i ] , y = Y [ i ] ; if ( count . has ( X [ i ] ) ) { sum -= count . get ( X [ i ] ) * X [ i ] ; sum += count . get ( X [ i ] ) * Y [ i ] ; } if ( count . has ( Y [ i ] ) ) { count . set ( Y [ i ] , count . get ( Y [ i ] ) + count . get ( X [ i ] ) ) ; } count . set ( X [ i ] , 0 ) ; document . write ( sum + " " ) ; } } var arr = [ 1 , 2 , 1 , 3 , 2 ] ; var X = [ 2 , 3 , 5 ] ; var Y = [ 3 , 1 , 2 ] ; var N = arr . length ; var Q = X . length ; sumOfTheArrayForQuery ( arr , N , X , Y , Q ) ;
Maximum OR value of a pair in an Array | Set 2 | Function to return the maximum bitwise OR for any pair of the given array in O ( n ) time complexity . ; Find the maximum element in the array ; Stores the maximum OR value ; Traverse the array and perform Bitwise OR between every array element with the maximum element ; Driver Code
function maxOR ( arr , n ) { let max_value = Math . max ( ... arr ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , ( max_value arr [ i ] ) ) ; } return ans ; } let arr = [ 3 , 6 , 8 , 16 ] ; let n = 4 ; document . write ( maxOR ( arr , n ) ) ;
Count of elements on the left which are divisible by current element | Set 2 | Utility function to print the elements of the array ; Function to increment the count for each factor of given val ; Function to generate and print the required array ; Find max element of array ; Create count array of maxi size ; For every element of the array ; Count [ A [ i ] ] denotes how many previous elements are there whose factor is the current element . ; Increment in count array for factors of A [ i ] ; Print the generated array ; Driver code
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function IncrementFactors ( count , val ) { for ( let i = 1 ; i * i <= val ; i ++ ) { if ( val % i == 0 ) { if ( i == parseInt ( val / i ) ) { count [ i ] ++ ; } else { count [ i ] ++ ; count [ parseInt ( val / i ) ] ++ ; } } } } function generateArr ( A , n ) { let B = new Array ( n ) ; let maxi = Math . max ( ... A ) ; let count = new Array ( maxi + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { B [ i ] = count [ A [ i ] ] ; IncrementFactors ( count , A [ i ] ) ; } printArr ( B , n ) ; } let arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; let n = arr . length ; generateArr ( arr , n ) ;
Minimum changes required to make all Array elements Prime | Function to generate all primes ; If p is a prime ; Mark all its multiples as non - prime ; Store all prime numbers ; Return the list of primes ; Function to calculate the minimum increments to convert every array elements to a prime ; Extract maximum element of the given array ; Extract the index which has the next greater prime ; Store the difference between the prime and the array element ; Driver Code
function SieveOfEratosthenes ( n ) { let prime = new Array ( 2 * n + 1 ) ; prime . fill ( true ) for ( let p = 2 ; p * p <= 2 * n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= 2 * n ; i += p ) prime [ i ] = false ; } } let primes = new Array ( ) ; for ( let p = 2 ; p <= 2 * n ; p ++ ) if ( prime [ p ] ) primes . push ( p ) ; return primes ; } function minChanges ( arr ) { let n = arr . length ; let ans = 0 ; let maxi = arr . sort ( ( a , b ) => b - a ) [ 0 ] ; let primes = SieveOfEratosthenes ( maxi ) ; for ( let i = 0 ; i < n ; i ++ ) { let x = - 1 for ( let j = 0 ; j < primes . length ; j ++ ) { if ( arr [ i ] == primes [ j ] ) { x = j break } else if ( arr [ i ] < primes [ j ] ) { x = j break } } let minm = Math . abs ( primes [ x ] - arr [ i ] ) ; if ( x > 1 ) { minm = Math . min ( minm , Math . abs ( primes [ x - 1 ] - arr [ i ] ) ) ; } ans += minm ; } return ans ; } let arr = [ 4 , 25 , 13 , 6 , 20 ] ; document . write ( minChanges ( arr ) ) ;
Sum of multiples of Array elements within a given range [ L , R ] | Function to find the sum of all multiples of N up to K ; Calculate the sum ; Return the sum ; Function to find the total sum ; If L is divisible by a [ i ] ; Otherwise ; Return the final sum ; Given array arr [ ] ; Given range ; Function Call
function calcSum ( k , n ) { let value = ( k * n * ( n + 1 ) ) / 2 ; return value ; } function findSum ( a , n , L , R ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( L % a [ i ] == 0 && L != 0 ) { sum += calcSum ( a [ i ] , Math . floor ( R / a [ i ] ) ) - calcSum ( a [ i ] , Math . floor ( ( L - 1 ) / a [ i ] ) ) ; } else { sum += calcSum ( a [ i ] , Math . floor ( R / a [ i ] ) ) - calcSum ( a [ i ] , Math . floor ( L / a [ i ] ) ) ; } } return sum ; } let arr = [ 2 , 7 , 3 , 8 ] ; let N = arr . length ; let L = 7 ; let R = 20 ; document . write ( findSum ( arr , N , L , R ) ) ;
Count all prime numbers in a given range whose sum of digits is also prime | Javascript implementation for the above approach ; Create an array for storing primes ; Create a prefix array that will contain whether sum is prime or not ; Function to find primes in the range and check whether the sum of digits of a prime number is prime or not ; Initialise Prime array arr [ ] ; Since 0 and 1 are not prime numbers we mark them as '0' ; Using Sieve Of Eratosthenes ; if the number is prime ; Mark all the multiples of i starting from square of i with '0' ie . composite ; '0' represents not prime ; Initialise a sum variable as 0 ; Check if the number is prime ; A temporary variable to store the number ; Loop to calculate the sum of digits ; Check if the sum of prime number is prime ; if prime mark 1 ; If not prime mark 0 ; computing prefix array ; Function to count the prime numbers in the range [ L , R ] ; Function Call to find primes ; Prlet the result ; Input range ; Function Call
let maxN = 1000000 ; let arr = Array . from ( { length : 1000001 } , ( _ , i ) => 0 ) ; let prefix = Array . from ( { length : 1000001 } , ( _ , i ) => 0 ) ; function findPrimes ( ) { for ( let i = 1 ; i <= maxN ; i ++ ) arr [ i ] = 1 ; arr [ 0 ] = 0 ; arr [ 1 ] = 0 ; for ( let i = 2 ; i * i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { for ( let j = i * i ; j <= maxN ; j += i ) { arr [ j ] = 0 ; } } } let sum = 0 ; prefix [ 0 ] = 0 ; for ( let i = 1 ; i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { let temp = i ; sum = 0 ; while ( temp > 0 ) { let x = temp % 10 ; sum += x ; temp = Math . floor ( temp / 10 ) ; if ( arr [ sum ] == 1 ) { prefix [ i ] = 1 ; } else { prefix [ i ] = 0 ; } } } } for ( let i = 1 ; i <= maxN ; i ++ ) { prefix [ i ] += prefix [ i - 1 ] ; } } function countNumbersInRange ( l , r ) { findPrimes ( ) ; let result = prefix [ r ] - prefix [ l - 1 ] ; document . write ( result + " " ) ; } let l , r ; l = 5 ; r = 20 ; countNumbersInRange ( l , r ) ;