giulio98/xlcost-single-prompt huggingface dataset:
# sum of each element raised to ( prime | function to return the required sum ; 
def getSum(arr, p):
    return len(arr)


if __name__ == "__main__":
    arr = [5, 6, 8]
    p = 7
    print(getSum(arr, p))

# Expected output:
3
# count numbers upto n which are both perfect square and perfect cube | function to return required count ; 
def SquareCube(N):
    cnt, i = 0, 1
    while (i ** 6 <= N):
        cnt += 1
        i += 1
    return cnt


N = 100000
print(SquareCube(N))

# Expected output:
6
# sum of integers upto n with given unit digit | function to return the required sum ; 
def getSum(n, d):
    sum = 0
    while (d <= n):
        sum += d
        d += 10
    return sum


n = 30
d = 3
print(getSum(n, d))

# Expected output:
39
# Summing the sum series | Python3 program to calculate the terms of summing of sum series ; Function to calculate twice of sum of first N natural numbers ; Function to calculate the terms of summing of sum series ;

MOD = 1000000007


def Sum(N):
    val = N * (N + 1)
    val = val % MOD
    return val


def sumX(N, M, K):
    for i in range(M):
        N = int(Sum(K + N))
    N = N % MOD
    return N


if __name__ == "__main__":
    MOD = 1000000007
    N, M, K = 1, 2, 3
    print(sumX(N, M, K))

# Expected output:
552
# logarithm | python 3 program to find log ( n ) using recursion ; 
def Log2n(n):
    return 1 + Log2n(n / 2)if (n > 1)else 0


n = 32
print(Log2n(n))

# Expected output:
5
# mode | function that sort input array a [ ] and calculate mode and median using counting sort . ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . initialize count array as 0. size of count array will be equal to ( max + 1 ) . ; store count of each element of input array ; mode is the index with maximum count ; 
def printMode(a, n):
    max_element = max(a)
    t = max_element + 1
    count = [0] * t
    for i in range(t):
        count[i] = 0
    for i in range(n):
        count[a[i]] += 1
    mode = 0
    k = count[0]
    for i in range(1, t):
        if (count[i] > k):
            k = count[i]
            mode = i
    print("mode = ", mode)


if __name__ == "__main__":
    a = [1, 4, 1, 2, 7, 1, 2, 5, 3, 6]
    n = len(a)
    printMode(a, n)

# Expected output:
mode =  1
# harmonic progression | python3 program to check if a given array can form harmonic progression ; find reciprocal of arr [ ] ; after finding reciprocal , check if the reciprocal is in a . p . to check for a . p . , first sort the reciprocal array , then check difference between consecutive elements ; 
def checkIsHP(arr):
    n = len(arr)
    if (n == 1):
        return True
    rec = []
    for i in range(0, len(arr)):
        a = 1 / arr[i]
        rec .append(a)
    return (rec)
    rec .sort()
    d = rec[1] - rec[0]
    for i in range(2, n):
        if (rec[i] - rec[i - 1] != d):
            return False
    return True


if __name__ == '__main__':
    arr = [1 / 5, 1 / 10, 1 / 15, 1 / 20, 1 / 25]
    if (checkIsHP(arr)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# arithmetic mean | prints n arithmetic means between a and b . ; calculate common difference ( d ) ; for finding n the arithmetic mean between a and b ; 
def printAMeans(A, B, N):
    d = (B - A) / (N + 1)
    for i in range(1, N + 1):
        print(int(A + i * d), end=" ")


A = 20
B = 32
N = 5
printAMeans(A, B, N)

# Expected output:
22 24 26 28 30
# prime factor | python program to print prime factors ; a function to print all prime factors of a given number n ; print the number of two 's that divide n ; n must be odd at this point so a skip of 2 ( i = i + 2 ) can be used ; while i divides n , print i ad divide n ; condition if n is a prime number greater than 2 ; 

import math


def primeFactors(n):
    while n % 2 == 0:
        print(2)
        n = n / 2
    for i in range(3, int(math .sqrt(n)) + 1, 2):
        while n % i == 0:
            print(i)
            n = n / i
    if n > 2:
        print(n)


n = 315
primeFactors(n)

# Expected output:
3
3
5
7
# check if the array has an element which is equal to product of remaining elements | function to check if the array has an element which is equal to product of all the remaining elements ; calculate the product of all the elements ; return true if any such element is found ; if no element is found ; 
def CheckArray(arr, n):
    prod = 1
    for i in range(0, n, 1):
        prod *= arr[i]
    for i in range(0, n, 1):
        if (arr[i] == prod / arr[i]):
            return True
    return False


if __name__ == '__main__':
    arr = [1, 2, 12, 3, 2]
    n = len(arr)
    if (CheckArray(arr, n)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# sum of common divisors of two numbers a and b | print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; 
def sum(a, b):
    sum = 0
    for i in range(1, min(a, b)):
        if (a % i == 0 and b % i == 0):
            sum += i
    return sum


A = 10
B = 15
print("Sum =", sum(A, B))

# Expected output:
Sum = 6
# minimum number of cuts required to make circle segments equal sized | python 3 program to find the minimum number of additional cuts required to make circle segments equal sized ; function to find the minimum number of additional cuts required to make circle segments are equal sized ; sort the array ; initial gcd value ; including the last segment ; 

import math


def minimumCuts(a, n):
    a .sort()
    gcd = a[1] - a[0]
    s = gcd
    for i in range(2, n):
        gcd = math .gcd(gcd, a[i] - a[i - 1])
        s += a[i] - a[i - 1]
    if (360 - s > 0):
        gcd = math .gcd(gcd, 360 - s)
    return (360 // gcd) - n


if __name__ == "__main__":
    arr = [30, 60, 180]
    n = len(arr)
    print(minimumCuts(arr, n))

# Expected output:
9
# find a number that divides maximum array elements | python3 program to find a number that divides maximum array elements ; stores smallest prime factor for every number ; calculating spf ( smallest prime factor ) for every number till maxn . time complexity : o ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking spf for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; a o ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; function to find a number that divides maximum array elements ; precalculating smallest prime factor ; hash to store frequency of each divisors ; traverse the array and get spf of each element ; calling getfactorization function ; review set view ; 

import math as mt
MAXN = 100001
spf = [0 for i in range(MAXN)]


def sieve():
    spf[1] = 1
    for i in range(2, MAXN):
        spf[i] = i
    for i in range(4, MAXN, 2):
        spf[i] = 2
    for i in range(3, mt .ceil(mt .sqrt(MAXN + 1))):
        if (spf[i] == i):
            for j in range(2 * i, MAXN, i):
                if (spf[j] == j):
                    spf[j] = i


def getFactorization(x):
    ret = list()
    while (x != 1):
        temp = spf[x]
        ret .append(temp)
        while (x % temp == 0):
            x = x // temp
    return ret


def maxElement(A, n):
    sieve()
    m = dict()
    for i in range(n):
        p = getFactorization(A[i])
        for i in range(len(p)):
            if p[i] in m .keys():
                m[p[i]] += 1
            else:
                m[p[i]] = 1
    cnt = 0
    ans = 10 ** 9 + 7
    for i in m:
        if (m[i] >= cnt):
            cnt = m[i]
            if ans > i:
                ans = i
            else:
                ans = ans
    return ans


A = [2, 5, 10]
n = len(A)
print(maxElement(A, n))

# Expected output:
2
# find selling price from given profit percentage and cost | function to calculate the selling price ; decimal equivalent of profit percentage ; find the selling price ; return the calculated selling price ; 
def SellingPrice(CP, PP):
    Pdecimal = 1 + (PP / 100)
    res = Pdecimal * CP
    return res


if __name__ == "__main__":
    C = 720
    P = 13
    print(SellingPrice(C, P))

# Expected output:
813.5999999999999
# product of all the composite numbers in an array | python3 program to find product of all the composite numberes in given array ; function to find the product of all composite niumbers in the given array ; find the maximum value in the array ; use sieve to find all prime numbers less than or equal to max_val create a boolean array " prime [ 0 . . n ] " . a value in prime [ i ] will finally be false if i is not a prime , else true . ; set 0 and 1 as primes as they don 't need to be  counted as composite numbers   ; if prime [ p ] is not changed , than it is prime ; update all multiples of p ; find the product of all composite numbers in the arr [ ] ; 

import math as mt


def compositeProduct(arr, n):
    max_val = max(arr)
    prime = [True for i in range(max_val + 1)]
    prime[0] = True
    prime[1] = True
    for p in range(2, mt .ceil(mt .sqrt(max_val))):
        if prime[p]:
            for i in range(p * 2, max_val + 1, p):
                prime[i] = False
    product = 1
    for i in range(n):
        if not prime[arr[i]]:
            product *= arr[i]
    return product


arr = [2, 3, 4, 5, 6, 7]
n = len(arr)
print(compositeProduct(arr, n))

# Expected output:
24
# find amount to be added to achieve target ratio in a given mixture | python3 program to find amount of water to be added to achieve given target ratio . ; 
def findAmount(X, W, Y):
    return (X * (Y - W) / (100 - Y))


X = 100
W = 50
Y = 60
print("Water to be added", findAmount(X, W, Y))

# Expected output:
Water to be added 25.0
# check if a number is a mystery number | finds reverse of 
def reverseNum(x):
    s = str(x)
    s = s[::-1]
    return int(s)


def isMysteryNumber(n):
    for i in range(1, n // 2 + 1):
        j = reverseNum(i)
        if i + j == n:
            print(i, j)
            return True
    print("Not a Mystery Number")
    return False


n = 121
isMysteryNumber(n)

# Expected output:
29 92
# replace every element of the array by product of all other elements | python 3 program to replace every element by the product of all other elements ; calculate the product of all the elements ; replace every element product of all other elements ; 
def ReplaceElements(arr, n):
    prod = 1
    for i in range(n):
        prod *= arr[i]
    for i in range(n):
        arr[i] = prod // arr[i]


if __name__ == "__main__":
    arr = [2, 3, 3, 5, 7]
    n = len(arr)
    ReplaceElements(arr, n)
    for i in range(n):
        print(arr[i], end=" ")

# Expected output:
315 210 210 126 90
# check if there is any pair in a given range with gcd is divisible by k | function to count such possible numbers ; if i is divisible by k ; if count of such numbers is greater than one ; 
def Check_is_possible(l, r, k):
    count = 0
    for i in range(l, r + 1):
        if (i % k == 0):
            count += 1
    return (count > 1)


l = 4
r = 12
k = 5
if (Check_is_possible(l, r, k)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# check if any permutation of n equals any power of k | function to check if n and k are anagrams ; function to check if any permutation of n exist such that it is some power of k ; generate all power of k under 10 ^ 18 ; check if any power of k is valid ; 
def isValid(N, K):
    m1 = []
    m2 = []
    while (N > 0):
        m1 .append(N % 10)
        N //= 10
    while (K > 0):
        m2 .append(K % 10)
        K //= 10
    if (m1 == m2):
        return True
    return False


def anyPermutation(N, K):
    powK = [0] * 100
    Limit = pow(10, 18)
    powK[0] = K
    i = 1
    while (powK[i - 1] * K < Limit):
        powK[i] = powK[i - 1] * K
        i += 1
    for j in range(i):
        if (isValid(N, powK[j])):
            return "True"
    return "False"


if __name__ == "__main__":
    N = 96889010407
    K = 7
    print(anyPermutation(N, K))

# Expected output:
True
# sum of first n natural numbers which are divisible by 2 and 7 | function to calculate the sum of numbers divisible by 2 or 7 ; 
def sum(N):
    S1 = ((N // 2)) * (2 * 2 + (N // 2 - 1) * 2) // 2
    S2 = ((N // 7)) * (2 * 7 + (N // 7 - 1) * 7) // 2
    S3 = ((N // 14)) * (2 * 14 + (N // 14 - 1) * 14) // 2
    return S1 + S2 - S3


if __name__ == '__main__':
    N = 20
    print(sum(N))

# Expected output:
117
# ways to color a skewed tree such that parent and child have different colors | fast_way is recursive method to calculate power ; 
def fastPow(N, K):
    if (K == 0):
        return 1
    temp = fastPow(N, int(K / 2))
    if (K % 2 == 0):
        return temp * temp
    else:
        return N * temp * temp


def countWays(N, K):
    return K * fastPow(K - 1, N - 1)


N = 3
K = 3
print(countWays(N, K))

# Expected output:
12
# Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | Python3 program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; m is the size of the array
def sumNth(A, B, m, n):
    res = 0
    if (n == 1):
        for i in range(m):
            res = res + A[i]
    elif (n == 2):
        for i in range(m):
            res = res + B[i] * m
    else:
        f = [0] * n
        f[0] = 0
        f[1] = 1
        for i in range(2, n):
            f[i] = f[i - 1] + f[i - 2]
        for i in range(m):
            res = (res + (m * (B[i] * f[n - 1])) + (m * (A[i] * f[n - 2])))
    return res


A = [1, 2, 3]
B = [4, 5, 6]
n = 3
m = len(A)
print(sumNth(A, B, m, n))

# Expected output:
63
# find sum of series 1 ^ 2 | function to find sum of series ; if n is even ; if n is odd ; return the result ; 
def sum_of_series(n):
    result = 0
    if (n % 2 == 0):
        result = -(n * (n + 1)) // 2
    else:
        result = (n * (n + 1)) // 2
    return result


if __name__ == "__main__":
    n = 3
    print(sum_of_series(n))
    n = 10
    print(sum_of_series(n))

# Expected output:
6
-55
# check whether the 

import math


def isPrime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    for i in range(5, int(math .sqrt(n) + 1), 6):
        if n % i == 0 or n % (i + 2) == 0:
            return False
    return True


def isCousinPrime(n1, n2):
    if (not (abs(n1 - n2) == 4)):
        return False
    else:
        return (isPrime(n1) and isPrime(n2))


n1 = 7
n2 = 11
if (isCousinPrime(n1, n2)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# chen prime number | python3 program to check chen prime number ; utility function to check semi - prime number ; increment count of prime number ; if count is greater than 2 , break loop ; if number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; return '1' if count is equal to '2' else return '0 ; utility function to check if a number is prime or not ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to check if the given number is chen prime number or not ; 

import math


def isSemiPrime(num):
    cnt = 0
    for i in range(2, int(math .sqrt(num)) + 1):
        while num % i == 0:
            num /= i
        cnt += 1
        if cnt >= 2:
            break
    if (num > 1):
        cnt += 1
    return cnt == 2


def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i = i + 6
    return True


def isChenPrime(n):
    if (isPrime(n) and (isSemiPrime(n + 2) or isPrime(n + 2))):
        return True
    else:
        return False


n = 7
if (isChenPrime(n)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# thabit number | utility function to check power of two ; function to check if the given number is thabit number ; add 1 to the number ; divide the number by 3 ; check if the given number is power of 2 ; 
def isPowerOfTwo(n):
    return (n and (not (n & (n - 1))))


def isThabitNumber(n):
    n = n + 1
    if (n % 3 == 0):
        n = n // 3
    else:
        return False
    if (isPowerOfTwo(n)):
        return True
    else:
        return False


n = 47
if (isThabitNumber(n)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# sum of all the prime numbers in a given range | suppose the constraint is n <= 1000 ; declare an array for dynamic approach ; method to compute the array ; declare an extra array as array ; iterate the loop till sqrt ( n ) time complexity is o ( log ( n ) x sqrt ( n ) ) ; if ith element of arr is 0 i . e . marked as prime ; mark all of it 's multiples till n as  non-prime by setting the locations to 1 ; update the array ' dp ' with the running sum of prime numbers within the range [ 1 , n ] time complexity is o ( n ) ; here , dp [ i ] is the sum of all the prime numbers within the range [ 1 , i ] ; 

import math


def sieve():
    array = [0] * (N + 1)
    array[0] = 1
    array[1] = 1
    for i in range(2, math .ceil(math .sqrt(N) + 1)):
        if array[i] == 0:
            for j in range(i * i, N + 1, i):
                array[j] = 1
    runningPrimeSum = 0
    for i in range(1, N + 1):
        if array[i] == 0:
            runningPrimeSum += i
        dp[i] = runningPrimeSum


N = 1000
dp = [0] * (N + 1)
l = 4
r = 13
sieve()
print(dp[r] - dp[l - 1])

# Expected output:
36
# smallest integer to be inserted to have equal sums | python3 program to find the smallest number to be added to make the sum of left and right subarrays equal ; function to find the minimum value to be added ; variable to store entire array sum ; variables to store sum of subarray1 and subarray 2 ; minimum value to be added ; traverse through the array ; sum of both halves ; calculate minimum number to be added ; 

import sys


def findMinEqualSums(a, N):
    sum = 0
    for i in range(0, N):
        sum = sum + a[i]
    sum1 = 0
    sum2 = 0
    min = sys .maxsize
    for i in range(0, N - 1):
        sum1 += a[i]
        sum2 = sum - sum1
        if (abs(sum1 - sum2) < min):
            min = abs(sum1 - sum2)
        if (min == 0):
            break
    return min


if __name__ == '__main__':
    a = [3, 2, 1, 5, 7, 8]
    N = len(a)
    print(findMinEqualSums(a, N))

# Expected output:
4
# implementation of wilson primality test | function to calculate the factorial ; function to check if the number is prime or not ; 
def fact(p):
    if (p <= 1):
        return 1
    return p * fact(p - 1)


def isPrime(p):
    if (p == 4):
        return 0
    return (fact(p >> 1) % p)


if (isPrime(127) == 0):
    print(0)
else:
    print(1)

# Expected output:
1
# find the total number of digits in ( n ! ) n | python program to find the total number of digits in ( n ! ) ^ n ; function to find the total number of digits in ( n ! ) ^ n ; finding x ; calculating n * x ; floor ( n * x ) + 1 equivalent to floor ( sum ) + 1 ; 

import math as ma


def CountDigits(n):
    if (n == 1):
        return 1
    sum = 0
    for i in range(2, n + 1):
        sum += ma .log(i, 10)
    sum *= n
    return ma .ceil(sum)


if __name__ == '__main__':
    N = 5
    print(CountDigits(N))

# Expected output:
11
# find the value of max ( f ( x ) ) | python 3 implementation of above approach ; function to calculate the value ; forming the prefix sum arrays ; taking the query ; finding the sum in the range l to r in array a ; finding the sum in the range l to r in array b ; finding the max value of the function ; finding the min value of the function ; 

MAX = 200006
CONS = 32766


def calc(a, b, lr, q, n):
    cc = 0
    for i in range(n - 1):
        a[i + 1] += a[i]
        b[i + 1] += b[i]
    while (q > 0):
        l = lr[cc]
        cc += 1
        r = lr[cc]
        cc += 1
        l -= 2
        r -= 1
        suma = a[r]
        sumb = b[r]
        if (l >= 0):
            suma -= a[l]
            sumb -= b[l]
        M = max(CONS * suma + CONS * sumb, -CONS * suma - CONS * sumb)
        M = max(M, max(CONS * suma - CONS * sumb, -CONS * suma + CONS * sumb))
        m = min(CONS * suma + CONS * sumb, -CONS * suma - CONS * sumb)
        m = min(m, min(CONS * suma - CONS * sumb, -CONS * suma + CONS * sumb))
        print(M - m)
        q -= 1


if __name__ == "__main__":
    n = 5
    q = 2
    a = [0, 7, 3, 4, 5]
    b = [0, 3, 1, 2, 3]
    lr = [0] * (q * 2)
    lr[0] = 1
    lr[1] = 1
    lr[2] = 1
    lr[3] = 3
    calc(a, b, lr, q, n)

# Expected output:
0
917448
# program to find the nth number of the series 2 , 10 , 24 , 44 , 70. ... . | function for calculating nth term of series ; return nth term ; 
def NthTerm(N):
    x = (3 * N * N) % 1000000009
    return ((x - N + 1000000009) % 1000000009)


if __name__ == "__main__":
    N = 4
    print(NthTerm(N))

# Expected output:
44
# check if a number is quartan prime or not | utility function to check if a number is prime or not ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; 
def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i = i + 6
    return True


n = 17
if (isPrime(n) and (n % 16 == 1)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# print a number strictly less than a given number such that all its digits are distinct . | function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ; creating a copy of i ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; 
def findNumber(n):
    i = n - 1
    while (i >= 0):
        count = [0 for i in range(10)]
        x = i
        count1 = 0
        count2 = 0
        while (x):
            count[x % 10] += 1
            x = int(x / 10)
            count1 += 1
        for j in range(0, 10, 1):
            if (count[j] == 1):
                count2 += 1
        if (count1 == count2):
            return i
        i -= 1


if __name__ == '__main__':
    n = 8490
    print(findNumber(n))

# Expected output:
8479
# find two distinct prime numbers with given product | from math lib . import everything ; function to generate all prime numbers less than n ; initialize all entries of boolean array as true . a value in isprime [ i ] will finally be false if i is not a prime , else true bool isprime [ n + 1 ] ; ; if isprime [ p ] is not changed , then it is a prime ; function to print a prime pair with given product ; generating primes using sieve ; traversing all numbers to find first pair ; 

from math import *


def SieveOfEratosthenes(n, isPrime):
    isPrime[0], isPrime[1] = False, False
    for i in range(2, n + 1):
        isPrime[i] = True
    for p in range(2, int(sqrt(n)) + 1):
        if isPrime[p]:
            for i in range(p * 2, n + 1, p):
                isPrime[i] = False


def findPrimePair(n):
    flag = 0
    isPrime = [False] * (n + 1)
    SieveOfEratosthenes(n, isPrime)
    for i in range(2, n):
        x = int(n / i)
        if (isPrime[i] & isPrime[x] and x != i and x * i == n):
            print(i, x)
            flag = 1
            break
    if not flag:
        print("No such pair found")


if __name__ == "__main__":
    n = 39
    findPrimePair(n)

# Expected output:
3 13