codeparrot/xlcost-text-to-code huggingface dataset:
Doolittle Algorithm : LU Decomposition | Python3 Program to decompose a matrix into lower and upper triangular matrix ; Decomposing matrix into Upper and Lower triangular matrix ; Upper Triangular ; Summation of L ( i , j ) * U ( j , k ) ; Evaluating U ( i , k ) ; Lower Triangular ; lower [ i ] [ i ] = 1 Diagonal as 1 ; Summation of L ( k , j ) * U ( j , i ) ; Evaluating L ( k , i ) ; setw is for displaying nicely ; Displaying the result : ; Lower ; Upper ; Driver code
MAX = 100 NEW_LINE def luDecomposition ( mat , n ) : NEW_LINE INDENT lower = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] NEW_LINE upper = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for k in range ( i , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i ) : NEW_LINE INDENT sum += ( lower [ i ] [ j ] * upper [ j ] [ k ] ) NEW_LINE DEDENT upper [ i ] [ k ] = mat [ i ] [ k ] - sum NEW_LINE DEDENT for k in range ( i , n ) : NEW_LINE INDENT if ( i == k ) : NEW_LINE else : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i ) : NEW_LINE INDENT sum += ( lower [ k ] [ j ] * upper [ j ] [ i ] ) NEW_LINE DEDENT lower [ k ] [ i ] = int ( ( mat [ k ] [ i ] - sum ) / upper [ i ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT print ( " Lower ▁ Triangular TABSYMBOL TABSYMBOL Upper ▁ Triangular " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( lower [ i ] [ j ] , end = " TABSYMBOL " ) NEW_LINE DEDENT print ( " " , end = " TABSYMBOL " ) NEW_LINE for j in range ( n ) : NEW_LINE INDENT print ( upper [ i ] [ j ] , end = " TABSYMBOL " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT mat = [ [ 2 , - 1 , - 2 ] , [ - 4 , 6 , 3 ] , [ - 4 , - 2 , 8 ] ] NEW_LINE luDecomposition ( mat , 3 ) NEW_LINE
Divide number into two parts divisible by given numbers | method prints divisible parts if possible , otherwise prints 'Not possible ; creating arrays to store reminder ; looping over all suffix and storing reminder with f ; getting suffix reminder from previous suffix reminder ; looping over all prefix and storing reminder with s ; getting prefix reminder from next prefix reminder ; updating base value ; now looping over all reminders to check partition condition ; if both reminders are 0 and digit itself is not 0 , then print result and return ; if we reach here , then string can ' be partitioned  under constraints ; Driver code
' NEW_LINE def printTwoDivisibleParts ( num , f , s ) : NEW_LINE INDENT N = len ( num ) ; NEW_LINE prefixReminder = [ 0 ] * ( N + 1 ) ; NEW_LINE suffixReminder = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( ord ( num [ i - 1 ] ) - 48 ) ) % f ; NEW_LINE DEDENT base = 1 ; NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( ord ( num [ i ] ) - 48 ) * base ) % s ; NEW_LINE base = ( base * 10 ) % s ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( prefixReminder [ i ] == 0 and suffixReminder [ i ] == 0 and num [ i ] != '0' ) : NEW_LINE INDENT print ( num [ 0 : i ] , num [ i : N ] ) ; NEW_LINE return 0 ; NEW_LINE DEDENT DEDENT print ( " Not ▁ Possible " ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = "246904096" ; NEW_LINE f = 12345 ; NEW_LINE s = 1024 ; NEW_LINE printTwoDivisibleParts ( num , f , s ) ; NEW_LINE DEDENT
Count of numbers satisfying m + sum ( m ) + sum ( sum ( m ) ) = N | function that returns sum of digits in a number ; initially sum of digits is 0 ; loop runs till all digits have been extracted ; last digit from backside ; sums up the digits ; the number is reduced to the number removing the last digit ; returns the sum of digits in a number ; function to calculate the count of such occurrences ; counter to calculate the occurrences ; loop to traverse from n - 97 to n ; calls the function to calculate the sum of digits of i ; calls the function to calculate the sum of digits of a ; if the summation is equal to n then increase counter by 1 ; returns the count ; driver program to test the above function ; calls the function to get the answer
def sum ( n ) : NEW_LINE INDENT rem = 0 NEW_LINE sum_of_digits = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT rem = n % 10 NEW_LINE sum_of_digits += rem NEW_LINE n = n // 10 NEW_LINE DEDENT return sum_of_digits NEW_LINE DEDENT def count ( n ) : NEW_LINE INDENT c = 0 NEW_LINE for i in range ( n - 97 , n + 1 ) : NEW_LINE INDENT a = sum ( i ) NEW_LINE b = sum ( a ) NEW_LINE if ( ( i + a + b ) == n ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT n = 9939 NEW_LINE print ( count ( n ) ) NEW_LINE
Check if a number is power of k using base changing method | Python program to check if a number can be raised to k ; loop to change base n to base = k ; Find current digit in base k ; If digit is neither 0 nor 1 ; Make sure that only one 1 is present . ; Driver code
def isPowerOfK ( n , k ) : NEW_LINE INDENT oneSeen = False NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % k NEW_LINE if ( digit > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( digit == 1 ) : NEW_LINE INDENT if ( oneSeen ) : NEW_LINE INDENT return False NEW_LINE DEDENT oneSeen = True NEW_LINE DEDENT n //= k NEW_LINE DEDENT return True NEW_LINE DEDENT n = 64 NEW_LINE k = 4 NEW_LINE if ( isPowerOfK ( n , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if number is palindrome or not in Octal | Python3 program to check if octal representation of a number is prime ; Function to Check no is in octal or not ; Function To check no is palindrome or not ; If number is already in octal , we traverse digits using repeated division with 10. Else we traverse digits using repeated division with 8 ; To store individual digits ; Traversing all digits ; checking if octal no is palindrome ; Driver Code
MAX_DIGITS = 20 ; NEW_LINE def isOctal ( n ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT if ( ( n % 10 ) >= 8 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT n = int ( n / 10 ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isPalindrome ( n ) : NEW_LINE INDENT divide = 8 if ( isOctal ( n ) == False ) else 10 NEW_LINE octal = [ ] NEW_LINE while ( n != 0 ) : NEW_LINE INDENT octal . append ( n % divide ) NEW_LINE n = int ( n / divide ) NEW_LINE DEDENT j = len ( octal ) - 1 NEW_LINE k = 0 NEW_LINE while ( k <= j ) : NEW_LINE INDENT if ( octal [ j ] != octal [ k ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT j -= 1 NEW_LINE k += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 97 ; NEW_LINE if ( isPalindrome ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find all factorial numbers less than or equal to n | Python3 program to find all factorial numbers smaller than or equal to n . ; Compute next factorial using previous ; Driver code
def printFactorialNums ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE x = 2 NEW_LINE while fact <= n : NEW_LINE INDENT print ( fact , end = " ▁ " ) NEW_LINE fact = fact * x NEW_LINE x += 1 NEW_LINE DEDENT DEDENT n = 100 NEW_LINE printFactorialNums ( n ) NEW_LINE
Happy Numbers | Returns sum of squares of digits of a number n . For example for n = 12 it returns 1 + 4 = 5 ; Returns true if n is Happy number else returns false . ; A set to store numbers during repeated square sum process ; Keep replacing n with sum of squares of digits until we either reach 1 or we endup in a cycle ; Number is Happy if we reach 1 ; Replace n with sum of squares of digits ; If n is already visited , a cycle is formed , means not Happy ; Mark n as visited ; Driver code
def sumDigitSquare ( n ) : NEW_LINE INDENT sq = 0 ; NEW_LINE while ( n != 0 ) : NEW_LINE INDENT digit = n % 10 NEW_LINE sq += digit * digit NEW_LINE n = n // 10 NEW_LINE DEDENT return sq ; NEW_LINE DEDENT def isHappy ( n ) : NEW_LINE INDENT s = set ( ) NEW_LINE s . add ( n ) NEW_LINE while ( True ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT n = sumDigitSquare ( n ) NEW_LINE if n in s : NEW_LINE INDENT return False NEW_LINE DEDENT s . add ( n ) NEW_LINE DEDENT return false ; NEW_LINE DEDENT n = 4 NEW_LINE if ( isHappy ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check whether a number has exactly three distinct factors or not | Python 3 program to check whether number has exactly three distinct factors or not ; Utility function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check whether given number has three distinct factors or not ; Find square root of number ; Check whether number is perfect square or not ; If number is perfect square , check whether square root is prime or not ; Driver program
from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT k = int ( sqrt ( n ) ) + 1 NEW_LINE for i in range ( 5 , k , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isThreeDisctFactors ( n ) : NEW_LINE INDENT sq = int ( sqrt ( n ) ) NEW_LINE if ( 1 * sq * sq != n ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( isPrime ( sq ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = 9 NEW_LINE if ( isThreeDisctFactors ( num ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT num = 15 NEW_LINE if ( isThreeDisctFactors ( num ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT num = 12397923568441 NEW_LINE if ( isThreeDisctFactors ( num ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find the last digit when factorial of A divides factorial of B | Function which computes the last digit of resultant of B ! / A ! ; if ( A == B ) : If A = B , B ! = A ! and B ! / A ! = 1 ; If difference ( B - A ) >= 5 , answer = 0 ; If non of the conditions are true , we iterate from A + 1 to B and multiply them . We are only concerned for the last digit , thus we take modulus of 10 ; driver function
def computeLastDigit ( A , B ) : NEW_LINE INDENT variable = 1 NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( ( B - A ) >= 5 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( A + 1 , B + 1 ) : NEW_LINE INDENT variable = ( variable * ( i % 10 ) ) % 10 NEW_LINE DEDENT return variable % 10 NEW_LINE DEDENT DEDENT print ( computeLastDigit ( 2632 , 2634 ) ) NEW_LINE
Program for sum of arithmetic series | Function to find sum of series . ; Driver function
def sumOfAP ( a , d , n ) : NEW_LINE INDENT sum = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT sum = sum + a NEW_LINE a = a + d NEW_LINE i = i + 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 20 NEW_LINE a = 2.5 NEW_LINE d = 1.5 NEW_LINE print ( sumOfAP ( a , d , n ) ) NEW_LINE
Product of factors of number | Python program to calculate product of factors of number ; function to product the factors ; If factors are equal , multiply only once ; Otherwise multiply both ; Driver Code
M = 1000000007 NEW_LINE def multiplyFactors ( n ) : NEW_LINE INDENT prod = 1 NEW_LINE i = 1 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n / i == i ) : NEW_LINE INDENT prod = ( prod * i ) % M NEW_LINE DEDENT else : NEW_LINE INDENT prod = ( prod * i ) % M NEW_LINE prod = ( prod * n / i ) % M NEW_LINE DEDENT DEDENT i = i + 1 NEW_LINE DEDENT return prod NEW_LINE DEDENT n = 12 NEW_LINE print ( multiplyFactors ( n ) ) NEW_LINE
Product of factors of number | Python program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver Code
M = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y % 2 == 1 ) : NEW_LINE INDENT res = ( res * x ) % M NEW_LINE DEDENT y = ( y >> 1 ) % M NEW_LINE x = ( x * x ) % M NEW_LINE DEDENT return res NEW_LINE DEDENT def countFactors ( n ) : NEW_LINE INDENT count = 0 NEW_LINE i = 1 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n / i == i ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = count + 2 NEW_LINE DEDENT DEDENT i = i + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def multiplyFactors ( n ) : NEW_LINE INDENT numFactor = countFactors ( n ) NEW_LINE product = power ( n , numFactor / 2 ) NEW_LINE if ( numFactor % 2 == 1 ) : NEW_LINE INDENT product = ( product * ( int ) ( math . sqrt ( n ) ) ) % M NEW_LINE DEDENT return product NEW_LINE DEDENT n = 12 NEW_LINE print multiplyFactors ( n ) NEW_LINE
Tribonacci Numbers | A space optimized based Python 3 program to print first n Tribinocci numbers . ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code
def printTrib ( n ) : NEW_LINE INDENT if ( n < 1 ) : NEW_LINE INDENT return NEW_LINE DEDENT first = 0 NEW_LINE second = 0 NEW_LINE third = 1 NEW_LINE print ( first , " ▁ " , end = " " ) NEW_LINE if ( n > 1 ) : NEW_LINE INDENT print ( second , " ▁ " , end = " " ) NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT print ( second , " ▁ " , end = " " ) NEW_LINE DEDENT for i in range ( 3 , n ) : NEW_LINE INDENT curr = first + second + third NEW_LINE first = second NEW_LINE second = third NEW_LINE third = curr NEW_LINE print ( curr , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE printTrib ( n ) NEW_LINE
Prime Number of Set Bits in Binary Representation | Set 2 | Function to create an array of prime numbers upto number 'n ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Append all the prime numbers to the list ; Utility function to count the number of set bits ; Driver program ; Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself .
' NEW_LINE import math as m NEW_LINE def SieveOfEratosthenes ( n ) : NEW_LINE INDENT prime = [ True for i in range ( n + 1 ) ] NEW_LINE p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT lis = [ ] NEW_LINE for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT lis . append ( p ) NEW_LINE DEDENT DEDENT return lis NEW_LINE DEDENT def setBits ( n ) : NEW_LINE INDENT return bin ( n ) [ 2 : ] . count ( '1' ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y = [ 4 , 8 ] NEW_LINE count = 0 NEW_LINE primeArr = SieveOfEratosthenes ( int ( m . ceil ( m . log ( y , 2 ) ) ) ) NEW_LINE for i in range ( x , y + 1 ) : NEW_LINE INDENT temp = setBits ( i ) NEW_LINE if temp in primeArr : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT
Count trailing zeroes present in binary representation of a given number using XOR | Python3 implementation of the above approach ; Function to print count of trailing zeroes present in binary representation of N ; Count set bits in ( N ^ ( N - 1 ) ) ; If res < 0 , return 0 ; Driver Code ; Function call to print the count of trailing zeroes in the binary representation of N
from math import log2 NEW_LINE def countTrailingZeroes ( N ) : NEW_LINE INDENT res = int ( log2 ( N ^ ( N - 1 ) ) ) NEW_LINE return res if res >= 0 else 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE print ( countTrailingZeroes ( N ) ) NEW_LINE DEDENT
Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | Function to generate all the possible permutation and get the max score ; If arr [ ] length is equal to N process the permutation ; Generating the permutations ; If the current element is chosen ; Mark the current element as true ; Recursively call for next possible permutation ; Backtracking ; Return the ans ; Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the final score ; Driver Code ; Stores the permutation ; To display the result
def getMax ( arr , ans , chosen , N ) : NEW_LINE INDENT if len ( arr ) == N : NEW_LINE INDENT ans = max ( ans , calcScr ( arr ) ) NEW_LINE return ans NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if chosen [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT chosen [ i ] = True NEW_LINE arr . append ( i ) NEW_LINE ans = getMax ( arr , ans , chosen , N ) NEW_LINE chosen [ i ] = False NEW_LINE arr . pop ( ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def calcScr ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT ans += ( i ^ arr [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 2 NEW_LINE arr = [ ] NEW_LINE ans = - 1 NEW_LINE chosen = [ False for i in range ( N ) ] NEW_LINE ans = getMax ( arr , ans , chosen , N ) NEW_LINE print ( ans ) NEW_LINE
Additive Congruence method for generating Pseudo Random Numbers | Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the linear congruential method ; Driver Code ; Seed value ; Modulus parameter ; Multiplier term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
def additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) : NEW_LINE INDENT randomNums [ 0 ] = Xo NEW_LINE for i in range ( 1 , noOfRandomNums ) : NEW_LINE INDENT randomNums [ i ] = ( randomNums [ i - 1 ] + c ) % m NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Xo = 3 NEW_LINE m = 15 NEW_LINE c = 2 NEW_LINE noOfRandomNums = 20 NEW_LINE randomNums = [ 0 ] * ( noOfRandomNums ) NEW_LINE additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) NEW_LINE for i in randomNums : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Number of ways to change the Array such that largest element is LCM of array | Modulo ; Fenwick tree to find number of indexes greater than x ; Function to compute x ^ y % MOD ; Loop to compute the x ^ y % MOD ; Function to update the Binary Indexed Tree ; Loop to update the BIT ; Function to find prefix sum upto idx ; Function to find number of ways to change the array such that MAX of array is same as LCM ; Updating BIT with the frequency of elements ; Maximum element in the array ; For storing factors of i ; Finding factors of i ; Sorting in descending order ; For storing ans ; For storing number of indexes greater than the i - 1 element ; Number of remaining factors ; Number of indexes in the array with element factor [ j ] and above ; Multiplying count with remFcators ^ ( indexes - prev ) ; Remove those counts which have lcm as i but i is not present ; Adding cnt - toSubtract to ans ; ; Driver Code
MOD = int ( 1e9 ) + 9 NEW_LINE MAXN = int ( 1e5 ) + 5 NEW_LINE BIT = [ 0 for _ in range ( MAXN ) ] NEW_LINE def power ( x , y ) : NEW_LINE INDENT if x == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 1 NEW_LINE while y > 0 : NEW_LINE INDENT if y % 2 == 1 : NEW_LINE INDENT ans = ( ans * x ) % MOD NEW_LINE DEDENT x = ( x * x ) % MOD NEW_LINE y = y // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT def updateBIT ( idx , val ) : NEW_LINE INDENT while idx < MAXN : NEW_LINE INDENT BIT [ idx ] += val NEW_LINE idx += idx & ( - idx ) NEW_LINE DEDENT DEDENT def queryBIT ( idx ) : NEW_LINE INDENT ans = 0 NEW_LINE while idx > 0 : NEW_LINE INDENT ans += BIT [ idx ] NEW_LINE idx -= idx & ( - idx ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def numWays ( arr ) : NEW_LINE INDENT mx = 0 NEW_LINE for i in arr : NEW_LINE INDENT updateBIT ( i , 1 ) NEW_LINE mx = max ( mx , i ) NEW_LINE DEDENT ans = 1 NEW_LINE for i in range ( 2 , mx + 1 ) : NEW_LINE INDENT factors = [ ] NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if j * j > i : NEW_LINE INDENT break NEW_LINE DEDENT if i % j == 0 : NEW_LINE INDENT factors . append ( j ) NEW_LINE if i // j != j : NEW_LINE INDENT factors . append ( i // j ) NEW_LINE DEDENT DEDENT DEDENT factors . sort ( ) NEW_LINE factors . reverse ( ) NEW_LINE cnt = 1 NEW_LINE prev = 0 NEW_LINE for j in range ( len ( factors ) ) : NEW_LINE INDENT remFactors = len ( factors ) - j NEW_LINE indexes = len ( arr ) - queryBIT ( factors [ j ] - 1 ) NEW_LINE cnt = ( cnt * power ( remFactors ,   \ indexes - prev ) ) % MOD NEW_LINE prev = max ( prev , indexes ) NEW_LINE DEDENT factors . remove ( factors [ 0 ] ) NEW_LINE toSubtract = 1 NEW_LINE prev = 0 NEW_LINE for j in range ( len ( factors ) ) : NEW_LINE INDENT remFactors = len ( factors ) - j NEW_LINE indexes = len ( arr ) - queryBIT ( factors [ j ] - 1 ) NEW_LINE toSubtract = ( toSubtract * power ( remFactors , indexes - prev ) ) NEW_LINE prev = max ( prev , indexes ) NEW_LINE DEDENT ans = ( ans + cnt - toSubtract + MOD ) % MOD ; NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 4 , 3 , 2 ] NEW_LINE ans = numWays ( arr ) ; NEW_LINE print ( ans ) NEW_LINE DEDENT
Second decagonal numbers | Function to find N - th term in the series ; Driver Code
def findNthTerm ( n ) : NEW_LINE INDENT print ( n * ( 4 * n + 3 ) ) NEW_LINE DEDENT N = 4 ; NEW_LINE findNthTerm ( N ) ; NEW_LINE
65537 | Function to find the nth 65537 - gon Number ; Driver Code
def gonNum65537 ( n ) : NEW_LINE INDENT return ( 65535 * n * n - 65533 * n ) // 2 ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( gonNum65537 ( n ) ) ; NEW_LINE
Hexacontatetragon numbers | Function to Find the Nth Hexacontatetragon Number ; Driver Code
def HexacontatetragonNum ( n ) : NEW_LINE INDENT return ( 62 * n * n - 60 * n ) / 2 ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( HexacontatetragonNum ( n ) ) ; NEW_LINE
Icosikaipentagon Number | Function to find the N - th icosikaipentagon number ; Driver code
def icosikaipentagonNum ( N ) : NEW_LINE INDENT return ( 23 * N * N - 21 * N ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ icosikaipentagon ▁ Number ▁ is ▁ " , icosikaipentagonNum ( n ) ) NEW_LINE
Chiliagon Number | Finding the nth chiliagon Number ; Driver Code
def chiliagonNum ( n ) : NEW_LINE INDENT return ( 998 * n * n - 996 * n ) // 2 ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( "3rd ▁ chiliagon ▁ Number ▁ is ▁ = ▁ " , chiliagonNum ( n ) ) ; NEW_LINE
Pentacontagon number | Finding the nth pentacontagon Number ; Driver Code
def pentacontagonNum ( n ) : NEW_LINE INDENT return ( 48 * n * n - 46 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ pentacontagon ▁ Number ▁ is ▁ = ▁ " , pentacontagonNum ( n ) ) NEW_LINE