giulio98/xlcost-single-prompt huggingface dataset:
# ramanujan prime | python3 program to find ramanujan numbers ; function to return a vector of primes ; 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 greater than or equal to the square of it . numbers which are multiple of p and are less than p ^ 2 are already been marked . ; print all prime numbers ; function to find number of primes less than or equal to x ; binary search to find out number of primes less than or equal to x ; function to find the nth ramanujan prime ; for n >= 1 , a ( n ) < 4 * n * log ( 4 n ) ; we start from upperbound and find where pi ( i ) - pi ( i / 2 ) < n the previous number being the nth ramanujan prime ; function to find first n ramanujan numbers ; get the prime numbers ; 

from math import log, ceil
MAX = 1000000


def addPrimes():
    n = MAX
    prime = [True for i in range(n + 1)]
    for p in range(2, n + 1):
        if p * p > n:
            break
        if (prime[p]):
            for i in range(2 * p, n + 1, p):
                prime[i] = False
    ans = []
    for p in range(2, n + 1):
        if (prime[p]):
            ans .append(p)
    return ans


def pi(x, v):
    l, r = 0, len(v) - 1
    m, i = 0, -1
    while (l <= r):
        m = (l + r) // 2
        if (v[m] <= x):
            i = m
            l = m + 1
        else:
            r = m - 1
    return i + 1


def Ramanujan(n, v):
    upperbound = ceil(4 * n * (log(4 * n) / log(2)))
    for i in range(upperbound, -1, -1):
        if (pi(i, v) - pi(i / 2, v) < n):
            return 1 + i


def Ramanujan_Numbers(n):
    c = 1
    v = addPrimes()
    for i in range(1, n + 1):
        print(Ramanujan(i, v), end="")
        if (i != n):
            print(end=", ")


n = 10
Ramanujan_Numbers(n)

# Expected output:
2, 11, 17, 29, 41, 47, 59, 67, 71, 97
# quadruplet pair with xor zero in the given array | python3 implementation of the approach ; function that returns true if the array contains a valid quadruplet pair ; we can always find a valid quadruplet pair for array size greater than max ; for smaller size arrays , perform brute force ; 

MAX = 130


def validQuadruple(arr, n):
    if (n >= MAX):
        return True
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                for l in range(k + 1, n):
                    if ((arr[i] ^ arr[j] ^ arr[k] ^ arr[l]) == 0):
                        return True
    return False


arr = [1, 0, 2, 3, 7]
n = len(arr)
if (validQuadruple(arr, n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find the number of words of x vowels and y consonants that can be formed from m vowels and n consonants | function to returns factorial of n ; function to find ncr ; function to find the number of words of x vowels and y consonants can be formed from m vowels and n consonants ; 
def fact(n):
    res = 1
    for i in range(2, n + 1, 1):
        res = res * i
    return res


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


def NumberOfWays(X, Y, M, N):
    return fact(X + Y) * nCr(M, X) * nCr(N, Y)


if __name__ == '__main__':
    X = 2
    Y = 2
    M = 3
    N = 3
    print(NumberOfWays(X, Y, M, N))

# Expected output:
216
# program for sum of cosh ( x ) series upto nth term | function to return the factorial of a number ; function to return the sum of the series ; 
def fact(n):
    i, fac = 1, 1
    for i in range(1, n + 1):
        fac = fac * i
    return fac


def log_Expansion(x, n):
    Sum = 0
    i = 0
    for i in range(n):
        Sum = Sum + pow(x, 2 * i) / fact(2 * i)
    return Sum


x = 1
n = 10
print(log_Expansion(x, n))

# Expected output:
1.543080634815244
# find prime numbers in the first half and second half of an array | function to check if a number is prime or not ; function to find whether elements are prime or not ; traverse in the given range ; check if a number is prime or not ; function to print the prime numbers in the first half and second half of an array ; 
def prime(n):
    for i in range(2, n):
        if i * i > n:
            break
        if (n % i == 0):
            return False
    return True


def prime_range(start, end, a):
    for i in range(start, end):
        if (prime(a[i])):
            print(a[i], end=" ")


def Print(arr, n):
    print("Prime numbers in the", "first half are ", end="")
    prime_range(0, n // 2, arr)
    print()
    print("Prime numbers in the", "second half are ", end="")
    prime_range(n // 2, n, arr)
    print()


arr = [2, 5, 10, 15, 17, 21, 23]
n = len(arr)
Print(arr, n)

# Expected output:
Prime numbers in the first half are 2 5 
Prime numbers in the second half are 17 23
# count of elements that can be deleted without disturbing the mean of the initial array | function to find the elements which do not change the mean on removal ; to store the sum of the array elements ; to store the initial mean ; to store the count of required elements ; iterate over the array ; finding the new mean ; if the new mean equals to the initial mean ; 
def countElements(arr, n):
    Sum = 0
    for i in range(n):
        Sum += arr[i]
    mean = Sum / n
    cnt = 0
    for i in range(n):
        newMean = (Sum - arr[i]) / (n - 1)
        if (newMean == mean):
            cnt += 1
    return cnt


arr = [1, 2, 3, 4, 5]
n = len(arr)
print(countElements(arr, n))

# Expected output:
1
# find maximum xor of k elements in an array | python implementation of the approach ; function to return the maximum xor for a subset of size j from the given array ; if the subset is complete then return the xor value of the selected elements ; return if already calculated for some mask and j at the i 'th index ; initialize answer to 0 ; if we can still include elements in our subset include the i 'th element ; exclude the i 'th element  ans store the max of both operations ; 

MAX = 10000
MAX_ELEMENT = 50
dp = [[[-1 for i in range(MAX)]for j in range(MAX_ELEMENT)]
      for k in range(MAX_ELEMENT)]


def Max_Xor(arr, i, j, mask, n):
    if (i >= n):
        if (j == 0):
            return mask
        else:
            return 0
    if (dp[i][j][mask] != -1):
        return dp[i][j][mask]
    ans = 0
    if (j > 0):
        ans = Max_Xor(arr, i + 1, j - 1, mask ^ arr[i], n)
    ans = max(ans, Max_Xor(arr, i + 1, j, mask, n))
    dp[i][j][mask] = ans
    return ans


arr = [2, 5, 4, 1, 3, 7, 6, 8]
n = len(arr)
k = 3
print(Max_Xor(arr, 0, k, 0, n))

# Expected output:
15
# find the prime p using given four integers | python3 program to possible prime number ; function to check if a number is prime or not ; function to find possible prime number ; find a possible prime number ; last condition ; 

from math import sqrt


def Prime(n):
    for j in range(2, int(sqrt(n)) + 1, 1):
        if (n % j == 0):
            return False
    return True


def find_prime(x, xsqmodp, y, ysqmodp):
    n = x * x - xsqmodp
    n1 = y * y - ysqmodp
    for j in range(2, max(int(sqrt(n)), int(sqrt(n1))), 1):
        if (n %
            j == 0 and (x * x) %
            j == xsqmodp and n1 %
            j == 0 and (y * y) %
                j == ysqmodp):
            if (Prime(j)):
                return j
        j1 = n // j
        if (n %
            j1 == 0 and (x * x) %
            j1 == xsqmodp and n1 %
            j1 == 0 and (y * y) %
                j1 == ysqmodp):
            if (Prime(j1)):
                return j1
        j1 = n1 // j
        if (n %
            j1 == 0 and (x * x) %
            j1 == xsqmodp and n1 %
            j1 == 0 and (y * y) %
                j1 == ysqmodp):
            if (Prime(j1)):
                return j1
    if (n == n1):
        return n


if __name__ == '__main__':
    x = 3
    xsqmodp = 0
    y = 5
    ysqmodp = 1
    print(find_prime(x, xsqmodp, y, ysqmodp))

# Expected output:
3
# time taken per hour for stoppage of car | python3 implementation of the approach ; function to return the time taken per hour for stoppage ; 

import math


def numberOfMinutes(S, S1):
    Min = 0
    Min = ((S - S1) / math .floor(S)) * 60
    return int(Min)


if __name__ == '__main__':
    S, S1 = 30, 10
    print(numberOfMinutes(S, S1), "min")

# Expected output:
40 min
# removing a number from array without changing its arithmetic mean | function to remove a number from the array without changing its arithmetic mean ; find sum of all elements ; if mean is an integer ; check if mean is present in the array or not ; 
def FindElement(a, n):
    s = 0
    for i in range(n):
        s = s + a[i]
    if s % n == 0:
        m = s // n
        for j in range(n):
            if a[j] == m:
                return m
    return -1


if __name__ == "__main__":
    a = [1, 2, 3, 4, 5]
    n = len(a)
    print(FindElement(a, n))

# Expected output:
3
# sum of the tan ( x ) expansion upto n terms | function to find factorial of a number ; to store factorial of a number ; return the factorial of a number ; function to find tan ( x ) upto n terms ; to store value of the expansion ; this loops here calculate bernoulli number which is further used to get the coefficient in the expansion of tan x ; print the value of expansion ; 
def fac(num):
    if (num == 0):
        return 1
    fact = 1
    for i in range(1, num + 1):
        fact = fact * i
    return fact


def Tanx_expansion(terms, x):
    sum = 0
    for i in range(1, terms + 1):
        B = 0
        Bn = 2 * i
        for k in range(Bn + 1):
            temp = 0
            for r in range(0, k + 1):
                temp = temp + pow(-1, r) * fac(k) * \
                    pow(r, Bn) / (fac(r) * fac(k - r))
            B = B + temp / ((k + 1))
        sum = sum + pow(-4, i) * (1 - pow(4, i)) * B * \
            pow(x, 2 * i - 1) / fac(2 * i)
    print("%.9f" % (sum))


if __name__ == '__main__':
    n, x = 6, 1
    Tanx_expansion(n, x)

# Expected output:
1.551367645
# minimum elements to be inserted in array to make adjacent differences equal | function to find gcd of two numbers ; function to calculate minimum numbers to be inserted to make equal differences between two consecutive elements ; check if there is only one element in the array then answer will be 0 ; calculate difference between first and second element of array ; if there is only two elements in the array then gcd of differences of consecutive elements of array will be equal to difference of first and second element of the array ; loop to calculate the gcd of the differences between consecutive elements of the array ; loop to calculate the elements to be inserted ; return the answer ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def minimum_elements(n, arr):
    if (n < 3):
        return 0
    ans = 0
    diff = arr[1] - arr[0]
    g = diff
    for i in range(2, n):
        diff = arr[i] - arr[i - 1]
        g = gcd(g, diff)
    for i in range(1, n):
        diff = arr[i] - arr[i - 1]
        cnt = diff // g
        ans += (cnt - 1)
    return ans


arr = [1, 5, 8, 10, 12, 16]
n = len(arr)
print(minimum_elements(n, arr))

# Expected output:
10
# check if a number is fermat pseudoprime | python3 program to check if n is fermat pseudoprime to the base a or not ; function to check if the given number is composite ; check if there is any divisor of n less than sqrt ( n ) ; effectively calculate ( x ^ y ) modulo mod ; initialize result ; if power is odd , then update the answer ; square the number and reduce the power to its half ; return the result ; function to check for fermat pseudoprime ; if it is composite and satisfy fermat criterion ; else return 0 ; 

from math import sqrt


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


def power(x, y, mod):
    res = 1
    while (y):
        if (y & 1):
            res = (res * x) % mod
        y = y >> 1
        x = (x * x) % mod
    return res


def Check(n, a):
    if (a > 1 and checkcomposite(n) and power(a, n - 1, n) == 1):
        return 1
    return 0


if __name__ == '__main__':
    N = 645
    a = 2
    print(Check(N, a))

# Expected output:
1
# largest and smallest digit of a number | function to the largest and smallest digit of a number ; find the largest digit ; find the smallest digit ; 
def Digits(n):
    largest = 0
    smallest = 9
    while (n):
        r = n % 10
        largest = max(r, largest)
        smallest = min(r, smallest)
        n = n // 10
    print(largest, smallest)


n = 2346
Digits(n)

# Expected output:
6 2
# probability of distributing m items among x bags such that first bag contains n items | function to find factorial of a number ; function to find ncr ; function to find probability of first bag to contain n items such that m items are distributed among x bags ; 
def factorial(n):
    if (n <= 1):
        return 1
    return n * factorial(n - 1)


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


def Probability(M, N, X):
    return float(nCr(M - N - 1, X - 2) / (nCr(M - 1, X - 1) * 1.0))


if __name__ == "__main__":
    M = 9
    X = 3
    N = 4
    print(Probability(M, N, X))

# Expected output:
0.14285714285714285
# count of n digit numbers possible which satisfy the given conditions | function to return the factorial of n ; function to return the count of numbers possible ; 
def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
    return res


def Count_number(N):
    return (N * fact(N))


N = 2
print(Count_number(N))

# Expected output:
4
# find the count of m character words which have at least one character repeated | function to return the factorial of a number ; function to return the value of npr ; function to return the total number of m length words which have at least a single character repeated more than once ; 
def fact(n):
    if (n <= 1):
        return 1
    return n * fact(n - 1)


def nPr(n, r):
    return fact(n) // fact(n - r)


def countWords(N, M):
    return pow(N, M) - nPr(N, M)


N = 10
M = 5
print(countWords(N, M))

# Expected output:
69760
# minimum and maximum possible length of the third side of a triangle | function to find the minimum and the maximum possible length of the third side of the given triangle ; not a valid triangle ; not a valid triangle ; 
def find_length(s1, s2):
    if (s1 <= 0 or s2 <= 0):
        print(-1, end="")
        return
    max_length = s1 + s2 - 1
    min_length = max(s1, s2) - min(s1, s2) + 1
    if (min_length > max_length):
        print(-1, end="")
        return
    print("Max =", max_length)
    print("Min =", min_length)


if __name__ == "__main__":
    s1 = 8
    s2 = 5
    find_length(s1, s2)

# Expected output:
Max = 12
Min = 4
# number formed by the rightmost set bit in n | function to return the integer formed by taking the rightmost set bit in n ; n & ( n - 1 ) unsets the first set bit from the right in n ; take xor with the original number the position of the ' changed ▁ bit ' will be set and rest will be unset ; 
def firstSetBit(n):
    x = n & (n - 1)
    return (n ^ x)


n = 12
print(firstSetBit(n))

# Expected output:
4
# number of n length sequences whose product is m | function to calculate the value of ncr effectively ; initializing the result ; multiply and divide simultaneously to avoid overflow ; return the answer ; function to return the number of sequences of length n such that their product is m ; hashmap to store the prime factors of m ; calculate the prime factors of m ; if i divides m it means it is a factor divide m by i till it could be divided to store the exponent ; increase the exponent count ; if the number is a prime number greater than sqrt ( m ) ; initializing the ans ; multiply the answer for every prime factor ; it . second represents the exponent of every prime factor ; return the result ; 
def ncr(n, r):
    res = 1
    for i in range(1, r + 1):
        res *= (n - r + i)
        res //= i
    return res


def NoofSequences(N, M):
    prime = {}
    for i in range(2, int(M ** (.5)) + 1):
        while (M % i == 0):
            prime[i] = prime .get(i, 0) + 1
            M //= i
    if (M > 1):
        prime[M] = prime .get(M, 0) + 1
    ans = 1
    for it in prime:
        ans *= (ncr(N + prime[it] - 1, N - 1))
    return ans


N = 2
M = 6
print(NoofSequences(N, M))

# Expected output:
4
# number of hours after which the second person moves ahead of the first person if they travel at a given speed | function to return the number of hours for the second person to move ahead ; time taken to equalize ; time taken to move ahead ; 
def findHours(a, b, k):
    if (a >= b):
        return -1
    time = k // (b - a)
    time = time + 1
    return time


a = 4
b = 5
k = 1
print(findHours(a, b, k))

# Expected output:
2
# number of ways of distributing n identical objects in r distinct groups with no groups empty | function to return the value of ncr effectively ; initialize the answer ; divide simultaneously by i to avoid overflow ; function to return the number of ways to distribute n identical objects in r distinct objects ; 
def ncr(n, r):
    ans = 1
    for i in range(1, r + 1):
        ans *= (n - r + i)
        ans //= i
    return ans


def NoOfDistributions(N, R):
    return ncr(N - 1, R - 1)


N = 4
R = 3
print(NoOfDistributions(N, R))

# Expected output:
3
# number of ways of distributing n identical objects in r distinct groups | function to return the value of ncr effectively ; initialize the answer ; divide simultaneously by i to avoid overflow ; function to return the number of ways to distribute n identical objects in r distinct objects ; 
def ncr(n, r):
    ans = 1
    for i in range(1, r + 1):
        ans *= (n - r + i)
        ans //= i
    return ans


def NoOfDistributions(N, R):
    return ncr(N + R - 1, R - 1)


N = 4
R = 3
print(NoOfDistributions(N, R))

# Expected output:
15
# smallest perfect cube in an array | python3 implementation of the approach ; function that returns true if n is a perfect cube ; takes the sqrt of the number ; checks if it is a perfect cube number ; function to return the smallest perfect cube from the array ; stores the minimum of all the perfect cubes from the array ; traverse all elements in the array ; store the minimum if current element is a perfect cube ; 

import sys


def checkPerfectcube(n):
    d = int(n ** (1 / 3))
    if (d * d * d == n):
        return True
    return False


def smallestPerfectCube(a, n):
    mini = sys .maxsize
    for i in range(n):
        if (checkPerfectcube(a[i])):
            mini = min(a[i], mini)
    return mini


if __name__ == "__main__":
    a = [16, 8, 25, 2, 3, 10]
    n = len(a)
    print(smallestPerfectCube(a, n))

# Expected output:
8
# find two vertices of an isosceles triangle in which there is rectangle with opposite corners ( 0 , 0 ) and ( x , y ) | function to find two vertices of an isosceles triangle in which there is rectangle with opposite side ( 0 , 0 ) and ( x , y ) ; required value ; ; print x1 and y1 ; print x2 and y3 ; 
def Vertices(x, y):
    val = abs(x) + abs(y)
    if x < 0:
        x = -1
    else:
        x = 1
    print(val * x, "0", end=" ")
    if y < 0:
        y = -1
    else:
        y = 1
    print("0", val * y)


if __name__ == "__main__":
    x = 3
    y = 3
    Vertices(x, y)

# Expected output:
6 0 0 6
# find a subarray whose sum is divisible by size of the array | function to find a subarray whose sum is a multiple of n ; prefix sum array to store cumulative sum ; single state dynamic programming relation for prefix sum array ; generating all sub - arrays ; if the sum of the sub - array [ i : j ] is a multiple of n ; if the function reaches here it means there are no subarrays with sum as a multiple of n ; 
def CheckSubarray(arr, N):
    presum = [0 for i in range(N + 1)]
    for i in range(1, N + 1):
        presum[i] = presum[i - 1] + arr[i - 1]
    for i in range(1, N + 1):
        for j in range(i, N + 1):
            if ((presum[j] - presum[i - 1]) % N == 0):
                print(i - 1, j - 1)
                return
    print("-1")


arr = [7, 5, 3, 7]
N = len(arr)
CheckSubarray(arr, N)

# Expected output:
0 1
# find a subarray whose sum is divisible by size of the array | function to check is there exists a subarray whose sum is a multiple of n ; prefix sum array to store cumulative sum ; single state dynamic programming relation for prefix sum array ; modulo class vector ; storing the index value in the modulo class vector ; if there exists a sub - array with starting index equal to zero ; in this class , there are more than two presums % n hence difference of any two subarrays would be a multiple of n ; 0 based indexing ; 
def CheckSubarray(arr, N):
    presum = [0 for i in range(N + 1)]
    for i in range(1, N + 1):
        presum[i] = presum[i - 1] + arr[i - 1]
    moduloclass = [[]] * N
    for i in range(1, N + 1, 1):
        moduloclass[presum[i] % N].append(i - 1)
    if (len(moduloclass[0]) > 0):
        print(0 + 1, moduloclass[0][0] + 2)
        return
    for i in range(1, N):
        if (len(moduloclass[i]) >= 2):
            print(moduloclass[i][0] + 1, moduloclass[i][1])
            return


if __name__ == '__main__':
    arr = [7, 3, 5, 2]
    N = len(arr)
    CheckSubarray(arr, N)

# Expected output:
1 2
# smallest number greater or equals to n such that it has no odd positioned bit set | function to count the total bits ; iterate and find the number of set bits ; right shift the number by 1 ; function to find the nearest number ; count the total number of bits ; to get the position ; if the last set bit is at odd position then answer will always be a number with the left bit set ; set all the even bits which are possible ; if the number still is less than n ; return the number by setting the next even set bit ; if we have reached this position it means tempsum > n hence turn off even bits to get the first possible number ; turn off the bit ; if it gets lower than n then set it and return that number ; 
def countBits(n):
    count = 0
    while (n > 0):
        count += 1
        n >>= 1
    return count


def findNearestNumber(n):
    cnt = countBits(n)
    cnt -= 1
    if (cnt % 2):
        return 1 << (cnt + 1)
    else:
        tempnum = 0
        for i in range(0, cnt + 1, 2):
            tempnum += 1 << i
        if (tempnum < n):
            return (1 << (cnt + 2))
        elif (tempnum == n):
            return n
        for i in range(0, cnt + 1, 2):
            tempnum -= (1 << i)
            if (tempnum < n):
                tempnum += (1 << i)
                return tempnum


n = 19
print(findNearestNumber(n))

# Expected output:
20
# count of numbers whose 0 th and nth bits are set | function to return the count of n - bit numbers whose 0 th and nth bits are set ; 
def countNum(n):
    if (n == 1):
        return 1
    count = pow(2, n - 2)
    return count


n = 3
print(countNum(n))

# Expected output:
2
# find a number containing n | function to return the generated by appending "10" n - 1 times ; initialising as empty ; function to return the decimal equivalent of the given binary string ; initializing base value to 1 i . e 2 ^ 0 ; function that calls the constructstring and binarytodecimal and returns the answer ; 
def constructString(n):
    s = ""
    for i in range(n):
        s += "10"
    return s


def binaryToDecimal(n):
    num = n
    dec_value = 0
    base = 1
    Len = len(num)
    for i in range(Len - 1, -1, -1):
        if (num[i] == '1'):
            dec_value += base
        base = base * 2
    return dec_value


def findNumber(n):
    s = constructString(n - 1)
    num = binaryToDecimal(s)
    return num


n = 4
print(findNumber(n))

# Expected output:
42