giulio98/xlcost-single-prompt huggingface dataset:
# check if a number can be expressed as a sum of consecutive numbers | this function returns true if n can be expressed sum of consecutive . ; we basically return true if n is a power of two ; 
def canBeSumofConsec(n):
    return ((n & (n - 1)) and n)


n = 15
if (canBeSumofConsec(n)):
    print("true")
else:
    print("false")

# Expected output:
true
# even fibonacci numbers sum | returns sum of even fibonacci numbers which are less than or equal to given limit . ; initialize first two even prime numbers and their sum ; calculating sum of even fibonacci value ; get next even value of fibonacci sequence ; if we go beyond limit , we break loop ; move to next even number and update sum ; 
def evenFibSum(limit):
    if (limit < 2):
        return 0
    ef1 = 0
    ef2 = 2
    sm = ef1 + ef2
    while (ef2 <= limit):
        ef3 = 4 * ef2 + ef1
        if (ef3 > limit):
            break
        ef1 = ef2
        ef2 = ef3
        sm = sm + ef2
    return sm


limit = 400
print(evenFibSum(limit))

# Expected output:
188
# find numbers with k odd divisors in a given range | python3 program to count numbers with k odd divisors in a range . ; utility function to check if number is perfect square or not ; utility function to return count of divisors of a number ; note that this loop runs till square root ; if divisors are equal , counbt it only once ; otherwise print both ; function to calculate all divisors having exactly k divisors between a and b ; calculate only for perfect square numbers ; check if number is perfect square or not ; total divisors of number equals to k or not ; 

import math


def isPerfect(n):
    s = math .sqrt(n)
    return (s * s == n)


def divisorsCount(n):
    count = 0
    for i in range(1, (int)(math .sqrt(n) + 2)):
        if (n % i == 0):
            if (n // i == i):
                count = count + 1
            else:
                count = count + 2
    return count


def kDivisors(a, b, k):
    count = 0
    for i in range(a, b + 1):
        if (isPerfect(i)):
            if (divisorsCount(i) == k):
                count = count + 1
    return count


a = 2
b = 49
k = 3
print(kDivisors(a, b, k))

# Expected output:
4
# nth even fibonacci number | function which return nth even fibonnaci number ; calculation of fn = 4 * ( fn - 1 ) + fn - 2 ; 
def evenFib(n):
    if (n < 1):
        return n
    if (n == 1):
        return 2
    return ((4 * evenFib(n - 1)) + evenFib(n - 2))


n = 7
print(evenFib(n))

# Expected output:
10946
# n 'th smart number | limit on result ; function to calculate n 'th smart number  ; initialize all numbers as not prime ; iterate to mark all primes and smart number ; traverse all numbers till maximum limit ; ' i ' is maked as prime number because it is not multiple of any other prime ; mark all multiples of ' i ' as non prime ; if i is the third prime factor of j then add it to result as it has at least three prime factors . ; sort all smart numbers ; return n 'th smart number  ; 

MAX = 3000


def smartNumber(n):
    primes = [0] * MAX
    result = []
    for i in range(2, MAX):
        if (primes[i] == 0):
            primes[i] = 1
            j = i * 2
            while (j < MAX):
                primes[j] -= 1
                if ((primes[j] + 3) == 0):
                    result .append(j)
                j = j + i
    result .sort()
    return result[n - 1]


n = 50
print(smartNumber(n))

# Expected output:
273
# repeated subtraction among two numbers | python3 program to count of steps until one of the two numbers become 0. ; returns count of steps before one of the numbers become 0 after repeated subtractions . ; if y divides x , then simply return x / y . ; else recur . note that this function works even if x is smaller than y because in that case first recursive call exchanges roles of x and y . ; 

import math


def countSteps(x, y):
    if (x % y == 0):
        return math .floor(x / y)
    return math .floor((x / y) + countSteps(y, x % y))


x = 100
y = 19
print(countSteps(x, y))

# Expected output:
13
# common divisors of two numbers | python3 implementation of program ; map to store the count of each prime factor of a ; function that calculate the count of each prime factor of a number ; function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find count of each prime factor of a ; stores number of common divisors ; find the count of prime factors of b using distinct prime factors of a ; prime factor of common divisor has minimum cnt of both a and b ; 

import math
ma = {}


def primeFactorize(a):
    sqt = int(math .sqrt(a))
    for i in range(2, sqt, 2):
        cnt = 0
        while (a % i == 0):
            cnt += 1
            a /= i
        ma[i] = cnt
    if (a > 1):
        ma[a] = 1


def commDiv(a, b):
    primeFactorize(a)
    res = 1
    for key, value in ma .items():
        cnt = 0
        while (b % key == 0):
            b /= key
            cnt += 1
        res *= (min(cnt, value) + 1)
    return res


a = 12
b = 24
print(commDiv(a, b))

# Expected output:
6
# count number of solutions of x ^ 2 = 1 ( mod p ) in given range | program to count number of values that satisfy x ^ 2 = 1 mod p where x lies in range [ 1 , n ] ; initialize result ; traverse all numbers smaller than given number p . note that we don 't traverse from  1 to n, but 1 to p ; if x is a solution , then count all numbers of the form x + i * p such that x + i * p is in range [ 1 , n ] ; the largest number in the form of x + p * i in range [ 1 , n ] ; add count of numbers of the form x + p * i . 1 is added for x itself . ; 
def findCountOfSolutions(n, p):
    ans = 0
    for x in range(1, p):
        if ((x * x) % p == 1):
            last = x + p * (n / p)
            if (last > n):
                last -= p
            ans += ((last - x) / p + 1)
    return int(ans)


n = 10
p = 5
print(findCountOfSolutions(n, p))

# Expected output:
4
# kaprekar constant | this function checks validity of kaprekar ' s ▁ constant . ▁ it ▁ returns ▁ kaprekar ' s constant for any four digit number " n " such that all digits of n are not same . ; store current n as previous number ; get four digits of 
def kaprekarRec(n, prev):
    if (n == 0):
        return 0
    prev = n
    digits = [0] * 4
    for i in range(4):
        digits[i] = n % 10
        n = int(n / 10)
    digits .sort()
    asc = 0
    for i in range(4):
        asc = asc * 10 + digits[i]
    digits .sort()
    desc = 0
    for i in range(3, -1, -1):
        desc = desc * 10 + digits[i]
    diff = abs(asc - desc)
    if (diff == prev):
        return diff
    return kaprekarRec(diff, prev)


def kaprekar(n):
    rev = 0
    return kaprekarRec(n, rev)


print(kaprekar(1000))
print(kaprekar(1112))
print(kaprekar(9812))

# Expected output:
6174
6174
6174
# bakhshali approximation for computing square roots | this python3 program gives result approximated to 5 decimal places . ; this will be the nearest perfect square to s ; this is the sqrt of psq ; find the nearest perfect square to s ; calculate d ; calculate p ; calculate a ; calculate sqrt ( s ) . ; 
def sqroot(s):
    pSq = 0
    N = 0
    for i in range(int(s), 0, -1):
        for j in range(1, i):
            if (j * j == i):
                pSq = i
                N = j
                break

        if (pSq > 0):
            break

    d = s - pSq
    P = d / (2.0 * N)
    A = N + P
    sqrt_of_s = A - ((P * P) / (2.0 * A))
    return sqrt_of_s


num = 9.2345
sqroot_of_num = sqroot(num)
print("Square root of ", num, "=", round(
    (sqroot_of_num * 100000.0) / 100000.0, 5))

# Expected output:
Square root of  9.2345 = 3.03883
# breaking an integer to get maximum product | method return x ^ a in log ( a ) time ; method returns maximum product obtained by breaking n ; base case 2 = 1 + 1 ; base case 3 = 2 + 1 ; breaking based on mod with 3 ; if divides evenly , then break into all 3 ; if division gives mod as 1 , then break as 4 + power of 3 for remaining part ; if division gives mod as 2 , then break as 2 + power of 3 for remaining part ; 
def power(x, a):
    res = 1
    while (a):
        if (a & 1):
            res = res * x
        x = x * x
        a >>= 1
    return res


def breakInteger(N):
    if (N == 2):
        return 1
    if (N == 3):
        return 2
    maxProduct = 0
    if (N % 3 == 0):
        maxProduct = power(3, int(N / 3))
        return maxProduct
    elif (N % 3 == 1):
        maxProduct = 2 * 2 * power(3, int(N / 3) - 1)
        return maxProduct
    elif (N % 3 == 2):
        maxProduct = 2 * power(3, int(N / 3))
        return maxProduct


maxProduct = breakInteger(10)
print(maxProduct)

# Expected output:
36
# finding sum of digits of a number until sum becomes single digit | python program to find sum of digits of a number until sum becomes single digit . ; method to find sum of digits of a number until sum becomes single digit ; 

import math


def digSum(n):
    sum = 0
    while (n > 0 or sum > 9):
        if (n == 0):
            n = sum
            sum = 0
        sum += n % 10
        n /= 10
    return sum


n = 1234
print(digSum(n))

# Expected output:
2.3456790123456845
# finding sum of digits of a number until sum becomes single digit | ''
def digSum(n):
    if (n == 0):
        return 0
    if (n % 9 == 0):
        return 9
    else:
        return (n % 9)


n = 9999
print(digSum(n))

# Expected output:
9
# multiples of 3 or 7 | returns count of all numbers smaller than or equal to n and multiples of 3 or 7 or both ; 
def countMultiples(n):
    return n / 3 + n / 7 - n / 21


n = ((int)(countMultiples(25)))
print("Count =", n)

# Expected output:
Count = 10
# reverse and add function | iterative function to reverse digits of num ; function to check whether the number is palindrome or not ; reverse and add function ; reversing the digits of the number ; adding the reversed number with the original ; checking whether the number is palindrome or not ; 
def reverseDigits(num):
    rev_num = 0
    while (num > 0):
        rev_num = rev_num * 10 + num % 10
        num = num//10
    return rev_num


def isPalindrome(num):
    return (reverseDigits(num) == num)


def ReverseandAdd(num):
    rev_num = 0
    while (num <= 4294967295):
        rev_num = reverseDigits(num)
        num = num + rev_num
        if (isPalindrome(num)):
            print((num))
            break
        else:
            if (num > 4294967295):
                print(("No palindrome exist"))


ReverseandAdd(195)
ReverseandAdd(265)

# Expected output:
9339
45254
# stein 's algorithm for finding gcd | function to implement stein 's algorithm ; gcd ( 0 , b ) == b ; gcd ( a , 0 ) == a , gcd ( 0 , 0 ) == 0 ; look for factors of 2 a is even ; b is odd ; both a and b are even ; a is odd , b is even ; reduce larger number ; 
def gcd(a, b):
    if (a == b):
        return a
    if (a == 0):
        return b
    if (b == 0):
        return a
    if ((~a & 1) == 1):
        if ((b & 1) == 1):
            return gcd(a >> 1, b)
        else:
            return (gcd(a >> 1, b >> 1) << 1)
    if ((~b & 1) == 1):
        return gcd(a, b >> 1)
    if (a > b):
        return gcd((a - b) >> 1, b)
    return gcd((b - a) >> 1, a)


a, b = 34, 17
print("Gcd of given numbers is ", gcd(a, b))

# Expected output:
Gcd of given numbers is  17
# print all n | n , sum -- > value of inputs out -- > output array index -- > index of next digit to be filled in output array ; base case ; if number becomes n - digit ; if sum of its digits is equal to given sum , print it ; traverse through every digit . note that here we ' re ▁ considering ▁ leading ▁ ▁ 0' s as digits ; append current digit to number ; recurse for next digit with reduced sum ; this is mainly a wrapper over findndigitnumsutil . it explicitly handles leading digit ; output array to store n - digit numbers ; fill 1 st position by every digit from 1 to 9 and calls findndigitnumsutil ( ) for remaining positions ; 
def findNDigitNumsUtil(n, sum, out, index):
    if (index > n or sum < 0):
        return
    f = ""
    if (index == n):
        if (sum == 0):
            out[index] = "\0"
            for i in out:
                f = f + i
            print(f, end=" ")
        return
    for i in range(10):
        out[index] = chr(i + ord('0'))
        findNDigitNumsUtil(n, sum - i, out, index + 1)


def findNDigitNums(n, sum):
    out = [False] * (n + 1)
    for i in range(1, 10):
        out[0] = chr(i + ord('0'))
        findNDigitNumsUtil(n, sum - i, out, 1)


if __name__ == "__main__":
    n = 2
    sum = 3
    findNDigitNums(n, sum)

# Expected output:
12 21 30
# Program for Armstrong Numbers |
def armstrong(n):
    number = str(n)
    n = len(number)
    output = 0
    for i in number:
        output = output + int(i)**n
    if output == int(number):
        return (True)
    else:
        return (False)


print(armstrong(153))
print(armstrong(120))

# Expected output:
True
False
# almost prime numbers | python3 program to print first n numbers that are k - primes ; a function to count all prime factors of a given number ; count the number of 2 s that divide n ; n must be odd at this point . so we can skip one element ( note i = i + 2 ) ; while i divides n , count i and divide n ; this condition is to handle the case when n is a prime number greater than 2 ; a function to print the first n numbers that are k - almost primes . ; print this number if it is k - prime ; increment count of k - primes printed so far ; 

import math


def countPrimeFactors(n):
    count = 0
    while (n % 2 == 0):
        n = n / 2
        count += 1
    i = 3
    while (i <= math .sqrt(n)):
        while (n % i == 0):
            n = n / i
            count += 1
        i = i + 2
    if (n > 2):
        count += 1
    return (count)


def printKAlmostPrimes(k, n):
    i = 1
    num = 2
    while (i <= n):
        if (countPrimeFactors(num) == k):
            print(num, end="")
            print(" ", end="")
            i += 1
        num += 1
    return


n = 10
k = 2
print("First n k-almost prime numbers:")
printKAlmostPrimes(k, n)

# Expected output:
First n k-almost prime numbers:
4 6 9 10 14 15 21 22 25 26
# program to add two fractions | function to return gcd of a and b ; function to convert the obtained fraction into it 's simplest form ; finding gcd of both terms ; converting both terms into simpler terms by dividing them by common factor ; function to add two fractions ; finding gcd of den1 and den2 ; denominator of final fraction obtained finding lcm of den1 and den2 lcm * gcd = a * b ; changing the fractions to have same denominator numerator of the final fraction obtained ; calling function to convert final fraction into it 's  simplest form ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def lowest(den3, num3):
    common_factor = gcd(num3, den3)
    den3 = int(den3 / common_factor)
    num3 = int(num3 / common_factor)
    print(num3, "/", den3)


def addFraction(num1, den1, num2, den2):
    den3 = gcd(den1, den2)
    den3 = (den1 * den2) / den3
    num3 = ((num1) * (den3 / den1) + (num2) * (den3 / den2))
    lowest(den3, num3)


num1 = 1
den1 = 500
num2 = 2
den2 = 1500
print(num1, "/", den1, " + ", num2, "/", den2, " is equal to ", end="")
addFraction(num1, den1, num2, den2)

# Expected output:
1 / 500  +  2 / 1500  is equal to 1 / 300
# the lazy caterer 's problem | this function receives an integer n and returns the maximum number of pieces that can be made form pancake using n cuts ; use the formula ; 
def findPieces(n):
    return (n * (n + 1)) // 2 + 1


print(findPieces(1))
print(findPieces(2))
print(findPieces(3))
print(findPieces(50))

# Expected output:
2
4
7
1276
# count digits in a factorial | set 2 | a optimised python3 program to find the number of digits in a factorial ; returns the number of digits present in n ! since the result can be large long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; use kamenetsky formula to calculate the number of digits ; 

import math


def findDigits(n):
    if (n < 0):
        return 0
    if (n <= 1):
        return 1
    x = ((n * math .log10(n / math .e) + math .log10(2 * math .pi * n) / 2.0))
    return math .floor(x) + 1


print(findDigits(1))
print(findDigits(50000000))
print(findDigits(1000000000))
print(findDigits(120))

# Expected output:
1
363233781
8565705523
199
# count digits in a factorial | set 1 | python3 program to find the number of digits in a factorial ; this function receives an integer n , and returns the number of digits present in n ! ; factorial exists only for n >= 0 ; base case ; else iterate through n and calculate the value ; 

import math


def findDigits(n):
    if (n < 0):
        return 0
    if (n <= 1):
        return 1
    digits = 0
    for i in range(2, n + 1):
        digits += math .log10(i)
    return math .floor(digits) + 1


print(findDigits(1))
print(findDigits(5))
print(findDigits(10))
print(findDigits(120))

# Expected output:
1
3
7
199
# find number of subarrays with even sum | python 3 program to count number of sub - arrays whose sum is even using brute force time complexity - o ( n ^ 2 ) space complexity - o ( 1 ) ; find sum of all subarrays and increment result if sum is even ; 
def countEvenSum(arr, n):
    result = 0
    for i in range(0, n, 1):
        sum = 0
        for j in range(i, n, 1):
            sum = sum + arr[j]
            if (sum % 2 == 0):
                result = result + 1
    return (result)


if __name__ == '__main__':
    arr = [1, 2, 2, 3, 4, 1]
    n = len(arr)
    print("The Number of Subarrays", "with even sum is", countEvenSum(arr, n))

# Expected output:
The Number of Subarrays with even sum is 9
# find number of subarrays with even sum | python 3 program to count number of sub - arrays with even sum using an efficient algorithm time complexity - o ( n ) space complexity - o ( 1 ) ; a temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single even element is also counted as a subarray ; initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ of ▁ arr [ 0 . . i ] ▁ ▁ under ▁ modulo ▁ 2 ▁ and ▁ increments ▁ even / odd ▁ ▁ count ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; increment even / odd count ; use handshake lemma to count even subarrays ( note that an even cam be formed by two even or two odd ) ; 
def countEvenSum(arr, n):
    temp = [1, 0]
    result = 0
    sum = 0
    for i in range(n):
        sum = ((sum + arr[i]) % 2 + 2) % 2
        temp[sum] += 1
    result = result + (temp[0] * (temp[0] - 1) // 2)
    result = result + (temp[1] * (temp[1] - 1) // 2)
    return (result)


if __name__ == "__main__":
    arr = [1, 2, 2, 3, 4, 1]
    n = len(arr)
    print("The Number of Subarrays with even" " sum is", countEvenSum(arr, n))

# Expected output:
The Number of Subarrays with even sum is 9
# sum of fibonacci numbers | computes value of first fibonacci numbers ; initialize result ; add remaining terms ; 
def calculateSum(n):
    if (n <= 0):
        return 0
    fibo = [0] * (n + 1)
    fibo[1] = 1
    sm = fibo[0] + fibo[1]
    for i in range(2, n + 1):
        fibo[i] = fibo[i - 1] + fibo[i - 2]
        sm = sm + fibo[i]
    return sm


n = 4
print("Sum of Fibonacci numbers is : ", calculateSum(n))

# Expected output:
Sum of Fibonacci numbers is :  7
# find all combinations that add upto given number | arr - array to store the combination index - next location in array num - given number reducednum - reduced number ; base condition ; if combination is found , print it ; find the previous number stored in arr [ ] . it helps in maintaining increasing order ; note loop starts from previous number i . e . at array location index - 1 ; next element of array is k ; call recursively with reduced number ; function to find out all combinations of positive numbers that add upto given number . it uses findcombinationsutil ( ) ; array to store the combinations it can contain max n elements ; find all combinations ; 
def findCombinationsUtil(arr, index, num, reducedNum):
    if (reducedNum < 0):
        return
    if (reducedNum == 0):
        for i in range(index):
            print(arr[i], end=" ")
        print("")
        return
    prev = 1 if (index == 0)else arr[index - 1]
    for k in range(prev, num + 1):
        arr[index] = k
        findCombinationsUtil(arr, index + 1, num, reducedNum - k)


def findCombinations(n):
    arr = [0] * n
    findCombinationsUtil(arr, 0, n, n)


n = 5
findCombinations(n)

# Expected output:
1 1 1 1 1 
1 1 1 2 
1 1 3 
1 2 2 
1 4 
2 3 
5
# find square root under modulo p | set 2 ( shanks tonelli algorithm ) | utility function to find pow ( base , exponent ) % modulus ; utility function to find gcd ; returns k such that b ^ k = 1 ( mod p ) ; initializing k with first odd prime number ; function return p - 1 ( = x argument ) as x * 2 ^ e , where x will be odd sending e as reference because updation is needed in actual e ; main function for finding the modular square root ; a and p should be coprime for finding the modular square root ; if below expression return ( p - 1 ) then modular square root is not possible ; expressing p - 1 , in terms of s * 2 ^ e , where s is odd number ; finding smallest q such that q ^ ( ( p - 1 ) / 2 ) ( mod p ) = p - 1 ; q - 1 is in place of ( - 1 % p ) ; initializing variable x , b and g ; keep looping until b become 1 or m becomes 0 ; finding m such that b ^ ( 2 ^ m ) = 1 ; updating value of x , g and b according to algorithm ; 
def pow1(base, exponent, modulus):
    result = 1
    base = base % modulus
    while (exponent > 0):
        if (exponent % 2 == 1):
            result = (result * base) % modulus
        exponent = int(exponent) >> 1
        base = (base * base) % modulus
    return result


def gcd(a, b):
    if (b == 0):
        return a
    else:
        return gcd(b, a % b)


def order(p, b):
    if (gcd(p, b) != 1):
        print("pandbarenotco-prime.")
        return -1
    k = 3
    while (True):
        if (pow1(b, k, p) == 1):
            return k
        k += 1


def convertx2e(x):
    z = 0
    while (x % 2 == 0):
        x = x / 2
        z += 1
    return [x, z]


def STonelli(n, p):
    if (gcd(n, p) != 1):
        print("aandparenotcoprime")
        return -1
    if (pow1(n, (p - 1) / 2, p) == (p - 1)):
        print("nosqrtpossible")
        return -1
    ar = convertx2e(p - 1)
    s = ar[0]
    e = ar[1]
    q = 2
    while (True):
        if (pow1(q, (p - 1) / 2, p) == (p - 1)):
            break
        q += 1
    x = pow1(n, (s + 1) / 2, p)
    b = pow1(n, s, p)
    g = pow1(q, s, p)
    r = e
    while (True):
        m = 0
        while (m < r):
            if (order(p, b) == -1):
                return -1
            if (order(p, b) == pow(2, m)):
                break
            m += 1
        if (m == 0):
            return x
        x = (x * pow1(g, pow(2, r - m - 1), p)) % p
        g = pow1(g, pow(2, r - m), p)
        b = (b * g) % p
        if (b == 1):
            return x
        r = m


n = 2
p = 113
x = STonelli(n, p)
if (x == -1):
    print("Modularsquarerootisnotexist")
else:
    print("Modular square root of", n, "and", p, "is", x)

# Expected output:
Modular square root of 2 and 113 is 62
# check if a number is a power of another number | python3 program to check given number number y ; logarithm function to calculate value ; note : this is double ; compare to the result1 or result2 both are equal ; 

import math


def isPower(x, y):
    res1 = math .log(y) // math .log(x)
    res2 = math .log(y) / math .log(x)
    return 1 if (res1 == res2)else 0


if __name__ == '__main__':
    print(isPower(27, 729))

# Expected output:
1