giulio98/xlcost-single-prompt huggingface dataset:
# evil number | returns number of 1 s from the binary number ; counting 1 s ; check if number is evil or not ; converting n to binary form ; calculating remainder storing the remainders in binary form as a number ; calling the count_one function to count and return number of 1 s in bin ; 
def count_one(n):
    c_one = 0
    while n != 0:
        rem = n % 10
        if rem == 1:
            c_one = c_one + 1
        n = n / 10
    return c_one


def checkEvil(n):
    i = 0
    binary = 0
    while n != 0:
        r = n % 2
        binary = binary + r * (int(10 ** i))
        n = n / 2
    n_one = count_one(binary)
    if n_one % 2 == 0:
        return True
    return False


num = 32
check = checkEvil(num)
if check:
    print(num, "is Evil Number")
else:
    print(num, "is Odious Number")

# Expected output:
32 is Evil Number
# count number of pairs ( a <= n , b <= n ) such that gcd ( a , b ) is b | returns number of valid pairs ; initialize k ; loop till imin <= n ; initialize result ; max i with given k floor ( n / k ) ; adding k * ( number of i with floor ( n / i ) = k to ans ; set imin = imax + 1 and k = n / imin ; 
def CountPairs(n):
    k = n
    imin = 1
    ans = 0
    while (imin <= n):
        imax = n / k
        ans += k * (imax - imin + 1)
        imin = imax + 1
        k = n / imin
    return ans


print(CountPairs(1))
print(CountPairs(2))
print(CountPairs(3))

# Expected output:
1.0
3.0
5.5
# finding power of prime number p in n ! | returns power of p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; 
def PowerOFPINnfactorial(n, p):
    ans = 0
    temp = p
    while (temp <= n):
        ans += n / temp
        temp = temp * p
    return int(ans)


print(PowerOFPINnfactorial(4, 2))

# Expected output:
3
# program for decimal to binary conversion | function to return the binary equivalent of decimal value n ; to store the binary number ; count used to store exponent value ; 
def decimalToBinary(N):
    B_Number = 0
    cnt = 0
    while (N != 0):
        rem = N % 2
        c = pow(10, cnt)
        B_Number += rem * c
        N //= 2
        cnt += 1
    return B_Number


N = 17
print(decimalToBinary(N))

# Expected output:
10001
# program for binary to decimal conversion | function to convert binary to decimal ; initializing base value to 1 , i . e 2 ^ 0 ; 
def binaryToDecimal(n):
    num = n
    dec_value = 0
    base = 1
    temp = num
    while (temp):
        last_digit = temp % 10
        temp = int(temp / 10)
        dec_value += last_digit * base
        base = base * 2
    return dec_value


num = 10101001
print(binaryToDecimal(num))

# Expected output:
169
# calculating factorials using stirling approximation | python3 program for calculating factorial of a number using stirling approximation ; function for calculating factorial ; value of natural e ; evaluating factorial using stirling approximation ; 

import math


def stirlingFactorial(n):
    if (n == 1):
        return 1
    e = 2.71
    z = (math .sqrt(2 * 3.14 * n) * math .pow((n / e), n))
    return math .floor(z)


print(stirlingFactorial(1))
print(stirlingFactorial(2))
print(stirlingFactorial(3))
print(stirlingFactorial(4))
print(stirlingFactorial(5))
print(stirlingFactorial(6))
print(stirlingFactorial(7))

# Expected output:
1
1
5
23
119
723
5086
# count pairs with odd xor | a function will return number of pair whose xor is odd ; to store count of odd and even numbers ; increase even if number is even otherwise increase odd ; return number of pairs ; 
def countXorPair(arr, n):
    odd = 0
    even = 0
    for i in range(n):
        if arr[i] % 2 == 0:
            even += 1
        else:
            odd += 1
    return odd * even


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

# Expected output:
2
# lychrel number implementation | max iterations ; function to check whether number is lychrel number ; function to check whether the number is palindrome ; function to reverse the number ; 

MAX_ITERATIONS = 20


def isPalindrome(number):
    return number == reverse(number)


def reverse(number):
    reverse = 0
    while (number > 0):
        remainder = number % 10
        reverse = (reverse * 10) + remainder
        number = int(number / 10)
    return reverse

def isLychrel(number):
    for i in range(MAX_ITERATIONS):
        number = number + reverse(number)
        if (isPalindrome(number)):
            return "false"
    return "true"


number = 295
print(number, " is lychrel? ", isLychrel(number))

# Expected output:
295  is lychrel?  true
# rectangular ( or pronic ) numbers | returns n - th rectangular number ; 
def findRectNum(n):
    return n * (n + 1)


n = 6
print(findRectNum(n))

# Expected output:
42
# program for muller method | python3 program to find root of a function , f ( x ) ; function to calculate f ( x ) ; taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; calculating various constants required to calculate x3 ; taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; 

import math
MAX_ITERATIONS = 10000


def f(x):
    return (1 * pow(x, 3) + 2 * x * x + 10 * x - 20)


def Muller(a, b, c):
    res = 0
    i = 0
    while (True):
        f1 = f(a)
        f2 = f(b)
        f3 = f(c)
        d1 = f1 - f3
        d2 = f2 - f3
        h1 = a - c
        h2 = b - c
        a0 = f3
        a1 = (((d2 * pow(h1, 2)) - (d1 * pow(h2, 2))) / ((h1 * h2) * (h1 - h2)))
        a2 = (((d1 * h2) - (d2 * h1)) / ((h1 * h2) * (h1 - h2)))
        x = ((-2 * a0) / (a1 + abs(math .sqrt(a1 * a1 - 4 * a0 * a2))))
        y = ((-2 * a0) / (a1 - abs(math .sqrt(a1 * a1 - 4 * a0 * a2))))
        if (x >= y):
            res = x + c
        else:
            res = y + c
        m = res * 100
        n = c * 100
        m = math .floor(m)
        n = math .floor(n)
        if (m == n):
            break
        a = b
        b = c
        c = res
        if (i > MAX_ITERATIONS):
            print("Root cannot be found using", "Muller's method")
            break
        i += 1
    if (i <= MAX_ITERATIONS):
        print("The value of the root is", round(res, 4))


a = 0
b = 1
c = 2
Muller(a, b, c)

# Expected output:
The value of the root is 1.3688
# finding n | python3 implementation for finding nth number made of prime digits only ; prints n - th number where each digit is a prime number ; finding the length of n - th number ; count of numbers with len - 1 digits ; count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else print the ith digit and break ; 

import math


def nthprimedigitsnumber(n):
    len = 1
    prev_count = 0
    while (1):
        curr_count = (prev_count + math .pow(4, len))
        if (prev_count < n and curr_count >= n):
            break
        len += 1
        prev_count = curr_count
    for i in range(1, len + 1):
        for j in range(1, 5):
            if (prev_count + pow(4, len - i) < n):
                prev_count += pow(4, len - i)
            else:
                if (j == 1):
                    print("2", end="")
                elif (j == 2):
                    print("3", end="")
                elif (j == 3):
                    print("5", end="")
                elif (j == 4):
                    print("7", end="")
                break
    print()


nthprimedigitsnumber(10)
nthprimedigitsnumber(21)

# Expected output:
33
222
# cassiniâ €™ s identity | returns ( - 1 ) ^ n ; 
def cassini(n):
    return -1 if (n & 1)else 1


n = 5
print(cassini(n))

# Expected output:
-1
# find if a number is divisible by every number in a list | python program which check is a number divided with every element in list or not ; checking if a number is divided by every element or not ; 
def findNoIsDivisibleOrNot(n, l=[]):
    for i in range(0, len(l)):
        if l[i] % n != 0:
            return 0
    return 1


l = [14, 12, 4, 18]
n = 2
if findNoIsDivisibleOrNot(n, l) == 1:
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find a range of composite numbers of given length | function to calculate factorial ; to print range of length n having all composite integers ; 
def factorial(n):
    a = 1
    for i in range(2, n + 1):
        a *= i
    return a


def printRange(n):
    a = factorial(n + 2) + 2
    b = a + n - 1
    print("[" + str(a) + ", " + str(b) + "]")


n = 3
printRange(n)

# Expected output:
[122, 124]
# find minimum value to assign all array elements so that array product becomes greater | python3 program to find minimum value that can be assigned to all elements so that product becomes greater than current product . ; sort the array to apply binary search ; using log property add every logarithmic value of element to val val = 0 where ld is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; 

import math


def findMinValue(arr, n):
    arr.sort()
    val = 0
    for i in range(n):
        val += (math.log(arr[i]))
    left = arr[0]
    right = arr[n - 1] + 1

    while (left <= right):
        mid = (left + right) // 2
        temp = n * (math.log(mid))
        if (val < temp):
            ans = mid
            right = mid - 1
        else:
            left = mid + 1
    return ans


if __name__ == "__main__":
    arr = [4, 2, 1, 10, 6]
    n = len(arr)
    print(findMinValue(arr, n))

# Expected output:
4
# find the sum of all the terms in the n | python 3 implementation to find the sum of all the terms in the nth row of the given series ; function to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; 

from math import pow


def sumOfTermsInNthRow(n):
    sum = n * (2 * pow(n, 2) + 1)
    return sum


if __name__ == '__main__':
    n = 4
    print("Sum of all the terms in nth row =", int(sumOfTermsInNthRow(n)))

# Expected output:
Sum of all the terms in nth row = 132
# first digit in product of an array of numbers | python implementation to find first digit of a single number ; keep dividing by 10 until it is greater than equal to 10 ; 
def firstDigit(x):
    while (x >= 10):
        x = x // 10
    return x


print(firstDigit(12345))
print(firstDigit(5432))

# Expected output:
1
5
# find the occurrences of digit d in the range [ 0. . n ] | python3 program to count appearances of a digit ' d ' in range from [ 0. . n ] ; initialize result ; count appearances in numbers starting from d . ; when the last digit is equal to d ; when the first digit is equal to d then ; increment result as well as number ; in case of reverse of number such as 12 and 21 ; 

import math


def getOccurence(n, d):
    result = 0
    itr = d
    while (itr <= n):
        if (itr % 10 == d):
            result += 1
        if (itr != 0 and math .floor(itr / 10) == d):
            result += 1
            itr += 1
        elif (math .floor(itr / 10) == d - 1):
            itr = itr + (10 - d)
        else:
            itr = itr + 10
    return result


n = 11
d = 1
print(getOccurence(n, d))

# Expected output:
4
# program to calculate the value of sin ( x ) and cos ( x ) using expansion | python 3 code for implementing cos function ; function for calculation ; converting degrees to radian ; maps the sum along the series ; holds the actual value of sin ( n ) ; 

from math import fabs, cos


def cal_cos(n):
    accuracy = 0.0001
    n = n * (3.142 / 180.0)
    x1 = 1
    cosx = x1
    cosval = cos(n)
    i = 1
    denominator = 2 * i * (2 * i - 1)
    x1 = -x1 * n * n / denominator
    cosx = cosx + x1
    i = i + 1
    while (accuracy <= fabs(cosval - cosx)):
        denominator = 2 * i * (2 * i - 1)
        x1 = -x1 * n * n / denominator
        cosx = cosx + x1
        i = i + 1
    print('{0:.6}'.format(cosx))


if __name__ == '__main__':
    n = 30
    cal_cos(n)

# Expected output:
0.86602
# find sum of digits in factorial of a number | function to multiply x with large number stored in vector v . result is stored in v . ; calculate res + prev carry ; updation at ith position ; returns sum of digits in n ! ; one by one multiply i to current vector and update the vector . ; find sum of digits in vector v [ ] ; 
def multiply(v, x):
    carry = 0
    size = len(v)
    for i in range(size):
        res = carry + v[i] * x
        v[i] = res % 10
        carry = res // 10

    while (carry != 0):
        v.append(carry % 10)
        carry //= 10


def findSumOfDigits(n):
    v = []
    v.append(1)
    for i in range(1, n + 1):
        multiply(v, i)
    sum = 0
    size = len(v)
    for i in range(size):
        sum += v[i]
    return sum


if __name__ == "__main__":
    n = 1000
    print(findSumOfDigits(n))

# Expected output:
10539
# find other two sides of a right angle triangle | finds two sides of a right angle triangle if it they exist . ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; 
def printOtherSides(n):
    if (n & 1):
        if (n == 1):
            print(-1)
        else:
            b = (n * n - 1) // 2
            c = (n * n + 1) // 2
            print("b =", b, ", c =", c)
    else:
        if (n == 2):
            print(-1)
        else:
            b = n * n // 4 - 1
            c = n * n // 4 + 1
            print("b =", b, ", c =", c)


a = 3
printOtherSides(a)

# Expected output:
b = 4 , c = 5
# minimum positive integer to divide a number such that the result is an odd | python3 program to make a number odd ; return 1 if already odd ; check on dividing with a number when the result becomes odd return that number ; if n is divided by i and n / i is odd then return i ; 
def makeOdd(n):
    if n % 2 != 0:
        return 1
    for i in range(2, n):
        if (n % i == 0 and (int)(n / i) % 2 == 1):
            return i


n = 36
print(makeOdd(n))

# Expected output:
4
# xor of all subarray xors | set 2 | returns xor of all subarray xors ; if even number of terms are there , all numbers will appear even number of times . so result is 0. ; else initialize result by 0 as ( a xor 0 = a ) ; 
def getTotalXorOfSubarrayXors(arr, N):
    if (N % 2 == 0):
        return 0
    res = 0
    for i in range(0, N, 2):
        res ^= arr[i]
    return res


if __name__ == "__main__":
    arr = [3, 5, 2, 4, 6]
    N = len(arr)
    print(getTotalXorOfSubarrayXors(arr, N))

# Expected output:
7
# fill array with 1 's using minimum iterations of filling neighbors | returns count of iterations to fill arr [ ] with 1 s . ; start traversing the array ; traverse until a 0 is found ; count contiguous 0 s ; condition for case 3 ; condition to check if case 1 satisfies : ; if count_zero is even ; if count_zero is odd ; reset count_zero ; case 2 ; update res ; 
def countIterations(arr, n):
    oneFound = False
    res = 0
    i = 0
    while (i < n):
        if (arr[i] == 1):
            oneFound = True
        while (i < n and arr[i] == 1):
            i += 1
        count_zero = 0
        while (i < n and arr[i] == 0):
            count_zero += 1
            i += 1
        if (oneFound == False and i == n):
            return -1
        curr_count = 0
        if (i < n and oneFound):
            if ((count_zero & 1) == 0):
                curr_count = count_zero // 2
            else:
                curr_count = (count_zero + 1) // 2
            count_zero = 0
        else:
            curr_count = count_zero
            count_zero = 0
        res = max(res, curr_count)
    return res


arr = [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0]
n = len(arr)
print(countIterations(arr, n))

# Expected output:
4
# express a number as sum of consecutive numbers | print consecutive numbers from last to first ; 
def printConsecutive(last, first):
    print(first, end="")
    first += 1
    for x in range(first, last + 1):
        print(" +", x, end="")


def findConsecutive(N):
    for last in range(1, N):
        for first in range(0, last):
            if 2 * N == (last - first) * (last + first + 1):
                print(N, "= ", end="")
                printConsecutive(last, first + 1)
                return
    print("-1")


n = 12
findConsecutive(n)

# Expected output:
12 = 3 + 4 + 5
# find n | return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; if i is odd ; 
def printNthElement(n):
    arr = [0] * (n + 1)
    arr[1] = 4
    arr[2] = 7
    for i in range(3, n + 1):
        if (i % 2 != 0):
            arr[i] = arr[i // 2] * 10 + 4
        else:
            arr[i] = arr[(i // 2) - 1] * 10 + 7
    return arr[n]


n = 6
print(printNthElement(n))

# Expected output:
77
# maximum sum of distinct numbers such that lcm of these numbers is n | returns maximum sum of numbers with lcm as n ; initialize result ; finding a divisor of n and adding it to max_sum ; 
def maxSumLCM(n):
    max_sum = 0
    i = 1
    while (i * i <= n):
        if (n % i == 0):
            max_sum = max_sum + i
            if (n // i != i):
                max_sum = max_sum + (n // i)
        i = i + 1
    return max_sum


n = 2
print(maxSumLCM(n))

# Expected output:
3
# maximum value of an integer for which factorial can be calculated on a machine | python3 program to find maximum value of an integer for which factorial can be calculated on your system ; when fact crosses its size it gives negative value ; 

import sys


def findMaxValue():
    res = 2
    fact = 2
    while (True):
        if (fact < 0 or fact > sys .maxsize):
            break
        res += 1
        fact = fact * res
    return res - 1


if __name__ == '__main__':
    print("Maximum value of integer:", findMaxValue())

# Expected output:
Maximum value of integer: 20
# check if lcm of array elements is divisible by a prime number or not | function to check any number of array is divisible by k or not ; if any array element is divisible by k , then lcm of whole array should also be divisible . ; 
def func(a, k, n):
    for i in range(0, n):
        if (a[i] % k == 0):
            return True


a = [14, 27, 38, 76, 84]
k = 19
res = func(a, k, 5)
if (res):
    print("true")
else:
    print("false")

# Expected output:
true
# find the closest and smaller tidy number | python 3 program to find closest tidy number smaller than the given number ; check whether string violates tidy property ; if string violates tidy property , then decrease the value stored at that index by 1 and replace all the value stored right to that index by 9 ; 
def tidyNum(str, len):
    for i in range(len - 2, -1, -1):
        if (str[i] > str[i + 1]):
            str[i] -= 1
            for j in range(i + 1, len):
                str[j] = 9
    return str


str = [1, 1, 3, 3, 3, 4, 4, 5, 5, 3, 8]
len = len(str)
num = tidyNum(str, len)
for i in range(0, len):
    print(str[i], end="")

# Expected output:
11333444999
# count of m digit integers that are divisible by an integer n | returns count of m digit numbers having n as divisor ; generating largest number of m digit ; generating largest number of m - 1 digit ; returning number of dividend ; 
def findCount(m, n):
    num1 = 0
    for i in range(0, m):
        num1 = (num1 * 10) + 9
    num2 = 0
    for i in range(0, (m - 1)):
        num2 = (num2 * 10) + 9
    return int((num1 / n) - (num2 / n))


m = 2
n = 6
print(findCount(m, n))

# Expected output:
15
# find the n | function to calculate nth number made of even digits only ; variable to note how many such numbers have been found till now ; bool variable to check if 1 , 3 , 5 , 7 , 9 is there or not ; checking each digit of the number ; if 1 , 3 , 5 , 7 , 9 is found temp is changed to false ; temp is true it means that it does not have 1 , 3 , 5 , 7 , 9 ; if nth such number is found , return it ; 
def findNthEvenDigitNumber(n):
    count = 0
    i = 0
    while (True):
        curr = i
        isCurrEvenDigit = True
        while (curr != 0):
            if (curr %
                10 == 1 or curr %
                10 == 3 or curr %
                10 == 5 or curr %
                10 == 7 or curr %
                    10 == 9):
                isCurrEvenDigit = False
            curr = curr // 10
        if (isCurrEvenDigit):
            count += 1
        if (count == n):
            return i
        i += 1


print(findNthEvenDigitNumber(2))
print(findNthEvenDigitNumber(10))

# Expected output:
2
28
# find the n | function to find nth number made of even digits only ; if n = 1 return 0 ; vector to store the digits when converted into base 5 ; reduce n to n - 1 to exclude 0 ; reduce n to base 5 number and store digits ; pushing the digits into vector ; variable to represent the number after converting it to base 5. since the digits are be in reverse order , we traverse vector from back ; return 2 * result ( to convert digits 0 , 1 , 2 , 3 , 4 to 0 , 2 , 4 , 6 , 8. ; 
def findNthEvenDigitNumber(n):
    if (n == 1):
        return 0
    v = []
    n = n - 1
    while (n > 0):
        v .append(n % 5)
        n = n // 5
    result = 0
    for i in range(len(v) - 1, -1, -1):
        result = result * 10
        result = result + v[i]
    return 2 * result


if __name__ == "__main__":
    print(findNthEvenDigitNumber(2))
    print(findNthEvenDigitNumber(10))

# Expected output:
2
28
# check if a large number is divisible by 25 or not | function to find that number divisible by 25 or not . ; if length of string is single digit then it 's  not divisible by 25 ; 
def isDivisibleBy25(st):
    n = len(st)
    if (n == 1):
        return False
    return ((int)(st[n - 1]) == 0 and ((int)(st[n - 2]) == 0)
            or ((int)(st[n - 2]) * 10 + (int)(st[n - 1]) % 25 == 0))


st = "76955"
if (isDivisibleBy25(st)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# check a large number is divisible by 16 or not | function to find that number divisible by 16 or not ; empty string ; if there is double digit ; if there is triple digit ; if number formed by last four digits is divisible by 16. ; 
def check(st):
    n = len(st)
    if (n == 0 and n == 1):
        return False
    if (n == 2):
        return ((int)(st[n - 2]) * 10 + ((int)(st[n - 1]) % 16 == 0))
    if (n == 3):
        return (((int)(st[n - 3]) * 100 + (int)(st[n - 2])
                * 10 + (int)(st[n - 1])) % 16 == 0)
    last = (int)(st[n - 1])
    second_last = (int)(st[n - 2])
    third_last = (int)(st[n - 3])
    fourth_last = (int)(st[n - 4])
    return ((fourth_last *
             1000 +
             third_last *
             100 +
             second_last *
             10 +
             last) %
            16 == 0)


st = "769528"
if (check(st)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# find index of given fibonacci number in constant time | a simple python 3 program to find index of given fibonacci number . ; if fibonacci number is less than 2 , its index will be same as number ; iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; 
def findIndex(n):
    if (n <= 1):
        return n
    a = 0
    b = 1
    c = 1
    res = 1
    while (c < n):
        c = a + b
        res = res + 1
        a = b
        b = c
    return res


result = findIndex(21)
print(result)

# Expected output:
8