giulio98/xlcost-single-prompt huggingface dataset:
# sum of prime numbers without odd prime digits | python3 program for above approach ; find all prime numbers ; store all prime numbers ; function to check if a digit is odd prime or not ; function to find sum ; to store required answer ; get all prime numbers ; traverse through all the prime numbers ; flag stores 1 if a number does not contain any odd primes ; find all digits of a number ; if number does not contain any odd primes ; return the required answer ; 

MAX = 100005


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


def is_prime(n):
    if n in [3, 5, 7]:
        return True
    return False


def find_Sum(n):
    Sum = 0
    v = addPrimes()
    for i in range(len(v)):
        flag = 1
        a = v[i]
        while (a != 0):
            d = a % 10
            a = a // 10
            if (is_prime(d)):
                flag = 0
                break
        if (flag == 1):
            n -= 1
            Sum = Sum + v[i]
        if n == 0:
            break
    return Sum


n = 7
print(find_Sum(n))

# Expected output:
252
# minimum value to be added to x such that it is at least y percent of n | python implementation of the approach ; function to return the required value that must be added to x so that it is at least y percent of n ; required value ; if x is already >= y percent of n ; 

import math


def minValue(n, x, y):
    val = (y * n) / 100
    if x >= val:
        return 0
    else:
        return math .ceil(val) - x


n = 10
x = 2
y = 40
print(minValue(n, x, y))

# Expected output:
2
# check if n is a factorial prime | python3 program to check if given number is a factorial prime ; 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 that returns true if n is a factorial prime ; if n is not prime then return false ; calculate factorial ; if n is a factorial prime ; n is not a factorial prime ; 

from math import sqrt


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(sqrt(n)) + 1, 6):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
    return True


def isFactorialPrime(n):
    if (not isPrime(n)):
        return False
    fact = 1
    i = 1
    while (fact <= n + 1):
        fact = fact * i
        if (n + 1 == fact or n - 1 == fact):
            return True
        i += 1
    return False


if __name__ == "__main__":
    n = 23
    if (isFactorialPrime(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# perfect cube greater than a given number | python 3 implementation of above approach ; function to find the next perfect cube ; 

from math import *


def nextPerfectCube(N):
    nextN = floor(N ** (1 / 3)) + 1
    return nextN ** 3


if __name__ == "__main__":
    n = 35
    print(nextPerfectCube(n))

# Expected output:
64
# sum of all the prime divisors of a number | python3 program to find sum of prime divisors of n ; function to check if the number is prime or not . ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to find sum of prime divisors of n ; return type of sqrt function if float ; both factors are same ; both factors are not same ( i and n / i ) ; 

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
    i = 5
    while i * i <= n:
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i = i + 6
    return True


def SumOfPrimeDivisors(n):
    Sum = 0
    root_n = (int)(math .sqrt(n))
    for i in range(1, root_n + 1):
        if (n % i == 0):
            if (i == (int)(n / i) and isPrime(i)):
                Sum += i
            else:
                if (isPrime(i)):
                    Sum += i
                if (isPrime((int)(n / i))):
                    Sum += (int)(n / i)
    return Sum


n = 60
print("Sum of prime divisors of 60 is", SumOfPrimeDivisors(n))

# Expected output:
Sum of prime divisors of 60 is 10
# position of n among the numbers made of 2 , 3 , 5 & 7 | program position of n among the numbers made of 2 , 3 , 5 & 7 ; if number is 2 then it is on the position pos * 2 + 1 ; if number is 3 then it is on the position pos * 2 + 2 ; if number is 5 then it is on the position pos * 2 + 3 ; if number is 7 then it is on the position pos * 2 + 4 ; 
def findpos(n):
    pos = 0
    for i in n:
        if i == '2':
            pos = pos * 4 + 1
        elif i == '3':
            pos = pos * 4 + 2
        elif i == '5':
            pos = pos * 4 + 3
        elif i == '7':
            pos = pos * 4 + 4
    return pos


n = "777"
print(findpos(n))

# Expected output:
84
# Finding a Non Transitive Co | Checks if any possible triplet ( a , b , c ) satifying the condition that ( a , b ) is coprime , ( b , c ) is coprime but ( a , c ) isnt ; Case 1 : Less than 3 numbers between L and R ; Case 2 : More than 3 numbers between L and R ; triplets should always be of form ( 2 k , 2 k + 1 , 2 k + 2 ) ; Case 3.1 : Exactly 3 numbers in range of form ( 2 k , 2 k + 1 , 2 k + 2 ) ; Case 3.2 : Exactly 3 numbers in range of form ( 2 k - 1 , 2 k , 2 k + 1 ) ; flag = True indicates that a pair exists between L and R ; finding possible Triplet between 2 and 10 ; finding possible Triplet between 23 and 46
def possibleTripletInRange(L, R):
    flag = False
    possibleA = 0
    possibleB = 0
    possibleC = 0
    numbersInRange = (R - L + 1)
    if (numbersInRange < 3):
        flag = False
    elif (numbersInRange > 3):
        flag = True
        if ((L % 2) > 0):
            L += 1
        possibleA = L
        possibleB = L + 1
        possibleC = L + 2
    else:
        if ((L % 2) == 0):
            flag = True
            possibleA = L
            possibleB = L + 1
            possibleC = L + 2
        else:
            flag = False
    if (flag):
        print(
            "(",
            possibleA,
            ",",
            possibleB,
            ",",
            possibleC,
            ") is one such",
            "possible triplet between",
            L,
            "and",
            R)
    else:
        print("No Such Triplet exists between", L, "and", R)


L = 2
R = 10
possibleTripletInRange(L, R)
L = 23
R = 46
possibleTripletInRange(L, R)

# Expected output:
( 2 , 3 , 4 ) is one such possible triplet between 2 and 10
( 24 , 25 , 26 ) is one such possible triplet between 24 and 46
# count n digit numbers not having a particular digit | python implementation of above method ; finding number of possible number with n digits excluding a particular digit ; checking if number of digits is zero ; checking if number of digits is one ; checking if number of digits is odd ; calling digitnumber function with ( digit - 1 ) / 2 digits ; calling digitnumber function with n / 2 digits ; calling digitnumber function checking if excluding digit is zero or non - zero ; 

mod = 1000000007


def digitNumber(n):
    if (n == 0):
        return 1
    if (n == 1):
        return 9
    if (n % 2 != 0):
        temp = digitNumber((n - 1) // 2) % mod
        return (9 * (temp * temp) % mod) % mod
    else:
        temp = digitNumber(n // 2) % mod
        return (temp * temp) % mod


def countExcluding(n, d):
    if (d == 0):
        return (9 * digitNumber(n - 1)) % mod
    else:
        return (8 * digitNumber(n - 1)) % mod


d = 9
n = 3
print(countExcluding(n, d))

# Expected output:
648
# check if 
def isPrime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True


def isEmirp(n):
    n = int(n)
    if isPrime(n) == False:
        return False
    rev = 0
    while n != 0:
        d = n % 10
        rev = rev * 10 + d
        n = int(n / 10)
    return isPrime(rev)


n = 13
if isEmirp(n):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to convert radian to degree | function for convertion ; 
def Convert(radian):
    pi = 3.14159
    degree = radian * (180 / pi)
    return degree


radian = 5
print("degree =", (Convert(radian)))

# Expected output:
degree = 286.4791395439889
# find trace of matrix formed by adding row | return sum of first n integers of an ap ; return the trace of sum of row - major matrix and column - major matrix ; finding nth element in ap in case of row major matrix . ; finding sum of first n integers of ap in case of row major matrix ; finding nth element in ap in case of row major matrix ; finding sum of first n integers of ap in case of column major matrix ; 
def sn(n, an):
    return (n * (1 + an)) / 2


def trace(n, m):
    an = 1 + (n - 1) * (m + 1)
    rowmajorSum = sn(n, an)
    an = 1 + (n - 1) * (n + 1)
    colmajorSum = sn(n, an)
    return int(rowmajorSum + colmajorSum)


N = 3
M = 3
print(trace(N, M))

# Expected output:
30
# maximum of smallest possible area that can get with exactly k cut of given rectangular | utility function ; for the 1 st case ; for the second case ; print final result ; 
def max_area(n, m, k):
    if (k > (n + m - 2)):
        print("Not possible")
    else:
        if (k < max(m, n) - 1):
            result = max(m * (n / (k + 1)), n * (m / (k + 1)))
        else:
            result = max(m / (k - n + 2), n / (k - m + 2))
        print(result)


n = 3
m = 4
k = 1
max_area(n, m, k)

# Expected output:
6.0
# program to find the area of a square | function to find the area ; 
def area_fun(side):
    area = side * side
    return area


side = 4
area = area_fun(side)
print(area)

# Expected output:
16
# count ways to express a number as sum of consecutive numbers | utility method to compute number of ways in which n can be represented as sum of consecutive number ; constraint on values of l gives us the time complexity as o ( n ^ 0.5 ) ; 
def countConsecutive(N):
    count = 0
    L = 1
    while (L * (L + 1) < 2 * N):
        a = (1.0 * N - (L * (L + 1)) / 2) / (L + 1)
        if (a - int(a) == 0.0):
            count += 1
        L += 1
    return count


N = 15
print(countConsecutive(N))
N = 10
print(countConsecutive(N))

# Expected output:
3
1
# automorphic number | function to check automorphic number ; store the square ; start comparing digits ; return false , if any digit of n doesn ' t ▁ ▁ match ▁ with ▁ its ▁ square ' s digits from last ; reduce n and square ; 
def isAutomorphic(N):
    sq = N * N
    while (N > 0):
        if (N % 10 != sq % 10):
            return False
        N /= 10
        sq /= 10
    return True


N = 5
if isAutomorphic(N):
    print("Automorphic")
else:
    print("Not Automorphic")

# Expected output:
Not Automorphic
# number with maximum number of prime factors | return smallest number having maximum prime factors . ; default value of boolean is false ; sieve of eratosthenes ; storing prime numbers . ; generating number having maximum prime factors . ; 
def maxPrimefactorNum(N):
    arr = [True] * (N + 5)
    i = 3
    while (i * i <= N):
        if (arr[i]):
            for j in range(i * i, N + 1, i):
                arr[j] = False
        i += 2
    prime = []
    prime .append(2)
    for i in range(3, N + 1, 2):
        if (arr[i]):
            prime .append(i)
    i = 0
    ans = 1
    while (ans * prime[i] <= N and i < len(prime)):
        ans *= prime[i]
        i += 1
    return ans


N = 40
print(maxPrimefactorNum(N))

# Expected output:
30
# find square root under modulo p | set 1 ( when p is in form of 4 * i + 3 ) | utility function to do modular exponentiation . it returns ( x ^ y ) % p . ; res = 1 initialize result x = x % p update x if it is more than or equal to p ; if y is odd , multiply x with result ; y must be even now y = y >> 1 y = y / 2 ; returns true if square root of n under modulo p exists . assumption : p is of the form 3 * i + 4 where i >= 1 ; try " + ( n ^ ( ( p ▁ + ▁ 1 ) / 4 ) ) " ; try " - ( n ▁ ^ ▁ ( ( p ▁ + ▁ 1 ) / 4 ) ) " ; if none of the above two work , then square root doesn 't exist ; 
def power(x, y, p):

    res = 1
    x = x % p

    while (y > 0):
        if (y & 1):
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p

    return res


def squareRoot(n, p):

    if (p % 4 != 3):
        print("Invalid Input")
        return
    n = n % p
    x = power(n, (p + 1) // 4, p)
    if ((x * x) % p == n):
        print("Square root is ", x)
        return

    x = p - x
    if ((x * x) % p == n):
        print("Square root is ", x)
        return

    print("Square root doesn't exist ")


p = 7
n = 2
squareRoot(n, p)

# Expected output:
Square root is  4
# primality test | set 3 ( millerâ €“ rabin ) | python3 program miller - rabin primality test ; utility function to do modular exponentiation . it returns ( x ^ y ) % p ; initialize result ; update x if it is more than or equal to p ; if y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; this function is called for all k trials . it returns false if n is composite and returns false if n is probably prime . d is an odd number such that d * 2 < sup > r < / sup > = n - 1 for some r >= 1 ; pick a random number in [ 2. . n - 2 ] corner cases make sure that n > 4 ; compute a ^ d % n ; keep squaring x while one of the following doesn 't  happen  (i) d does not reach n-1  (ii) (x^2) % n is not 1  (iii) (x^2) % n is not n-1 ; return composite ; it returns false if n is composite and returns true if n is probably prime . k is an input parameter that determines accuracy level . higher value of k indicates more accuracy . ; corner cases ; find r such that n = 2 ^ d * r + 1 for some r >= 1 ; iterate given nber of ' k ' times ; 

import random


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

    return res


def miillerTest(d, n):
    a = 2 + random.randint(1, n - 4)
    x = power(a, d, n)

    if (x == 1 or x == n - 1):
        return True
    while (d != n - 1):
        x = (x * x) % n
        d *= 2

        if (x == 1):
            return False
        if (x == n - 1):
            return True
    return False


def isPrime(n, k):
    if (n <= 1 or n == 4):
        return False
    if (n <= 3):
        return True
    d = n - 1
    while (d % 2 == 0):
        d //= 2
    for i in range(k):
        if (miillerTest(d, n) == False):
            return False

    return True


k = 4

print("All primes smaller than 100: ")
for n in range(1, 100):
    if (isPrime(n, k)):
        print(n, end=" ")

# Expected output:
All primes smaller than 100: 
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
# length of the longest consecutive 1 s in binary representation | function to find length of the longest consecutive 1 s in binary representation of a number ; initialize result ; count the number of iterations to reach x = 0. ; this operation reduces length of every sequence of 1 s by one . ; 
def maxConsecutiveOnes(x):
    count = 0
    while (x != 0):
        x = (x & (x << 1))
        count = count + 1
    return count


print(maxConsecutiveOnes(14))
print(maxConsecutiveOnes(222))

# Expected output:
3
4
# subtract two numbers without using arithmetic operators | python program to subtract two numbers without using arithmetic operators ; iterate till there is no carry ; borrow contains common set bits of y and unset bits of x ; subtraction of bits of x and y where at least one of the bits is not set ; borrow is shifted by one so that subtracting it from x gives the required sum ; 
def subtract(x, y):
    while (y != 0):
        borrow = (~x) & y
        x = x ^ y
        y = borrow << 1
    return x


x = 29
y = 13
print("x - y is", subtract(x, y))

# Expected output:
x - y is 16
# subtract two numbers without using arithmetic operators | python program to subtract two number without using arithmetic operator recursive implementation . ; 
def subtract(x, y):
    if (y == 0):
        return x
    return subtract(x ^ y, (~x & y) << 1)


x = 29
y = 13
print("x - y is", subtract(x, y))

# Expected output:
x - y is 16
# kth ancestor of all nodes in an n | function to add an edge in the tree ; dfs to find the kth ancestor of every node ; pushing current node in the vector ; traverse its neighbors ; if k ancestors are not found for current node ; add the kth ancestor for the node ; function to find kth ancestor of each node ; building the tree ; stores all parents of a node ; store kth ancestor of all nodes ; print the ancestors ; 
def addEdge(v, x, y):
    v[x].append(y)
    v[y].append(x)


def dfs(tree, temp, ancestor, u, parent, k):
    temp .append(u)
    for i in tree[u]:
        if (i == parent):
            continue
        dfs(tree, temp, ancestor, i, u, k)
    temp .pop()
    if (len(temp) < k):
        ancestor[u] = -1
    else:
        ancestor[u] = temp[len(temp) - k]


def KthAncestor(N, K, E, edges):
    tree = [[]for i in range(N + 1)]
    for i in range(E):
        addEdge(tree, edges[i][0], edges[i][1])
    temp = []
    ancestor = [0] * (N + 1)
    dfs(tree, temp, ancestor, 1, 0, K)
    for i in range(1, N + 1):
        print(ancestor[i], end=" ")


if __name__ == '__main__':
    N = 9
    K = 2
    E = 8
    edges = [[1, 2], [1, 3], [2, 4], [2, 5], [2, 6], [3, 7], [3, 8], [3, 9]]
    KthAncestor(N, K, E, edges)

# Expected output:
-1 -1 -1 1 1 1 1 1 1
# queries to count array elements greater than or equal to a given number with updates | function to build a segment tree ; check for base case ; find mid point ; recursively build the segment tree ; function for push down operation on the segment tree ; function to update the segment tree ; complete overlap ; find mid ; perform push down operation on segment tree ; recursively update the segment tree ; function to process the queryy ; base case ; find mid ; perform push down operation on segment tree ; recursively calculate the result of the queryy ; return the result ; function to count the numbers which are greater than the given queryy ; sort the input array ; create segment tree of size 4 * n vector < int > sum , add , ans ; build the segment tree ; iterate over the queries ; store result in array ; update the elements in the given range ; print the result of queries ; 
def build(sum, a, l, r, rt):
    if (l == r):
        sum[rt] = a[l - 1]
        return
    m = (l + r) >> 1
    build(sum, a, l, m, rt << 1)
    build(sum, a, m + 1, r, rt << 1 | 1)


def pushDown(sum, add, rt, ln, rn):
    if (add[rt]):
        add[rt << 1] += add[rt]
        add[rt << 1 | 1] += add[rt]
        sum[rt << 1] += add[rt] * ln
        sum[rt << 1 | 1] += add[rt] * rn
        add[rt] = 0


def update(sum, add, L, R, C, l, r, rt):
    if (L <= l and r <= R):
        sum[rt] += C * (r - l + 1)
        add[rt] += C
        return
    m = (l + r) >> 1
    pushDown(sum, add, rt, m - l + 1, r - m)
    if (L <= m):
        update(sum, add, L, R, C, l, m, rt << 1)
    if (R > m):
        update(sum, add, L, R, C, m + 1, r, rt << 1 | 1)


def queryy(sum, add, L, R, l, r, rt):
    if (L <= l and r <= R):
        return sum[rt]
    m = (l + r) >> 1
    pushDown(sum, add, rt, m - l + 1, r - m)
    ans = 0
    if (L <= m):
        ans += queryy(sum, add, L, R, l, m, rt << 1)
    if (R > m):
        ans += queryy(sum, add, L, R, m + 1, r, (rt << 1 | 1))
    return ans


def sequenceMaintenance(n, q, a, b, m):
    a = sorted(a)
    sum = [0] * (4 * n)
    add = [0] * (4 * n)
    ans = []
    build(sum, a, 1, n, 1)
    for i in range(q):
        l = 1
        r = n
        pos = -1
        while (l <= r):
            m = (l + r) >> 1
            if (queryy(sum, add, m, m, 1, n, 1) >= b[i]):
                r = m - 1
                pos = m
            else:
                l = m + 1
        if (pos == -1):
            ans .append(0)
        else:
            ans .append(n - pos + 1)
            update(sum, add, pos, n, -m, 1, n, 1)
    for i in ans:
        print(i, end=" ")


if __name__ == '__main__':
    N = 4
    Q = 3
    M = 1
    arr = [1, 2, 3, 4]
    query = [4, 3, 1]
    sequenceMaintenance(N, Q, arr, query, M)

# Expected output:
1 2 4
# minimize array length by repeatedly replacing co | python3 program for the above approach ; function to find the final array length by replacing coprime pair with 1 ; iterate over all pairs of element ; check if gcd is 1 ; if no coprime pair found return false ; 

import math


def hasCoprimePair(arr, n):
    for i in range(n - 1):
        for j in range(i + 1, n):
            if (math .gcd(arr[i], arr[j]) == 1):
                return True
    return False


if __name__ == "__main__":
    n = 3
    arr = [6, 9, 15]
    if (hasCoprimePair(arr, n)):
        print(1)
    else:
        print(n)

# Expected output:
3
# count of ways to split n into triplets forming a triangle | function to return the required number of ways ; check if a , b , c can form a triangle ; return number of ways ; 
def Numberofways(n):
    count = 0
    for a in range(1, n):
        for b in range(1, n):
            c = n - (a + b)
            if (a < b + c and b < a + c and c < a + b):
                count += 1
    return count


n = 15
print(Numberofways(n))

# Expected output:
28
# count of pairs having each element equal to index of the other from an array | function to print the count of pair ; iterate over all the elements of the array ; increment the count ; print the result ; 
def countPairs(N, arr):
    count = 0
    for i in range(N):
        if (i == arr[arr[i] - 1] - 1):
            count += 1
    print(count // 2)


if __name__ == "__main__":
    arr = [2, 1, 4, 3]
    N = len(arr)
    countPairs(N, arr)

# Expected output:
2
# find length of longest fibonacci like subsequence | function to return the max length of fibonacci subsequence ; store all array elements in a hash table ; check until next fib element is found ; next element of fib subseq ; 
def LongestFibSubseq(A, n):
    S = set(A)
    maxLen = 0
    for i in range(0, n):
        for j in range(i + 1, n):
            x = A[j]
            y = A[i] + A[j]
            length = 2
            while y in S:
                z = x + y
                x = y
                y = z
                length += 1
                maxLen = max(maxLen, length)
    return maxLen if maxLen >= 3 else 0


if __name__ == "__main__":
    A = [1, 2, 3, 4, 5, 6, 7, 8]
    n = len(A)
    print(LongestFibSubseq(A, n))

# Expected output:
5
# maximum types of candies a person can eat if only n / 2 of them can be eaten | function to find number of candy types ; declare a hashset to store candies ; traverse the given array and inserts element into set ; return the result ; function to find maximum number of types of candies a person can eat ; store the number of candies allowed to eat ; store the number of candy types ; return the result ; 
def num_candyTypes(candies):
    s = set()
    for i in range(len(candies)):
        s .add(candies[i])
    return len(s)


def distribute_candies(candies):
    allowed = len(candies) / 2
    types = num_candyTypes(candies)
    if (types < allowed):
        print(int(types))
    else:
        print(int(allowed))


if __name__ == '__main__':
    candies = [4, 4, 5, 5, 3, 3]
    distribute_candies(candies)

# Expected output:
3
# length of diagonals of a rhombus using length of side and vertex angle | python program to implement the above approach ; function to calculate the length of diagonals of a rhombus using length of sides and vertex angle ; 

import math


def Length_Diagonals(a, theta):
    p = a * math .sqrt(2 + (2 * math .cos(math .radians(theta))))
    q = a * math .sqrt(2 - (2 * math .cos(math .radians(theta))))
    return [p, q]


A = 6
theta = 45
ans = Length_Diagonals(A, theta)
print(round(ans[0], 2), round(ans[1], 2))

# Expected output:
11.09 4.59