giulio98/xlcost-single-prompt huggingface dataset:
# check if a number can be expressed as 2 ^ x + 2 ^ y | utility function to check if a number is power of 2 or not ; utility function to determine the value of previous power of 2 ; function to check if n can be expressed as 2 ^ x + 2 ^ y or not ; if value of n is 0 or 1 it can not be expressed as 2 ^ x + 2 ^ y ; if n is power of two then it can be expressed as sum of 2 ^ x + 2 ^ y ; if the remainder after subtracting previous power of 2 is also a power of 2 then it can be expressed as 2 ^ x + 2 ^ y ; 
def isPowerOfTwo(n):
    return (n and (not (n & (n - 1))))


def previousPowerOfTwo(n):
    while (n & n - 1):
        n = n & n - 1
    return n


def checkSum(n):
    if (n == 0 or n == 1):
        return False
    elif (isPowerOfTwo(n)):
        print(n // 2, n // 2)
        return True
    else:
        x = previousPowerOfTwo(n)
        y = n - x
        if (isPowerOfTwo(y)):
            print(x, y)
            return True
        else:
            return False


n1 = 20
if (checkSum(n1)):
    print("No")
n2 = 11
if (checkSum(n2)):
    print("No")

# Expected output:
16 4
No
# 10 's complement of a decimal number | python3 program to find 10 's complement ; function to find 10 's complement ; calculating total digits in num ; restore num ; calculate 10 's complement ; 

import math


def complement(num):
    i = 0
    len = 0
    comp = 0
    temp = num
    while (1):
        len += 1
        num = int(num / 10)
        if (abs(num) == 0):
            break
    num = temp
    comp = math .pow(10, len) - num
    return int(comp)


print(complement(25))
print(complement(456))

# Expected output:
75
544
# program to find hcf ( highest common factor ) of 2 numbers | recursive function to return gcd of a and b ; everything divides 0 ; base case ; a is greater ; 
def gcd(a, b):
    if (a == 0 and b == 0):
        return 0
    if (a == 0):
        return b
    if (b == 0):
        return a
    if (a == b):
        return a
    if (a > b):
        return gcd(a - b, b)
    return gcd(a, b - a)


a = 98
b = 56
if (gcd(a, b)):
    print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
    print('not found')

# Expected output:
GCD of 98 and 56 is 14
# number of sub arrays with odd sum | function to find number of subarrays with odd sum ; ' odd ' stores number of odd numbers upto ith index ' c _ odd ' stores number of odd sum subarrays starting at ith index ' result ' stores the number of odd sum subarrays ; first find number of odd sum subarrays starting at 0 th index ; find number of odd sum subarrays starting at ith index add to result ; 
def countOddSum(a, n):
    c_odd = 0
    result = 0
    odd = False
    for i in range(n):
        if (a[i] % 2 == 1):
            if (odd):
                odd = False
            else:
                odd = True
        if (odd):
            c_odd += 1
    for i in range(n):
        result += c_odd
        if (a[i] % 2 == 1):
            c_odd = (n - i - c_odd)
    return result


if __name__ == '__main__':
    ar = [5, 4, 4, 5, 1, 3]
    n = len(ar)
    print("The Number of Subarrays" + "with odd sum is ", countOddSum(ar, n))

# Expected output:
The Number of Subarrayswith odd sum is  12
# calculating n | python program to find n - th real root of x ; initialize boundary values ; used for taking approximations of the answer ; do binary search ; 
def findNthRoot(x, n):
    x = float(x)
    n = int(n)
    if (x >= 0 and x <= 1):
        low = x
        high = 1
    else:
        low = 1
        high = x
    epsilon = 0.00000001
    guess = (low + high) / 2
    while abs(guess ** n - x) >= epsilon:
        if guess ** n > x:
            high = guess
        else:
            low = guess
        guess = (low + high) / 2
    print(guess)


x = 5
n = 2
findNthRoot(x, n)

# Expected output:
2.2360679768025875
# sum of all elements up to nth row in a pascal triangle | function to find sum of all elements upto nth row . ; initialize sum with 0 ; loop to calculate power of 2 upto n and add them ; 
def calculateSum(n):
    sum = 0
    for row in range(n):
        sum = sum + (1 << row)
    return sum


n = 10
print("Sum of all elements:", calculateSum(n))

# Expected output:
Sum of all elements: 1023
# number of sequences which has head at alternate positions to the right of the first head | function to calculate total sequences possible ; value of n is even ; value of n is odd ; 
def findAllSequence(N):
    if (N % 2 == 0):
        return (pow(2, N / 2 + 1) + pow(2, N / 2) - 2)
    else:
        return (pow(2, (N + 1) / 2) + pow(2, (N + 1) / 2) - 2)


N = 2
print(int(findAllSequence(N)))

# Expected output:
4
# number of ones in the smallest repunit | function to find number of 1 s in smallest repunit multiple of the number ; to store number of 1 s in smallest repunit multiple of the number . ; initialize rem with 1 ; run loop until rem becomes zero ; rem * 10 + 1 here represents the repunit modulo n ; when remainder becomes 0 return count ; 
def countOnes(n):
    count = 1
    rem = 1
    while (rem != 0):
        rem = (rem * 10 + 1) % n
        count = count + 1
    return count


n = 13
print(countOnes(n))

# Expected output:
6
# largest of two distinct numbers without using any conditional statements or operators | function to find the largest number ; 
def largestNum(a, b):
    return a * (bool)(a // b) + b * (bool)(b // a)


a = 22
b = 1231
print(largestNum(a, b))

# Expected output:
1231
# number of distinct meeting points on a circular road | python3 program to find number of distinct point of meet on a circular road ; returns the number of distinct meeting points . ; find the relative speed . ; convert the negative value to positive . ; 

import math


def numberOfmeet(a, b):
    ans = 0
    if (a > b):
        ans = a - b
    else:
        ans = b - a
    if (a < 0):
        a = a * (-1)
    if (b < 0):
        b = b * (-1)
    return int(ans / math .gcd(a, b))


a = 1
b = -1
print(numberOfmeet(a, b))

# Expected output:
2
# minimum number of square free divisors | python 3 program to find the minimum number of square free divisors ; initializing max with sqrt ( 10 ^ 6 ) ; 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 ; print all prime numbers ; this function returns the minimum number of square free divisors ; precomputing prime factors ; holds max of max power of all prime factors ; holds the max power of current prime factor ; if number itself is prime , it will be included as answer and thus minimum required answer is 1 ; 

from math import sqrt
MAX = 1005


def SieveOfEratosthenes(primes):
    prime = [True for i in range(MAX)]
    for p in range(2, int(sqrt(MAX)) + 1, 1):
        if (prime[p]):
            for i in range(p * 2, MAX, p):
                prime[i] = False
    for p in range(2, MAX, 1):
        if (prime[p]):
            primes .append(p)
    return primes


def minimumSquareFreeDivisors(N):
    prime = []
    primes = []
    primes = SieveOfEratosthenes(prime)
    max_count = 0
    i = 0
    while (len(primes) and primes[i] * primes[i] <= N):
        if (N % primes[i] == 0):
            tmp = 0
            while (N % primes[i] == 0):
                tmp += 1
                N /= primes[i]
            max_count = max(max_count, tmp)
        i += 1
    if (max_count == 0):
        max_count = 1
    return max_count


if __name__ == '__main__':
    N = 24
    print(
        "Minimum Number of Square Free Divisors is",
        minimumSquareFreeDivisors(N))
    N = 6
    print(
        "Minimum Number of Square Free Divisors is",
        minimumSquareFreeDivisors(N))

# Expected output:
Minimum Number of Square Free Divisors is 3
Minimum Number of Square Free Divisors is 1
# subsequence of size k with maximum possible gcd | python 3 program to find subsequence of size k with maximum possible gcd . ; function to find gcd of sub sequence of size k with max gcd in the array ; computing highest element ; array to store the count of divisors i . e . potential gcds ; iterating over every element ; calculating all the divisors ; divisor found ; incrementing count for divisor ; element / divisor is also a divisor checking if both divisors are not same ; checking the highest potential gcd ; if this divisor can divide at least k numbers , it is a gcd of at least one sub sequence of size k ; 

import math


def findMaxGCD(arr, n, k):
    high = max(arr)
    divisors = [0] * (high + 1)
    for i in range(n):
        for j in range(1, int(math .sqrt(arr[i])) + 1):
            if (arr[i] % j == 0):
                divisors[j] += 1
                if (j != arr[i] // j):
                    divisors[arr[i] // j] += 1
    for i in range(high, 0, -1):
        if (divisors[i] >= k):
            return i


if __name__ == "__main__":
    arr = [1, 2, 4, 8, 8, 12]
    k = 3
    n = len(arr)
    print(findMaxGCD(arr, n, k))

# Expected output:
4
# system of linear equations in three variables using cramer 's rule | this functions finds the determinant of matrix ; this function finds the solution of system of linear equations using cramer 's rule ; matrix d using coeff as given in cramer 's rule ; matrix d1 using coeff as given in cramer 's rule ; matrix d2 using coeff as given in cramer 's rule ; matrix d3 using coeff as given in cramer 's rule ; calculating determinant of matrices d , d1 , d2 , d3 ; case 1 ; coeff have a unique solution . apply cramer 's rule ; calculating z using cramer 's rule ; case 2 ; 
def determinantOfMatrix(mat):
    ans = (mat[0][0] *
           (mat[1][1] *
            mat[2][2] -
            mat[2][1] *
            mat[1][2]) -
           mat[0][1] *
           (mat[1][0] *
            mat[2][2] -
            mat[1][2] *
            mat[2][0]) +
           mat[0][2] *
           (mat[1][0] *
            mat[2][1] -
            mat[1][1] *
            mat[2][0]))
    return ans


def findSolution(coeff):
    d = [[coeff[0][0], coeff[0][1], coeff[0][2]], [coeff[1][0], coeff[1]
                                                   [1], coeff[1][2]], [coeff[2][0], coeff[2][1], coeff[2][2]]]
    d1 = [[coeff[0][3], coeff[0][1], coeff[0][2]], [coeff[1][3], coeff[1]
                                                    [1], coeff[1][2]], [coeff[2][3], coeff[2][1], coeff[2][2]]]
    d2 = [[coeff[0][0], coeff[0][3], coeff[0][2]], [coeff[1][0], coeff[1]
                                                    [3], coeff[1][2]], [coeff[2][0], coeff[2][3], coeff[2][2]]]
    d3 = [[coeff[0][0], coeff[0][1], coeff[0][3]], [coeff[1][0], coeff[1]
                                                    [1], coeff[1][3]], [coeff[2][0], coeff[2][1], coeff[2][3]]]
    D = determinantOfMatrix(d)
    D1 = determinantOfMatrix(d1)
    D2 = determinantOfMatrix(d2)
    D3 = determinantOfMatrix(d3)
    print("D is : ", D)
    print("D1 is : ", D1)
    print("D2 is : ", D2)
    print("D3 is : ", D3)
    if (D != 0):
        x = D1 / D
        y = D2 / D
        z = D3 / D
        print("Value of x is : ", x)
        print("Value of y is : ", y)
        print("Value of z is : ", z)
    else:
        if (D1 == 0 and D2 == 0 and D3 == 0):
            print("Infinite solutions")
        elif (D1 != 0 or D2 != 0 or D3 != 0):
            print("No solutions")


if __name__ == "__main__":
    coeff = [[2, -1, 3, 9], [1, 1, 1, 6], [1, -1, 1, 2]]
    findSolution(coeff)

# Expected output:
D is :  -2
D1 is :  -2
D2 is :  -4
D3 is :  -6
Value of x is :  1.0
Value of y is :  2.0
Value of z is :  3.0
# find larger of x ^ y and y ^ x | python3 program to print greater of x ^ y and y ^ x ; 

import math


def printGreater(x, y):
    X = y * math .log(x)
    Y = x * math .log(y)
    if (abs(X - Y) < 1e-9):
        print("Equal")
    elif (X > Y):
        print(x, "^", y)
    else:
        print(y, "^", x)


x = 5
y = 8
printGreater(x, y)

# Expected output:
5 ^ 8
# n | function to print nth term of series ; 
def sumOfSeries(n):
    return n * (n + 1) * (6 * n * n * n + 9 * n * n + n - 1) / 30


n = 4
print(sumOfSeries(n))

# Expected output:
354.0
# product of first n factorials | to compute ( a * b ) % mod ; res = 0 initialize result ; if b is odd , add ' a ' to result ; multiply ' a ' with 2 ; divide b by 2 ; return result ; this function computes factorials and product by using above function i . e . modular multiplication ; initialize product and fact with 1 ; ith factorial ; product of first i factorials ; if at any iteration , product becomes divisible by mod , simply return 0 ; 
def mulmod(a, b, mod):
    a = a % mod
    res = 0
    while (b > 0):
        if (b % 2 == 1):
            res = (res + a) % mod
        a = (a * 2) % mod
        b //= 2
    return res % mod


def findProduct(N):
    product = 1
    fact = 1
    MOD = 1e9 + 7
    for i in range(1, N + 1):
        fact = mulmod(fact, i, MOD)
        product = mulmod(product, fact, MOD)
        if not product:
            return 0
    return int(product)


N = 3
print(findProduct(N))
N = 5
print(findProduct(N))

# Expected output:
12
34560
# check if sum of divisors of two numbers are same | python3 program to find if two numbers are equivalent or not ; function to calculate sum of all proper divisors num -- > given natural number ; to store sum of divisors ; find all divisors and add them ; function to check if both numbers are equivalent or not ; 

import math


def divSum(n):
    sum = 1
    i = 2
    while (i * i <= n):
        if (n % i == 0):
            sum = (sum + i + math .floor(n / i))
        i += 1
    return sum


def areEquivalent(num1, num2):
    return divSum(num1) == divSum(num2)


num1 = 559
num2 = 703
if (areEquivalent(num1, num2)):
    print("Equivalent")
else:
    print("Not Equivalent")

# Expected output:
Equivalent
# dodecahedral number | function to calculate dodecahedral number ; formula to calculate nth dodecahedral number ; 
def dodecahedral_num(n):
    return n * (3 * n - 1) * (3 * n - 2) // 2


n = 5
print("%sth Dodecahedral number :" % n, dodecahedral_num(n))

# Expected output:
5th Dodecahedral number : 455
# count of divisors having more set bits than quotient on dividing n | python3 program to find number of divisors which on integer division produce quotient having less set bit than divisor ; return the count of set bit . ; check if q and d have same number of set bit . ; binary search to find the point at which number of set in q is less than or equal to d . ; while left index is less than right index ; finding the middle . ; check if q and d have same number of set it or not . ; 

import math


def bit(x):
    ans = 0
    while (x):
        x /= 2
        ans = ans + 1
    return ans


def check(d, x):
    if (bit(x / d) <= bit(d)):
        return True
    return False


def bs(n):
    l = 1
    r = int(math .sqrt(n))
    while (l < r):
        m = int((l + r) / 2)
        if (check(m, n)):
            r = m
        else:
            l = m + 1
    if (check(l, n) == False):
        return math .floor(l + 1)
    else:
        return math .floor(l)


def countDivisor(n):
    return n - bs(n) + 1


n = 5
print(countDivisor(n))

# Expected output:
4
# check if two people starting from different points ever meet | python3 program to find if two people starting from different positions ever meet or not . ; if speed of a person at a position before other person is smaller , then return false . ; making sure that x1 is greater ; until one person crosses other ; first person taking one jump in each iteration ; second person taking one jump in each iteration ; 
def everMeet(x1, x2, v1, v2):
    if (x1 < x2 and v1 <= v2):
        return False
    if (x1 > x2 and v1 >= v2):
        return False
    if (x1 < x2):
        x1, x2 = x2, x1
        v1, v2 = v2, v1
    while (x1 >= x2):
        if (x1 == x2):
            return True
        x1 = x1 + v1
        x2 = x2 + v2
    return False


x1 = 5
v1 = 8
x2 = 4
v2 = 7
if (everMeet(x1, x2, v1, v2)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# divisibility by 3 where each digit is the sum of all prefix digits modulo 10 | function to check the divisibility ; cycle ; no of residual terms ; if no of residue term = 0 ; if no of residue term = 1 ; if no of residue term = 2 ; if no of residue term = 3 ; sum of all digits ; divisibility check ; 
def check(k, d0, d1):
    s = ((2 * (d0 + d1)) %
         10 + (4 * (d0 + d1)) %
         10 + (8 * (d0 + d1)) %
         10 + (6 * (d0 + d1)) %
         10)
    a = (k - 3) % 4
    if (a == 0):
        x = 0
    elif (a == 1):
        x = (2 * (d0 + d1)) % 10
    elif (a == 2):
        x = ((2 * (d0 + d1)) % 10 + (4 * (d0 + d1)) % 10)
    elif (a == 3):
        x = ((2 * (d0 + d1)) %
             10 + (4 * (d0 + d1)) %
             10 + (8 * (d0 + d1)) %
             10)
    sum = d0 + d1 + ((k - 3) // 4) * s + x
    if (sum % 3 == 0):
        return "YES"
    else:
        return "NO"


if __name__ == '__main__':
    k = 13
    d0 = 8
    d1 = 1
    print(check(k, d0, d1))
    k = 5
    d0 = 3
    d1 = 4
    print(check(k, d0, d1))

# Expected output:
YES
NO
# program to print collatz sequence | python 3 program to print collatz sequence ; we simply follow steps while we do not reach 1 ; if n is odd ; if even ; print 1 at the end ; 
def printCollatz(n):
    while n != 1:
        print(n, end=' ')
        if n & 1:
            n = 3 * n + 1
        else:
            n = n // 2
    print(n)


printCollatz(6)

# Expected output:
6 3 10 5 16 8 4 2 1
# powers of 2 to required sum | python3 program to find the blocks for given number . ; converting the decimal number into its binary equivalent . ; displaying the output when the bit is '1' in binary equivalent of number . ; 
def block(x):
    v = []
    print("Blocks for %d : " % x, end="")
    while (x > 0):
        v .append(int(x % 2))
        x = int(x / 2)
    for i in range(0, len(v)):
        if (v[i] == 1):
            print(i, end="")
            if (i != len(v) - 1):
                print(", ", end="")
    print("")


block(71307)
block(1213)
block(29)
block(100)

# Expected output:
Blocks for 71307 : 0, 1, 3, 7, 9, 10, 12, 16
Blocks for 1213 : 0, 2, 3, 4, 5, 7, 10
Blocks for 29 : 0, 2, 3, 4
Blocks for 100 : 2, 5, 6
import math


def findNumberOfDigits(n, base):
    dig = (math.floor(math.log(n) / math.log(base)) + 1)
    print("The Number of digits of Number {} in base {} is {}".format(n, base, dig))


n = 1446
base = 7
findNumberOfDigits(n, base)

# Expected output:
The Number of digits of Number 1446 in base 7 is 4
# nesbitt 's inequality | python3 code to verify nesbitt 's inequality ; 3 parts of the inequality sum ; 
def isValidNesbitt(a, b, c):
    A = a / (b + c)
    B = b / (a + c)
    C = c / (a + b)
    inequality = A + B + C
    return (inequality >= 1.5)


a = 1.0
b = 2.0
c = 3.0
if (isValidNesbitt(a, b, c)):
    print(
        "Nesbitt's inequality satisfied.",
        " for real numbers ",
        a,
        ", ",
        b,
        ", ",
        c)
else:
    print("Not satisfied")

# Expected output:
Nesbitt's inequality satisfied.  for real numbers  1.0 ,  2.0 ,  3.0
# cube free numbers smaller than n | efficient python3 program to print all cube free numbers smaller than or equal to n . ; initialize all numbers as not cube free ; traverse through all possible cube roots ; if i itself is cube free ; mark all multiples of i as not cube free ; print all cube free numbers ; 
def printCubeFree(n):
    cubFree = [1] * (n + 1)
    i = 2
    while (i * i * i <= n):
        if (cubFree[i] == 1):
            multiple = 1
            while (i * i * i * multiple <= n):
                cubFree[i * i * i * multiple] = 0
                multiple += 1
        i += 1
    for i in range(2, n + 1):
        if (cubFree[i] == 1):
            print(i, end=" ")


if __name__ == "__main__":
    printCubeFree(20)

# Expected output:
2 3 4 5 6 7 9 10 11 12 13 14 15 17 18 19 20
# squared triangular number ( sum of cubes ) | python3 program to check if a given number is sum of cubes of natural numbers . ; returns root of n ( n + 1 ) / 2 = num if num is triangular ( or integerroot exists ) . else returns - 1. ; considering the equation n * ( n + 1 ) / 2 = num . the equation is : a ( n ^ 2 ) + bn + c = 0 "; ; find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; returns square root of x if it is perfect square . else returns - 1. ; find floating point value of square root of x . ; if square root is an integer ; function to find if the given number is sum of the cubes of first n natural numbers ; 

import math


def isTriangular(num):
    if (num < 0):
        return False
    c = (-2 * num)
    b = 1
    a = 1
    d = (b * b) - (4 * a * c)
    if (d < 0):
        return -1
    root1 = (-b + math .sqrt(d)) // (2 * a)
    root2 = (-b - math .sqrt(d)) // (2 * a)
    if (root1 > 0 and math .floor(root1) == root1):
        return root1
    if (root2 > 0 and math .floor(root2) == root2):
        return root2
    return -1


def isPerfectSquare(x):
    sr = math .sqrt(x)
    if ((sr - math .floor(sr)) == 0):
        return math .floor(sr)
    else:
        return -1


def findS(s):
    sr = isPerfectSquare(s)
    if (sr == -1):
        return -1
    return int(isTriangular(sr))


s = 9
n = findS(s)
if (n == -1):
    print("-1")
else:
    print(n)

# Expected output:
2
# smallest even digits number not less than n | function to return the answer when the first odd digit is 9 ; traverse towwars the left to find the non - 8 digit ; index digit ; if digit is not 8 , then break ; if on the left side of the '9' , no 8 is found then we return by adding a 2 and 0 's ; till non - 8 digit add all numbers ; if non - 8 is even or odd than add the next even . ; add 0 to right of 9 ; function to return the smallest number with all digits even ; convert the number to string to perform operations ; find out the first odd number ; if no odd numbers are there , than n is the answer ; if the odd number is 9 , than tricky case handles it ; add all digits till first odd ; increase the odd digit by 1 ; add 0 to the right of the odd number ; 
def trickyCase(s, index):
    index1 = -1
    for i in range(index - 1, -1, -1):
        digit = s[i] - '0'
        if (digit != 8):
            index1 = i
            break
    if (index1 == -1):
        return 2 * pow(10, len(s))
    num = 0
    for i in range(index1):
        num = num * 10 + (s[i] - '0')
    if (s[index1] % 2 == 0):
        num = num * 10 + (s[index1] - '0' + 2)
    else:
        num = num * 10 + (s[index1] - '0' + 1)
    for i in range(index1 + 1, len(s)):
        num = num * 10
    return num


def smallestNumber(n):
    num = 0
    s = ""
    duplicate = n
    while (n):
        s = chr(n % 10 + 48) + s
        n = int(n / 10)
    index = -1
    for i in range(len(s)):
        digit = ord(s[i]) - ord('0')
        if (digit & 1):
            index = i
            break
    if (index == -1):
        return duplicate
    if (s[index] == '9'):
        num = trickyCase(s, index)
        return num
    for i in range(index):
        num = num * 10 + ord(s[i]) - ord('0')
    num = num * 10 + (ord(s[index]) - ord('0') + 1)
    for i in range(index + 1, len(s)):
        num = num * 10
    return num


N = 2397
print(smallestNumber(N))

# Expected output:
2400
# n | python3 program to find n - th number with sum of digits as 10. ; find sum of digits in current no . ; if sum is 10 , we increment count ; if count becomes n , we return current number . ; 
def findNth(n):
    count = 0
    curr = 19
    while (True):
        sum = 0
        x = curr
        while (x > 0):
            sum = sum + x % 10
            x = int(x / 10)
        if (sum == 10):
            count += 1
        if (count == n):
            return curr
        curr += 9
    return -1


print(findNth(5))

# Expected output:
55
# sum of pairwise products | simple python3 program to find sum of given series . ; 
def findSum(n):
    sm = 0
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            sm = sm + i * j
    return sm


n = 5
print(findSum(n))

# Expected output:
140