giulio98/xlcost-single-prompt huggingface dataset:
# length of longest prime subsequence in an array | python 3 program to find the length of longest prime subsequence in an array ; function to create sieve to check primes ; false here indicates that it is not prime ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p , set them to non - prime ; function to find the longest subsequence which contain all prime numbers ; precompute n primes ; find the length of longest prime subsequence ; 

N = 100005


def SieveOfEratosthenes(prime, p_size):
    prime[0] = False
    prime[1] = False
    p = 2
    while p * p <= p_size:
        if (prime[p]):
            for i in range(p * 2, p_size + 1, p):
                prime[i] = False
        p += 1


def longestPrimeSubsequence(arr, n):
    prime = [True] * (N + 1)
    SieveOfEratosthenes(prime, N)
    answer = 0
    for i in range(n):
        if (prime[arr[i]]):
            answer += 1
    return answer


if __name__ == "__main__":
    arr = [3, 4, 11, 2, 9, 21]
    n = len(arr)
    print(longestPrimeSubsequence(arr, n))

# Expected output:
3
# check if an octal number is even or odd | check if the number is odd or even ; check if the last digit is either '0' , '2' , '4' , or '6 ; 
def even_or_odd(N):
    l = len(N)

    if (N[l - 1] == '0' or N[l - 1] ==
            '2' or N[l - 1] == '4' or N[l - 1] == '6'):
        return ("Even")
    else:
        return ("Odd")


N = "735"
print(even_or_odd(N))

# Expected output:
Odd
# runge | a sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; finds value of y for a given x using step size h and initial value y0 at x0 . ; count number of iterations using step size or step height h ; iterate for number of iterations ; apply runge kutta formulas to find next value of y ; update next value of y ; update next value of x ; 
def dydx(x, y):
    return (x + y - 2)


def rungeKutta(x0, y0, x, h):
    n = round((x - x0) / h)
    y = y0
    for i in range(1, n + 1):
        k1 = h * dydx(x0, y)
        k2 = h * dydx(x0 + 0.5 * h, y + 0.5 * k1)
        y = y + (1.0 / 6.0) * (k1 + 2 * k2)
        x0 = x0 + h
    return y


if __name__ == "__main__":
    x0 = 0
    y = 1
    x = 2
    h = 0.2
    print("y(x) =", rungeKutta(x0, y, x, h))

# Expected output:
y(x) = 0.6455901672629244
# count of fibonacci pairs which satisfy the given equation | python program to find the count of fibonacci pairs ( x , y ) which satisfy the equation ax + by = n ; array to store the fibonacci numbers ; array to store the number of ordered pairs ; function to find if a number is a perfect square ; function that returns 1 if n is non - fibonacci number else 0 ; n is fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; function to store the fibonacci numbers and their frequency in form a * x + b * y ; storing the fibonacci numbers ; for loop to find all the possible combinations of the fibonacci numbers ; finding the number of ordered pairs ; 

import math
size = 101
fib = [0] * 100010
freq = [0] * (100010)


def isPerfectSquare(x):
    s = int(math .sqrt(x))
    return (s * s) == x


def isFibonacci(n):
    if (isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4)):
        return 1
    return 0


def compute(a, b):
    for i in range(1, 100010):
        fib[i] = isFibonacci(i)
    for x in range(1, 100010):
        for y in range(1, size):
            if (fib[x] == 1 and fib[y] == 1 and a * x + b * y < 100010):
                freq[a * x + b * y] += 1


Q = 2
A = 5
B = 10
compute(A, B)
arr = [50, 150]
for i in range(Q):
    print(freq[arr[i]], end=" ")

# Expected output:
1 0
# maximise the sum of two numbers using at most one swap between them | python program to maximise the sum of two numbers using at most one swap between them ; function to maximize the sum by swapping only one digit ; store digits of max ( n1 , n2 ) ; store digits of min ( n1 , n2 ) ; if length of the two numbers are unequal ; find the most significant number and the most significant index for swapping ; if both numbers are of equal length ; fetch the index of current maximum digit present in both the arrays ; compute the difference ; find the most significant index and the most significant digit to be swapped ; restore the new numbers ; display the maximized sum ; 

MAX = 100


def findMaxSum(n1, n2):
    arr1 = [0] * (MAX)
    arr2 = [0] * (MAX)
    l1 = 0
    l2 = 0
    max1 = max(n1, n2)
    min1 = min(n1, n2)
    i = max1
    while i > 0:
        arr1[l1] = (i % 10)
        l1 += 1
        i //= 10
    i = min1
    while i > 0:
        arr2[l2] = (i % 10)
        l2 += 1
        i //= 10
    f = 0
    if (l1 != l2):
        index = arr2 .index(max(arr2))
        for i in range(l1 - 1, (l2 - 1), -1):
            if (arr1[i] < arr2[index]):
                (arr1[i], arr2[index]) = (arr2[index], arr1[i])
                f = 1
                break
    if (f != 1):
        index1 = 0
        index2 = 0
        diff1 = 0
        diff2 = 0
        for i in range(l2 - 1, -1, -1):
            index1 = arr1 .index(max(arr1[:i]))
            index2 = arr2 .index(max(arr2[:i]))
            diff1 = (arr2[index2] - arr1[i])
            diff2 = (arr1[index1] - arr2[i])
            if (diff1 > 0 or diff2 > 0):
                if (diff1 > diff2):
                    arr1[i], arr2[index2] = arr2[index2], arr1[i]
                    break
                elif (diff2 > diff1):
                    arr2[i], arr1[index1] = arr1[index1], arr2[i]
                    break
                elif (diff1 == diff2):
                    if (index1 <= index2):
                        arr2[i], arr1[index1] = arr1[index1], arr2[i]
                        break
                    elif (index2 <= index1):
                        arr1[i], arr2[index2] = arr2[index2], arr1[i]
                        break
    f_n1 = 0
    f_n2 = 0
    for i in range(l1 - 1, -1, -1):
        f_n1 = (f_n1 * 10) + arr1[i]
        f_n2 = (f_n2 * 10) + arr2[i]
    print(f_n1 + f_n2)


N1 = 9987
N2 = 123
findMaxSum(N1, N2)

# Expected output:
10740
# check if a number is divisible by 47 or not | function to check if the number is divisible by 47 or not ; while there are at least two digits ; extracting the last ; truncating the number ; subtracting fourteen times the last digit to the remaining number ; finally return if the two - digit number is divisible by 43 or not ; 
def isDivisible(n):
    while n // 100:
        d = n % 10
        n //= 10
        n = abs(n - (d * 14))
    return (n % 47 == 0)


if __name__ == "__main__":
    n = 59173
    if (isDivisible(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check if a hexadecimal number is even or odd | check if the number is odd or even ; check if the last digit is either '0' , '2' , '4' , '6' , '8' , ' a ' ( = 10 ) , ' c ' ( = 12 ) or ' e ' ( = 14 ) ; 
def even_or_odd(N):
    l = len(N)
    if (N[l -
          1] == '0' or N[l -
                         1] == '2' or N[l -
                                        1] == '4' or N[l -
                                                       1] == '6' or N[l -
                                                                      1] == '8' or N[l -
                                                                                     1] == 'A' or N[l -
                                                                                                    1] == 'C' or N[l -
                                                                                                                   1] == 'E'):
        return ("Even")
    else:
        return ("Odd")


N = "AB3454D"
print(even_or_odd(N))

# Expected output:
Odd
# check if a number is divisible by 31 or not | function to check if the number is divisible by 31 or not ; while there are at least two digits ; extracting the last ; truncating the number ; subtracting three times the last digit to the remaining number ; finally return if the two - digit number is divisible by 31 or not ; 
def isDivisible(n):
    while n // 100:
        d = n % 10
        n //= 10
        n = abs(n - (d * 3))
    return (n % 31 == 0)


if __name__ == "__main__":
    n = 1922
    if (isDivisible(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check if the number is divisible 43 or not | function to check if the number is divisible by 43 or not ; while there are at least two digits ; extracting the last ; truncating the number ; adding thirteen times the last digit to the remaining number ; finally return if the two - digit number is divisible by 43 or not ; 
def isDivisible(n):
    while n // 100:
        d = n % 10
        n //= 10
        n = abs(n + (d * 13))
    return (n % 43 == 0)


if __name__ == "__main__":
    N = 2795
    if (isDivisible(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the next non | python 3 implementation of the approach ; function to check if a number is perfect square ; function to check if a number is fibinacci number ; n is fibinacci if either ( 5 * n * n + 4 ) , ( 5 * n * n - 4 ) or both is a perferct square ; function to find the next non - fibinacci number ; case 1 if n <= 3 , then 4 will be next non - fibinacci number ; case 2 if n + 1 is fibinacci , then n + 2 will be next non - fibinacci number ; if n + 1 is non - fibinacci , then n + 2 will be next non - fibinacci number ; 

from math import sqrt


def isPerfectSquare(x):
    s = sqrt(x)
    return (s * s == x)


def isFibonacci(N):
    return isPerfectSquare(5 * N * N + 4) or isPerfectSquare(5 * N * N - 4)


def nextNonFibonacci(N):
    if (N <= 3):
        return 4
    if (isFibonacci(N + 1)):
        return N + 2
    else:
        return N


if __name__ == '__main__':
    N = 3
    print(nextNonFibonacci(N))
    N = 4
    print(nextNonFibonacci(N))
    N = 7
    print(nextNonFibonacci(N))

# Expected output:
4
6
9
# represent k ^ n as the sum of exactly n numbers | python 3 program to represent k ^ n as the sum of exactly n numbers ; function to print n numbers whose sum is a power of k ; printing k ^ 1 ; loop to print the difference of powers from k ^ 2 ; 

from math import pow


def printf(n, k):
    print(int(k), end=" ")
    for i in range(2, n + 1, 1):
        x = pow(k, i) - pow(k, i - 1)
        print(int(x), end=" ")


if __name__ == '__main__':
    N = 3
    K = 4
    printf(N, K)

# Expected output:
4 12 48
# check if the given number is divisible by 71 or not | function to check if the number is divisible by 71 or not ; while there are at least two digits ; extracting the last ; truncating the number ; subtracting seven times the last digit to the remaining number ; finally return if the two - digit number is divisible by 71 or not ; 
def isDivisible(n):
    while n // 100:
        d = n % 10
        n //= 10
        n = abs(n - (d * 7))
    return (n % 71 == 0)


if __name__ == "__main__":
    N = 5041
    if (isDivisible(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# count of prime digits of a number which divides the number | function to find the number of digits in number which divides the number and is also a prime number ; only 2 , 3 , 5 and 7 are prime one - digit number ; loop to compute all the digits of the number untill it is not equal to the zero ; fetching each digit of the number ; checking if digit is greater than 0 and can divides n and is prime too ; 
def countDigit(n):
    prime = [False] * 10
    prime[2] = True
    prime[3] = True
    prime[5] = True
    prime[7] = True
    temp = n
    count = 0
    while (temp != 0):
        d = temp % 10
        temp //= 10
        if (d > 0 and n % d == 0 and prime[d]):
            count += 1
    return count


n = 1032
print(countDigit(n))

# Expected output:
2
# bitwise operations on digits of a number | python 3 implementation of the approach ; function to find the digits ; function to find or of all digits of a number ; find or of all digits ; return or of digits ; function to find and of all digits of a number ; find and of all digits ; return and of digits ; function to find xor of all digits of a number ; find xor of all digits ; return xor of digits ; driver code ; find and store all digits ; find xor of digits ; find or of digits ; find and of digits ; 

digit = [0] * (100000)


def findDigits(n):
    count = 0
    while (n != 0):
        digit[count] = n % 10
        n = n // 10
        count += 1
    return count


def OR_of_Digits(n, count):
    ans = 0
    for i in range(count):
        ans = ans | digit[i]
    return ans


def AND_of_Digits(n, count):
    ans = 0
    for i in range(count):
        ans = ans & digit[i]
    return ans


def XOR_of_Digits(n, count):
    ans = 0
    for i in range(count):
        ans = ans ^ digit[i]
    return ans


def bitwise_operation(N):
    countOfDigit = findDigits(N)
    print("XOR = ", XOR_of_Digits(N, countOfDigit))
    print("OR = ", OR_of_Digits(N, countOfDigit))
    print("AND = ", AND_of_Digits(N, countOfDigit))


N = 123456
bitwise_operation(N)

# Expected output:
XOR =  7
OR =  7
AND =  0
# length of longest subarray with product greater than or equal to 0 | function that count the length of longest subarray with product greater than or equals to zero ; if product is greater than zero , return array size ; traverse the array and if any negative element found then update the length of longest subarray with the length of left and right subarray ; 
def maxLength(arr, N):
    product = 1
    Len = 0
    for i in arr:
        product *= i
    if (product >= 0):
        return N
    for i in range(N):
        if (arr[i] < 0):
            Len = max(Len, max(N - i - 1, i))
    return Len


if __name__ == '__main__':
    arr = [-1, 1, 1, -2, 3, 2, -1]
    N = len(arr)
    print(maxLength(arr, N))

# Expected output:
6
# print a pair of numbers with the given sum and product | python3 program to find any pair which has sum s and product p . ; prints roots of quadratic equation ax * 2 + bx + c = 0 ; calculating the sq root value for b * b - 4 * a * c ; finding the roots ; check if the roots are valid or not ; finding the roots ; check if the roots are valid or not ; when d < 0 ; no such pair exists in this case ; 

from math import sqrt


def findRoots(b, c):
    a = 1
    d = b * b - 4 * a * c
    sqrt_val = sqrt(abs(d))
    if (d > 0):
        x = -b + sqrt_val
        y = -b - sqrt_val
        root1 = (x) // (2 * a)
        root2 = (y) // (2 * a)
        if (root1 + root2 == -1 * b and root1 * root2 == c):
            print(int(root1), ",", int(root2))
        else:
            print(-1)
    elif (d == 0):
        root = -b // (2 * a)
        if (root + root == -1 * b and root * root == c):
            print(root, ",", root)
        else:
            print(-1)
    else:
        print(-1)


if __name__ == '__main__':
    S = 5
    P = 6
    findRoots(-S, P)
    S = 5
    P = 9
    findRoots(-S, P)

# Expected output:
3 , 2
-1
# print n numbers such that their product is a perfect cube | function to find the n numbers such that their product is a perfect cube ; loop to traverse each number from 1 to n ; print the cube of i as the ith term of the output ; 
def findNumbers(N):
    i = 1
    while (i <= N):
        print((i * i * i), end=" ")
        i += 1


if __name__ == '__main__':
    N = 4
    findNumbers(N)

# Expected output:
1 8 27 64
# sum of all proper divisors from 1 to n | utility function to find sum of all proper divisor of number up to n ; loop to iterate over all the numbers from 1 to n ; find all divisors of i and add them ; subtracting ' i ' so that the number itself is not included ; 
def properDivisorSum(n):
    sum = 0
    for i in range(n + 1):
        for j in range(1, i + 1):
            if j * j > i:
                break
            if (i % j == 0):
                if (i // j == j):
                    sum += j
                else:
                    sum += j + i // j
        sum = sum - i
    return sum


if __name__ == '__main__':
    n = 4
    print(properDivisorSum(n))
    n = 5
    print(properDivisorSum(n))

# Expected output:
5
6
# sum of all proper divisors from 1 to n | utility function to find sum of all proper divisor of number up to n ; loop to find the proper divisor of every number from 1 to n ; 
def properDivisorSum(n):
    sum = 0
    for i in range(1, n + 1):
        sum += (n // i) * i
    return sum - n * (n + 1) // 2


n = 4
print(properDivisorSum(n))
n = 5
print(properDivisorSum(n))

# Expected output:
5
6
# minimum change in given value so that it lies in all given ranges | function to find the minimum difference in the number d such that d is inside of every range ; loop to find the common range out of the given array of ranges . ; storing the start and end index ; sorting the range ; finding the maximum starting value of common segment ; finding the minimum ending value of common segment ; if there is no common segment ; if the given number is between the computed common range . ; finding the minimum distance ; 
def findMinimumOperation(n, d, arrays):
    cnt = 0
    first = -10 ** 9
    end = 10 ** 9
    while (n):
        arr = [arrays[cnt][0], arrays[cnt][1]]
        arr = sorted(arr)
        first = max(first, arr[0])
        end = min(end, arr[1])
        cnt += 1
        n -= 1
    if (first > end):
        print("-1", end="")
    else:
        if (d >= first and d <= end):
            print("0", end="")
        else:
            print(min(abs(first - d), abs(d - end)), end="")


if __name__ == '__main__':
    n = 3
    d = 3
    arrays = [[0, 7], [2, 14], [4, 6]]
    findMinimumOperation(n, d, arrays)

# Expected output:
1
# number of factors of very large number n modulo m where m is any prime number | python 3 implementation to find the number of factors of very large number n modulo m ; function for modular multiplication ; function to find the number of factors of large number n ; count the number of times 2 divides the number n ; condition to check if 2 divides it ; check for all the possible numbers that can divide it ; loop to check the number of times prime number i divides it ; condition to check if prime number i divides it ; condition to check if n at the end is a prime number . ; 

from math import sqrt
mod = 1000000007


def mult(a, b):
    return ((a % mod) * (b % mod)) % mod


def calculate_factors(n):
    cnt = 0
    ans = 1
    while (n % 2 == 0):
        cnt += 1
        n = n // 2
    if (cnt):
        ans = mult(ans, (cnt + 1))
    for i in range(3, int(sqrt(n)), 2):
        cnt = 0
        while (n % i == 0):
            cnt += 1
            n = n // i
        if (cnt):
            ans = mult(ans, (cnt + 1))
    if (n > 2):
        ans = mult(ans, 2)
    return ans % mod


if __name__ == '__main__':
    n = 19374857
    mod = 17
    print(calculate_factors(n))

# Expected output:
8
# maximum value of b less than a such that a ^ b = a + b | function to find the maximum value of b such that a ^ b = a + b ; binary representation of a ; loop to find the negation of the integer a ; output ; 
def maxValue(a):
    a = bin(a)[2:]
    b = ''
    for i in list(a):
        b += str(int(not int(i)))
    print(int(b, 2))
    return int(b, 2)


if __name__ == '__main__':
    a = 4
    maxValue(a)

# Expected output:
3
# check if n can be expressed as product of 3 distinct numbers | function to find 3 distinct number with given product ; declare a vector to store divisors ; store all divisors of number in array ; store all the occurence of divisors ; check if n is not equals to - 1 then n is also a prime factor ; initialize the variables with 1 ; check for first number a ; check for second number b ; check for third number c ; check for all unwanted condition ; 
def getnumbers(n):
    divisor = []
    for i in range(2, n + 1):
        while (n % i == 0):
            divisor .append(i)
            n //= i
    if (n != 1):
        divisor .append(n)
    a, b, c, size = 0, 0, 0, 0
    a = b = c = 1
    size = len(divisor)
    for i in range(size):
        if (a == 1):
            a = a * divisor[i]
        elif (b == 1 or b == a):
            b = b * divisor[i]
        else:
            c = c * divisor[i]
    if (a == 1 or b == 1 or c == 1 or a == b or b == c or a == c):
        print("-1")
    else:
        print(a, b, c)


n = 64
getnumbers(n)

# Expected output:
2 4 8
# check if all prime factors of number n are unique or not | function that returns the all the distinct prime factors in a vector ; if n is divisible by 2 ; divide n till all factors of 2 ; check for the prime numbers other than 2 ; store i in prime [ ] i is a factor of n ; divide n till all factors of i ; if n is greater than 2 , then n is prime number after n divided by all factors ; returns the vector prime ; function that check whether n is the product of distinct prime factors or not ; returns the vector to store all the distinct prime factors ; to find the product of all distinct prime factors ; find the product ; if product is equals to n , print yes , else print no ; 
def primeFactors(n):
    Prime = []
    if (n % 2 == 0):
        Prime .append(2)
    while (n % 2 == 0):
        n = n // 2
    for i in range(3, int(n ** (1 / 2)), 2):
        if (n % i == 0):
            Prime .append(i)
        while (n % i == 0):
            n = n // i
    if (n > 2):
        Prime .append(n)
    return Prime


def checkDistinctPrime(n):
    Prime = primeFactors(n)
    product = 1
    for i in Prime:
        product *= i
    if (product == n):
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    N = 30
    checkDistinctPrime(N)

# Expected output:
YES
# count perfect power of k in a range [ l , r ] | python 3 implementation to find the count of numbers those are powers of k in range l to r ; function to find the nth root of the number ; initially guessing a random number between 0 to 9 ; smaller eps , denotes more accuracy ; initializing difference between two roots by int_max ; loo3p until we reach desired accuracy ; calculating current value from previous value ; function to count the perfect powers of k in range l to r ; 

import sys
from math import pow, ceil, floor
import random


def nthRoot(A, N):
    xPre = (random .randint(0, 9)) % 10
    eps = 1e-3
    delX = sys .maxsize
    while (delX > eps):
        xK = ((N - 1.0) * xPre + A / pow(xPre, N - 1)) / N
        delX = abs(xK - xPre)
        xPre = xK
    return xK


def countPowers(a, b, k):
    return (floor(nthRoot(b, k)) - ceil(nthRoot(a, k)) + 1)


if __name__ == '__main__':
    a = 7
    b = 28
    k = 2
    print("Count of Powers is", countPowers(a, b, k))

# Expected output:
Count of Powers is 3
# minimum number of primes required such that their sum is equal to n | function to check if n is prime ; function to count the minimum prime required for given sum n ; case 1 : ; case 2 : ; case 3 : ; case 3 a : ; case 3 b : ; 
def isPrime(n):
    for i in range(2, int(n ** (1 / 2)) + 1):
        if (n % i == 0):
            return False
    return True


def printMinCountPrime(N):
    if (isPrime(N)):
        minCount = 1
    elif (N % 2 == 0):
        minCount = 2
    else:
        if (isPrime(N - 2)):
            minCount = 2
        else:
            minCount = 3
    print(minCount)


if __name__ == "__main__":
    N = 100
    printMinCountPrime(N)

# Expected output:
2