giulio98/xlcost-single-prompt huggingface dataset:
# program to find n | return n - th number in series made of 3 and 5 ; create an array of size ( n + 1 ) ; if i is odd ; 
def printNthElement(n):
    arr = [0] * (n + 1)
    arr[1] = 3
    arr[2] = 5
    for i in range(3, n + 1):
        if (i % 2 != 0):
            arr[i] = arr[i // 2] * 10 + 3
        else:
            arr[i] = arr[(i // 2) - 1] * 10 + 5
    return arr[n]


n = 6
print(printNthElement(n))

# Expected output:
55
# Program to find Nth term of the series 3 , 6 , 18 , 24 , ... | function to calculate Nth term of series ; By using above formula ; get the value of N ; Calculate and print the Nth term
def nthTerm(N):
    return (N * ((N // 2) + ((N % 2) * 2) + N))


if __name__ == '__main__':
    N = 5
    print("Nth term for N = ", N, " : ", nthTerm(N))

# Expected output:
Nth term for N =  5  :  45
# program to print binomial expansion series | function to print the series ; calculating and printing first term ; computing and printing remaining terms ; find current term using previous terms we increment power of x by 1 , decrement power of a by 1 and compute nci using previous term by multiplying previous term with ( n - i + 1 ) / i ; 
def series(A, X, n):
    term = pow(A, n)
    print(term, end=" ")
    for i in range(1, n + 1):
        term = int(term * X * (n - i + 1) / (i * A))
        print(term, end=" ")


A = 3
X = 4
n = 5
series(A, X, n)

# Expected output:
243 1620 4320 5760 3840 1024
# check if a number is divisible by 8 using bitwise operators | python program to check whether the number is divisible by 8 or not using bitwise operator ; function to check number is div by 8 or not using bitwise operator ; 

import math


def Div_by_8(n):
    return (((n >> 3) << 3) == n)


n = 16
if (Div_by_8(n)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# average of even numbers till a given even number | function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; 
def averageEven(n):
    if (n % 2 != 0):
        print("Invalid Input")
        return -1
    sm = 0
    count = 0
    while (n >= 2):
        count = count + 1
        sm = sm + n
        n = n - 2
    return sm // count


n = 16
print(averageEven(n))

# Expected output:
9
# average of even numbers till a given even number | function to calculate the average of even numbers ; 
def averageEven(n):
    if (n % 2 != 0):
        print("Invalid Input")
        return -1
    return (n + 2) // 2


n = 16
print(averageEven(n))

# Expected output:
9
# Largest number that divides x and is co | Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; function to find largest coprime divisor ; divisor code
def gcd(a, b):
    if a == 0 or b == 0:
        return 0
    if a == b:
        return a
    if a > b:
        return gcd(a - b, b)
    return gcd(a, b - a)


def cpFact(x, y):
    while gcd(x, y) != 1:
        x = x / gcd(x, y)
    return int(x)


x = 15
y = 3
print(cpFact(x, y))
x = 14
y = 28
print(cpFact(x, y))
x = 7
y = 3
print(cpFact(x, y))

# Expected output:
5
1
7
# count numbers with unit digit k in given range | returns count of numbers with k as last digit . ; 
def counLastDigitK(low, high, k):
    count = 0
    for i in range(low, high + 1):
        if (i % 10 == k):
            count += 1
    return count


low = 3
high = 35
k = 3
print(counLastDigitK(low, high, k))

# Expected output:
4
# taxicab numbers | python3 implementation to print first n taxicab ( 2 ) numbers ; starting from 1 , check every number if it is taxicab until count reaches n . ; try all possible pairs ( j , k ) whose cube sums can be i . ; taxicab ( 2 ) found ; 

import math


def printTaxicab2(N):
    i, count = 1, 0
    while (count < N):
        int_count = 0
        for j in range(1, math .ceil(pow(i, 1.0 / 3)) + 1):
            for k in range(j + 1, math .ceil(pow(i, 1.0 / 3)) + 1):
                if (j * j * j + k * k * k == i):
                    int_count += 1
        if (int_count == 2):
            count += 1
            print(count, " ", i)
        i += 1


N = 5
printTaxicab2(N)

# Expected output:
1   1729
2   4104
3   13832
4   20683
5   32832
# composite number | a optimized school method based python program to check if a number is composite . ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; 
def isComposite(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return False
    if (n % 2 == 0 or n % 3 == 0):
        return True
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return True
        i = i + 6
    return False


print("true")if (isComposite(11))else print("false")
print("true")if (isComposite(15))else print("false")

# Expected output:
false
true
# insert minimum number in array so that sum of array becomes prime | function to check if a number is prime or not ; corner case ; check from 2 to n - 1 ; find prime number greater than a number ; find prime greater than n ; check if num is prime ; increment num ; to find number to be added so sum of array is prime ; to find sum of array elements ; if sum is already prime return 0 ; to find prime number greater than sum ; return difference of sum and num ; 
def isPrime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True


def findPrime(n):
    num = n + 1
    while (num):
        if isPrime(num):
            return num
        num += 1
    return 0


def minNumber(arr):
    s = 0
    for i in range(0, len(arr)):
        s += arr[i]
    if isPrime(s):
        return 0
    num = findPrime(s)
    return num - s


arr = [2, 4, 6, 8, 12]
print(minNumber(arr))

# Expected output:
5
# sum of divisors of factorial of a number | function to calculate factorial ; function to calculate sum of divisor ; returns sum of divisors of n ! ; 
def fact(n):
    if (n == 0):
        return 1
    return n * fact(n - 1)


def div(x):
    ans = 0
    for i in range(1, x + 1):
        if (x % i == 0):
            ans += i
    return ans


def sumFactDiv(n):
    return div(fact(n))


n = 4
print(sumFactDiv(n))

# Expected output:
60
# sum of divisors of factorial of a number | allprimes [ ] stores all prime numbers less than or equal to n . ; fills above vector allprimes [ ] for a given n ; 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 . ; loop to update prime [ ] ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; store primes in the vector allprimes ; function to find all result of factorial number ; initialize result ; find exponents of all primes which divides n and less than n ; current divisor ; find the highest power ( stored in exp ) ' ▁ ▁ of ▁ allprimes [ i ] ▁ that ▁ divides ▁ n ▁ using ▁ ▁ legendre ' s formula . ; using the divisor function to calculate the sum ; return total divisors ; 

allPrimes = []


def sieve(n):
    prime = [True] * (n + 1)
    p = 2
    while (p * p <= n):
        if (prime[p]):
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    for p in range(2, n + 1):
        if (prime[p]):
            allPrimes .append(p)


def factorialDivisors(n):
    result = 1
    for i in range(len(allPrimes)):
        p = allPrimes[i]
        exp = 0
        while (p <= n):
            exp = exp + int(n / p)
            p = p * allPrimes[i]
        result = int(
            result * (pow(allPrimes[i], exp + 1) - 1) / (allPrimes[i] - 1))
    return result


print(factorialDivisors(4))

# Expected output:
1
# pandigital number in a given base | return true if n is pandigit else return false . ; checking length is less than base ; traversing each digit of the number . ; if digit is integer ; if digit is alphabet ; checking hash array , if any index is unmarked . ; 
def checkPandigital(b, n):
    if (len(n) < b):
        return 0
    hash = [0] * b
    for i in range(len(n)):
        if (n[i] >= '0' and n[i] <= '9'):
            hash[ord(n[i]) - ord('0')] = 1
        elif (ord(n[i]) - ord('A') <= b - 11):
            hash[ord(n[i]) - ord('A') + 10] = 1
    for i in range(b):
        if (hash[i] == 0):
            return 0
    return 1


b = 13
n = "1298450376ABC"
if (checkPandigital(b, n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# convert a number m to n using minimum number of given operations | function to find minimum number of given operations to convert m to n ; only way is to do - 1 ( m - n ) : times ; not possible ; n is greater and n is odd ; perform ' - 1' on m ( or + 1 on n ) : ; n is even ; perform ' * 2' on m ( or n / 2 on n ) : ; 
def conver(m, n):
    if (m == n):
        return 0
    if (m > n):
        return m - n
    if (m <= 0 and n > 0):
        return -1
    if (n % 2 == 1):
        return 1 + conver(m, n + 1)
    else:
        return 1 + conver(m, n / 2)


m = 3
n = 11
print("Minimum number of operations :", conver(m, n))

# Expected output:
Minimum number of operations : 3
# seeds ( or seed roots ) of a number | python3 program to find seed of a number ; stores product of digits of x in proddig [ x ] ; if x has single digit ; if digit product is already computed ; if digit product is not computed before . ; prints all seeds of n ; find all seeds using proddig [ ] ; if there was no seed ; print seeds ; 

MAX = 10000
prodDig = [0] * MAX


def getDigitProduct(x):
    if (x < 10):
        return x
    if (prodDig[x] != 0):
        return prodDig[x]
    prod = (int(x % 10) * getDigitProduct(int(x / 10)))
    prodDig[x] = prod
    return prod


def findSeed(n):
    res = []
    for i in range(1, int(n / 2 + 2)):
        if (i * getDigitProduct(i) == n):
            res .append(i)
    if (len(res) == 0):
        print("NO seed exists")
        return
    for i in range(len(res)):
        print(res[i], end=" ")


n = 138
findSeed(n)

# Expected output:
23
# number with maximum number of prime factors | python 3 program to find integer having maximum number of prime factor in first n natural numbers . ; return smallest number having maximum prime factors . ; sieve of eratosthenes method to count number of prime factors . ; finding number having maximum number of prime factor . ; 

from math import sqrt


def maxPrimefactorNum(N):
    arr = [0 for i in range(N + 5)]
    for i in range(2, int(sqrt(N)) + 1, 1):
        if (arr[i] == 0):
            for j in range(2 * i, N + 1, i):
                arr[j] += 1
        arr[i] = 1
    maxval = 0
    maxint = 1
    for i in range(1, N + 1, 1):
        if (arr[i] > maxval):
            maxval = arr[i]
            maxint = i
    return maxint


if __name__ == '__main__':
    N = 40
    print(maxPrimefactorNum(N))

# Expected output:
30
# sum of all subarrays | set 1 | function compute sum all sub - array ; computing sum of subarray using formula ; return all subarray sum ; 
def SubArraySum(arr, n):
    result = 0
    for i in range(0, n):
        result += (arr[i] * (i + 1) * (n - i))
    return result


arr = [1, 2, 3]
n = len(arr)
print("Sum of SubArray : ", SubArraySum(arr, n))

# Expected output:
Sum of SubArray :  20
# highest power of 2 less than or equal to given number | python3 program to find highest power of 2 smaller than or equal to n . ; if i is a power of 2 ; 
def highestPowerof2(n):
    res = 0
    for i in range(n, 0, -1):
        if ((i & (i - 1)) == 0):
            res = i
            break
    return res


n = 10
print(highestPowerof2(n))

# Expected output:
8
# find cube pairs | set 2 ( a n ^ ( 1 / 3 ) solution ) | python3 program to find pairs that can represent the given number as sum of two cubes ; function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create a array of size of size 'cuberoot ; for index i , cube [ i ] will contain i ^ 3 ; find all pairs in above sorted array cube [ ] whose sum is equal to n ; 

import math


def findPairs(n):
    cubeRoot = int(math.pow(n, 1.0 / 3.0))
    cube = [0] * (cubeRoot + 1)
    for i in range(1, cubeRoot + 1):
        cube[i] = i * i * i
    l = 1
    r = cubeRoot

    while (l < r):
        if (cube[l] + cube[r] < n):
            l += 1
        elif (cube[l] + cube[r] > n):
            r -= 1
        else:
            print("(", l, ", ", math.floor(r),
                  ")", end="")
            print()
            l += 1
            r -= 1


n = 20683
findPairs(n)

# Expected output:
( 10 ,  27 )
( 19 ,  24 )
# find cube pairs | set 1 ( a n ^ ( 2 / 3 ) solution ) | function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create an empty map ; consider all pairs such with values less than cuberoot ; find sum of current pair ( x , y ) ; do nothing if sum is not equal to given number ; if sum is seen before , we found two pairs ; if sum is seen for the first time ; 
def findPairs(n):
    cubeRoot = pow(n, 1.0 / 3.0)
    s = {}
    for x in range(int(cubeRoot)):
        for y in range(x + 1, int(cubeRoot) + 1):
            sum = x * x * x + y * y * y
            if (sum != n):
                continue
            if sum in s .keys():
                print("(" +
                      str(s[sum][0]) +
                      ", " +
                      str(s[sum][1]) +
                      ") and (" +
                      str(x) +
                      ", " +
                      str(y) +
                      ")" +
                      "")
            else:
                s[sum] = [x, y]


if __name__ == "__main__":
    n = 13832
    findPairs(n)

# Expected output:
(2, 24) and (18, 20)
# find the minimum difference between shifted tables of two numbers | python3 program to find the minimum difference between any two terms of two tables ; utility function to find gcd of a and b ; returns minimum difference between any two terms of shifted tables of ' a ' and ' b ' . ' x ' is shift in table of ' a ' and ' y ' is shift in table of ' b ' . ; calculate gcd of a nd b ; calculate difference between x and y ; 

import math as mt


def gcd(a, b):
    while (b != 0):
        t = b
        b = a % b
        a = t
    return a


def findMinDiff(a, b, x, y):
    g = gcd(a, b)
    diff = abs(x - y) % g
    return min(diff, g - diff)


a, b, x, y = 20, 52, 5, 7
print(findMinDiff(a, b, x, y))

# Expected output:
2
# find all divisors of a natural number | set 2 | a o ( sqrt ( n ) ) java program that prints all divisors in sorted order ; method to print the divisors ; list to store half of the divisors ; check if divisors are equal ; otherwise print both ; the list will be printed in reverse ; 

import math


def printDivisors(n):
    list = []
    for i in range(1, int(math .sqrt(n) + 1)):
        if (n % i == 0):
            if (n / i == i):
                print(i, end=" ")
            else:
                print(i, end=" ")
                list .append(int(n / i))
    for i in list[::-1]:
        print(i, end=" ")


print("The divisors of 100 are: ")
printDivisors(100)

# Expected output:
The divisors of 100 are: 
1 2 4 5 10 20 25 50 100
# find all divisors of a natural number | set 2 | a o ( sqrt ( n ) ) program that prints all divisors in sorted order ; function to print the divisors ; 

from math import *


def printDivisors(n):
    i = 1
    while (i * i < n):
        if (n % i == 0):
            print(i, end=" ")
        i += 1
    for i in range(int(sqrt(n)), 0, -1):
        if (n % i == 0):
            print(n // i, end=" ")


print("The divisors of 100 are: ")
printDivisors(100)

# Expected output:
The divisors of 100 are: 
1 2 4 5 10 20 25 50 100
# find all factors of a natural number | set 1 | method to print the divisors ; 
def printDivisors(n):
    i = 1
    while i <= n:
        if (n % i == 0):
            print(i,)
        i = i + 1


print("The divisors of 100 are: ")
printDivisors(100)

# Expected output:
The divisors of 100 are: 
1
2
4
5
10
20
25
50
100
# find all factors of a natural number | set 1 | a better ( than naive ) solution to find all divisiors ; method to print the divisors ; note that this loop runs till square root ; if divisors are equal , print only one ; otherwise print both ; 

import math


def printDivisors(n):
    i = 1
    while i <= math .sqrt(n):
        if (n % i == 0):
            if (n / i == i):
                print(i,)
            else:
                print(i, n / i,)
        i = i + 1


print("The divisors of 100 are: ")
printDivisors(100)

# Expected output:
The divisors of 100 are: 
1 100.0
2 50.0
4 25.0
5 20.0
10
# sieve of atkin | python 3 program for implementation of sieve of atkin ; 2 and 3 are known to be prime ; initialise the sieve array with false values ; mark sieve [ n ] is true if one of the following is true : a ) n = ( 4 * x * x ) + ( y * y ) has odd number of solutions , i . e . , there exist odd number of distinct pairs ( x , y ) that satisfy the equation and n % 12 = 1 or n % 12 = 5. b ) n = ( 3 * x * x ) + ( y * y ) has odd number of solutions and n % 12 = 7 c ) n = ( 3 * x * x ) - ( y * y ) has odd number of solutions , x > y and n % 12 = 11 ; main part of sieve of atkin ; mark all multiples of squares as non - prime ; print primes using sieve [ ] ; 
def SieveOfAtkin(limit):
    if (limit > 2):
        print(2, end=" ")
    if (limit > 3):
        print(3, end=" ")
    sieve = [False] * limit
    for i in range(0, limit):
        sieve[i] = False
    x = 1
    while (x * x < limit):
        y = 1
        while (y * y < limit):
            n = (4 * x * x) + (y * y)
            if (n <= limit and (n % 12 == 1 or n % 12 == 5)):
                sieve[n] ^= True
            n = (3 * x * x) + (y * y)
            if (n <= limit and n % 12 == 7):
                sieve[n] ^= True
            n = (3 * x * x) - (y * y)
            if (x > y and n <= limit and n % 12 == 11):
                sieve[n] ^= True
            y += 1
        x += 1
    r = 5
    while (r * r < limit):
        if (sieve[r]):
            for i in range(r * r, limit, r * r):
                sieve[i] = False
    for a in range(5, limit):
        if (sieve[a]):
            print(a, end=" ")


limit = 20
SieveOfAtkin(limit)

# Expected output:
2 3 5 7 11 13 17 19
# find if a point lies inside a circle | python3 program to check if a point lies inside a circle or not ; compare radius of circle with distance of its center from given point ; 
def isInside(circle_x, circle_y, rad, x, y):
    if ((x - circle_x) * (x - circle_x) +
            (y - circle_y) * (y - circle_y) <= rad * rad):
        return True
    else:
        return False


x = 1
y = 1
circle_x = 0
circle_y = 1
rad = 2
if (isInside(circle_x, circle_y, rad, x, y)):
    print("Inside")
else:
    print("Outside")

# Expected output:
Inside
# program to check if a given number is lucky ( all digits are different ) | python program to check if a given number is lucky ; this function returns true if n is lucky ; create an array of size 10 and initialize all elements as false . this array is used to check if a digit is already seen or not . ; traverse through all digits of given number ; find the last digit ; if digit is already seen , return false ; mark this digit as seen ; remove the last digit from number ; 

import math


def isLucky(n):
    ar = [0] * 10
    while (n > 0):
        digit = math .floor(n % 10)
        if (ar[digit]):
            return 0
        ar[digit] = 1
        n = n / 10
    return 1


arr = [1291, 897, 4566, 1232, 80, 700]
n = len(arr)
for i in range(0, n):
    k = arr[i]
    if (isLucky(k)):
        print(k, " is Lucky ")
    else:
        print(k, " is not Lucky ")

# Expected output:
1291  is not Lucky 
897  is not Lucky 
4566  is not Lucky 
1232  is not Lucky 
80  is not Lucky 
700  is not Lucky
# print squares of first n natural numbers without using * , / and | python3 program to print squares of first ' n ' natural numbers wothout using * , / and - ; initialize ' square ' and first odd number ; calculate and print squares ; print square ; update ' square ' and 'odd ; 
def printSquares(n):
    square = 0
    odd = 1
    for x in range(0, n):
        print(square, end=" ")
        square = square + odd
        odd = odd + 2


n = 5
printSquares(n)

# Expected output:
0 1 4 9 16
# write a program to reverse digits of a number | python 3 program to reverse digits of a number ; recursive function to reverse digits of num ; 

rev_num = 0
base_pos = 1


def reversDigits(num):
    global rev_num
    global base_pos
    if (num > 0):
        reversDigits((int)(num / 10))
        rev_num += (num % 10) * base_pos
        base_pos *= 10
    return rev_num


num = 4562
print("Reverse of no. is ", reversDigits(num))

# Expected output:
Reverse of no. is  2654
# find a number such that maximum in array is minimum possible after xor | recursive function that find the minimum value after exclusive - or ; condition if ref size is zero or bit is negative then return 0 ; condition if current bit is off then push current value in curr_off vector ; condition if current bit is on then push current value in curr_on vector ; condition if curr_off is empty then call recursive function on curr_on vector ; condition if curr_on is empty then call recursive function on curr_off vector ; return the minimum of curr_off and curr_on and add power of 2 of current bit ; function that print the minimum value after exclusive - or ; pushing values in vector ; printing answer ; 
def RecursiveFunction(ref, bit):
    if (len(ref) == 0 or bit < 0):
        return 0
    curr_on = []
    curr_off = []
    for i in range(len(ref)):
        if (((ref[i] >> bit) & 1) == 0):
            curr_off .append(ref[i])
        else:
            curr_on .append(ref[i])
    if (len(curr_off) == 0):
        return RecursiveFunction(curr_on, bit - 1)
    if (len(curr_on) == 0):
        return RecursiveFunction(curr_off, bit - 1)
    return (min(RecursiveFunction(curr_off, bit - 1),
            RecursiveFunction(curr_on, bit - 1)) + (1 << bit))


def PrintMinimum(a, n):
    v = []
    for i in range(n):
        v .append(a[i])
    print(RecursiveFunction(v, 30))


arr = [3, 2, 1]
size = len(arr)
PrintMinimum(arr, size)

# Expected output:
2
# count of elements which are equal to the xor of the next two elements | function to return the count of elements which are equal to the xor of the next two elements ; to store the required count ; for every element of the array such that it has at least two elements appearing after it in the array ; if current element is equal to the xor of the next two elements in the array ; 
def cntElements(arr, n):
    cnt = 0
    for i in range(n - 2):
        if (arr[i] == (arr[i + 1] ^ arr[i + 2])):
            cnt += 1
    return cnt


arr = [4, 2, 1, 3, 7, 8]
n = len(arr)
print(cntElements(arr, n))

# Expected output:
1
# number of triplets in array having subarray xor equal | function to return the count ; initialise result ; pick 1 st element of the triplet ; pick 2 nd element of the triplet ; pick 3 rd element of the triplet ; taking xor in the first subarray ; taking xor in the second subarray ; if both xor is equal ; 
def xor_triplet(arr, n):
    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j, n):
                xor1 = 0
                xor2 = 0
                for x in range(i, j):
                    xor1 ^= arr[x]
                for x in range(j, k + 1):
                    xor2 ^= arr[x]
                if (xor1 == xor2):
                    ans += 1
    return ans


if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5]
    n = len(arr)
    print(xor_triplet(arr, n))

# Expected output:
5
# print bitwise and set of a number n | function to find bitwise subsets efficient approach ; 
def printSubsets(n):
    i = n
    while (i != 0):
        print(i, end=" ")
        i = (i - 1) & n
    print("0")


n = 9
printSubsets(n)

# Expected output:
9 8 1 0