codeparrot/xlcost-text-to-code huggingface dataset:
Program to find Nth odd Fibonacci Number | Function to find nth odd fibonacci number ; Driver Code
function oddFib ( n ) { n = ( 3 * n + 1 ) / 2 ; var a = - 1 , b = 1 , c , i ; for ( i = 1 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } var n = 4 ; document . write ( oddFib ( n ) ) ;
Find a pair ( n , r ) in an integer array such that value of nPr is maximum | Function to print the pair ( n , r ) such that nPr is maximum possible ; There should be atleast 2 elements ; Findex the largest 2 elements ; Driver code
function findPair ( arr , n ) { if ( n < 2 ) { document . write ( " " ) ; return ; } let i , first , second ; first = second = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } document . write ( " " + first + " " + second ) ; } let arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] ; let n = arr . length ; findPair ( arr , n ) ;
Sum of values of all possible non | Function to return the required sum ; Find the sum of the array elements ; Every element appears 2 ^ ( n - 1 ) times ; Driver code
function sum ( arr , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } sum = sum * parseInt ( Math . pow ( 2 , n - 1 ) ) ; return sum ; } var arr = [ 2 , 1 , 5 , 6 ] ; var n = arr . length ; document . write ( sum ( arr , n ) ) ;
Printing the Triangle Pattern using last term N | Function to demonstrate printing pattern ; number of spaces ; character to be printed ; outer loop to handle number of rows n in this case ; inner loop to handle number spaces values changing acc . to requirement ; decrementing k after each loop ; inner loop to handle number of columns values changing acc . to outer loop ; printing stars ; ending line after each row ; Function to find the max height or the number of lines in the triangle pattern ; Driver Function
function triangle ( n ) { var k = 2 * n - 2 ; var ch = 1 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < k ; j ++ ) document . write ( " " ) ; k = k - 1 ; for ( var j = 0 ; j <= i ; j ++ ) { document . write ( ch ++ + " " ) ; } document . write ( " " ) ; } } function maxHeight ( n ) { return parseInt ( ( ( parseInt ( Math . sqrt ( 1 + 8.0 * n ) ) ) - 1 ) / 2 ) ; } var N = 9 ; triangle ( maxHeight ( N ) ) ;
Number of ways in which N can be represented as the sum of two positive integers | Function to return the number of distinct ways to represent n as the sum of two integers ; Driver code
function ways ( n ) { return parseInt ( n / 2 ) ; } var n = 2 ; document . write ( ways ( n ) ) ;
Number of ways to erase exactly one element in the Binary Array to make XOR zero | Function to find the number of ways ; Calculate the number of 1 ' s ▁ and ▁ 0' s ; Considering the 4 cases ; Driver code
function no_of_ways ( a , n ) { let count_0 = 0 , count_1 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; } if ( count_1 % 2 == 0 ) return count_0 ; else return count_1 ; } let n = 4 ; let a1 = [ 1 , 1 , 0 , 0 ] ; document . write ( no_of_ways ( a1 , n ) + " " ) ; n = 5 ; let a2 = [ 1 , 1 , 1 , 0 , 0 ] ; document . write ( no_of_ways ( a2 , n ) + " " ) ; n = 5 ; let a3 = [ 1 , 1 , 0 , 0 , 0 ] ; document . write ( no_of_ways ( a3 , n ) + " " ) ; n = 6 ; let a4 = [ 1 , 1 , 1 , 0 , 0 , 0 ] ; document . write ( no_of_ways ( a4 , n ) + " " ) ;
Minimum absolute difference between N and any power of 2 | Function to return the highest power of 2 less than or equal to n ; Function to return the smallest power of 2 greater than or equal to n ; Function that returns the minimum absolute difference between n and any power of 2 ; Driver code
function prevPowerof2 ( n ) { var p = parseInt ( Math . log ( n ) / Math . log ( 2 ) ) ; return parseInt ( Math . pow ( 2 , p ) ) ; } function nextPowerOf2 ( n ) { var p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } function minDiff ( n ) { var low = prevPowerof2 ( n ) ; var high = nextPowerOf2 ( n ) ; return Math . min ( n - low , high - n ) ; } var n = 6 ; document . write ( minDiff ( n ) ) ;
Maximum possible number with the given operation | Function to return the maximum possible integer that can be obtained from the given integer after performing the given operations ; For every digit ; Digits greater than or equal to 5 need not to be changed as changing them will lead to a smaller number ; The resulting integer cannot have leading zero ; Driver code
function maxInt ( str ) { var str2 = " " ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] < ' ' ) { var l = ( ' ' . charCodeAt ( 0 ) - str [ i ] . charCodeAt ( 0 ) ) + ' ' . charCodeAt ( 0 ) ; str2 = str2 . concat ( String . fromCharCode ( l ) ) ; } } if ( str2 [ 0 ] == ' ' ) str2 [ 0 ] = ' ' ; return str2 ; } var str = " " ; document . write ( maxInt ( str ) )
Find the ratio of number of elements in two Arrays from their individual and combined average | Javascript function to find the ratio of number of array elements ; calculating GCD of them ; make neumarator and denominator coprime ; Driver Code
function FindRatio ( a , b , c ) { let up = Math . abs ( b - c ) ; let down = Math . abs ( c - a ) ; let g = gcd ( up , down ) ; up = parseInt ( up / g ) ; down = parseInt ( down / g ) ; document . write ( up + " " + down + " " ) ; } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } let a = 4 , b = 10 , c = 6 ; FindRatio ( a , b , c ) ;
Maximum distance between two 1 's in Binary representation of N | Javascript program to find the Maximum distance between two 1 's in Binary representation of N ; Compute the binary representation ; if N is a power of 2 then return - 1 ; else find the distance between the first position of 1 and last position of 1 ; Driver code
function longest_gap ( N ) { let distance = 0 , count = 0 , first_1 = - 1 , last_1 = - 1 ; while ( N ) { count ++ ; let r = N & 1 ; if ( r == 1 ) { first_1 = first_1 == - 1 ? count : first_1 ; last_1 = count ; } N = parseInt ( N / 2 ) ; } if ( last_1 <= first_1 ) { return - 1 ; } else { distance = ( last_1 - first_1 - 1 ) ; return distance ; } } let N = 131 ; document . write ( longest_gap ( N ) + " " ) ; N = 8 ; document . write ( longest_gap ( N ) + " " ) ; N = 17 ; document . write ( longest_gap ( N ) + " " ) ; N = 33 ; document . write ( longest_gap ( N ) + " " ) ;
Check if it is possible to move from ( 0 , 0 ) to ( X , Y ) in exactly K steps | Function that returns true if it is possible to move from ( 0 , 0 ) to ( x , y ) in exactly k moves ; Minimum moves required ; If possible ; Driver code
function isPossible ( x , y , k ) { var minMoves = Math . abs ( x ) + Math . abs ( y ) ; if ( k >= minMoves && ( k - minMoves ) % 2 == 0 ) return true ; return false ; } var x = 5 , y = 8 , k = 20 ; if ( isPossible ( x , y , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether N is a Factorion or not | Javascript implementation of the approach ; Function that returns true if n is a Factorion ; fact [ i ] will store i ! ; A copy of the given integer ; To store the sum of factorials of the digits of n ; Get the last digit ; Add the factorial of the current digit to the sum ; Remove the last digit ; Driver code
const MAX = 10 ; function isFactorion ( n ) { let fact = new Array ( MAX ) ; fact [ 0 ] = 1 ; for ( let i = 1 ; i < MAX ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; let org = n ; let sum = 0 ; while ( n > 0 ) { let d = n % 10 ; sum += fact [ d ] ; n = parseInt ( n / 10 ) ; } if ( sum == org ) return true ; return false ; } let n = 40585 ; if ( isFactorion ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the possible permutation of the bits of N | Javascript implementation of the approach ; Function that returns true if it is possible to arrange the bits of n in alternate fashion ; To store the count of 1 s in the binary representation of n ; If the number set bits and the number of unset bits is equal ; Driver code
const TOTAL_BITS = 32 ; function CountBits ( value ) { let count = 0 ; while ( value != 0 ) { count ++ ; value &= value - 1 ; } return count ; } function isPossible ( n ) { let cnt = CountBits ( n ) ; if ( cnt == parseInt ( TOTAL_BITS / 2 ) ) return true ; return false ; } let n = 524280 ; if ( isPossible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if two Integer are anagrams of each other | Javascript implementation of the approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagarams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Driver code
const TEN = 10 ; function updateFreq ( n , freq ) { while ( n ) { let digit = n % TEN ; freq [ digit ] ++ ; n = parseInt ( n / TEN ) ; } } function areAnagrams ( a , b ) { let freqA = new Array ( TEN ) . fill ( 0 ) ; let freqB = new Array ( TEN ) . fill ( 0 ) ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( let i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } let a = 240 , b = 204 ; if ( areAnagrams ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible | Function to find the permutation of the first N natural numbers such that the sum of ( i % Pi ) is maximum possible and return the maximum sum ; Driver code ; Function call
function Max_Sum ( n ) { return parseInt ( ( n * ( n - 1 ) ) / 2 ) ; } let n = 8 ; document . write ( Max_Sum ( n ) ) ;
Check if the number formed by the last digits of N numbers is divisible by 10 or not | Function that returns true if the number formed by the last digits of all the elements is divisible by 10 ; Last digit of the last element ; Number formed will be divisible by 10 ; Driver code
function isDivisible ( arr , n ) { let lastDigit = arr [ n - 1 ] % 10 ; if ( lastDigit == 0 ) return true ; return false ; } let arr = [ 12 , 65 , 46 , 37 , 99 ] ; let n = arr . length ; if ( isDivisible ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of Multiples of A , B or C less than or equal to N | Function to return the gcd of a and b ; Function to return the count of integers from the range [ 1 , num ] which are divisible by either a , b or c ; Calculate the number of terms divisible by a , b and c then remove the terms which are divisible by both ( a , b ) or ( b , c ) or ( c , a ) and then add the numbers which are divisible by a , b and c ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function divTermCount ( a , b , c , num ) { return Math . ceil ( ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / ( ( a * b ) / gcd ( a , b ) ) ) - ( num / ( ( c * b ) / gcd ( c , b ) ) ) - ( num / ( ( a * c ) / gcd ( a , c ) ) ) + ( num / ( ( a * b * c ) / gcd ( gcd ( a , b ) , c ) ) ) ) ) ; } n = 13 ; var a = 7 , b = 3 , c = 5 , n = 100 ; document . write ( divTermCount ( a , b , c , n ) ) ;
Array containing power of 2 whose XOR and Sum of elements equals X | Function to return the required array ; Store the power of 2 ; while n is greater than 0 ; if there is 1 in binary representation ; Divide n by 2 Multiply p2 by 2 ; Driver code ; Get the answer ; Printing the array
function getArray ( n ) { let ans = [ ] ; let p2 = 1 ; while ( n > 0 ) { if ( n & 1 ) ans . push ( p2 ) ; n >>= 1 ; p2 *= 2 ; } return ans ; } let n = 15 ; let ans = getArray ( n ) ; for ( let i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] + " " ) ;
Find out the correct position of the ball after shuffling | javascript implementation of the above approach ; Function to generate the index of the glass containing the ball ; Change the index ; Change the index ; Prvar the index ; Driver Code ; Storing all the shuffle operation
M = 3 ; N = 2 ; function getIndex ( n , shuffle ) { for ( i = 0 ; i < 3 ; i ++ ) { if ( shuffle [ i ] [ 0 ] == n ) n = shuffle [ i ] [ 1 ] ; else if ( shuffle [ i ] [ 1 ] == n ) n = shuffle [ i ] [ 0 ] ; } document . write ( n ) ; } var n = 3 ; var shuffle = [ [ 3 , 1 ] , [ 2 , 1 ] , [ 1 , 2 ] ] ; getIndex ( n , shuffle ) ;
Count the number of subsequences of length k having equal LCM and HCF | Returns factorial of n ; Returns nCr for the given values of r and n ; Map to store the frequencies of each elements ; Loop to store the frequencies of elements in the map ; Using nCR formula to calculate the number of subsequences of a given length ; Driver Code ; Function calling
function fact ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function nCr ( n , r ) { return fact ( n ) / ( 1 * fact ( r ) * fact ( n - r ) ) ; } function number_of_subsequences ( arr , k , n ) { let s = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } for ( let [ key , value ] of mp . entries ( ) ) { s = s + 1 * nCr ( value , k ) ; } return s ; } let arr = [ 1 , 1 , 1 , 1 , 2 , 2 , 2 ] ; let k = 2 ; let n = arr . length ; document . write ( number_of_subsequences ( arr , k , n ) ) ;
Find the sum of all possible pairs in an array of N elements | Function to return the sum of the elements of all possible pairs from the array ; To store the required sum ; For every element of the array ; It appears ( 2 * n ) times ; Driver code
function sumPairs ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] * ( 2 * n ) ) ; } return sum ; } let arr = [ 1 , 2 , 3 ] ; let n = arr . length ; document . write ( sumPairs ( arr , n ) ) ;
Minimum sum obtained from groups of four elements from the given array | Function to return the minimum required sum ; To store the required sum ; Sort the array in descending order ; The indices which give 0 or 1 as the remainder when divided by 4 will be the maximum two elements of the group ; Driver code
function minSum ( arr , n ) { let sum = 0 ; arr . sort ( function ( a , b ) { return b - a } ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 4 < 2 ) sum = sum + arr [ i ] ; } return sum ; } let arr = [ 1 , 1 , 10 , 2 , 2 , 2 , 1 ] ; let n = arr . length ; document . write ( minSum ( arr , n ) ) ;
Count of triples ( A , B , C ) where A * C is greater than B * B | function to return the count of the valid triplets ; Driver Code ; function calling
function countTriplets ( A , B , C ) { let ans = 0 ; for ( let i = 1 ; i <= A ; i ++ ) { for ( let j = 1 ; j <= B ; j ++ ) { for ( let k = 1 ; k <= C ; k ++ ) { if ( i * k > j * j ) ans ++ ; } } } return ans ; } let A , B , C ; A = 3 , B = 2 , C = 2 ; document . write ( countTriplets ( A , B , C ) ) ;
Count of triples ( A , B , C ) where A * C is greater than B * B | Counts the number of triplets for a given value of b ; Count all triples in which a = i ; Smallest value j such that i * j > B2 ; Count all ( i , B2 , x ) such that x >= j ; count all ( x , B2 , y ) such that x >= j this counts all such triples in which a >= j ; As all triples with a >= j have been counted reduce A to j - 1. ; Counts the number of triples that satisfy the given constraints ; GetCount of triples in which b = i ; Driver Code ; Function calling
function getCount ( A , B2 , C ) { let count = 0 ; for ( let i = 1 ; i <= A ; i ++ ) { let j = parseInt ( B2 / i ) + 1 ; if ( C >= j ) count = ( count + C - j + 1 ) ; if ( A >= j && C >= i ) count = ( count + ( C - i + 1 ) * ( A - j + 1 ) ) ; if ( A >= j ) A = j - 1 ; } return count ; } function countTriplets ( A , B , ) { let ans = 0 ; for ( let i = 1 ; i <= B ; i ++ ) { ans = ( ans + getCount ( A , i * i , C ) ) ; } return ans ; } let A , B , C ; A = 3 , B = 2 , C = 2 ; document . write ( countTriplets ( A , B , C ) ) ;
Summation of floor of harmonic progression | Function to return the summation of the given harmonic series ; To store the summation ; Floor of sqrt ( n ) ; Summation of floor ( n / i ) ; From the formula ; Driver code
function getSum ( n ) { let sum = 0 ; let k = parseInt ( Math . sqrt ( n ) ) ; for ( let i = 1 ; i <= k ; i ++ ) { sum += Math . floor ( n / i ) ; } sum *= 2 ; sum -= Math . pow ( k , 2 ) ; return sum ; } let n = 5 ; document . write ( getSum ( n ) ) ;
Count of distinct remainders when N is divided by all the numbers from the range [ 1 , N ] | Function to return the count of distinct remainders that can be obtained when n is divided by every element from the range [ 1 , n ] ; If n is even ; If n is odd ; Driver code
function distinctRemainders ( n ) { if ( n % 2 == 0 ) return parseInt ( n / 2 ) ; return ( 1 + parseInt ( n / 2 ) ) ; } let n = 5 ; document . write ( distinctRemainders ( n ) ) ;
Count total unset bits in all the numbers from 1 to N | Function to return the count of unset bits in the binary representation of all the numbers from 1 to n ; To store the count of unset bits ; For every integer from the range [ 1 , n ] ; A copy of the current integer ; Count of unset bits in the current integer ; If current bit is unset ; Driver code
function countUnsetBits ( n ) { var cnt = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { var temp = i ; while ( temp ) { if ( temp % 2 == 0 ) cnt ++ ; temp = parseInt ( temp / 2 ) ; } } return cnt ; } var n = 5 ; document . write ( countUnsetBits ( n ) ) ;
Find if a degree sequence can form a simple graph | Havel | Function that returns true if a simple graph exists ; Keep performing the operations until one of the stopping condition is met ; Sort the list in non - decreasing order ; Check if all the elements are equal to 0 ; Store the first element in a variable and delete it from the list ; Check if enough elements are present in the list ; Subtract first element from next v elements ; Check if negative element is encountered after subtraction ; Driver Code
function graphExists ( a , n ) { while ( 1 ) { a . sort ( ( a , b ) => b - a ) if ( a [ 0 ] == 0 ) return true ; var v = a [ 0 ] ; a . shift ( ) ; if ( v > a . length ) return false ; for ( var i = 0 ; i < v ; i ++ ) { a [ i ] -- ; if ( a [ i ] < 0 ) return false ; } } } var a = [ 3 , 3 , 3 , 3 ] ; var n = a . length ; graphExists ( a , n ) ? document . write ( " " ) : document . write ( " " ) ;
Number of sub arrays with negative product | Function to return the count of subarrays with negative product ; Replace current element with 1 if it is positive else replace it with - 1 instead ; Take product with previous element to form the prefix product ; Count positive and negative elements in the prefix product array ; Return the required count of subarrays ; Driver code
function negProdSubArr ( arr , n ) { let positive = 1 , negative = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } let arr = [ 5 , - 4 , - 3 , 2 , - 5 ] ; let n = arr . length ; document . write ( negProdSubArr ( arr , n ) ) ;
Repeated sum of first N natural numbers | Function to return the sum of the first n natural numbers ; Function to return the repeated sum ; Perform the operation exactly k times ; Update n with the sum of first n natural numbers ; Driver code
function sum ( n ) { var sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } function repeatedSum ( n , k ) { for ( i = 0 ; i < k ; i ++ ) { n = sum ( n ) ; } return n ; } var n = 2 , k = 2 ; document . write ( repeatedSum ( n , k ) ) ;
Difference between Sum of Cubes and Sum of First N Natural Numbers | JavaScript program to find the difference between the sum of the cubes of the first N natural numbers and the sum of the first N natural number ; Sum of first n natural numbers ; Find the required difference ; Driver Code
function difference ( n ) { let S , res ; S = Math . floor ( ( n * ( n + 1 ) ) / 2 ) ; res = S * ( S - 1 ) ; return res ; } let n = 5 ; document . write ( difference ( n ) ) ;
Check if the sum of digits of number is divisible by all of its digits | Function that returns true if all the digits of n divide the sum of the digits of n ; Store a copy of the original number ; Find the sum of the digits of n ; Restore the original value ; Check if all the digits divide the calculated sum ; If current digit doesn 't  divide the sum ; Driver code
function isDivisible ( n ) { var temp = n ; var sum = 0 ; while ( n != 0 ) { var digit = parseInt ( n % 10 ) ; sum += digit ; n = parseInt ( n / 10 ) ; } n = temp ; while ( n != 0 ) { var digit = parseInt ( n % 10 ) ; if ( sum % digit != 0 ) return false ; n = parseInt ( n / 10 ) ; } return true ; } var n = 123 ; if ( isDivisible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if the sum of digits of number is divisible by all of its digits | JavaScript implementation of above approach ; Converting integer to string ; Initialising sum to 0 ; Traversing through the string ; Converting character to int ; Comparing number and sum Traversing again ; Check if any digit is not dividing the sum ; Return false ; If any value is not returned then all the digits are dividing the sum SO return true ; Driver Code ; passing this number to get result function
function getResult ( n ) { var st = n . toString ( ) ; var sum = 0 var length = st . length ; for ( var i = 0 ; i < st . length ; i ++ ) { sum = sum + Number ( st [ i ] ) } for ( var i = 0 ; i < st . length ; i ++ ) { if ( sum % Number ( st [ i ] ) != 0 ) { return ' ' } else { return ' ' ; } } } var n = 123 ; document . write ( getResult ( n ) ) ;
Program for Mobius Function | Set 2 | Javascript implementation of the approach ; Function to calculate least prime factor of each number ; If it is a prime number ; For all multiples which are not visited yet . ; Function to find the value of Mobius function for all the numbers from 1 to n ; To store the values of Mobius function ; If number is one ; If number has a squared prime factor ; Multiply - 1 with the previous number ; Driver code ; Function to find least prime factor ; Function to find mobius function
const N = 100005 ; let lpf = new Array ( N ) ; function least_prime_factor ( ) { for ( let i = 2 ; i < N ; i ++ ) if ( ! lpf [ i ] ) for ( let j = i ; j < N ; j += i ) if ( ! lpf [ j ] ) lpf [ j ] = i ; } function Mobius ( n ) { let mobius = new Array ( N ) ; for ( let i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ parseInt ( i / lpf [ i ] ) ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ parseInt ( i / lpf [ i ] ) ] ; } } for ( let i = 1 ; i <= n ; i ++ ) document . write ( mobius [ i ] + " " ) ; } let n = 5 ; least_prime_factor ( ) ; Mobius ( n ) ;