giulio98/xlcost-single-prompt huggingface dataset:
# hensel 's lemma | function to find the modular inverse of a modulo m ; apply the euclidean algorithm , to find the modular inverse ; function to find the derivative of f ( x ) and f '(x) = 3 * (x ^ 2) ; function to find the image of x in f ( x ) = x ^ 3 - k . ; function to find the next power of the number ; next power of prime for which solution is to be found ; using hensel 's recursion to  find the solution(next_a) for  next power of prime ; if next_a < 0 return equivalent positive remainder modulo p ; return the next power of a ; function to find the solution of the required exponent of prime ; the lemma does not work for derivative of f ( x ) at a1 ; looping from 1 to power of prime whose solution is to be found ; final answer after evaluating all the exponents up till the required exponent ; 
def inv(a, m):
    m0 = m
    x0 = 0
    x1 = 1
    if (m == 1):
        return 0
    while (a > 1):
        q = a // m
        t = m
        m = a % m
        a = t
        t = x0
        x0 = x1 - q * x0
        x1 = t
    if (x1 < 0):
        x1 += m0
    return x1


def derivative(x):
    return 3 * x * x


def Image(x, k):
    return x * x * x - k


def next_power(a_t, t, a1, prime, k):
    power_p = int(pow(prime, t + 1))
    next_a = (a_t - Image(a_t, k) * inv(derivative(a1), prime)) % power_p
    if (next_a < 0):
        next_a += power_p
        return next_a
    return next_a


def powerOfPrime(prime, power, k, a1):
    if (derivative(a1) != 0):
        a_t = a1
        for p in range(1, power):
            a_t = next_power(a_t, p, a1, prime, k)
        return a_t
    return -1


prime = 7
a1 = 3
power = 2
k = 3
print(powerOfPrime(prime, power, k, a1))

# Expected output:
6
# count of numbers with all digits same in a given range | python3 program to count the total numbers in the range l and r which have all the digit same ; function that count the total numbersprogram between l and r which have all the digit same ; length of r ; tmp has all digits as 1 ; for each multiple of tmp in range 1 to 9 , check if it present in range [ l , r ] ; increment the required count ; 

import math


def count_same_digit(L, R):
    tmp = 0
    ans = 0
    n = int(math .log10(R) + 1)
    for i in range(0, n):
        tmp = tmp * 10 + 1
        for j in range(1, 9):
            if (L <= (tmp * j) and (tmp * j) <= R):
                ans += 1
    return ans


L = 12
R = 68
print(count_same_digit(L, R))

# Expected output:
5
# minimum lcm of all pairs in a given array | python3 program to find the pair having minimum lcm ; function that return pair having minimum lcm ; find max element in the array as the gcd of two elements from the array can 't greater than max element. ; created a 2d array to store minimum two multiple of any particular i . ; we already found two smallest multiple ; iterating over all gcd ; iterating over its multiple ; if we already found the two smallest multiple of i ; choosing smallest two multiple ; calculating lcm ; return final answer ; 

import sys


def minLCM(arr, n):
    mx = 0
    for i in range(n):
        mx = max(mx, arr[i])
    mul = [[]for i in range(mx + 1)]
    for i in range(n):
        if (len(mul[arr[i]]) > 1):
            continue
        mul[arr[i]].append(arr[i])
    for i in range(1, mx + 1):
        for j in range(i + i, mx + 1, i):
            if (len(mul[i]) > 1):
                break
            for k in mul[j]:
                if (len(mul[i]) > 1):
                    break
                mul[i].append(k)
    ans = sys .maxsize
    for i in range(1, mx + 1):
        if (len(mul[i]) <= 1):
            continue
        a, b = mul[i][0], mul[i][1]
        lcm = (a * b) // i
        ans = min(ans, lcm)
    return ans


arr = [2, 4, 3, 6, 5]
n = len(arr)
print(minLCM(arr, n))

# Expected output:
4
# count of triplets of numbers 1 to n such that middle element is always largest | function to find number of triplets for given number n such that middle element is always greater than left and right side element . ; check if arrangement is possible or not ; else return total ways ; 
def findArrangement(N):
    if (N < 3):
        return 0
    return ((N) * (N - 1) * (N - 2)) // 3


N = 10
print(findArrangement(N))

# Expected output:
240
# lexicographically smallest array formed by at most one swap for every pair of adjacent indices | function to find the lexicographically smallest array ; maximum swaps possible ; hash to store swaps performed ; let current element be the minimum possible ; find actual position of the minimum element ; update minimum element and its position ; perform swaps if possible ; insert current swap in hash ; print the required array ; 
def findSmallestArray(A, n):
    count = n - 1
    mp = {''}
    for i in range(0, n):
        if (count <= 0):
            break
        mn = A[i]
        pos = i
        for j in range(i + 1, n):
            if (A[j] < mn):
                mn = A[j]
                pos = j
        while (pos > i and count > 0 and ((pos - 1, pos)not in mp)):
            mp .add((pos - 1, pos))
            A[pos], A[pos - 1] = A[pos - 1], A[pos]
            pos -= 1
            count -= 1
    for i in range(0, n):
        print(A[i], end=" ")


A = [2, 1, 4, 3, 6, 5]
n = len(A)
findSmallestArray(A, n)

# Expected output:
1 2 3 4 5 6
# sum of all perfect square divisors of numbers from 1 to n | python3 program to find the sum of all perfect square divisors of numbers from 1 to n ; function for finding inverse of a number iteratively here we will find the inverse of 6 , since it appears as denominator in the formula of sum of squares from 1 to n ; store the value of the inverse of 6 once as we don 't need to call the function again and again ; formula for finding the sum of first n squares ; no perfect square exists which is less than 4 ; starting from 2 , present value of start is denoted here as curstart ; finding end of the segment for which the contribution will be same ; using the above mentioned formula to find ans % mod ; now for mthe next iteration start will become end + 1 ; finally return the answer ; 

from math import *
MOD = 1000000007


def inv(a):
    o = 1
    p = MOD - 2
    while (p > 0):
        if (p % 2 == 1):
            o = (o * a) % MOD
        a = (a * a) % MOD
        p >>= 1
    return o


inv6 = inv(6)


def sumOfSquares(n):
    n %= MOD
    return (((n * (n + 1)) % MOD * (2 * n + 1)) % MOD * inv6) % MOD


def sums(n):
    if (n < 4):
        return 0
    curStart = 2
    ans = 0
    sqrtN = int(sqrt(n))
    while (curStart <= n // curStart):
        V = n // (curStart * curStart)
        end = int(sqrt(n // V))
        ans += ((n // (curStart * curStart) %
                 MOD * (sumOfSquares(end) + MOD - sumOfSquares(curStart - 1))) %
                MOD)
        if (ans >= MOD):
            ans -= MOD
        curStart = end + 1
    return ans

if __name__ == '__main__':
    Input = [5]
    for x in Input:
        print("sum of all perfect square ", end='')
        print("divisors from 1 to", x, "is: ", end='')
        print(x + sums(x))

# Expected output:
sum of all perfect square divisors from 1 to 5 is: 9
# check whether the binary equivalent of a number ends with "001" or not | function returns true if s1 is suffix of s2 ; function to check if binary equivalent of a number ends in "001" or not ; to store the binary number ; count used to store exponent value ; 
def isSuffix(s1, s2):
    n1 = len(s1)
    n2 = len(s2)
    if (n1 > n2):
        return False
    for i in range(n1):
        if (s1[n1 - i - 1] != s2[n2 - i - 1]):
            return False
    return True


def CheckBinaryEquivalent(N):
    B_Number = 0
    cnt = 0
    while (N != 0):
        rem = N % 2
        c = 10 ** cnt
        B_Number += rem * c
        N //= 2
        cnt += 1
    bin = str(B_Number)
    return isSuffix("001", bin)


if __name__ == "__main__":
    N = 9
    if (CheckBinaryEquivalent(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check whether the binary equivalent of a number ends with "001" or not | function to check if binary equivalent of a number ends in "001" or not ; to check if binary equivalent of a number ends in "001" or not ; 
def CheckBinaryEquivalent(N):
    return (N - 1) % 8 == 0


if __name__ == "__main__":
    N = 9
    if (CheckBinaryEquivalent(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# panarithmic numbers within a given range | python3 program to print practical numbers in given range ; function to compute divisors of a number ; vector to store divisors ; 1 will always be a divisor ; check if i is squareroot of a then only one time insert it in ans ; function to check that a number can be represented as summ of distinct divisor or not ; the value of subsett [ i ] [ j ] will be true if there is a subsett of sett [ 0. . j - 1 ] with summ equal to i ; if summ is 0 , then answer is true ; if summ is not 0 and sett is empty , then answer is false ; fill the subsett table in bottom up manner ; return the possibility of given summ ; function to check a number is practical or not ; vector to store divisors ; if all numbers can be represented as summ of unique divisors ; function to prpractical numbers in a range ; 

import math


def get_divisors(A):
    ans = []
    ans .append(1)
    for i in range(2, math .floor(math .sqrt(A)) + 1):
        if (A % i == 0):
            ans .append(i)
            if ((i * i) != A):
                ans .append(A // i)
    return ans


def summ_Possible(sett, summ):
    n = len(sett)
    subsett = [[0 for i in range(summ + 1)]for j in range(n + 1)]
    for i in range(n + 1):
        subsett[i][0] = True
    for i in range(1, summ + 1):
        subsett[0][i] = False
    for i in range(n + 1):
        for j in range(summ + 1):
            if (j < sett[i - 1]):
                subsett[i][j] = subsett[i - 1][j]
            if (j >= sett[i - 1]):
                subsett[i][j] = (subsett[i - 1][j]
                                 or subsett[i - 1][j - sett[i - 1]])
    return subsett[n][summ]


def Is_Practical(A):
    divisors = []
    divisors = get_divisors(A)
    for i in range(2, A):
        if (summ_Possible(divisors, i) == False):
            return False
    return True


def print_practica_No(A, B):
    for i in range(A, B + 1):
        if (Is_Practical(i)):
            print(i, end=" ")


A = 1
B = 100
print_practica_No(A, B)

# Expected output:
1 2 3 4 6 8 12 16 18 20 24 28 30 32 36 40 42 48 54 56 60 64 66 72 78 80 84 88 90 96 100
# maximize the division result of array using given operations | function to find the max result ; sort the array in descending order ; loop to divide in this order arr [ 0 ] / ( arr [ 1 ] / arr [ 2 ] / ... . arr [ n - 2 ] / arr [ n - 1 ] ) ; return the final result ; 
def maxDivision(arr, n):
    arr .sort(reverse=True)
    mxdiv = arr[1]
    for i in range(2, n):
        mxdiv = mxdiv / arr[i]
    return arr[0] / mxdiv


arr = [100, 1000, 10, 2]
n = len(arr)
print(maxDivision(arr, n))

# Expected output:
200.0
# print all distinct coprime sets possible from 1 to n | function to prall co - prime sets ; check if n is less than 4 then simply prall values till n ; for all the values of n > 3 ; check if n is even then every set will contain 2 adjacent elements up - to n ; if n is odd then every set will contain 2 adjacent element except the last set which will have last three elements ; last element for odd case ; 
def coPrimeSet(n):
    firstadj = 0
    secadj = 0
    if (n < 4):
        print("( ")
        for i in range(1, n + 1):
            print(i + ", ")
        print(")")
    else:
        if (n % 2 == 0):
            for i in range(0, n / 2):
                firstadj = 2 * i + 1
                secadj = 2 * i + 2
                print("(", firstadj, ", ", secadj, ")")
        else:
            for i in range(0, int(n / 2) - 1):
                firstadj = 2 * i + 1
                secadj = 2 * i + 2
                print("(", firstadj, ", ", secadj, ")")
            print("(", (n - 2), ", ", (n - 1), ", ", n, ")")


if __name__ == '__main__':
    n = 5
    coPrimeSet(n)

# Expected output:
( 1 ,  2 )
( 3 ,  4 ,  5 )
# count subarrays with sum equal to its xor value | function to count the number of subarrays such that xor of all the elements of that subarray is equal to sum of the elements ; maintain two pointers left and right ; iterating through the array ; calculate the window where the above condition is satisfied ; count will be ( right - left ) ; remove the previous element as it is already included ; 
def operation(arr, N):
    right = 0
    ans = 0
    num = 0
    for left in range(0, N):
        while (right < N and num + arr[right] == (num ^ arr[right])):
            num += arr[right]
            right += 1
        ans += right - left
        if (left == right):
            right += 1
        else:
            num -= arr[left]
    return ans


arr = [1, 2, 3, 4, 5]
N = len(arr)
print(operation(arr, N))

# Expected output:
7
# count of pairs in an array whose sum is prime | function for sieve of eratosthenes ; function to count total number of pairs of elements whose sum is prime ; 
def sieveOfEratosthenes(N):
    isPrime = [True for i in range(N + 1)]
    isPrime[0] = False
    isPrime[1] = False
    i = 2
    while ((i * i) <= N):
        if (isPrime[i]):
            j = 2
            while (i * j <= N):
                isPrime[i * j] = False
                j += 1
        i += 1
    return isPrime


def numPairsWithPrimeSum(arr, n):
    N = 2 * 1000000
    isPrime = sieveOfEratosthenes(N)
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            sum = arr[i] + arr[j]
            if (isPrime[sum]):
                count += 1
    return count


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    n = len(arr)
    print(numPairsWithPrimeSum(arr, n))

# Expected output:
5
# check if count of even divisors of n is equal to count of odd divisors | function to check if count of even and odd divisors are equal ; if ( n - 2 ) % 4 is an integer , then return true else return false ; 
def divisorsSame(n):
    return (n - 2) % 4 == 0


N = 6
if (divisorsSame(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# nth term of a sequence formed by sum of current term with product of its largest and smallest digit | function to find integer ; because 1 st integer is k itself ; initialize min_d and max_d ; updating min_d and max_d ; break if min digit is 0 ; 
def find(K, N):
    N = N - 1
    for i in range(0, N):
        curr_term = K
        min_d = 9
        max_d = 0
        while curr_term > 0:
            r = int(curr_term % 10)
            min_d = min(min_d, r)
            max_d = max(max_d, r)
            curr_term = int(curr_term / 10)
        if min_d == 0:
            break
        K = K + min_d * max_d
        return K


K = 487
N = 2
print(find(K, N))

# Expected output:
519
# count of subarrays whose sum is a perfect square | python3 code for the above approach . ; function to find count of subarrays whose sum is a perfect square . ; to search for index with ( current prefix sum - j * j ) ; storing the prefix sum ; used to track the minimum value in prefixsum ; calculating the prefixsum and tracking the prefixmin ; below statement is used if array contains negative numbers ; counts the no of subarrays with perfect square sum ; as 0 is a perfect square , so we initialize 0 th index - key with value 1 ; here we count the perfect square subarray sum by searching if there is a prefix with sum = ( current prefixsum - ( sq * sq ) ) ; increasing our subarray count ; increasing the current prefixsum index value in map by 1 to count the other perfect squares while traversing further ; 

from collections import defaultdict


def countSubarrays(arr, n):
    mp = defaultdict(lambda: 0)
    prefixSum = [0] * n
    prefixMin = 0
    prefixSum[0] = arr[0]
    prefixMin = min(prefixMin, prefixSum[0])
    for i in range(1, n):
        prefixSum[i] = prefixSum[i - 1] + arr[i]
        prefixMin = min(prefixMin, prefixSum[i])
    countSubs = 0
    mp[0] = 1
    for i in range(n):
        j = 0
        while prefixSum[i] - j * j >= prefixMin:
            if prefixSum[i] - j * j in mp:
                countSubs += mp[prefixSum[i] - j * j]
            j += 1
        mp[prefixSum[i]] += 1
    return countSubs


arr = [2, 3, -5, 6, -7, 4]
n = len(arr)
ans = countSubarrays(arr, n)
print(ans)

# Expected output:
5
# find if two given quadratic equations have common roots or not | function to check if 2 quadratic equations have common roots or not . ; 
def checkSolution(a1, b1, c1, a2, b2, c2):
    return ((a1 / a2) == (b1 / b2) and (b1 / b2) == (c1 / c2))


a1, b1, c1 = 1, -5, 6
a2, b2, c2 = 2, -10, 12
if (checkSolution(a1, b1, c1, a2, b2, c2)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# check if roots of a quadratic equation are numerically equal but opposite in sign or not | function to find the required answer ; 
def checkSolution(a, b, c):
    if b == 0:
        print("Yes")
    else:
        print("No")


a = 2
b = 0
c = 2
checkSolution(a, b, c)

# Expected output:
Yes
# count of index pairs in array whose range product is a positive integer | python3 program to find the count of index pairs in the array positive range product ; condition if number of negative elements is even then increase even_count ; otherwise increase odd_count ; condition if current element is negative ; condition if number of negative elements is even then add even_count in answer ; otherwise add odd_count in answer ; 
def positiveProduct(arr, n):
    even_count = 0
    odd_count = 0
    total_count = 0
    ans = 0
    for i in range(n):
        if (total_count % 2 == 0):
            even_count += 1
        else:
            odd_count += 1
        if (arr[i] < 0):
            total_count += 1
        if (total_count % 2 == 0):
            ans += even_count
        else:
            ans += odd_count
    print(ans)


if __name__ == '__main__':
    A = [5, -3, 3, -1, 1]
    size = len(A)
    positiveProduct(A, size)

# Expected output:
7
# pair of integers having difference of their fifth power as x | python3 implementation to find a pair of integers a & b such that difference of fifth power is equal to the given number x ; function to find a pair of integers a & b such that difference of fifth power is equal to the given number x ; loop to choose every possible pair with in the range ; check if equation holds ; 

import math


def findPair(x):
    lim = 120
    for i in range(-lim, lim + 1):
        for j in range(-lim, lim + 1):
            if (math .pow(i, 5) - math .pow(j, 5) == x):
                print(i, end=' ')
                print(j, end='')
                return
    print("-1")


X = 33
findPair(X)

# Expected output:
1 -2
# sum of gcd of all numbers upto n with n itself | function to find sum of gcd of each numbers ; consider all prime factors of no . and subtract their multiples from result ; check if p is a prime factor ; if yes , then update no and result ; if no has a prime factor greater than math . sqrt ( n ) then at - most one such prime factor exists ; return the result ; finding gcd of pairs ; calculate the divisors ; return count of numbers from 1 to n with gcd d with n ; check if d1 and d2 are equal then skip this ; 
def getCount(d, n):
    no = n // d
    result = no
    for p in range(2, int(pow(no, 1 / 2)) + 1):
        if (no % p == 0):
            while (no % p == 0):
                no //= p
            result -= result // p
    if (no > 1):
        result -= result // no
    return result


def sumOfGCDofPairs(n):
    res = 0
    for i in range(1, int(pow(n, 1 / 2)) + 1):
        if (n % i == 0):
            d1 = i
            d2 = n // i
            res += d1 * getCount(d1, n)
            if (d1 != d2):
                res += d2 * getCount(d2, n)
    return res


if __name__ == '__main__':
    n = 12
    print(sumOfGCDofPairs(n))

# Expected output:
40
# check whether array represents a fibonacci series or not | returns true if a permutation of arr [ 0. . n - 1 ] can form a fibonacci series ; sort array ; after sorting , check if every element is equal to the sum of previous 2 elements ; 
def checkIsFibonacci(arr, n):
    if (n == 1 or n == 2):
        return True
    arr .sort()
    for i in range(2, n):
        if ((arr[i - 1] + arr[i - 2]) != arr[i]):
            return False
    return True


if __name__ == "__main__":
    arr = [8, 3, 5, 13]
    n = len(arr)
    if (checkIsFibonacci(arr, n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# last digit in a power of 2 | python3 program to find last digit in a power of 2. ; corner case ; find the shift in current cycle and return value accordingly ; 
def lastDigit2PowerN(n):
    if n == 0:
        return 1
    elif n % 4 == 1:
        return 2
    elif n % 4 == 2:
        return 4
    elif n % 4 == 3:
        return 8
    else:
        return 6


for n in range(20):
    print(lastDigit2PowerN(n), end=" ")

# Expected output:
1 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8
# sum of all subsequences of length k | function to find ncr ; function that returns factorial of n ; function for finding sum of all k length subsequences ; calculate the sum of array ; calculate nck ; return the final result ; 
def nCr(n, r):
    return fact(n) / (fact(r) * fact(n - r))


def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
    return res


def sumSubsequences(arr, n, k):
    sum = 0
    for i in range(0, n):
        sum = sum + arr[i]
    kLengthSubSequence = nCr(n, k)
    ans = sum * ((k * kLengthSubSequence) / n)
    return ans


arr = [7, 8, 9, 2]
k = 2
n = len(arr)
print(sumSubsequences(arr, n, k))

# Expected output:
78.0
# sum of first k numbers which are not divisible by n | function to find the sum ; find the last multiple of n ; find the k - th non - multiple of n ; calculate the sum of all elements from 1 to val ; calculate the sum of all multiples of n between 1 to val ; 
def findSum(n, k):
    val = (k // (n - 1)) * n
    rem = k % (n - 1)
    if (rem == 0):
        val = val - 1
    else:
        val = val + rem
    sum = (val * (val + 1)) // 2
    x = k // (n - 1)
    sum_of_multiples = (x * (x + 1) * n) // 2
    sum -= sum_of_multiples
    return sum


n = 7
k = 13
print(findSum(n, k))

# Expected output:
99
# count of the non | python3 program to find count of non - prime divisors of given number ; function to factors of the given number ; loop to find the divisors of the number 2 ; loop to find the divisors of the given number upto sqrt ( n ) ; condition to check if the rest number is also a prime number ; function to find the non - prime divisors of the given number ; loop to count the number of the total divisors of given number ; 

from math import sqrt


def getFactorization(x):
    count = 0
    v = []
    while (x % 2 == 0):
        count += 1
        x = x // 2
    if (count != 0):
        v .append(count)
    for i in range(3, int(sqrt(x)) + 12):
        count = 0
        while (x % i == 0):
            count += 1
            x //= i
        if (count != 0):
            v .append(count)
    if (x > 1):
        v .append(1)
    return v


def nonPrimeDivisors(N):
    v = getFactorization(N)
    ret = 1
    for i in range(len(v)):
        ret = ret * (v[i] + 1)
    ret = ret - len(v)
    return ret


if __name__ == '__main__':
    N = 8
    print(nonPrimeDivisors(N))

# Expected output:
3