giulio98/xlcost-single-prompt huggingface dataset:
# find maximum operations to reduce n to 1 | python3 program to find maximum number moves possible ; to store number of prime factors of each number ; function to find number of prime factors of each number ; if i is a prime number ; increase value by one from it 's preveious multiple ; make prefix sum this will be helpful for multiple test cases ; 

N = 1000005
primeFactors = [0] * N


def findPrimeFactors():
    for i in range(2, N):
        if (primeFactors[i] == 0):
            for j in range(i, N, i):
                primeFactors[j] = primeFactors[j // i] + 1
    for i in range(1, N):
        primeFactors[i] += primeFactors[i - 1]


if __name__ == "__main__":
    findPrimeFactors()
    a = 6
    b = 3
    print(primeFactors[a] - primeFactors[b])

# Expected output:
5
# smallest integer with digit sum m and multiple of n | python 3 implementation of the above approach ; function to return digit sum ; function to find out the smallest integer ; start of the iterator ( smallest multiple of n ) ; 

from math import floor, pow
import sys


def digitSum(n):
    ans = 0
    while (n):
        ans += n % 10
        n = int(n / 10)
    return ans


def findInt(n, m):
    minDigit = floor(m / 9)
    start = (int(pow(10, minDigit)) - int(pow(10, minDigit)) % n)
    while (start < sys .maxsize):
        if (digitSum(start) == m):
            return start
        else:
            start += n
    return -1


if __name__ == '__main__':
    n = 13
    m = 32
    print(findInt(n, m))

# Expected output:
8879
# maximum sum after repeatedly dividing n by a divisor | python 3 implementation of the above approach ; function to find the smallest divisor ; function to find the maximum sum ; 

from math import sqrt


def smallestDivisor(n):
    mx = int(sqrt(n))
    for i in range(2, mx + 1, 1):
        if (n % i == 0):
            return i
    return n


def maxSum(n):
    res = n
    while (n > 1):
        divi = smallestDivisor(n)
        n = int(n / divi)
        res += n
    return res


if __name__ == '__main__':
    n = 34
    print(maxSum(n))

# Expected output:
52
# make all elements of an array equal with the given operation | function that returns true if all the elements of the array can be made equal with the given operation ; to store the sum of the array elements and the maximum element from the array ; 
def isPossible(n, k, arr):
    sum = arr[0]
    maxVal = arr[0]
    for i in range(1, n):
        sum += arr[i]
        maxVal = max(maxVal, arr[i])
    if (int(maxVal) > int((sum + k) / n)):
        return False
    return True


if __name__ == '__main__':
    k = 8
    arr = [1, 2, 3, 4]
    n = len(arr)
    if (isPossible(n, k, arr)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# maximize the value of x + y + z such that ax + by + cz = n | python3 implementation of the approach ; function to return the maximum value of ( x + y + z ) such that ( ax + by + cz = n ) ; i represents possible values of a * x ; j represents possible values of b * y ; if z is an integer ; 

from math import *


def maxResult(n, a, b, c):
    maxVal = 0
    for i in range(0, n + 1, a):
        for j in range(0, n - i + 1, b):
            z = (n - (i + j)) / c
            if (floor(z) == ceil(z)):
                x = i // a
                y = j // b
                maxVal = max(maxVal, x + y + int(z))
    return maxVal


if __name__ == "__main__":
    n = 10
    a = 5
    b = 3
    c = 4
    print(maxResult(n, a, b, c))

# Expected output:
3
# make all numbers of an array equal | function that returns true if all the array elements can be made equal with the given operation ; divide number by 2 ; divide number by 3 ; 
def EqualNumbers(a, n):
    for i in range(0, n):
        while a[i] % 2 == 0:
            a[i] //= 2
        while a[i] % 3 == 0:
            a[i] //= 3
        if a[i] != a[0]:
            return False
    return True


if __name__ == "__main__":
    a = [50, 75, 150]
    n = len(a)
    if EqualNumbers(a, n):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# maximum gcd from given product of unknowns | python3 implementation of the approach ; function to return the required gcd ; count the number of times 2 divides p ; equivalent to p = p / 2 ; ; if 2 divides p ; check all the possible numbers that can divide p ; if n in the end is a prime number ; return the required gcd ; 

import math


def max_gcd(n, p):
    count = 0
    gcd = 1
    while (p % 2 == 0):
        p >>= 1
        count = count + 1
    if (count > 0):
        gcd = gcd * pow(2, count // n)
    for i in range(3, (int)(math .sqrt(p)), 2):
        count = 0
        while (p % i == 0):
            count = count + 1
            p = p // i
        if (count > 0):
            gcd = gcd * pow(i, count // n)
    if (p > 2):
        gcd = gcd * pow(p, 1 // n)
    return gcd


n = 3
p = 80
print(max_gcd(n, p))

# Expected output:
2
# minimum positive integer divisible by c and is not in range [ a , b ] | function to return the required number ; if doesn 't belong to the range  then c is the required number ; else get the next multiple of c starting from b + 1 ; 
def getMinNum(a, b, c):
    if (c < a or c > b):
        return c
    x = ((b // c) * c) + c
    return x


a, b, c = 2, 4, 4
print(getMinNum(a, b, c))

# Expected output:
8
# count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | function to return the count of required pairs ; special case ; number which will give the max value for ( ( n % i ) % j ) % n ; to store the maximum possible value of ( ( n % i ) % j ) % n ; count of possible pairs ; 
def countPairs(n):
    if (n == 2):
        return 4
    num = ((n // 2) + 1)
    max = n % num
    count = n - max
    return count


if __name__ == "__main__":
    n = 5
print(countPairs(n))

# Expected output:
3
# remove characters from a numeric string such that string becomes divisible by 8 | python3 program to remove digits from a numeric string such that the number becomes divisible by 8 ; function that return true if sub is a sub - sequence in s ; function to return a multiple of 8 formed after removing 0 or more characters from the given string ; iterate over all multiples of 8 ; if current multiple exists as a subsequence in the given string ; 

import math as mt


def checkSub(sub, s):
    j = 0
    for i in range(len(s)):
        if (sub[j] == s[i]):
            j += 1
    if j == int(len(sub)):
        return True
    else:
        return False


def getMultiple(s):
    for i in range(0, 10 ** 3, 8):
        if (checkSub(str(i), s)):
            return i
    return -1


s = "3454"
print(getMultiple(s))

# Expected output:
344
# program to check if a number is divisible by any of its digits | python implementation of above approach ; converting integer to string ; traversing the string ; if the number is divisible by digits then return yes ; if no digits are dividing the number then return no ; 
def getResult(n):
    st = str(n)
    for i in st:
        if (n % int(i) == 0):
            return 'Yes'
    return 'No'


n = 9876543
print(getResult(n))

# Expected output:
Yes
# program to find sum of harmonic series | python program to find sum of harmonic series using recursion ; base condition ; 
def sum(n):
    if n < 2:
        return 1
    else:
        return 1 / n + (sum(n - 1))


print(sum(8))
print(sum(10))

# Expected output:
2.7178571428571425
2.9289682539682538
# sum of p terms of an ap if mth and nth terms are given | python3 implementation of the above approach ; function to calculate the value of the ; calculate value of d using formula ; calculate value of a using formula ; return pair ; function to calculate value sum of first p numbers of the series ; first calculate value of a and d ; calculate the sum by using formula ; return the sum ; 

import math as mt


def findingValues(m, n, mth, nth):
    d = ((abs(mth - nth)) / abs((m - 1) - (n - 1)))
    a = mth - ((m - 1) * d)
    return a, d


def findSum(m, n, mth, nth, p):
    a, d = findingValues(m, n, mth, nth)
    Sum = (p * (2 * a + (p - 1) * d)) / 2
    return Sum


m = 6
n = 10
mTerm = 12
nTerm = 20
p = 5
print(findSum(m, n, mTerm, nTerm, p))

# Expected output:
30.0
# print all integers that are sum of powers of two given numbers | function to print powerful integers ; set is used to store distinct numbers in sorted order ; store all the powers of y < bound in a vector to avoid calculating them again and again ; x ^ i ; if num is within limits insert it into the set ; break out of the inner loop ; adding any number to it will be out of bounds ; increment i ; print the contents of the set ; 
def powerfulIntegers(x, y, bound):
    s = set()
    powersOfY = []
    powersOfY .append(1)
    i = y
    while i < bound and y != 1:
        powersOfY .append(i)
        i *= y
    i = 0
    while (True):
        xPowI = pow(x, i)
        for j in powersOfY:
            num = xPowI + j
            if (num <= bound):
                s .add(num)
            else:
                break
        if (xPowI >= bound or x == 1):
            break
        i += 1
    for itr in s:
        print(itr, end=" ")


if __name__ == "__main__":
    x = 2
    y = 3
    bound = 10
    powerfulIntegers(x, y, bound)

# Expected output:
2 3 4 5 7 9 10
# distribute n candies among k people | python3 code for better approach to distribute candies ; function to find out the number of candies every person received ; count number of complete turns ; get the last term ; stores the number of candies ; last term of last and current series ; sum of current and last series ; sum of current series only ; if sum of current is less than n ; else : individually distribute ; first term ; distribute candies till there ; candies available ; not available ; count the total candies ; print the total candies ; 

import math as mt


def candies(n, k):
    count = 0
    ind = 1
    arr = [0 for i in range(k)]
    while n > 0:
        f1 = (ind - 1) * k
        f2 = ind * k
        sum1 = (f1 * (f1 + 1)) // 2
        sum2 = (f2 * (f2 + 1)) // 2
        res = sum2 - sum1
        if (res <= n):
            count += 1
            n -= res
            ind += 1
            i = 0
            term = ((ind - 1) * k) + 1
            while (n > 0):
                if (term <= n):
                    arr[i] = term
                    i += 1
                    n -= term
                    term += 1
                else:
                    arr[i] = n
                    i += 1
                    n = 0
    for i in range(k):
        arr[i] += ((count * (i + 1)) + (k * (count * (count - 1)) // 2))
    for i in range(k):
        print(arr[i], end=" ")


n, k = 10, 3
candies(n, k)

# Expected output:
5 2 3
# distribute n candies among k people | function to find out the number of candies every person received ; count number of complete turns ; get the last term ; stores the number of candies ; do a binary search to find the number whose sum is less than n . ; get mide ; if sum is below n ; find number of complete turns ; right halve ; left halve ; last term of last complete series ; subtract the sum till ; first term of incomplete series ; count the total candies ; print the total candies ; 
def candies(n, k):
    count = 0
    ind = 1
    arr = [0] * k
    low = 0
    high = n
    while (low <= high):
        mid = (low + high) >> 1
        sum = (mid * (mid + 1)) >> 1
        if (sum <= n):
            count = int(mid / k)
            low = mid + 1
        else:
            high = mid - 1
    last = (count * k)
    n -= int((last * (last + 1)) / 2)
    i = 0
    term = (count * k) + 1
    while (n):
        if (term <= n):
            arr[i] = term
            i += 1
            n -= term
            term += 1
        else:
            arr[i] += n
            n = 0
    for i in range(k):
        arr[i] += ((count * (i + 1)) + int(k * (count * (count - 1)) / 2))
    for i in range(k):
        print(arr[i], end=" ")


n = 7
k = 4
candies(n, k)

# Expected output:
1 2 3 1
# smallest multiple of 3 which consists of three given non | function to return the minimum number divisible by 3 formed by the given digits ; sort the given array in ascending ; check if any single digit is divisible by 3 ; check if any two digit number formed by the given digits is divisible by 3 starting from the minimum ; generate the two digit number ; if none of the above is true , we can form three digit number by taking a [ 0 ] three times . ; 
def printSmallest(a, n):
    sum0, sum1 = 0, 0
    a = sorted(a)
    for i in range(n):
        if (a[i] % 3 == 0):
            return a[i]
    for i in range(n):
        for j in range(n):
            num = (a[i] * 10) + a[j]
            if (num % 3 == 0):
                return num
    return a[0] * 100 + a[0] * 10 + a[0]


arr = [7, 7, 1]
n = len(arr)
print(printSmallest(arr, n))

# Expected output:
111
# Print matrix after applying increment operations in M ranges | Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Size of the matrix ; Queries
def updateMatrix(n, q, mat):
    for i in range(0, len(q)):
        X1 = q[i][0]
        Y1 = q[i][1]
        X2 = q[i][2]
        Y2 = q[i][3]
        mat[X1][Y1] = mat[X1][Y1] + 1
        if (Y2 + 1 < n):
            mat[X2][Y2 + 1] = mat[X2][Y2 + 1] - 1
        elif (X2 + 1 < n):
            mat[X2 + 1][0] = mat[X2 + 1][0] - 1
    sum = 0
    for i in range(0, n):
        for j in range(0, n):
            sum = sum + mat[i][j]
            print(sum, end=' ')
        print(" ")


n = 5
mat = [[0 for i in range(n)]for i in range(n)]
q = [[0, 0, 1, 2], [1, 2, 3, 4], [1, 4, 3, 4]]
updateMatrix(n, q, mat)

# Expected output:
1 1 1 1 1  
1 1 2 1 2  
2 2 2 2 2  
2 2 2 2 2  
0 0 0 0 0
# replace the maximum element in the array by coefficient of range | utility function to print the contents of the array ; function to replace the maximum element from the array with the coefficient of range of the array ; maximum element from the array ; minimum element from the array ; calculate the coefficient of range for the array ; assuming all the array elements are distinct . replace the maximum element with the coefficient of range of the array ; print the updated array ; 
def printArr(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def replaceMax(arr, n):
    max_element = max(arr)
    min_element = min(arr)
    ranges = max_element - min_element
    coeffOfRange = ranges / (max_element + min_element)
    for i in range(n):
        if (arr[i] == max_element):
            arr[i] = coeffOfRange
            break
    printArr(arr, n)


if __name__ == "__main__":
    arr = [15, 16, 10, 9, 6, 7, 17]
    n = len(arr)
    replaceMax(arr, n)

# Expected output:
15 16 10 9 6 7 0.4782608695652174
# divide the two given numbers by their common divisors | print the numbers after dividing them by their common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; 
def divide(a, b):
    for i in range(2, min(a, b) + 1):
        while (a % i == 0 and b % i == 0):
            a = a // i
            b = b // i
    print("A =", a, ", B =", b)


if __name__ == "__main__":
    A, B = 10, 15
    divide(A, B)

# Expected output:
A = 2 , B = 3
# divide the two given numbers by their common divisors | function to calculate gcd of two numbers ; function to calculate all common divisors of two given numbers a , b -- > input eger numbers ; find gcd of a , b ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def commDiv(a, b):
    n = gcd(a, b)
    a = a // n
    b = b // n
    print("A =", a, ", B =", b)


a, b = 10, 15
commDiv(a, b)

# Expected output:
A = 2 , B = 3
# minimum absolute difference between n and a power of 2 | python3 implementation of the above approach ; function to return the minimum difference between n and a power of 2 ; power of 2 closest to n on its left ; power of 2 closest to n on its right ; return the minimum abs difference ; 

import math


def minAbsDiff(n):
    left = 1 << (int)(math .floor(math .log2(n)))
    right = left * 2
    return min((n - left), (right - n))


if __name__ == "__main__":
    n = 15
    print(minAbsDiff(n))

# Expected output:
1
# find probability that a player wins when probabilities of hitting the target are given | function to return the probability of the winner ; 
def find_probability(p, q, r, s):
    t = (1 - p / q) * (1 - r / s)
    ans = (p / q) / (1 - t)
    return round(ans, 9)


if __name__ == "__main__":
    p, q, r, s = 1, 2, 1, 2
    print(find_probability(p, q, r, s))

# Expected output:
0.666666667
# represent n as the sum of exactly k powers of two | set 2 | function to print k numbers which are powers of two and whose sum is equal to n ; initialising the sum with k ; initialising an array a with k elements and filling all elements with 1 ; iterating a [ ] from k - 1 to 0 ; update sum and a [ i ] till sum + a [ i ] is less than equal to n ; impossible to find the combination ; possible solution is stored in a [ ] ; 
def FindAllElements(n, k):
    sum = k
    A = [1 for i in range(k)]
    i = k - 1
    while (i >= 0):
        while (sum + A[i] <= n):
            sum += A[i]
            A[i] *= 2
        i -= 1
    if (sum != n):
        print("Impossible")
    else:
        for i in range(0, k, 1):
            print(A[i], end=' ')


if __name__ == '__main__':
    n = 12
    k = 6
    FindAllElements(n, k)

# Expected output:
1 1 1 1 4 4
# check whether a + b = c or not after removing all zeroes from a , b and c | function to remove zeroes ; initialize result to zero holds the result after removing zeroes from no ; initialize variable d to 1 that holds digits of no ; loop while n is greater then zero ; check if n mod 10 is not equal to zero ; store the result by removing zeroes and increment d by 10 ; go to the next digit ; return the result ; function to check if sum is true after removing all zeroes . ; call removezero ( ) for both sides and check whether they are equal after removing zeroes . ; 
def removeZero(n):
    res = 0
    d = 1
    while (n > 0):
        if (n % 10 != 0):
            res += (n % 10) * d
            d *= 10
        n //= 10
    return res


def isEqual(a, b):
    if (removeZero(a) + removeZero(b) == removeZero(a + b)):
        return True
    return False


a = 105
b = 106
if (isEqual(a, b)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# a sum array puzzle | python3 implementation of above approach ; allocate memory for temporary arrays leftsum [ ] , rightsum [ ] and sum [ ] ; left most element of left array is always 0 ; rightmost most element of right array is always 0 ; construct the left array ; construct the right array ; construct the sum array using left [ ] and right [ ] ; print the constructed prod array ; 
def sumArray(arr, n):
    leftSum = [0 for i in range(n)]
    rightSum = [0 for i in range(n)]
    Sum = [0 for i in range(n)]
    i, j = 0, 0
    leftSum[0] = 0
    rightSum[n - 1] = 0
    for i in range(1, n):
        leftSum[i] = arr[i - 1] + leftSum[i - 1]
    for j in range(n - 2, -1, -1):
        rightSum[j] = arr[j + 1] + rightSum[j + 1]
    for i in range(0, n):
        Sum[i] = leftSum[i] + rightSum[i]
    for i in range(n):
        print(Sum[i], end=" ")


arr = [3, 6, 4, 8, 9]
n = len(arr)
sumArray(arr, n)

# Expected output:
27 24 26 22 21
# find minimum x such that ( x % k ) * ( x / k ) == n | set | python3 program to find the minimum positive x such that the given equation holds true ; this function gives the required answer ; iterate for all the factors ; check if i is a factor ; consider i to be a and n / i to be b ; consider i to be b and n / i to be a ; 

import sys


def minimumX(n, k):
    mini = sys .maxsize
    i = 1
    while i * i <= n:
        if (n % i == 0):
            fir = i
            sec = n // i
            num1 = fir * k + sec
            res = (num1 // k) * (num1 % k)
            if (res == n):
                mini = min(num1, mini)
            num2 = sec * k + fir
            res = (num2 // k) * (num2 % k)
            if (res == n):
                mini = min(num2, mini)
        i += 1
    return mini


if __name__ == "__main__":
    n = 4
    k = 6
    print(minimumX(n, k))
    n = 5
    k = 5
    print(minimumX(n, k))

# Expected output:
10
26
# find minimum x such that ( x % k ) * ( x / k ) == n | this function gives the required answer ; iterate over all possible remainders ; it must divide n ; 
def minimumX(n, k):
    ans = 10 ** 18
    for i in range(k - 1, 0, -1):
        if n % i == 0:
            ans = min(ans, i + (n / i) * k)
    return ans


n, k = 4, 6
print(minimumX(n, k))
n, k = 5, 5
print(minimumX(n, k))

# Expected output:
10.0
26.0
# find nth hermite number | function to return nth hermite number ; base conditions ; 
def getHermiteNumber(n):
    if n == 0:
        return 1
    if n == 1:
        return 0
    else:
        return (-2 * (n - 1) * getHermiteNumber(n - 2))


n = 6
print(getHermiteNumber(n))

# Expected output:
-120
# find numbers a and b that satisfy the given conditions | function to print the required numbers ; suppose b = n and we want a % b = 0 and also ( a / b ) < n so a = b * ( n - 1 ) ; special case if n = 1 we get a = 0 so ( a * b ) < n ; if no pair satisfies the conditions ; 
def find(n):
    b = n
    a = b * (n - 1)
    if a * b > n and a // b < n:
        print("a = {}, b = {}".format(a, b))
    else:
        print(-1)


if __name__ == "__main__":
    n = 10
    find(n)

# Expected output:
a = 90, b = 10
# closest perfect square and its distance | function to check if a number is perfect square or not ; function to find the closest perfect square taking minimum steps to reach from a number ; variables to store first perfect square number above and below n ; finding first perfect square number greater than n ; finding first perfect square number less than n ; variables to store the differences ; 

from math import sqrt, floor


def isPerfect(N):
    if (sqrt(N) - floor(sqrt(N)) != 0):
        return False
    return True


def getClosestPerfectSquare(N):
    if (isPerfect(N)):
        print(N, "0")
        return
    aboveN = -1
    belowN = -1
    n1 = 0
    n1 = N + 1
    while (True):
        if (isPerfect(n1)):
            aboveN = n1
            break
        else:
            n1 += 1
    n1 = N - 1
    while (True):
        if (isPerfect(n1)):
            belowN = n1
            break
        else:
            n1 -= 1
    diff1 = aboveN - N
    diff2 = N - belowN
    if (diff1 > diff2):
        print(belowN, diff2)
    else:
        print(aboveN, diff1)


N = 1500
getClosestPerfectSquare(N)

# Expected output:
1521 21
# fraction | function to return gcd of a and b ; function to convert the obtained fraction into it 's simplest form ; finding gcd of both terms ; converting both terms into simpler terms by dividing them by common factor ; function to add two fractions ; finding gcd of den1 and den2 ; denominator of final fraction obtained finding lcm of den1 and den2 lcm * gcd = a * b ; changing the fractions to have same denominator numerator of the final fraction obtained ; calling function to convert final fraction into it 's  simplest form ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def lowest(den3, num3):
    common_factor = gcd(num3, den3)
    den3 = int(den3 / common_factor)
    num3 = int(num3 / common_factor)
    print(num3, "/", den3)


def addFraction(num1, den1, num2, den2):
    den3 = gcd(den1, den2)
    den3 = (den1 * den2) / den3
    num3 = ((num1) * (den3 / den1) + (num2) * (den3 / den2))
    lowest(den3, num3)


num1 = 1
den1 = 500
num2 = 2
den2 = 1500
print(num1, "/", den1, " + ", num2, "/", den2, " is equal to ", end="")
addFraction(num1, den1, num2, den2)

# Expected output:
1 / 500  +  2 / 1500  is equal to 1 / 300