giulio98/xlcost-single-prompt huggingface dataset:
# largest divisor of a number not divisible by a perfect square | efficient python3 program to find the largest divisor not divisible by any perfect square greater than 1 ; function to find the largest divisor not divisible by any perfect square greater than 1 ; if the number is divisible by i * i , then remove one i ; now all squares are removed from n ; 

import math


def findLargestDivisor(n):
    for i in range(2, int(math .sqrt(n)) + 1):
        while (n % (i * i) == 0):
            n = n // i
    return n


if __name__ == "__main__":
    n = 12
    print(findLargestDivisor(n))
    n = 97
    print(findLargestDivisor(n))

# Expected output:
6
97
# arithmetic progression | returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression ; sort array ; after sorting , difference between consecutive elements must be same . ; 
def checkIsAP(arr, n):
    if (n == 1):
        return True
    arr .sort()
    d = arr[1] - arr[0]
    for i in range(2, n):
        if (arr[i] - arr[i - 1] != d):
            return False
    return True


arr = [20, 15, 5, 0, 10]
n = len(arr)
print("Yes")if (checkIsAP(arr, n))else print("No")

# Expected output:
Yes
# check if a number is triperfect number | returns true if n is triperfect ; to store sum of divisors . adding 1 and n since they are divisors of n . ; find all divisors and add them ; if sum of divisors is equal to 3 * n , then n is a triperfect number ; 
def isTriPerfect(n):
    sum = 1 + n
    i = 2
    while i * i <= n:
        if n % i == 0:
            if n / i == i:
                sum = sum + i
            else:
                sum = sum + i + n / i
        i += 1
    return (True if sum == 3 * n and n != 1 else False)


n = 120
if isTriPerfect(n):
    print(n, "is a Triperfect number")

# Expected output:
120 is a Triperfect number
# sum of first n natural numbers which are divisible by x or y | python 3 program to find sum of numbers from 1 to n which are divisible by x or y ; function to calculate the sum of numbers divisible by x or y ; 

from math import ceil, floor


def sum(N, X, Y):
    S1 = floor(floor(N / X) * floor(2 * X + floor(N / X - 1) * X) / 2)
    S2 = floor(floor(N / Y)) * floor(2 * Y + floor(N / Y - 1) * Y) / 2
    S3 = floor(floor(N / (X * Y))) * floor(2 * (X * Y) +
                                           floor(N / (X * Y) - 1) * (X * Y)) / 2
    return S1 + S2 - S3


if __name__ == '__main__':
    N = 14
    X = 3
    Y = 5
    print(int(sum(N, X, Y)))

# Expected output:
45
# count numbers from range whose prime factors are only 2 and 3 | function to count the number within a range whose prime factors are only 2 and 3 ; start with 2 so that 1 doesn 't get counted ; while num is divisible by 2 , divide it by 2 ; while num is divisible by 3 , divide it by 3 ; if num got reduced to 1 then it has only 2 and 3 as prime factors ; 
def findTwoThreePrime(l, r):
    if (l == 1):
        l += 1
    count = 0
    for i in range(l, r + 1):
        num = i
        while (num % 2 == 0):
            num //= 2
        while (num % 3 == 0):
            num //= 3
        if (num == 1):
            count += 1
    return count


if __name__ == "__main__":
    l = 1
    r = 10
    print(findTwoThreePrime(l, r))

# Expected output:
6
# maximum number with same digit factorial product | function to return the required number ; count the frequency of each digit ; 4 ! can be expressed as 2 ! * 2 ! * 3 ! ; 6 ! can be expressed as 5 ! * 3 ! ; 8 ! can be expressed as 7 ! * 2 ! * 2 ! * 2 ! ; 9 ! can be expressed as 7 ! * 3 ! * 3 ! * 2 ! ; to store the required number ; if number has only either 1 and 0 as its digits ; generate the greatest number possible ; 
def getNumber(s):

    number_of_digits = len(s)

    freq = [0]*10
    for i in range(number_of_digits):
        if (s[i] == '1' or s[i] == '2' or s[i] == '3' or s[i] == '5' or s[i] == '7'):
            freq[ord(s[i]) - 48] += 1
        if (s[i] == '4'):
            freq[2] += 2
            freq[3] += 1
        if (s[i] == '6'):
            freq[5] += 1
            freq[3] += 1

        if (s[i] == '8'):
            freq[7] += 1
            freq[2] += 3

        if (s[i] == '9'):
            freq[7] += 1
            freq[3] += 2
            freq[2] += 1
    t = ""
    if (freq[1] == number_of_digits or freq[0] == number_of_digits or (freq[0] + freq[1]) == number_of_digits):
        return s
    else:
        for i in range(9, 1, -1):
            ctr = freq[i]
            while (ctr > 0):
                t += chr(i + 48)
                ctr -= 1

        return t


s = "1280"
print(getNumber(s))

# Expected output:
72222
# program to find first n iccanobif numbers | iterative function to reverse digits of num ; function to print first n icanobif numbers ; initialize first , second numbers ; print first two numbers ; reversing digit of previous two terms and adding them ; 
def reversedigit(num):
    rev_num = 0
    while num > 0:
        rev_num = rev_num * 10 + num % 10
        num = num // 10
    return rev_num


def icanobifNumbers(N):
    first = 0
    second = 1
    if N == 1:
        print(first)
    elif N == 2:
        print(first, second)
    else:
        print(first, second, end=" ")
        for i in range(3, N + 1):
            x = reversedigit(first)
            y = reversedigit(second)
            print(x + y, end=" ")
            temp = second
            second = x + y
            first = temp


N = 12
icanobifNumbers(N)

# Expected output:
0 1 1 2 3 5 8 13 39 124 514 836
# add n digits to a such that it is divisible by b after each addition | python3 implementation of the approach ; try all digits from ( 0 to 9 ) ; fails in the first move itself ; add ( n - 1 ) 0 's ; 
def addNDigits(a, b, n):
    num = a
    for i in range(10):
        tmp = a * 10 + i
        if (tmp % b == 0):
            a = tmp
            break
    if (num == a):
        return -1
    for j in range(n - 1):
        a *= 10
    return a


if __name__ == "__main__":
    a = 5
    b = 3
    n = 3
    print(addNDigits(a, b, n))

# Expected output:
5100
# count number of triplets ( a , b , c ) such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n | python3 program to find number of triplets 1 <= a <= b <= c <= n , such that a ^ 2 + b ^ 2 = c ^ 2 ; function to ind number of triplets 1 <= a <= b <= c <= n , such that a ^ 2 + b ^ 2 = c ^ 2 ; to store required answer ; run nested loops for first two numbers . ; third number ; check if third number is perfect square and less than n ; 

import math


def Triplets(n):
    ans = 0
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            x = i * i + j * j
            y = int(math .sqrt(x))
            if (y * y == x and y <= n):
                ans += 1
    return ans


if __name__ == "__main__":
    n = 10
    print(Triplets(n))

# Expected output:
2
# sum of the digits of a number n written in all bases from 2 to n / 2 | python 3 implementation of the approach ; function to calculate the sum of the digits of n in the given base ; sum of digits ; digit of n in the given base ; add the digit ; function to calculate the sum of digits of n in bases from 2 to n / 2 ; to store digit sum in all base ; function call for multiple bases ; 

from math import floor


def solve(n, base):
    sum = 0
    while (n > 0):
        remainder = n % base
        sum = sum + remainder
        n = int(n / base)
    return sum


def SumsOfDigits(n):
    sum = 0
    N = floor(n / 2)
    for base in range(2, N + 1, 1):
        sum = sum + solve(n, base)
    print(sum)


if __name__ == '__main__':
    n = 8
    SumsOfDigits(n)

# Expected output:
7
# largest number in an array that is not a perfect cube | python 3 program to find the largest non - perfect cube number among n numbers ; function to check if a number is perfect cube number or not ; takes the sqrt of the number ; checks if it is a perfect cube number ; function to find the largest non perfect cube number in the array ; stores the maximum of all perfect cube numbers ; traverse all elements in the array ; store the maximum if current element is a non perfect cube ; 

import math


def checkPerfectcube(n):
    cube_root = n ** (1. / 3.)
    if round(cube_root)**3 == n:
        return True
    else:
        return False


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


if __name__ == '__main__':
    a = [16, 64, 25, 2, 3, 10]
    n = len(a)
    print(largestNonPerfectcubeNumber(a, n))

# Expected output:
25
# check if n can be represented as sum of integers chosen from set { a , b } | function to find if number n can be represented as sum of a ' s ▁ and ▁ b ' s ; base condition ; if x is already visited ; set x as possible ; recursive call ; 
def checkIfPossibleRec(x, a, b, isPossible, n):
    if x > n:
        return
    if isPossible[x]:
        return
    isPossible[x] = True
    checkIfPossibleRec(x + a, a, b, isPossible, n)
    checkIfPossibleRec(x + b, a, b, isPossible, n)


def checkPossible(n, a, b):
    isPossible = [False] * (n + 1)
    checkIfPossibleRec(0, a, b, isPossible, n)
    return isPossible[n]


if __name__ == "__main__":
    a, b, n = 3, 7, 8
    if checkPossible(a, b, n):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# sum of all odd natural numbers in range l and r | function to return the sum of all odd natural numbers ; function to return the sum of all odd numbers in range l and r ; 
def sumOdd(n):
    terms = (n + 1) // 2
    sum1 = terms * terms
    return sum1


def suminRange(l, r):
    return sumOdd(r) - sumOdd(l - 1)


l = 2
r = 5
print("Sum of odd natural number", "from L to R is", suminRange(l, r))

# Expected output:
Sum of odd natural number from L to R is 8
# sum of common divisors of two numbers a and b | python 3 implementation of above approach ; function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; find the sum of divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; 

from math import gcd, sqrt


def sumcommDiv(a, b):
    n = gcd(a, b)
    sum = 0
    N = int(sqrt(n)) + 1
    for i in range(1, N, 1):
        if (n % i == 0):
            if (n / i == i):
                sum += i
            else:
                sum += (n / i) + i
    return sum


if __name__ == '__main__':
    a = 10
    b = 15
    print("Sum =", int(sumcommDiv(a, b)))

# Expected output:
Sum = 6
# check if a number is formed by concatenation of 1 , 14 or 144 only | function to check if a number is formed by concatenation of 1 , 14 or 144 only ; check for each possible digit if given number consist other then 1 , 14 , 144 print no else print yes ; 
def checkNumber(N):
    temp = N
    while (temp > 0):
        if (temp % 1000 == 144):
            temp /= 1000
        elif (temp % 100 == 14):
            temp /= 100
        elif (temp % 10 == 1):
            temp /= 10
        else:
            return "YES"
    return "NO"


N = 1414
print(checkNumber(N))

# Expected output:
YES
# fibonacci problem ( value of fib ( n ) * fib ( n ) | python 3 implementation of the approach ; 
def getResult(n):
    if n & 1:
        return 1
    return -1


n = 3
print(getResult(n))

# Expected output:
1
# find two numbers with sum and product both same as n | python 3 program to find a and b such that a * b = n and a + b = n ; function to return the smallest string ; not possible ; find a and b ; 

from math import sqrt


def findAandB(N):
    val = N * N - 4.0 * N
    if (val < 0):
        print("NO")
        return
    a = (N + sqrt(val)) / 2.0
    b = (N - sqrt(val)) / 2.0
    print("a =", '{0:.6}'.format(a))
    print("b =", '{0:.6}'.format(b))


if __name__ == '__main__':
    N = 69.0
    findAandB(N)

# Expected output:
a = 67.9851
b = 1.01493
# find minimum operations needed to make an array beautiful | function to find minimum operations required to make array beautiful ; counting consecutive zeros . ; check that start and end are same ; check is zero and one are equal ; 
def minOperations(A, n):
    if n & 1:
        return -1
    zeros, consZeros, ones = 0, 0, 0
    for i in range(n):
        if A[i]:
            zeros += 1
        else:
            ones += 1
        if (i + 1 < n):
            if A[i] == 0 and A[i + 1] == 0:
                consZeros += 1
    if A[0] == A[n - 1] and A[0] == 0:
        consZeros += 1
    if zeros == ones:
        return consZeros
    else:
        return -1


if __name__ == "__main__":
    A = [1, 1, 0, 0]
    n = len(A)
    print(minOperations(A, n))

# Expected output:
1
# steps to reduce n to zero by subtracting its most significant digit at every step | function to count the number of digits in a number m ; function to count the number of steps to reach 0 ; count the total number of stesp ; iterate till we reach 0 ; count the digits in last ; decrease it by 1 ; find the number on whose division , we get the first digit ; first digit in last ; find the first number less than last where the first digit changes ; find the number of numbers with same first digit that are jumped ; count the steps ; the next number with a different first digit ; 
def countdig(m):
    if (m == 0):
        return 0
    else:
        return 1 + countdig(m // 10)


def countSteps(x):
    c = 0
    last = x
    while (last):
        digits = countdig(last)
        digits -= 1
        divisor = pow(10, digits)
        first = last // divisor
        lastnumber = first * divisor
        skipped = (last - lastnumber) // first
        skipped += 1
        c += skipped
        last = last - (first * skipped)
    return c


n = 14
print(countSteps(n))

# Expected output:
6
# gcd of a number raised to some power and another number | calculates modular exponentiation , i . e . , ( x ^ y ) % p in o ( log y ) ; 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 gcd of a ^ n and b ; 
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 gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def powerGCD(a, b, n):
    e = power(a, n, b)
    return gcd(e, b)


if __name__ == "__main__":
    a = 5
    b = 4
    n = 2
    print(powerGCD(a, b, n))

# Expected output:
1
# largest number not greater than n all the digits of which are odd | function to check if all digits of a number are odd ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the largest number with all digits odd ; iterate till we find a number with all digits odd ; 
def allOddDigits(n):
    while (n):
        if ((n % 10) % 2 == 0):
            return False
        n = int(n / 10)
    return True


def largestNumber(n):
    if (n % 2 == 0):
        n -= 1
    i = n
    while (1):
        if (allOddDigits(i)):
            return i
        i -= 2


if __name__ == '__main__':
    N = 23
    print(largestNumber(N))

# Expected output:
19
# largest number not greater than n all the digits of which are odd | function to return the largest number with all digits odd ; convert the number to a string for easy operations ; find first even digit ; if no even digit , then n is the answer ; till first even digit , add all odd numbers ; decrease 1 from the even digit ; add 9 in the rest of the digits ; 
def largestNumber(n):
    s = ""
    duplicate = n
    while (n):
        s = chr(n % 10 + 48) + s
        n //= 10
    index = -1
    for i in range(len(s)):
        if (((ord(s[i]) - ord('0')) % 2 & 1) == 0):
            index = i
            break
    if (index == -1):
        return duplicate
    num = 0
    for i in range(index):
        num = num * 10 + (ord(s[i]) - ord('0'))
    num = num * 10 + (ord(s[index]) - ord('0') - 1)
    for i in range(index + 1, len(s)):
        num = num * 10 + 9
    return num


N = 24578
print(largestNumber(N))

# Expected output:
19999
# count number less than n which are product of perfect squares | python 3 program to count number less than n which are product of any two perfect squares ; function to count number less than n which are product of any two perfect squares ; 

import math


def countNumbers(N):
    return int(math .sqrt(N)) - 1


if __name__ == "__main__":
    N = 36
    print(countNumbers(N))

# Expected output:
5
# count ordered pairs with product less than n | python3 implementation of above approach ; function to return count of ordered pairs whose product are less than n ; initialize count to 0 ; count total pairs ; multiply by 2 to get ordered_pairs ; subtract redundant pairs ( a , b ) where a == b . ; return answer ; 

from math import sqrt


def countOrderedPairs(N):
    count_pairs = 0
    p = int(sqrt(N - 1)) + 1
    q = int(sqrt(N)) + 2
    for i in range(1, p, 1):
        for j in range(i, q, 1):
            count_pairs += 1
    count_pairs *= 2
    count_pairs -= int(sqrt(N - 1))
    return count_pairs


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

# Expected output:
8
# absolute difference of all pairwise consecutive elements in an array | function to print pairwise absolute difference of consecutive elements ; absolute difference between consecutive numbers ; 
def pairwiseDifference(arr, n):
    for i in range(n - 1):
        diff = abs(arr[i] - arr[i + 1])
        print(diff, end=" ")


if __name__ == "__main__":
    arr = [4, 10, 15, 5, 6]
    n = len(arr)
    pairwiseDifference(arr, n)

# Expected output:
6 5 10 1
# find the sum of all multiples of 2 and 5 below n | function to find sum of ap series ; number of terms ; function to find the sum of all multiples of 2 and 5 below n ; since , we need the sum of multiples less than n ; 
def sumAP(n, d):
    n = int(n / d)
    return (n) * (1 + n) * (d / 2)


def sumMultiples(n):
    n -= 1
    return (int(sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10)))


n = 20
print(sumMultiples(n))

# Expected output:
110
# program to find count of numbers having odd number of divisors in given range | function to return the count of divisors of a number ; count the powers of the current prime i which divides a ; update the count of divisors ; reset the count ; if the remaining a is prime then a ^ 1 will be one of its prime factors ; function to count numbers having odd number of divisors in range [ a , b ] ; to store the count of elements having odd number of divisors ; iterate from a to b and find the count of their divisors ; to store the count of divisors of i ; if the divisor count of i is odd ; 
def divisor(a):
    div = 1
    count = 0
    for i in range(2, int(pow(a, 1 / 2)) + 1):
        while (a % i == 0):
            count += 1
            a = a / i
        div = div * (count + 1)
        count = 0
    if (a > 1):
        div = div * (2)
    return div


def OddDivCount(a, b):
    res = 0
    for i in range(a, b + 1):
        divCount = divisor(i)
        if (divCount % 2):
            res += 1
    return res


if __name__ == '__main__':
    a, b = 1, 10
    print(OddDivCount(a, b))

# Expected output:
3
# check if there is any pair in a given range with gcd is divisible by k | returns count of numbers in [ l r ] that are divisible by k . ; add 1 explicitly as l is divisible by k ; l is not divisible by k ; 
def Check_is_possible(l, r, k):
    div_count = (r // k) - (l // k)
    if l % k == 0:
        div_count += 1
    return div_count > 1


if __name__ == "__main__":
    l, r, k = 30, 70, 10
    if Check_is_possible(l, r, k):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# find sum of n | calculate sum of nth group ; 
def nth_group(n):
    return n * (2 * pow(n, 2) + 1)


N = 5
print(nth_group(N))

# Expected output:
255
# find if a molecule can be formed from 3 atoms using their valence numbers | function to check if it is possible ; 
def printPossible(a, b, c):
    if ((a + b + c) % 2 != 0 or a + b < c):
        print("NO")
    else:
        print("YES")


if __name__ == "__main__":
    a = 2
    b = 4
    c = 2
    printPossible(a, b, c)

# Expected output:
YES
# check if a number is a trojan number | python 3 program to check if a number is trojan number or not ; function to check if a number can be expressed as x ^ y ; try all numbers from 2 to sqrt ( n ) as base ; keep increasing y while power ' p ' is smaller than n . ; function to check if a number is strong ; count the number for each prime factor ; minimum number of prime divisors should be 2 ; function to check if a number is trojan number ; 

from math import sqrt, pow


def isPerfectPower(n):
    if n == 1:
        return True
    for x in range(2, int(sqrt(n)) + 1):
        y = 2
        p = pow(x, y)
        while p <= n and p > 0:
            if p == n:
                return True
            y += 1
            p = pow(x, y)
    return False


def isStrongNumber(n):
    count = {i: 0 for i in range(n)}
    while n % 2 == 0:
        n = n // 2
        count[2] += 1
    for i in range(3, int(sqrt(n)) + 1, 2):
        while n % i == 0:
            n = n // i
            count[i] += 1
    if n > 2:
        count[n] += 1
    flag = 0
    for key, value in count .items():
        if value == 1:
            flag = 1
            break
    if flag == 1:
        return False
    return True


def isTrojan(n):
    return isPerfectPower(n) == False and isStrongNumber(n)


if __name__ == '__main__':
    n = 108
    if (isTrojan(n)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# find the sum of first n terms of the series 2 ã — 3 + 4 ã — 4 + 6 ã — 5 + 8 ã — 6 + ... | calculate sum upto n term of series ; 
def Sum_upto_nth_Term(n):
    return n * (n + 1) * (2 * n + 7) // 3


N = 5
print(Sum_upto_nth_Term(N))

# Expected output:
170
# Absolute Difference between the Sum of Non | Python3 program to find the Absolute Difference between the Sum of Non - Prime numbers and Prime numbers of an Array ; Function to find the difference between the sum of non - primes and the sum of primes of an array . ; Find 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 . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the sum of primes in S1 and the sum of non primes in S2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Get the array ; Find the absolute difference

import sys


def CalculateDifference(arr, n):
    max_val = -1
    for i in range(0, n):
        if (arr[i] > max_val):
            max_val = arr[i]
    prime = [True for i in range(max_val + 1)]
    prime[0] = False
    prime[1] = False
    p = 2
    while (p * p <= max_val):
        if prime[p]:
            for i in range(p * 2, max_val + 1, p):
                prime[i] = False
        p += 1
    S1 = 0
    S2 = 0
    for i in range(0, n):
        if prime[arr[i]]:
            S1 += arr[i]
        elif arr[i] != 1:
            S2 += arr[i]
    return abs(S2 - S1)


arr = [1, 3, 5, 10, 15, 7]
n = len(arr)
print(CalculateDifference(arr, n))

# Expected output:
10
# program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | function to compute the series sum ; to store the value of s [ i - 1 ] ; iterate over n to store sum in total ; update previous with s [ i ] ; 
def sum(x, n):
    total = 1.0
    previous = 1.0
    for i in range(1, n + 1):
        previous = (previous * x) / (i + 1)
        total = total + previous
    return total


if __name__ == '__main__':
    x = 5
    n = 4
    print("Sum is: ", sum(x, n))

# Expected output:
Sum is:  18.083333333333336