giulio98/xlcost-single-prompt huggingface dataset:
# 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 ; 
def sum(n):
    rem = 0
    sum_of_digits = 0
    while (n > 0):
        rem = n % 10
        sum_of_digits += rem
        n = n // 10
    return sum_of_digits


def count(n):
    c = 0
    for i in range(n - 97, n + 1):
        a = sum(i)
        b = sum(a)
        if ((i + a + b) == n):
            c += 1
    return c


n = 9939
print(count(n))

# Expected output:
4
# 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 . ; 
def isPowerOfK(n, k):
    oneSeen = False
    while (n > 0):
        digit = n % k
        if (digit > 1):
            return False
        if (digit == 1):
            if (oneSeen):
                return False
            oneSeen = True
        n //= k
    return True


n = 64
k = 4
if (isPowerOfK(n, k)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# 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 ; 

MAX_DIGITS = 20


def isOctal(n):
    while (n):
        if ((n % 10) >= 8):
            return False
        else:
            n = int(n / 10)
    return True


def isPalindrome(n):
    divide = 8 if (isOctal(n) == False)else 10
    octal = []
    while (n != 0):
        octal .append(n % divide)
        n = int(n / divide)
    j = len(octal) - 1
    k = 0
    while (k <= j):
        if (octal[j] != octal[k]):
            return False
        j -= 1
        k += 1
    return True


if __name__ == '__main__':
    n = 97
    if (isPalindrome(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# 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 ; 
def printFactorialNums(n):
    fact = 1
    x = 2
    while fact <= n:
        print(fact, end=" ")
        fact = fact * x
        x += 1


n = 100
printFactorialNums(n)

# Expected output:
1 2 6 24
# 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 ; 
def sumDigitSquare(n):
    sq = 0
    while (n != 0):
        digit = n % 10
        sq += digit * digit
        n = n // 10
    return sq


def isHappy(n):
    s = set()
    s .add(n)
    while (True):
        if (n == 1):
            return True
        n = sumDigitSquare(n)
        if n in s:
            return False
        s .add(n)
    return false


n = 4
if (isHappy(n)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# 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 ; 

from math import sqrt


def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    k = int(sqrt(n)) + 1
    for i in range(5, k, 6):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
    return True


def isThreeDisctFactors(n):
    sq = int(sqrt(n))
    if (1 * sq * sq != n):
        return False
    if (isPrime(sq)):
        return True
    else:
        return False


if __name__ == '__main__':
    num = 9
    if (isThreeDisctFactors(num)):
        print("Yes")
    else:
        print("No")
    num = 15
    if (isThreeDisctFactors(num)):
        print("Yes")
    else:
        print("No")
    num = 12397923568441
    if (isThreeDisctFactors(num)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
No
No
# 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 ; 
def computeLastDigit(A, B):
    variable = 1
    if (A == B):
        return 1
    elif (B - A) >= 5:
        return 0
    else:
        for i in range(A + 1, B + 1):
            variable = (variable * (i % 10)) % 10
        return variable % 10


print(computeLastDigit(2632, 2634))

# Expected output:
2
# program for sum of arithmetic series | function to find sum of series . ; 
def sumOfAP(a, d, n):
    sum = 0
    i = 0
    while i < n:
        sum = sum + a
        a = a + d
        i = i + 1
    return sum


n = 20
a = 2.5
d = 1.5
print(sumOfAP(a, d, n))

# Expected output:
335.0
# 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 ; 

M = 1000000007


def multiplyFactors(n):
    prod = 1
    i = 1
    while i * i <= n:
        if (n % i == 0):
            if (n / i == i):
                prod = (prod * i) % M
            else:
                prod = (prod * i) % M
                prod = (prod * n / i) % M
        i = i + 1
    return prod


n = 12
print(multiplyFactors(n))

# Expected output:
1728.0
# 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 ; 
def printTrib(n):
    if (n < 1):
        return
    first = 0
    second = 0
    third = 1
    print(first, " ", end="")
    if (n > 1):
        print(second, " ", end="")
    if (n > 2):
        print(second, " ", end="")
    for i in range(3, n):
        curr = first + second + third
        first = second
        second = third
        third = curr
        print(curr, " ", end="")


n = 10
printTrib(n)

# Expected output:
0  0  0  1  2  4  7  13  24  44
# 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 ; 

from math import log2


def countTrailingZeroes(N):
    res = int(log2(N ^ (N - 1)))
    return res if res >= 0 else 0


if __name__ == '__main__':
    N = 12
    print(countTrailingZeroes(N))

# Expected output:
2
# 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 ; 
def calcScr(arr):
    ans = 0
    for i in range(len(arr)):
        ans += (i ^ arr[i])
    return ans


def getMax(arr, ans, chosen, N):
    if len(arr) == N:
        ans = max(ans, calcScr(arr))
        return ans
    for i in range(N):
        if chosen[i]:
            continue
        chosen[i] = True
        arr .append(i)
        ans = getMax(arr, ans, chosen, N)
        chosen[i] = False
        arr .pop()
    return ans


N = 2
arr = []
ans = -1
chosen = [False for i in range(N)]
ans = getMax(arr, ans, chosen, N)
print(ans)

# Expected output:
2
# 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 ; 
def additiveCongruentialMethod(Xo, m, c, randomNums, noOfRandomNums):
    randomNums[0] = Xo
    for i in range(1, noOfRandomNums):
        randomNums[i] = (randomNums[i - 1] + c) % m


if __name__ == '__main__':
    Xo = 3
    m = 15
    c = 2
    noOfRandomNums = 20
    randomNums = [0] * (noOfRandomNums)
    additiveCongruentialMethod(Xo, m, c, randomNums, noOfRandomNums)
    for i in randomNums:
        print(i, end=" ")

# Expected output:
3 5 7 9 11 13 0 2 4 6 8 10 12 14 1 3 5 7 9 11
# 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 ; ; 

MOD = int(1e9) + 9
MAXN = int(1e5) + 5
BIT = [0 for _ in range(MAXN)]


def power(x, y):
    if x == 0:
        return 0
    ans = 1
    while y > 0:
        if y % 2 == 1:
            ans = (ans * x) % MOD
        x = (x * x) % MOD
        y = y // 2
    return ans


def updateBIT(idx, val):
    while idx < MAXN:
        BIT[idx] += val
        idx += idx & (-idx)


def queryBIT(idx):
    ans = 0
    while idx > 0:
        ans += BIT[idx]
        idx -= idx & (-idx)
    return ans


def numWays(arr):
    mx = 0
    for i in arr:
        updateBIT(i, 1)
        mx = max(mx, i)
    ans = 1
    for i in range(2, mx + 1):
        factors = []
        for j in range(1, i + 1):
            if j * j > i:
                break
            if i % j == 0:
                factors .append(j)
                if i // j != j:
                    factors .append(i // j)
        factors .sort()
        factors .reverse()
        cnt = 1
        prev = 0
        for j in range(len(factors)):
            remFactors = len(factors) - j
            indexes = len(arr) - queryBIT(factors[j] - 1)
            cnt = (cnt * power(remFactors, indexes - prev)) % MOD
            prev = max(prev, indexes)
        factors .remove(factors[0])
        toSubtract = 1
        prev = 0
        for j in range(len(factors)):
            remFactors = len(factors) - j
            indexes = len(arr) - queryBIT(factors[j] - 1)
            toSubtract = (toSubtract * power(remFactors, indexes - prev))
            prev = max(prev, indexes)
        ans = (ans + cnt - toSubtract + MOD) % MOD
    return ans


if __name__ == "__main__":
    arr = [1, 4, 3, 2]
    ans = numWays(arr)
    print(ans)

# Expected output:
15
# second decagonal numbers | function to find n - th term in the series ; 
def findNthTerm(n):
    print(n * (4 * n + 3))


N = 4
findNthTerm(N)

# Expected output:
76
# 65537 | function to find the nth 65537 - gon number ; 
def gonNum65537(n):
    return (65535 * n * n - 65533 * n) // 2


n = 3
print(gonNum65537(n))

# Expected output:
196608
# hexacontatetragon numbers | function to find the nth hexacontatetragon number ; 
def HexacontatetragonNum(n):
    return (62 * n * n - 60 * n) / 2


n = 3
print(HexacontatetragonNum(n))

# Expected output:
189.0
# icosikaipentagon number | function to find the n - th icosikaipentagon number ; 
def icosikaipentagonNum(N):
    return (23 * N * N - 21 * N) // 2


n = 3
print("3rd icosikaipentagon Number is ", icosikaipentagonNum(n))

# Expected output:
3rd icosikaipentagon Number is  72
# chiliagon number | finding the nth chiliagon number ; 
def chiliagonNum(n):
    return (998 * n * n - 996 * n) // 2


n = 3
print("3rd chiliagon Number is = ", chiliagonNum(n))

# Expected output:
3rd chiliagon Number is =  2997
# pentacontagon number | finding the nth pentacontagon number ; 
def pentacontagonNum(n):
    return (48 * n * n - 46 * n) // 2


n = 3
print("3rd pentacontagon Number is = ", pentacontagonNum(n))

# Expected output:
3rd pentacontagon Number is =  147
# array value by repeatedly replacing max 2 elements with their absolute difference | python3 program to find the array value by repeatedly replacing max 2 elements with their absolute difference ; function that return last value of array ; build a binary max_heap . ; multipying by - 1 for max heap ; for max 2 elements ; iterate until queue is not empty ; if only 1 element is left ; return the last remaining value ; check that difference is non zero ; 

from queue import PriorityQueue


def lastElement(arr):
    pq = PriorityQueue()
    for i in range(len(arr)):
        pq .put(-1 * arr[i])
    m1 = 0
    m2 = 0
    while not pq .empty():
        if pq .qsize() == 1:
            return -1 * pq .get()
        else:
            m1 = -1 * pq .get()
            m2 = -1 * pq .get()
        if m1 != m2:
            pq .put(-1 * abs(m1 - m2))
    return 0


arr = [2, 7, 4, 1, 8, 1, 1]
print(lastElement(arr))

# Expected output:
0
# number formed by adding product of its max and min digit k times | function to find the formed number ; k -= 1 m ( 1 ) = n ; check if minimum digit is 0 ; function that returns the product of maximum and minimum digit of n number . ; find the last digit . ; moves to next digit ; 
def prod_of_max_min(n):
    largest = 0
    smallest = 10
    while (n != 0):
        r = n % 10
        largest = max(r, largest)
        smallest = min(r, smallest)
        n = n // 10
    return largest * smallest


def formed_no(N, K):
    if (K == 1):
        return N
    answer = N
    while (K != 0):
        a_current = prod_of_max_min(answer)
        if (a_current == 0):
            break
        answer += a_current
        K -= 1
    return answer


if __name__ == "__main__":
    N = 487
    K = 100000000
    print(formed_no(N, K))

# Expected output:
950
# logarithm tricks for competitive programming | python3 implementation count the number of digits in a number ; function to count the number of digits in a number ; 

import math


def countDigit(n):
    return (math .floor(math .log10(n) + 1))


if __name__ == "__main__":
    n = 80
    print(countDigit(n))

# Expected output:
2
# program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | function to find the sum of the series and print n terms of the given series ; first term ; loop to print the n terms of the series and find their sum ; 
def sum(x, n):
    total = 1.0
    multi = x
    print(1, end=" ")
    for i in range(1, n):
        total = total + multi
        print('%.1f' % multi, end=" ")
        multi = multi * x
    print('')
    return total


x = 2
n = 5
print('%.2f' % sum(x, n))

# Expected output:
1 2.0 4.0 8.0 16.0 
31.00
# find the remainder when n is divided by 4 using bitwise and operator | function to find the remainder ; bitwise and with 3 ; return x ; 
def findRemainder(n):
    x = n & 3
    return x


if __name__ == '__main__':
    N = 43
    ans = findRemainder(N)
    print(ans)

# Expected output:
3
# find all autobiographical numbers with given number of digits | python implementation to find autobiographical numbers with length n ; function to return if the number is autobiographical or not ; converting the integer number to string ; extracting each character from each index one by one and converting into an integer ; initialize count as 0 ; check if it is equal to the index i if true then increment the count ; it is an autobiographical number ; return false if the count and the index number are not equal ; function to print autobiographical number with given number of digits ; left boundary of interval ; right boundary of interval ; flag = 0 implies that the number is not an autobiographical no . ; 

from math import pow


def isAutoBio(num):
    autoStr = str(num)
    for i in range(0, len(autoStr)):
        index = int(autoStr[i])
        cnt = 0
        for j in range(0, len(autoStr)):
            number = int(autoStr[j])
            if number == i:
                cnt += 1
        if cnt != index:
            return False
    return True


def findAutoBios(n):
    low = int(pow(10, n - 1))
    high = int(pow(10, n) - 1)
    flag = 0
    for i in range(low, high + 1):
        if isAutoBio(i):
            flag = 1
            print(i, end=', ')
    if flag == 0:
        print("There is no Autobiographical Number with " + str(n) + " digits")


if __name__ == "__main__":
    N = 0
    findAutoBios(N)
    N = 4
    findAutoBios(N)

# Expected output:
There is no Autobiographical Number with 0 digits
1210, 2020,
# check whether the number can be made palindromic after adding k | function to check whether a number is a palindrome or not ; convert num to stringing ; comparing kth character from the beginning and n - kth character from the end . if all the characters match , then the number is a palindrome ; if all the above conditions satisfy , it means that the number is a palindrome ; 
def checkPalindrome(num):
    string = str(num)
    l = 0
    r = len(string) - 1
    while (l < r):
        if (string[l] != string[r]):
            print("No")
            return
        l = l + 1
        r = r - 1
    print("Yes")
    return


if __name__ == '__main__':
    n = 19
    k = 3
    checkPalindrome(n + k)

# Expected output:
Yes
# count of subsets with sum equal to x using recursion | recursive function to return the count of subsets with sum equal to the given value ; the recursion is stopped at n - th level where all the subsets of the given array have been checked ; incrementing the count if sum is equal to 0 and returning the count ; recursively calling the function for two cases either the element can be counted in the subset if the element is counted , then the remaining sum to be checked is sum - the selected element if the element is not included , then the remaining sum to be checked is the total sum ; 
def subsetSum(arr, n, i, sum, count):
    if (i == n):
        if (sum == 0):
            count += 1
        return count
    count = subsetSum(arr, n, i + 1, sum - arr[i], count)
    count = subsetSum(arr, n, i + 1, sum, count)
    return count


arr = [1, 2, 3, 4, 5]
sum = 10
n = len(arr)
print(subsetSum(arr, n, 0, sum, 0))

# Expected output:
3
# sum of the count of number of adjacent squares in an m x n grid | function to calculate the sum of all cells adjacent value ; 
def summ(m, n):
    return 8 * m * n - 6 * m - 6 * n + 4


m = 3
n = 2
print(summ(m, n))

# Expected output:
22
# find the volume of rectangular right wedge | function to return the volume of the rectangular right wedge ; 
def volumeRec(a, b, e, h):
    return (((b * h) / 6) * (2 * a + e))


if __name__ == "__main__":
    a = 2
    b = 5
    e = 5
    h = 6
    print("Volume = ", volumeRec(a, b, e, h))

# Expected output:
Volume =  45.0
# count squares with odd side length in chessboard | function to return the count of odd length squares possible ; to store the required count ; for all odd values of i ; add the count of possible squares of length i ; return the required count ; 
def count_square(n):
    count = 0
    for i in range(1, n + 1, 2):
        k = n - i + 1
        count += (k * k)
    return count


N = 8
print(count_square(N))

# Expected output:
120
# count of elements whose absolute difference with the sum of all the other elements is greater than k | function to return the number of anomalies ; to store the count of anomalies ; to store the sum of the array elements ; find the sum of the array elements ; count the anomalies ; 
def countAnomalies(arr, n, k):
    cnt = 0
    i, Sum = 0, 0
    for i in range(n):
        Sum += arr[i]
    for i in range(n):
        if (abs(arr[i] - (Sum - arr[i])) > k):
            cnt += 1
    return cnt


arr = [1, 3, 5]
n = len(arr)
k = 1
print(countAnomalies(arr, n, k))

# Expected output:
2
# find the number of integers x in range ( 1 , n ) for which x and x + 1 have same number of divisors | python3 implementation of the above approach ; to store number of divisors and prefix sum of such numbers ; function to find the number of integers 1 < x < n for which x and x + 1 have the same number of positive divisors ; count the number of divisors ; run a loop upto sqrt ( i ) ; if j is divisor of i ; if it is perfect square ; x and x + 1 have same number of positive divisors ; 

from math import sqrt
N = 100005
d = [0] * N
pre = [0] * N


def Positive_Divisors():
    for i in range(N):
        for j in range(1, int(sqrt(i)) + 1):
            if (i % j == 0):
                if (j * j == i):
                    d[i] += 1
                else:
                    d[i] += 2
    ans = 0
    for i in range(2, N):
        if (d[i] == d[i - 1]):
            ans += 1
        pre[i] = ans


if __name__ == "__main__":
    Positive_Divisors()
    n = 15
    print(pre[n])

# Expected output:
2
# length of the smallest number which is divisible by k and formed by using 1 's only | function to return length of the resultant number ; if k is a multiple of 2 or 5 ; generate all possible numbers 1 , 11 , 111 , 111 , ... , k 1 's ; if number is divisible by k then return the length ; 
def numLen(K):
    if (K % 2 == 0 or K % 5 == 0):
        return -1
    number = 0
    len = 1
    for len in range(1, K + 1):
        number = number * 10 + 1
        if ((number % K == 0)):
            return len
    return -1


K = 7
print(numLen(K))

# Expected output:
6
# find if the given number is present in the infinite sequence or not | function that returns true if the sequence will contain b ; 
def doesContainB(a, b, c):
    if (a == b):
        return True
    if ((b - a) * c > 0 and (b - a) % c == 0):
        return True
    return False


if __name__ == '__main__':
    a, b, c = 1, 7, 3
    if (doesContainB(a, b, c)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find a permutation of 2 n numbers such that the result of given expression is exactly 2 k | function to find the required permutation of first 2 * n natural numbers ; iterate in blocks of 2 ; we need more increments , so print in reverse order ; we have enough increments , so print in same order ; 
def printPermutation(n, k):
    for i in range(1, n + 1):
        x = 2 * i - 1
        y = 2 * i
        if (i <= k):
            print(y, x, end=" ")
        else:
            print(x, y, end=" ")


if __name__ == "__main__":
    n = 2
    k = 1
    printPermutation(n, k)

# Expected output:
2 1 3 4