giulio98/xlcost-single-prompt huggingface dataset:
# sum of pairwise products | efficient python3 program to find sum of given series . ; sum of multiples of 1 is 1 * ( 1 + 2 + . . ) ; adding sum of multiples of numbers other than 1 , starting from 2. ; subtract previous number from current multiple . ; for example , for 2 , we get sum as ( 2 + 3 + 4 + ... . ) * 2 ; 
def findSum(n):
    multiTerms = n * (n + 1) // 2
    sm = multiTerms
    for i in range(2, n + 1):
        multiTerms = multiTerms - (i - 1)
        sm = sm + multiTerms * i
    return sm


n = 5
print(findSum(n))

# Expected output:
140
# lemoine 's conjecture | python code to verify lemoine 's conjecture for any odd number >= 7 ; function to check if a number is prime or not ; representing n as p + ( 2 * q ) to satisfy lemoine 's conjecture ; declaring a map to hold pairs ( p , q ) ; finding various values of p for each q to satisfy n = p + ( 2 * q ) ; after finding a pair that satisfies the equation , check if both p and q are prime or not ; if both p and q are prime , store them in the map ; displaying all pairs ( p , q ) that satisfy lemoine ' s ▁ conjecture ▁ for ▁ the ▁ number ▁ ' n ; 

from math import sqrt


def isPrime(n: int) -> bool:
    if n < 2:
        return False
    for i in range(2, int(sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True


def lemoine(n: int) -> None:
    pr = dict()
    for q in range(1, n // 2 + 1):
        p = n - 2 * q
        if isPrime(p) and isPrime(q):
            if p not in pr:
                pr[p] = q


if __name__ == "__main__":
    n = 39
    print(n, "can be expressed as ")
    lemoine(n)

# Expected output:
39 can be expressed as
# sum of n digit numbers divisible by a given number | find the sum of having n digit and divisible by the number ; compute the first and last term ; first number which is divisible by given number ; last number which is divisible by given number ; total divisible number ; return the total sum ; 
def totalSumDivisibleByNum(digit, number):
    firstnum = pow(10, digit - 1)
    lastnum = pow(10, digit)
    firstnum = (firstnum - firstnum % number) + number
    lastnum = (lastnum - lastnum % number)
    count = ((lastnum - firstnum) / number + 1)
    return int(((lastnum + firstnum) * count) / 2)


digit = 3
num = 7
print(totalSumDivisibleByNum(digit, num))

# Expected output:
70336
# Program for N | Python 3 Program to find nth term of Arithmetic progression ; using formula to find the Nth term t ( n ) = a ( 1 ) + ( n - 1 ) * d ; starting number ; Common difference ; N th term to be find ; Display the output
def Nth_of_AP(a, d, N):
    return (a + (N - 1) * d)


a = 2
d = 1
N = 5
print("The ", N, "th term of the series is : ", Nth_of_AP(a, d, N))

# Expected output:
The  5 th term of the series is :  6
# fibbinary numbers ( no consecutive 1 s in binary ) | function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; 
def checkFibinnary(n):
    prev_last = 0
    while (n):
        if ((n & 1) and prev_last):
            return False
        prev_last = n & 1
        n >>= 1
    return True


n = 10
if (checkFibinnary(n)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# sum of the series 5 + 55 + 555 + . . up to n terms | function which return the the sum of series ; 
def sumOfSeries(n):
    return (int)(0.6172 * (pow(10, n) - 1) - 0.55 * n)


n = 2
print(sumOfSeries(n))

# Expected output:
60
# nonagonal number | function to find nth nonagonal number . ; formula to find nth nonagonal number . ; 
def Nonagonal(n):
    return int(n * (7 * n - 5) / 2)


n = 10
print(Nonagonal(n))

# Expected output:
325
# check if a large number is divisible by 20 | python3 program to check if a large number is divisible by 20. ; get number with last two digits ; check if the number formed by last two digits is divisible by 5 and 4. ; 

import math


def divisibleBy20(num):
    lastTwoDigits = int(num[-2:])
    return ((lastTwoDigits % 5 == 0 and lastTwoDigits % 4 == 0))


num = "63284689320"
if (divisibleBy20(num)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# divisibility by 12 for a large number | python program to check if number is divisible by 12 ; if number greater then 3 ; find last digit ; no is odd ; find second last digit ; find sum of all digits ; f number is less then r equal to 100 ; 

import math


def isDvisibleBy12(num):
    if (len(num) >= 3):
        d1 = int(num[len(num) - 1])
        if (d1 % 2 != 0):
            return False
        d2 = int(num[len(num) - 2])
        sum = 0
        for i in range(0, len(num)):
            sum += int(num[i])
        return (sum % 3 == 0 and (d2 * 10 + d1) % 4 == 0)
    else:
        number = int(num)
        return (number % 12 == 0)


num = "12244824607284961224"
if (isDvisibleBy12(num)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# largest number that is not a perfect square | python program to find the largest non perfect square number among n numbers ; takes the sqrt of the number ; checks if it is a perfect square number ; function to find the largest non perfect square number ; stores the maximum of all non perfect square numbers ; traverse for all elements in the array ; store the maximum if not a perfect square ; 

import math


def check(n):
    d = int(math .sqrt(n))
    if (d * d == n):
        return True
    return False


def largestNonPerfectSquareNumber(a, n):
    maxi = -1
    for i in range(0, n):
        if (check(a[i]) == False):
            maxi = max(a[i], maxi)
    return maxi


a = [16, 20, 25, 2, 3, 10]
n = len(a)
print(largestNonPerfectSquareNumber(a, n))

# Expected output:
20
# Program to print Arithmetic Progression series | Python 3 Program to print an arithmetic progression series ; Printing AP by simply adding d to previous term . ; starting number ; Common difference ; N th term to be find
def printAP(a, d, n):
    curr_term = a
    for i in range(1, n + 1):
        print(curr_term, end=' ')
        curr_term = curr_term + d


a = 2
d = 1
n = 5
printAP(a, d, n)

# Expected output:
2 3 4 5 6
# count number of trailing zeros in product of array | returns count of zeros in product of array ; count number of 2 s in each element ; count number of 5 s in each element ; return the minimum ; 
def countZeros(a, n):
    count2 = 0
    count5 = 0
    for i in range(0, n):
        while (a[i] % 2 == 0):
            a[i] = a[i] // 2
            count2 = count2 + 1
        while (a[i] % 5 == 0):
            a[i] = a[i] // 5
            count5 = count5 + 1
    if (count2 < count5):
        return count2
    else:
        return count5


a = [10, 100, 20, 30, 50, 90, 12, 80]
n = len(a)
print(countZeros(a, n))

# Expected output:
9
# sum of square of first n even numbers | efficient python3 code to find sum of square of first n even numbers ; 
def squareSum(n):
    return int(2 * n * (n + 1) * (2 * n + 1) / 3)


ans = squareSum(8)
print(ans)

# Expected output:
816
# mã ¼ nchhausen number | python 3 code for ma14nchhausen number ; pwr [ i ] is going to store i raised to power i . ; function to check out whether the number is ma14nchhausen number or not ; precompute i raised to power i for every i ; the input here is fixed i . e . it will check up to n ; check the integer for ma14nchhausen number , if yes then print out the number ; 

import math
pwr = [0] * 10


def isMunchhausen(n):
    sm = 0
    temp = n
    while (temp):
        sm = sm + pwr[(temp % 10)]
        temp = temp // 10
    return (sm == n)


def printMunchhausenNumbers(n):
    for i in range(0, 10):
        pwr[i] = math .pow((float)(i), (float)(i))
    for i in range(1, n + 1):
        if (isMunchhausen(i)):
            print(i)


n = 10000
printMunchhausenNumbers(n)

# Expected output:
1
3435
# k | python3 code to compute k - th digit in a ^ b ; computin a ^ b in python ; getting last digit ; increasing count by 1 ; if current number is required digit ; remove last digit ; 
def kthdigit(a, b, k):
    p = a ** b
    count = 0
    while (p > 0 and count < k):
        rem = p % 10
        count = count + 1
        if (count == k):
            return rem
        p = p / 10


a = 5
b = 2
k = 1
ans = kthdigit(a, b, k)
print(ans)

# Expected output:
5
# recursive sum of digit in n ^ x , where n and x are very large | python3 code for sum of digit of n ^ x ; function to get sum of digits of a number ; function to return sum ; find sum of digits in n ; find remainder of exponent ; 

import math


def digSum(n):
    if (n == 0):
        return 0
    if n % 9 == 0:
        return 9
    else:
        return (n % 9)


def PowDigSum(n, x):
    sum = digSum(n)
    rem = x % 6
    if ((sum == 3 or sum == 6) and x > 1):
        return 9
    elif (x == 1):
        return sum
    elif (x == 0):
        return 1
    elif (rem == 0):
        return digSum(math .pow(sum, 6))
    else:
        return digSum(math .pow(sum, rem))


n = 33333
x = 332654
print(PowDigSum(n, x))

# Expected output:
9
# container with most water | python3 code for max water container ; calculating the max area ; 
def maxArea(A):
    l = 0
    r = len(A) - 1
    area = 0
    while l < r:
        area = max(area, min(A[l], A[r]) * (r - l))
        if A[l] < A[r]:
            l += 1
        else:
            r -= 1
    return area


a = [1, 5, 4, 3]
b = [3, 1, 2, 4, 5]
print(maxArea(a))
print(maxArea(b))

# Expected output:
6
12
# program for mobius function | python program to evaluate mobius def m ( n ) = 1 if n = 1 m ( n ) = 0 if any prime factor of n is contained twice m ( n ) = ( - 1 ) ^ ( no of distinctprime factors ) ; def to check if n is prime or not ; handling 2 separately ; if 2 ^ 2 also divides n ; check for all other prime factors ; if i divides n ; if i ^ 2 also divides n ; 

import math


def isPrime(n):
    if (n < 2):
        return False
    for i in range(2, n + 1):
        if (n % i == 0):
            return False
        i = i * i
    return True


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


N = 17
print("MobiusdefsM(N)atN={}is:{}".format(N, mobius(N)))
print("MobiusdefsM(N)atN=25is:{}".format(mobius(25)))
print("MobiusdefsM(N)atN=6is:{}".format(mobius(6)))

# Expected output:
MobiusdefsM(N)atN=17is:-1
MobiusdefsM(N)atN=25is:0
MobiusdefsM(N)atN=6is:1
# sum of squares of binomial coefficients | function to return product of number from start to end . ; return the sum of square of binomial coefficient ; 
def factorial(start, end):
    res = 1
    for i in range(start, end + 1):
        res *= i
    return res


def sumofsquare(n):
    return int(factorial(n + 1, 2 * n) / factorial(1, n))


n = 4
print(sumofsquare(n))

# Expected output:
70
# find nth fibonacci number using golden ratio | approximate value of golden ratio ; fibonacci numbers upto n = 5 ; function to find nth fibonacci number ; fibonacci numbers for n < 6 ; else start counting from 5 th term ; 

PHI = 1.6180339
f = [0, 1, 1, 2, 3, 5]


def fib(n):
    if n < 6:
        return f[n]
    t = 5
    fn = 5
    while t < n:
        fn = round(fn * PHI)
        t += 1
    return fn


n = 9
print(n, "th Fibonacci Number =", fib(n))

# Expected output:
9 th Fibonacci Number = 34
# Euler Method for solving differential equation | Consider a differential equation dy / dx = ( x + y + xy ) ; Function for euler formula ; Iterating till the point at which we need approximation ; Printing approximation ; Initial Values ; Value of x at which we need approximation
def func(x, y):
    return (x + y + x * y)


def euler(x0, y, h, x):
    temp = -0
    while x0 < x:
        temp = y
        y = y + h * func(x0, y)
        x0 = x0 + h
    print("Approximate solution at x = ", x, " is ", "%.6f" % y)


x0 = 0
y0 = 1
h = 0.025
x = 0.1
euler(x0, y0, h, x)

# Expected output:
Approximate solution at x =  0.1  is  1.111673
# find x and y satisfying ax + by = n | function to find the solution ; traverse for all possible values ; check if it is satisfying the equation ; 
def solution(a, b, n):
    i = 0
    while i * a <= n:
        if (n - (i * a)) % b == 0:
            print("x = ", i, ", y = ", int((n - (i * a)) / b))
            return 0
        i = i + 1
    print("No solution")


a = 2
b = 3
n = 7
solution(a, b, n)

# Expected output:
x =  2 , y =  1
# sum of binomial coefficients | python program to find the sum of binomial coefficient . ; returns value of binomial coefficient sum ; 

import math


def binomialCoeffSum(n):
    return (1 << n)


n = 4
print(binomialCoeffSum(n))

# Expected output:
16
# program to compute division upto n decimal places | python3 program to compute division upto n decimal places . ; base cases ; since n <= 0 , don 't  compute after the decimal ; handling negative numbers ; integral division ; now one by print digits after dot using school division method . ; 
def precisionCompute(x, y, n):
    if y == 0:
        print("Infinite")
        return
    if x == 0:
        print(0)
        return
    if n <= 0:
        print(x / y)
        return
    if (((x > 0) and (y < 0)) or ((x < 0) and (y > 0))):
        print("-", end="")
        if x < 0:
            x = -x
        if y < 0:
            y = -y
    d = x / y
    for i in range(0, n + 1):
        print(d)
        x = x - (y * d)
        if x == 0:
            break
        x = x * 10
        d = x / y
        if (i == 0):
            print(".", end="")


x = 22
y = 7
n = 15
precisionCompute(x, y, n)

# Expected output:
3.142857142857143
# program to determine the quadrant of the cartesian plane | function to check quadrant ; 
def quadrant(x, y):
    if (x > 0 and y > 0):
        print("lies in First quadrant")
    elif (x < 0 and y > 0):
        print("lies in Second quadrant")
    elif (x < 0 and y < 0):
        print("lies in Third quadrant")
    elif (x > 0 and y < 0):
        print("lies in Fourth quadrant")
    elif (x == 0 and y > 0):
        print("lies at positive y axis")
    elif (x == 0 and y < 0):
        print("lies at negative y axis")
    elif (y == 0 and x < 0):
        print("lies at negative x axis")
    elif (y == 0 and x > 0):
        print("lies at positive x axis")
    else:
        print("lies at origin")


x = 1
y = 1
quadrant(x, y)

# Expected output:
lies in First quadrant
# check if a number is full prime | function to check digits ; check all digits are prime or not ; check if digits are prime or not ; to check if n is prime or not ; check for all factors ; to check if n is full prime ; the order is important here for efficiency . ; 
def checkDigits(n):
    while (n):
        dig = n % 10
        if (dig != 2 and dig != 3 and dig != 5 and dig != 7):
            return 0
        n = n / 10
    return 1


def prime(n):
    if (n == 1):
        return 0
    i = 2
    while i * i <= n:
        if (n % i == 0):
            return 0
        i = i + 1
    return 1


def isFullPrime(n):
    return (checkDigits(n) and prime(n))


n = 53
if (isFullPrime(n)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# narcissistic number | python program for checking of narcissistic number ; traversing through the string ; converting character to int ; converting string to integer ; comparing number and sum ; 
def getResult(st):
    sum = 0
    length = len(st)
    for i in st:
        sum = sum + int(i)**length
    number = int(st)
    if (number == sum):
        return "true"
    else:
        return "false"


st = "153"
print(getResult(st))

# Expected output:
true
# schrã ¶ derâ €“ hipparchus number | a memoization based optimized python3 program to find n - th schraderahipparchus number ; 
def nthSHN(n, dp):
    if (n == 1 or n == 2):
        dp[n] = 1
        return dp[n]
    if (dp[n] != -1):
        return dp[n]
    dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) - (n - 3) * nthSHN(n - 2, dp)) / n
    return dp[n]


n = 6
dp = [-1 for i in range(500)]
print(nthSHN(n, dp))

# Expected output:
197.0
# sum of first n even numbers | function to find sum of first n even numbers ; sum of first n even numbers ; next even number ; required sum ; 
def evensum(n):
    curr = 2
    sum = 0
    i = 1
    while i <= n:
        sum += curr
        curr += 2
        i = i + 1
    return sum


n = 20
print("sum of first ", n, "even number is: ", evensum(n))

# Expected output:
sum of first  20 even number is:  420
# sum of first n even numbers | function to find sum of first n even numbers ; required sum ; 
def evensum(n):
    return n * (n + 1)


n = 20
print("sum of first", n, "even number is: ", evensum(n))

# Expected output:
sum of first 20 even number is:  420
# number of gp ( geometric progression ) subsequences of size 3 | python3 program to count gp subsequences of size 3. ; returns count of g . p . subsequences with length 3 and common ratio r ; hashing to maintain left and right array elements to the main count ; stores the answer ; traverse through the elements ; traverse through all elements and find out the number of elements as k1 * k2 ; keep the count of left and right elements left is a [ i ] / r and right a [ i ] * r ; if the current element is divisible by k , count elements in left hash . ; decrease the count in right hash ; number of right elements ; calculate the answer ; left count of a [ i ] ; returns answer ; 

from collections import defaultdict


def subsequences(a, n, r):
    left = defaultdict(lambda: 0)
    right = defaultdict(lambda: 0)
    ans = 0
    for i in range(0, n):
        c1, c2 = 0, 0
        if a[i] % r == 0:
            c1 = left[a[i] // r]
        right[a[i]] -= 1
        c2 = right[a[i] * r]
        ans += c1 * c2
        left[a[i]] += 1
    return ans


if __name__ == "__main__":
    a = [1, 2, 6, 2, 3, 6, 9, 18, 3, 9]
    n = len(a)
    r = 3
    print(subsequences(a, n, r))

# Expected output:
-1
# find element in array that divides all array elements | returns gcd of two numbers ; function to return the desired number if exists ; find gcd of array ; check if gcd is present in array ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def findNumber(arr, n):
    ans = arr[0]
    for i in range(0, n):
        ans = gcd(ans, arr[i])
    for i in range(0, n):
        if (arr[i] == ans):
            return ans
    return -1


arr = [2, 2, 4]
n = len(arr)
print(findNumber(arr, n))

# Expected output:
2
# special prime numbers | python3 program to check whether there exist at least k or not in range [ 2. . n ] ; generating all the prime numbers from 2 to n . ; if a prime number is special prime number , then we increments the value of k . ; if at least k special prime numbers are present , then we return 1. else we return 0 from outside of the outer loop . ; 

primes = []


def SieveofEratosthenes(n):
    visited = [False] * (n + 2)
    for i in range(2, n + 2):
        if (visited[i] == False):
            for j in range(i * i, n + 2, i):
                visited[j] = True
            primes .append(i)


def specialPrimeNumbers(n, k):
    SieveofEratosthenes(n)
    count = 0
    for i in range(len(primes)):
        for j in range(i - 1):
            if (primes[j] + primes[j + 1] + 1 == primes[i]):
                count += 1
                break
        if (count == k):
            return True
    return False


n = 27
k = 2
if (specialPrimeNumbers(n, k)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# prime factors of a big number | python3 program to print prime factors and their powers . ; function to calculate all the prime factors and count of every prime factor ; count the number of times 2 divides ; equivalent to n = n / 2 ; ; if 2 divides it ; check for all the possible numbers that can divide it ; if n at the end is a prime number . ; 

import math


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


n = 1000000000000000000
factorize(n)

# Expected output:
2 18
5 18
# minimum gcd operations to make all array elements one | __gcd function ; function to count number of moves . ; counting number of ones . ; if there is a one ; find smallest subarray with gcd equals to one . ; to calculate gcd ; not possible ; final answer ; 
def __gcd(a, b):
    if (a == 0):
        return b
    return __gcd(b % a, a)


def minimumMoves(A, N):
    one = 0
    for i in range(N):
        if (A[i] == 1):
            one += 1
    if (one != 0):
        return N - one
    minimum = +2147483647
    for i in range(N):
        g = A[i]
        for j in range(i + 1, N):
            g = __gcd(A[j], g)
            if (g == 1):
                minimum = min(minimum, j - i)
                break
    if (minimum == +2147483647):
        return -1
    else:
        return N + minimum - 1


A = [2, 4, 3, 9]
N = len(A)
print(minimumMoves(A, N))

# Expected output:
4
# given n and standard deviation , find n elements | python program to find n elements ; function to print series of n elements ; if s . d . is 0 then print all elements as 0. ; print n 0 's ; if s . d . is even ; print - sd , + sd , - sd , + sd ; if odd ; if odd convert n to a float integer ; print one element to be 0 ; print ( n - 1 ) elements as xi derived from the formula ; 

import math


def series(n, d):
    if d == 0:
        for i in range(n):
            print("0", end=' ')
        return 1
    if n % 2 == 0:
        i = 1
        while i <= n:
            print("%.5f" % ((math .pow(-1, i) * d)), end=' ')
            i += 1
    else:
        m = n
        r = (m / (m - 1))
        g = (float)(d * float(math .sqrt(r)))
        print("0 ", end=' ')
        i = 1
        while i < n:
            print("%.5f" % (math .pow(-1, i) * g), end=' ')
            i = i + 1
    print("")


n = 3
d = 3
series(n, d)

# Expected output:
0  -3.67423 3.67423
# total no of 1 's in numbers | python3 code to count the frequency of 1 in numbers less than or equal to the given number . ; 
def countDigitOne(n):
    countr = 0
    for i in range(1, n + 1):
        str1 = str(i)
        countr += str1 .count("1")
    return countr


n = 13
print(countDigitOne(n))
n = 131
print(countDigitOne(n))
n = 159
print(countDigitOne(n))

# Expected output:
6
66
96
# exponential squaring ( fast modulo multiplication ) | prime modulo value ; for cases where exponent is not an even value ; 

N = 1000000007


def exponentiation(bas, exp):
    t = 1
    while (exp > 0):
        if (exp % 2 != 0):
            t = (t * bas) % N
        bas = (bas * bas) % N
        exp = int(exp / 2)
    return t % N


bas = 5
exp = 100000
modulo = exponentiation(bas, exp)
print(modulo)

# Expected output:
754573817
# gcd of factorials of two numbers | python code to find gcd of factorials of two numbers . ; 

import math


def gcdOfFactorial(m, n):
    return math .factorial(min(m, n))


m = 5
n = 9
print(gcdOfFactorial(m, n))

# Expected output:
120
# recursive sum of digits of a number is prime or not | function for recursive digit sum ; function to check if prime or not the single digit ; calls function which returns sum till single digit ; checking prime ; 
def recDigSum(n):
    if n == 0:
        return 0
    else:
        if n % 9 == 0:
            return 9
        else:
            return n % 9


def check(n):
    n = recDigSum(n)
    if n == 2 or n == 3 or n == 5 or n == 7:
        print("Yes")
    else:
        print("No")


n = 5602
check(n)

# Expected output:
No
# find n | python program to find the value at n - th place in the given sequence ; definition of findnumber function ; finding x from equation n = x ( x + 1 ) / 2 + 1 ; base of current block ; value of n - th element ; 

import math


def findNumber(n):
    x = int(math .floor((-1 + math .sqrt(1 + 8 * n - 8)) / 2))
    base = (x * (x + 1)) / 2 + 1
    return n - base + 1


n = 55
print(findNumber(n))

# Expected output:
10.0