giulio98/xlcost-single-prompt huggingface dataset:
# number of triangles formed from a set of points on three lines | returns factorial of a number ; calculate c ( n , r ) ; 
def factorial(n):
    fact = 1
    for i in range(2, n + 1):
        fact = fact * i
    return fact


def ncr(n, r):
    return (factorial(n) // (factorial(r) * factorial(n - r)))


if __name__ == "__main__":
    m = 3
    n = 4
    k = 5
    totalTriangles = (ncr(m + n + k, 3) - ncr(m, 3) - ncr(n, 3) - ncr(k, 3))
    print(totalTriangles)

# Expected output:
205
# check whether sum of digits at odd places of a number is divisible by k | function that checks the divisibility of the sum of the digits at odd places of the given number ; if position is odd ; 
def SumDivisible(n, k):
    sum = 0
    position = 1
    while (n > 0):
        if (position % 2 == 1):
            sum += n % 10
        n = n // 10
        position += 1
    if (sum % k == 0):
        return True
    return False


if __name__ == "__main__":
    n = 592452
    k = 3
    if (SumDivisible(n, k)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# check whether sum of digits at odd places of a number is divisible by k | python3 implementation of the above approach ; converting integer to string ; traversing the string ; 
def sumDivisible(n, k):
    sum = 0
    num = str(n)
    for i in range(len(num)):
        if (i % 2 != 0):
            sum = sum + int(num[i])
    if sum % k == 0:
        return True
    return False


n = 592452
k = 3
if sumDivisible(n, k):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# check if a triangle of positive area is possible with the given angles | python program to check if a triangle of positive area is possible with the given angles ; checking if the sum of three angles is 180 and none of the angles is zero ; checking if sum of any two angles is greater than equal to the third one ; 
def isTriangleExists(a, b, c):
    if (a != 0 and b != 0 and c != 0 and (a + b + c) == 180):
        if ((a + b) >= c or (b + c) >= a or (a + c) >= b):
            return "YES"
        else:
            return "NO"
    else:
        return "NO"


a, b, c = 50, 60, 70
print(isTriangleExists(50, 60, 70))

# Expected output:
YES
# find maximum value of x such that n ! % ( k ^ x ) = 0 | python 3 program to maximize the value of x such that n ! % ( k ^ x ) = 0 ; function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; find square root of k and add 1 to it ; run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; minimum store ; 

import math


def findX(n, k):
    r = n
    m = int(math .sqrt(k)) + 1
    i = 2
    while i <= m and k > 1:
        if (i == m):
            i = k
        u = 0
        v = 0
        while k % i == 0:
            k //= i
            v += 1
        if (v > 0):
            t = n
            while (t > 0):
                t //= i
                u += t
            r = min(r, u // v)
        i += 1
    return r


if __name__ == "__main__":
    n = 5
    k = 2
    print(findX(n, k))

# Expected output:
3
# ways of selecting men and women from a group to make a team | returns factorial of the number ; function to calculate ncr ; function to calculate the total possible ways ; 
def fact(n):
    fact = 1
    for i in range(2, n + 1):
        fact *= i
    return fact


def ncr(n, r):
    ncr = fact(n) // (fact(r) * fact(n - r))
    return ncr


def ways(m, w, n, k):
    ans = 0
    while (m >= k):
        ans += ncr(m, k) * ncr(w, n - k)
        k += 1
    return ans


m = 7
w = 6
n = 5
k = 3
print(ways(m, w, n, k))

# Expected output:
756
# sum of greatest odd divisor of numbers in given range | function to return sum of first n odd numbers ; recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; odd n ; even n ; function to return sum of greatest odd divisor of numbers in range [ a , b ] ; 
def square(n):
    return n * n


def sum(n):
    if (n == 0):
        return 0
    if (n % 2 == 1):
        return (square(int((n + 1) / 2)) + sum(int(n / 2)))
    else:
        return (square(int(n / 2)) + sum(int(n / 2)))


def oddDivSum(a, b):
    return sum(b) - sum(a - 1)


a, b = 3, 9
print(oddDivSum(a, b))

# Expected output:
29
# minimum numbers needed to express every integer below n as a sum | function to count length of binary expression of n ; 
def countBits(n):
    count = 0
    while (n):
        count += 1
        n >>= 1
    return count


n = 32
print("Minimum value of K is =", countBits(n))

# Expected output:
Minimum value of K is = 6
# check if a number is an achilles number or not | program to check if the given number is an achilles number ; function to check if the number is powerful number ; first divide the number repeatedly by 2 ; if only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; find highest power of " factor " that divides n ; if only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . since prime numbers are not powerful , we return false if n is not 1. ; utility function to check if number is a perfect power or not ; function to check achilles number ; 

from math import sqrt, log


def isPowerful(n):
    while (n % 2 == 0):
        power = 0
        while (n % 2 == 0):
            n /= 2
            power += 1
        if (power == 1):
            return False
    p = int(sqrt(n)) + 1
    for factor in range(3, p, 2):
        power = 0
        while (n % factor == 0):
            n = n / factor
            power += 1
        if (power == 1):
            return False
    return (n == 1)


def isPower(a):
    if (a == 1):
        return True
    p = int(sqrt(a)) + 1
    for i in range(2, a, 1):
        val = log(a) / log(i)
        if ((val - int(val)) < 0.00000001):
            return True
    return False


def isAchillesNumber(n):
    if (isPowerful(n) and isPower(n) == False):
        return True
    else:
        return False


if __name__ == '__main__':
    n = 72
    if (isAchillesNumber(n)):
        print("YES")
    else:
        print("NO")
    n = 36
    if (isAchillesNumber(n)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
NO
# program to find count of numbers having odd number of divisors in given range | function to count numbers having odd number of divisors in range [ a , b ] ; variable to odd divisor count ; iterate from a to b and count their number of divisors ; variable to divisor count ; if count of divisor is odd then increase res by 1 ; 
def OddDivCount(a, b):
    res = 0
    for i in range(a, b + 1):
        divCount = 0
        for j in range(1, i + 1):
            if (i % j == 0):
                divCount += 1
        if (divCount % 2):
            res += 1
    return res


if __name__ == "__main__":
    a = 1
    b = 10
    print(OddDivCount(a, b))

# Expected output:
3
# largest factor of a given number which is a perfect square | python 3 program to find the largest factor of a number which is also a perfect square ; function to find the largest factor of a given number which is a perfect square ; initialise the answer to 1 ; finding the prime factors till sqrt ( num ) ; frequency of the prime factor in the factorisation initialised to 0 ; if the frequency is odd then multiply i frequency - 1 times to the answer ; else if it is even , multiply it frequency times ; 

import math


def largestSquareFactor(num):
    answer = 1
    for i in range(2, int(math .sqrt(num))):
        cnt = 0
        j = i
        while (num % j == 0):
            cnt += 1
            j *= i
        if (cnt & 1):
            cnt -= 1
            answer *= pow(i, cnt)
        else:
            answer *= pow(i, cnt)
    return answer


if __name__ == "__main__":
    N = 420
    print(largestSquareFactor(N))

# Expected output:
4
# find the nth term of the series 2 + 6 + 13 + 23 + . . . | calculate nth term of given series ; 
def Nth_Term(n):
    return (3 * pow(n, 2) - n + 2) // (2)


N = 5
print(Nth_Term(N))

# Expected output:
36
# all possible numbers of n digits and base b without leading zeros | function to count all permutations ; count of all permutations ; count of permutations with leading zeros ; return the permutations without leading zeros ; 
def countPermutations(N, B):
    x = B ** N
    y = B ** (N - 1)
    print(x - y)


if __name__ == "__main__":
    N, B = 6, 4
    countPermutations(N, B)

# Expected output:
3072
# absolute difference between the product of non | function to find the difference between the product of non - primes and the product of primes of an array . ; find maximum value in the array ; use sieve to find all prime numbers less than or equal to max_val create a boolean array " prime [ 0 . . n ] " . a value in prime [ i ] will finally be false if i is not a prime , else true . ; remaining part of sieve ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; store the product of primes in p1 and the product of non primes in p2 ; the number is prime ; the number is non - prime ; return the absolute difference ; 
def calculateDifference(arr, n):
    max_val = max(arr)
    prime = (max_val + 1) * [True]
    prime[0] = False
    prime[1] = False
    p = 2
    while p * p <= max_val:
        if prime[p]:
            for i in range(p * 2, max_val + 1, p):
                prime[i] = False
        p += 1
    P1 = 1
    P2 = 1
    for i in range(n):
        if prime[arr[i]]:
            P1 *= arr[i]
        elif arr[i] != 1:
            P2 *= arr[i]
    return abs(P2 - P1)


if __name__ == '__main__':
    arr = [1, 3, 5, 10, 15, 7]
    n = len(arr)
    print(calculateDifference(arr, n))

# Expected output:
45
# maximum count of equal numbers in an array after performing given operations | function to find the maximum number of equal numbers in an array ; to store sum of elements ; if sum of numbers is not divisible by n ; 
def EqualNumbers(a, n):
    sum = 0
    for i in range(n):
        sum += a[i]
    if (sum % n):
        return n - 1
    return n


a = [1, 4, 1]
n = len(a)
print(EqualNumbers(a, n))

# Expected output:
3
# steps required to visit m points in order on a circular ring of n points | function to count the steps required ; start at 1 ; initialize steps ; if nxt is greater than cur ; now we are at a [ i ] ; 
def findSteps(n, m, a):
    cur = 1
    steps = 0
    for i in range(0, m):
        if (a[i] >= cur):
            steps += (a[i] - cur)
        else:
            steps += (n - cur + a[i])
        cur = a[i]
    return steps


n = 3
m = 3
a = [2, 1, 2]
print(findSteps(n, m, a))

# Expected output:
4
# program to convert hexadecimal number to binary | function to convert hexadecimal to binary number ; 
def HexToBin(hexdec):
    for i in hexdec:
        if i == '0':
            print('0000', end='')
        elif i == '1':
            print('0001', end='')
        elif i == '2':
            print('0010', end='')
        elif i == '3':
            print('0011', end='')
        elif i == '4':
            print('0100', end='')
        elif i == '5':
            print('0101', end='')
        elif i == '6':
            print('0110', end='')
        elif i == '7':
            print('0111', end='')
        elif i == '8':
            print('1000', end='')
        elif i == '9':
            print('1001', end='')
        elif i == 'A' or i == 'a':
            print('1010', end='')
        elif i == 'B' or i == 'b':
            print('1011', end='')
        elif i == 'C' or i == 'c':
            print('1100', end='')
        elif i == 'D' or i == 'd':
            print('1101', end='')
        elif i == 'E' or i == 'e':
            print('1110', end='')
        elif i == 'F' or i == 'f':
            print('1111', end='')
        else:
            print("Invalidhexadecimaldigit" + str(hexdec[i]), end='')


if __name__ == "__main__":
    hexdec = "1AC5"
    print("Equivalent Binary value is : ", end='')
    HexToBin(hexdec)

# Expected output:
Equivalent Binary value is : 0001101011000101
# count unordered pairs ( i , j ) such that product of a [ i ] and a [ j ] is power of two | function to check if x is power of 2 ; first x in the below expression is for the case when x is 0 ; function to count unordered pairs ; is a number can be expressed as power of two ; count total number of unordered pairs ; 
def isPowerOfTwo(x):
    return (x and (not (x & (x - 1))))


def Count_pairs(a, n):
    count = 0
    for i in range(n):
        if isPowerOfTwo(a[i]):
            count += 1
    ans = (count * (count - 1)) / 2
    print(ans)


if __name__ == "__main__":
    a = [2, 5, 8, 16, 128]
    n = len(a)
    Count_pairs(a, n)

# Expected output:
6.0
# ways of dividing a group into two halves such that two elements are in different groups | this function will return the factorial of a given number ; this function will calculate ncr of given n and r ; this function will calculate number of ways ; 
def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result


def nCr(n, r):
    return (factorial(n) // (factorial(r) * factorial(n - r)))


def calculate_result(n):
    result = 2 * nCr((n - 2), (n // 2 - 1))
    return result


if __name__ == "__main__":
    a, b = 2, 4
    print(calculate_result(2 * a))
    print(calculate_result(2 * b))

# Expected output:
4
40
# print values of ' a ' in equation ( a + b ) <= n and a + b is divisible by x | function to find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; least possible which is divisible by x ; run a loop to get required answer ; increase value by x ; answer is possible ; 
def PossibleValues(b, x, n):
    leastdivisible = int(b / x + 1) * x
    flag = 1
    while (leastdivisible <= n):
        if (leastdivisible - b >= 1):
            print(leastdivisible - b, end=" ")
            leastdivisible += x
            flag = 0
        else:
            break
    if (flag != 0):
        print(-1)


if __name__ == '__main__':
    b = 10
    x = 6
    n = 40
    PossibleValues(b, x, n)

# Expected output:
2 8 14 20 26
# check if all sub | function to calculate product of digits between given indexes ; function to check if all sub - numbers have distinct digit product ; length of number n ; digit array ; set to maintain digit products ; finding all possible subarrays ; 
def digitProduct(digits, start, end):
    pro = 1
    for i in range(start, end + 1):
        pro *= digits[i]
    return pro


def isDistinct(N):
    s = str(N)
    length = len(s)
    digits = [None] * length
    products = set()
    for i in range(0, length):
        digits[i] = int(s[i])
    for i in range(0, length):
        for j in range(i, length):
            val = digitProduct(digits, i, j)
            if val in products:
                return False
            else:
                products .add(val)
    return True


if __name__ == "__main__":
    N = 324
    if isDistinct(N):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# ulam number sequence | python3 code to print nth ulam number ; array to store ulam number ; function to compute ulam number ; set to search specific ulam number efficiently ; push first 2 two term of the sequence in the array and set for further calculation ; loop to generate ulam number ; traverse the array and check if i can be represented as sum of two distinct element of the array ; check if i - arr [ j ] exist in the array or not using set . if yes , then i can be represented as sum of arr [ j ] + ( i - arr [ j ] ) ; if count is greater than 2 break the loop ; if count is 2 that means i can be represented as sum of two distinct terms of the sequence ; i is ulam number ; 

MAX = 10000
arr = []


def ulam():
    s = set()
    arr .append(1)
    s .add(1)
    arr .append(2)
    s .add(2)
    for i in range(3, MAX):
        count = 0
        for j in range(0, len(arr)):
            if (i - arr[j]) in s and arr[j] != (i - arr[j]):
                count += 1
            if count > 2:
                break
        if count == 2:
            arr .append(i)
            s .add(i)


if __name__ == "__main__":
    ulam()
    n = 9
    print(arr[n - 1])

# Expected output:
16
# hilbert number | utility function to return nth hilbert number ; 
def nthHilbertNumber(n):
    return 4 * (n - 1) + 1


n = 5
print(nthHilbertNumber(n))

# Expected output:
17
# program to find the nth kynea number | function to calculate nth kynea number ; firstly calculate 2 ^ n + 1 ; now calculate ( 2 ^ n + 1 ) ^ 2 ; now calculate ( ( 2 ^ n + 1 ) ^ 2 ) - 2 ; return nth kynea number ; 
def nthKyneaNumber(n):
    n = (1 << n) + 1
    n = n * n
    n = n - 2
    return n


n = 2
print(nthKyneaNumber(n))

# Expected output:
23
# program to find the nth kynea number | function to calculate nth kynea number ; calculate nth kynea number ; 
def nthKyneaNumber(n):
    return ((1 << (2 * n)) + (1 << (n + 1)) - 1)


n = 2
print(nthKyneaNumber(n))

# Expected output:
23
# program to check whether a number is proth number or not | utility function to check power of two ; function to check if the 
def isPowerOfTwo(n):
    return (n and (not (n & (n - 1))))


def isProthNumber(n):
    k = 1
    while (k < (n // k)):
        if (n % k == 0):
            if (isPowerOfTwo(n // k)):
                return True
        k = k + 2
    return False


n = 25
if (isProthNumber(n - 1)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# find last two digits of sum of n factorials | function to find the unit ' s ▁ and ▁ ten ' s place digit ; let us write for cases when n is smaller than or equal to 10 ; we know following ( 1 ! + 2 ! + 3 ! + 4 ! ... + 10 ! ) % 100 = 13 ( n >= 10 ) ; 
def get_last_two_digit(N):
    if N <= 10:
        ans = 0
        fac = 1
        for i in range(1, N + 1):
            fac = fac * i
            ans += fac
        ans = ans % 100
        return ans
    else:
        return 13


N = 1
for N in range(1, 11):
    print("For N = ", N, ": ", get_last_two_digit(N), sep=' ')

# Expected output:
For N =  1 :  1
For N =  2 :  3
For N =  3 :  9
For N =  4 :  33
For N =  5 :  53
For N =  6 :  73
For N =  7 :  13
For N =  8 :  33
For N =  9 :  13
For N =  10 :  13
# check whether product of ' n ' numbers is even or odd | function to check whether product of ' n ' numbers is even or odd ; if a single even number is found , then final product will be an even number ; product is an odd number ; 
def isProductEven(arr, n):
    for i in range(0, n):
        if ((arr[i] & 1) == 0):
            return True
    return False


arr = [2, 4, 3, 5]
n = len(arr)
if (isProductEven(arr, n)):
    print("Even")
else:
    print("Odd")

# Expected output:
Even
# sum of squares of fibonacci numbers | function to calculate sum of squares of fibonacci numbers ; initialize result ; add remaining terms ; 
def calculateSquareSum(n):
    fibo = [0] * (n + 1)
    if (n <= 0):
        return 0
    fibo[0] = 0
    fibo[1] = 1
    sum = ((fibo[0] * fibo[0]) + (fibo[1] * fibo[1]))
    for i in range(2, n + 1):
        fibo[i] = (fibo[i - 1] + fibo[i - 2])
        sum += (fibo[i] * fibo[i])
    return sum


n = 6
print("Sum of squares of Fibonacci numbers is :", calculateSquareSum(n))

# Expected output:
Sum of squares of Fibonacci numbers is : 104
# value of the series ( 1 ^ 3 + 2 ^ 3 + 3 ^ 3 + ... + n ^ 3 ) mod 4 for a given n | function for obtaining the value of f ( n ) mod 4 ; find the remainder of n when divided by 4 ; if n is of the form 4 k or 4 k + 3 ; if n is of the form 4 k + 1 or 4 k + 2 ; 
def fnMod(n):
    rem = n % 4
    if (rem == 0 or rem == 3):
        return 0
    elif (rem == 1 or rem == 2):
        return 1


if __name__ == "__main__":
    n = 6
    print(fnMod(n))

# Expected output:
1
# minimum increment operations to make the array in increasing order | function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; 
def MinimumMoves(a, n, x):
    ans = 0
    for i in range(1, n):
        if a[i] <= a[i - 1]:
            p = (a[i - 1] - a[i]) // x + 1
            ans += p
            a[i] += p * x
    return ans


if __name__ == "__main__":
    arr = [1, 3, 3, 2]
    x = 2
    n = len(arr)
    print(MinimumMoves(arr, n, x))

# Expected output:
3
# check if a large number is divisible by 2 , 3 and 5 or not | function to return sum of digits of a number ; function to check if a large number is divisible by 2 , 3 and 5 or not ; 
def SumOfDigits(str, n):
    sum = 0
    for i in range(0, n):
        sum += int(ord(str[i]) - ord('0'))
    return sum


def Divisible(str, n):
    if ((SumOfDigits(str, n) % 3 == 0 and str[n - 1] == '0')):
        return True
    return False


if __name__ == "__main__":
    str = "263730746028908374890"
    n = len(str)
    if (Divisible(str, n)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# count all the numbers in a range with smallest factor as k | function to check if k is a prime number or not ; corner case ; check from 2 to n - 1 ; function to check if a number is not divisible by any number between 2 and k - 1 ; to check if the num is divisible by any numbers between 2 and k - 1 ; if not divisible by any number between 2 and k - 1 but divisible by k ; function to find count of numbers in range [ a , b ] with smallest factor as k ; a number can be divisible only by k and not by any number less than k only if k is a prime ; to check if a number has smallest factor as k ; 
def isPrime(k):
    if (k <= 1):
        return False
    for i in range(2, k):
        if (k % i == 0):
            return false
    return True


def check(num, k):
    flag = 1
    for i in range(2, k):
        if (num % i == 0):
            flag = 0
    if (flag == 1):
        if (num % k == 0):
            return 1
        else:
            return 0
    else:
        return 0


def findCount(a, b, k):
    count = 0
    if (not isPrime(k)):
        return 0
    else:
        for i in range(a, b + 1):
            ans = check(i, k)
            if (ans == 1):
                count += 1
            else:
                continue
    return count


if __name__ == "__main__":
    a = 2020
    b = 6300
    k = 29
    print(findCount(a, b, k))

# Expected output:
28
# largest number with the given set of n digits that is divisible by 2 , 3 and 5 | function to find the largest integer with the given set ; find sum of all the digits look if any 0 is present or not ; if 0 is not present , the resultant number won 't be divisible by 5 ; sort all the elements in a non - decreasing manner ; if there is just one element 0 ; find the remainder of the sum of digits when divided by 3 ; there can a remainder as 1 or 2 ; traverse from the end of the digits ; first element which has the same remainder , remove it ; if there is no element which has a same remainder as y ; subtract it by 3 ( could be one or two ) ; delete two minimal digits which has a remainder as y ; print all the digits as a single integer ; 
def findLargest(n, v):
    flag = 0
    sum = 0
    for i in range(n):
        if (v[i] == 0):
            flag = 1
        sum += v[i]
    if (flag == 0):
        print("Not possible")

    else:
        v.sort(reverse=True)
        if (v[0] == 0):
            print("0")
            return 0

        else:
            flag = 0
            y = sum % 3
            if (y != 0):
                i = n - 1
                while (i >= 0):
                    if (v[i] % 3 == y):
                        v.remove(v[i])
                        flag = 1
                        break
                    i -= 1
                if (flag == 0):
                    y = 3 - y

                    cnt = 0
                    i = n - 1
                    while (i >= 0):
                        if (v[i] % 3 == y):
                            v.remove(v[i])
                            cnt += 1

                            if (cnt >= 2):
                                break

                        i -= 1
            for i in (v):
                print(i, end="")


if __name__ == '__main__':
    n = 11
    v = [3, 9, 9, 6, 4, 3,
         6, 4, 9, 6, 0]

    findLargest(n, v)

# Expected output:
999666330