giulio98/xlcost-single-prompt huggingface dataset:
# primitive root of a prime number n modulo n | python3 program to find primitive root of a given number n ; returns true if n is prime ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; iterative function to calculate ( x ^ n ) % p in o ( logy ) ; 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 ; utility function to store prime factors of a number ; print the number of 2 s that divide n ; n must be odd at this po . so we can skip one element ( note i = i + 2 ) ; while i divides n , print i and divide n ; this condition is to handle the case when n is a prime number greater than 2 ; function to find smallest primitive root of n ; check if n is prime or not ; find value of euler totient function of n . since n is a prime number , the value of euler totient function is n - 1 as there are n - 1 relatively prime numbers . ; find prime factors of phi and store in a set ; check for every number from 2 to phi ; iterate through all prime factors of phi . and check if we found a power with value 1 ; check if r ^ ( ( phi ) / primefactors ) mod n is 1 or not ; if there was no power with value 1. ; if no primitive root found ; 

from math import sqrt


def isPrime(n):

    # Corner cases
    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 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 findPrimefactors(s, n):
    while (n % 2 == 0):
        s.add(2)
        n = n // 2
    for i in range(3, int(sqrt(n)), 2):
        while (n % i == 0):

            s.add(i)
            n = n // i
    if (n > 2):
        s.add(n)


def findPrimitive(n):
    s = set()
    if (isPrime(n) == False):
        return -1
    phi = n - 1
    findPrimefactors(s, phi)
    for r in range(2, phi + 1):
        flag = False
        for it in s:
            if (power(r, phi // it, n) == 1):

                flag = True
                break
        if (flag == False):
            return r
    return -1


n = 761
print("Smallest primitive root of", n, "is", findPrimitive(n))

# Expected output:
Smallest primitive root of 761 is 6
# minimum number of power terms with sum equal to n | return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; consider n = a * x + b where a = n / x and b = n % x . ; update count of powers for 1 's added ; repeat the process for reduced n ; 
def minPower(n, x):
    if (x == 1):
        return n
    ans = 0
    while (n > 0):
        ans += (n % x)
        n //= x
    return ans


n = 5
x = 3
print(minPower(n, x))

# Expected output:
3
# sum of perrin numbers | function for sum of first n perrin number . ; if ( n == 0 ) : n = 0 ; if ( n == 1 ) : n = 1 ; if ( n == 2 ) : n = 2 ; calculate k = 5 sum of three previous step . ; sum remaining numbers ; calculate next term ; 
def calSum(n):
    a = 3
    b = 0
    c = 2
    return 3
    return 3
    return 5
    sum = 5
    while (n > 2):
        d = a + b
        sum = sum + d
        a = b
        b = c
        c = d
        n = n - 1
    return sum


n = 9
print(calSum(n))

# Expected output:
3
# print the kth common factor of two numbers | returns k 'th common factor of x and y. ; find smaller of two numbers ; count common factors until we either reach small or count becomes k . ; if we reached small ; 
def findKHCF(x, y, k):
    small = min(x, y)
    count = 1
    for i in range(2, small + 1):
        if (x % i == 0 and y % i == 0):
            count = count + 1
        if (count == k):
            return i
    return -1


x = 4
y = 24
k = 3
print(findKHCF(x, y, k))

# Expected output:
4
# find minimum number to be divided to make a number a perfect square | python program to find minimum number which divide n to make it a perfect square . ; return the minimum number to be divided to make n a perfect square . ; since 2 is only even prime , compute its power separately . ; if count is odd , it must be removed by dividing n by prime number . ; if count is odd , it must be removed by dividing n by prime number . ; 

import math


def findMinNumber(n):
    count = 0
    ans = 1
    while n % 2 == 0:
        count += 1
        n //= 2
    if count % 2 is not 0:
        ans *= 2
    for i in range(3, (int)(math .sqrt(n)) + 1, 2):
        count = 0
        while n % i == 0:
            count += 1
            n //= i
        if count % 2 is not 0:
            ans *= i
    if n > 2:
        ans *= n
    return ans


n = 72
print(findMinNumber(n))

# Expected output:
2
# program to implement collatz conjecture | function to find if n reaches to 1 or not . ; if there is a cycle formed , we can 't reach 1. ; if n is odd then pass n = 3 n + 1 else n = n / 2 ; wrapper over istoonerec ( ) ; to store numbers visited using recursive calls . ; 
def isToOneRec(n: int, s: set) -> bool:
    if n == 1:
        return True
    if n in s:
        return False
    if n % 2:
        return isToOneRec(3 * n + 1, s)
    else:
        return isToOneRec(n // 2, s)


def isToOne(n: int) -> bool:
    s = set()
    return isToOneRec(n, s)


if __name__ == "__main__":
    n = 5
    if isToOne(n):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# program to implement collatz conjecture | function to find if n reaches to 1 or not . ; return true if n is positive ; 
def isToOne(n):
    return (n > 0)


n = 5
if isToOne(n):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# gcd of two numbers formed by n repeating x and y times | return the greatest common divisor of two numbers . ; prints greatest common divisor of number formed by n repeating x times and y times . ; finding gcd of x and y . ; print n , g times . ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def findgcd(n, x, y):
    g = gcd(x, y)
    for i in range(g):
        print(n)


n = 123
x = 5
y = 2
findgcd(n, x, y)

# Expected output:
123
# count natural numbers whose factorials are divisible by x but not y | gcd function to compute the greatest divisor among a and b ; returns first number whose factorial is divisible by x . ; result ; remove common factors ; we found first i . ; count of natural numbers whose factorials are divisible by x but not y . ; return difference between first natural number whose factorial is divisible by y and first natural number whose factorial is divisible by x . ; 
def gcd(a, b):
    if ((a % b) == 0):
        return b
    return gcd(b, a % b)


def firstFactorialDivisibleNumber(x):
    i = 1
    new_x = x
    for i in range(1, x):
        new_x /= gcd(i, new_x)
        if (new_x == 1):
            break
    return i


def countFactorialXNotY(x, y):
    return (firstFactorialDivisibleNumber(
        y) - firstFactorialDivisibleNumber(x))


x = 15
y = 25
print(countFactorialXNotY(x, y))

# Expected output:
5
# find the first natural number whose factorial is divisible by x | returns first number whose factorial divides x . ; i = 1 ; result ; 
def firstFactorialDivisibleNumber(x):
    fact = 1
    for i in range(1, x):
        fact = fact * i
        if (fact % x == 0):
            break
    return i


x = 16
print(firstFactorialDivisibleNumber(x))

# Expected output:
6
# find two prime numbers with given sum | 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 ; update all multiples of p ; prints a prime pair with given sum ; generating primes using sieve ; traversing all numbers to find first pair ; 
def SieveOfEratosthenes(n, isPrime):
    isPrime[0] = isPrime[1] = False
    for i in range(2, n + 1):
        isPrime[i] = True
    p = 2
    while (p * p <= n):
        if (isPrime[p]):
            i = p * p
            while (i <= n):
                isPrime[i] = False
                i += p
        p += 1


def findPrimePair(n):
    isPrime = [0] * (n + 1)
    SieveOfEratosthenes(n, isPrime)
    for i in range(0, n):
        if (isPrime[i] and isPrime[n - i]):
            print(i, (n - i))
            return


n = 74
findPrimePair(n)

# Expected output:
3 71
# count numbers with same first and last digits | utility method to get first digit of x ; method to return count of numbers with same starting and ending digit from 1 upto x ; get ten - spans from 1 to x ; add 9 to consider all 1 digit numbers ; find first and last digits ; if last digit is greater than first digit then decrease count by 1 ; method to return count of numbers with same starting and ending digit between start and end ; 
def getFirstDigit(x):
    while (x >= 10):
        x //= 10
    return x


def getCountWithSameStartAndEndFrom1(x):
    if (x < 10):
        return x
    tens = x // 10
    res = tens + 9
    firstDigit = getFirstDigit(x)
    lastDigit = x % 10
    if (lastDigit < firstDigit):
        res = res - 1
    return res


def getCountWithSameStartAndEnd(start, end):
    return (getCountWithSameStartAndEndFrom1(end) -
            getCountWithSameStartAndEndFrom1(start - 1))


start = 5
end = 40
print(getCountWithSameStartAndEnd(start, end))

# Expected output:
8
# right | 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 ; update all multiples of p ; returns true if n is right - truncatable , else false ; generating primes using sieve ; checking whether the number remains prime when the last ( " right " ) digit is successively removed ; 
def sieveOfEratosthenes(n, isPrime):
    isPrime[0] = isPrime[1] = False
    for i in range(2, n + 1):
        isPrime[i] = True
    p = 2
    while (p * p <= n):
        if (isPrime[p]):
            i = p * 2
            while (i <= n):
                isPrime[i] = False
                i = i + p
        p = p + 1


def rightTruPrime(n):
    isPrime = [None] * (n + 1)
    sieveOfEratosthenes(n, isPrime)
    while (n != 0):
        if (isPrime[n]):
            n = n // 10
        else:
            return False
    return True


n = 59399
if (rightTruPrime(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# mersenne prime | generate all prime numbers less than n . ; initialize all entries of boolean array as true . a value in prime [ i ] will finally be false if i is not a prime , else true bool prime [ n + 1 ] ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; function to generate mersenne primes less than or equal to n ; create a boolean array " prime [ 0 . . n ] " ; generating primes using sieve ; generate all numbers of the form 2 ^ k - 1 and smaller than or equal to n . ; checking whether number is prime and is one less then the power of 2 ; 
def SieveOfEratosthenes(n, prime):
    for i in range(0, n + 1):
        prime[i] = True
    p = 2
    while (p * p <= n):
        if (prime[p]):
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1


def mersennePrimes(n):
    prime = [0] * (n + 1)
    SieveOfEratosthenes(n, prime)
    k = 2
    while (((1 << k) - 1) <= n):
        num = (1 << k) - 1
        if (prime[num]):
            print(num, end=" ")
        k += 1


n = 31
print("Mersenne prime numbers smaller", "than or equal to ", n)
mersennePrimes(n)

# Expected output:
Mersenne prime numbers smaller than or equal to  31
3 7 31
# find sum of modulo k of first n natural number | return sum of modulo k of first n natural numbers . ; iterate from 1 to n && evaluating and adding i % k . ; 
def findSum(N, K):
    ans = 0
    for i in range(1, N + 1):
        ans += (i % K)
    return ans


N = 10
K = 2
print(findSum(N, K))

# Expected output:
5
# find sum of modulo k of first n natural number | return sum of modulo k of first n natural numbers . ; counting the number of times 1 , 2 , . . , k - 1 , 0 sequence occurs . ; finding the number of elements left which are incomplete of sequence leads to case 1 type . ; adding multiplication of number of times 1 , 2 , . . , k - 1 , 0 sequence occurs and sum of first k natural number and sequence from case 1. ; 
def findSum(N, K):
    ans = 0
    y = N / K
    x = N % K
    ans = ((K * (K - 1) / 2) * y + (x * (x + 1)) / 2)
    return int(ans)


N = 10
K = 2
print(findSum(N, K))

# Expected output:
5
# smallest number to multiply to convert floating point to natural | python program to find the smallest number to multiply to convert a floating point number into natural number . finding gcd of two number ; returns smallest integer k such that k * str becomes natural . str is an input floating point number ; find size of string representing a floating point number . ; below is used to find denominator in fraction form . ; used to find value of count_after_dot ; to find numerator in fraction form of given number . for example , for 30.25 , numerator would be 3025. ; if there was no dot , then number is already a natural . ; find denominator in fraction form . for example , for 30.25 , denominator is 100 ; result is denominator divided by gcd - of - numerator - and - denominator . for example , for 30.25 , result is 100 / gcd ( 3025 , 100 ) = 100 / 25 = 4 ; 

import math


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


def findnum(str):
    n = len(str)
    count_after_dot = 0
    dot_seen = 0
    num = 0
    for i in range(n):
        if (str[i] != '.'):
            num = num * 10 + int(str[i])
            if (dot_seen == 1):
                count_after_dot += 1
        else:
            dot_seen = 1
    if (dot_seen == 0):
        return 1
    dem = int(math .pow(10, count_after_dot))
    return (dem / gcd(num, dem))


str = "5.125"
print(findnum(str))

# Expected output:
8.0
# find the maximum number of handshakes | calculating the maximum number of handshake using derived formula . ; 
def maxHandshake(n):
    return int((n * (n - 1)) / 2)


n = 10
print(maxHandshake(n))

# Expected output:
45
# count digits in given number n which divide n | utility function to check divisibility by digit ; ( n [ i ] - '0' ) gives the digit value and form the number ; function to count digits which appears in n and divide n divide [ 10 ] -- > array which tells that particular digit divides n or not count [ 10 ] -- > counts frequency of digits which divide n ; we initialize all digits of n as not divisible by n . ; start checking divisibility of n by digits 2 to 9 ; if digit divides n then mark it as true ; now traverse the number string to find and increment result whenever a digit divides n . ; 
def divisible(N, digit):
    ans = 0
    for i in range(len(N)):
        ans = (ans * 10 + (ord(N[i]) - ord('0')))
        ans %= digit
    return (ans == 0)


def allDigits(N):
    divide = [False] * 10
    for digit in range(2, 10):
        if (divisible(N, digit)):
            divide[digit] = True
    result = 0
    for i in range(len(N)):
        if (divide[(ord(N[i]) - ord('0'))]):
            result += 1
    return result


N = "122324"
print(allDigits(N))

# Expected output:
4
# aliquot sequence | python implementation of optimized approach to generate aliquot sequence ; function to calculate sum of all proper divisors ; note that this loop runs till square root of n ; if divisors are equal , take only one of them ; otherwise take both ; calculate sum of all proper divisors only ; function to print aliquot sequence for an input n . ; print the first term ; calculate next term from previous term ; print next term ; 

from math import sqrt


def getSum(n):
    summ = 0
    for i in range(1, int(sqrt(n)) + 1):
        if n % i == 0:
            if n // i == i:
                summ += i
            else:
                summ += i
                summ += n // i
    return summ - n


def printAliquot(n):
    print(n, end=" ")
    s = set()
    s .add(n)
    nextt = 0
    while n > 0:
        n = getSum(n)
        if n in s:
            print("Repeats with", n)
            break
        print(n, end=" ")
        s .add(n)


if __name__ == "__main__":
    printAliquot(12)

# Expected output:
12 16 15 9 4 3 1 0
# count numbers which can be constructed using two numbers | returns count of numbers from 1 to n that can be formed using x and y . ; create an auxiliary array and initialize it as false . an entry arr [ i ] = true is going to mean that i can be formed using x and y ; x and y can be formed using x and y . ; initialize result ; traverse all numbers and increment result if a number can be formed using x and y . ; if i can be formed using x and y ; then i + x and i + y can also be formed using x and y . ; increment result ; 
def countNums(n, x, y):
    arr = [False for i in range(n + 2)]
    if (x <= n):
        arr[x] = True
    if (y <= n):
        arr[y] = True
    result = 0
    for i in range(min(x, y), n + 1):
        if (arr[i]):
            if (i + x <= n):
                arr[i + x] = True
            if (i + y <= n):
                arr[i + y] = True
            result = result + 1
    return result


n = 15
x = 5
y = 7
print(countNums(n, x, y))

# Expected output:
6
# emirp numbers | function to find reverse of any number ; sieve method used for generating emirp number ( use of sieve oferatosthenes ) ; create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . a value in prime [ i ] will finally be false if i is not a prime , else true . ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; traverse all prime numbers ; find reverse a number ; a number is emrip if it is not a palindrome number and its reverse is also prime . ; mark reverse prime as false so that it 's  not printed again ; 
def reverse(x):
    rev = 0
    while (x > 0):
        rev = (rev * 10) + x % 10
        x = int(x / 10)
    return rev


def printEmirp(n):
    prime = [1] * (n + 1)
    p = 2
    while (p * p <= n):
        if (prime[p] == 1):
            for i in range(p * 2, n + 1, p):
                prime[i] = 0
        p += 1
    for p in range(2, n + 1):
        if (prime[p] == 1):
            rev = reverse(p)
            if (p != rev and rev <= n and prime[rev] == 1):
                print(p, rev, end=" ")
                prime[rev] = 0


n = 100
printEmirp(n)

# Expected output:
13 31 17 71 37 73 79 97
# abundant number | an optimized solution to check abundant number in python ; function to calculate sum of divisors ; note that this loop runs till square root of n ; if divisors are equal , take only one of them ; else : otherwise take both ; calculate sum of all proper divisors only ; function to check abundant number ; return true if sum of divisors is greater than n . ; 

import math


def getSum(n):
    sum = 0
    i = 1
    while i <= (math .sqrt(n)):
        if n % i == 0:
            if n / i == i:
                sum = sum + i
                sum = sum + i
                sum = sum + (n / i)
        i = i + 1
    sum = sum - n
    return sum


def checkAbundant(n):
    if (getSum(n) > n):
        return 1
    else:
        return 0


if (checkAbundant(12) == 1):
    print("YES")
else:
    print("NO")
if (checkAbundant(15) == 1):
    print("YES")
else:
    print("NO")

# Expected output:
NO
NO
# powerful number | python program to find if a number is powerful or not . ; function to check if the number is powerful ; first divide the number repeatedly by 2 ; if only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; find highest power of " factor " that divides n ; if only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime numenr . since prime numbers are not powerful , we return false if n is not 1. ; 

import math


def isPowerful(n):
    while (n % 2 == 0):
        power = 0
        while (n % 2 == 0):
            n = n // 2
            power = power + 1
        if (power == 1):
            return False
    for factor in range(3, int(math .sqrt(n)) + 1, 2):
        power = 0
        while (n % factor == 0):
            n = n // factor
            power = power + 1
        if (power == 1):
            return false
    return (n == 1)


print("YES"if isPowerful(20)else "NO")
print("YES"if isPowerful(27)else "NO")

# Expected output:
NO
YES
# deficient number | python program to implement an optimized solution to check deficient number ; function to calculate sum of divisors ; note that this loop runs till square root of n ; if divisors are equal , take only one of them ; else : otherwise take both ; function to check deficient number ; check if sum ( n ) < 2 * n ; 

import math


def divisorsSum(n):
    sum = 0
    i = 1
    while i <= math .sqrt(n):
        if (n % i == 0):
            if (n / i == i):
                sum = sum + i
                sum = sum + i
                sum = sum + (n / i)
        i = i + 1
    return sum


def isDeficient(n):
    return (divisorsSum(n) < (2 * n))


if (isDeficient(12)):
    print("YES")
else:
    print("NO")
if (isDeficient(15)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
YES
# harshad ( or niven ) number | python implementation of above approach ; converting integer to string ; initialising sum to 0 ; traversing through the string ; converting character to int ; comparing number and sum ; 
def checkHarshad(n):
    st = str(n)
    sum = 0
    length = len(st)
    for i in st:
        sum = sum + int(i)
    if (n % sum == 0):
        return "Yes"
    else:
        return "No"


number = 18
print(checkHarshad(number))

# Expected output:
Yes
# kaprekar number | python program to check if a number is kaprekar number or not ; returns true if n is a kaprekar number , else false ; count number of digits in square ; split the square at different poitns and see if sum of any pair of splitted numbers is equal to n . ; to avoid numbers like 10 , 100 , 1000 ( these are not karprekar numbers ; find sum of current parts and compare with n ; compare with original number ; 

import math


def iskaprekar(n):
    if n == 1:
        return True
    sq_n = n * n
    count_digits = 1
    while not sq_n == 0:
        count_digits = count_digits + 1
        sq_n = sq_n // 10

    sq_n = n*n
    r_digits = 0
    while r_digits < count_digits:
        r_digits = r_digits + 1
        eq_parts = (int)(math.pow(10, r_digits))
        if eq_parts == n:
            continue

        sum = sq_n//eq_parts + sq_n % eq_parts
        if sum == n:
            return True
    return False


i = 1
while i < 10000:
    if (iskaprekar(i)):
        print(i, " ",)
    i = i + 1

# Expected output:
1  
9  
45  
55  
99  
297  
703  
999  
2223  
2728  
4879  
4950  
5050  
5292  
7272  
7777  
9999
# keith number | returns true if x is keith , else false . ; store all digits of x in a vector " terms " also find number of digits and store in " n " . ; n = 0 ; n is number of digits in x ; to get digits in right order ( from msb to lsb ) ; keep finding next trms of a sequence generated using digits of x until we either reach x or a number greate than x ; next term is sum of previous n terms ; when the control comes out of the while loop , either the next_term is equal to the number or greater than it . if next_term is equal to x , then x is a keith number , else not ; 
def isKeith(x):
    terms = []
    temp = x
    n = 0
    while (temp > 0):
        terms .append(temp % 10)
        temp = int(temp / 10)
        n += 1
    terms .reverse()
    next_term = 0
    i = n
    while (next_term < x):
        next_term = 0
        for j in range(1, n + 1):
            next_term += terms[i - j]
        terms .append(next_term)
        i += 1
    return (next_term == x)


print("Yes")if (isKeith(14))else print("No")
print("Yes")if (isKeith(12))else print("No")
print("Yes")if (isKeith(197))else print("No")

# Expected output:
Yes
No
Yes