dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import math
MAX = 1000001
prime = [ True ] * MAX
def SieveOfEratosthenes ( ) :
    p = 2
    while p * p <= MAX :
        if prime [ p ] == True :
            for i in range ( p * p , MAX , p ) :
                prime [ i ] = False
        p += 1
def getMid ( s , e ) :
    return s + ( e - s ) // 2
def getSumUtil ( st , ss , se , qs , qe , si ) :
    if qs <= ss and qe >= se :
        return st [ si ]
    if se < qs or ss > qe :
        return 0
    mid = getMid ( ss , se )
    return ( getSumUtil ( st , ss , mid , qs , qe , 2 * si + 1 ) + getSumUtil ( st , mid + 1 , se , qs , qe , 2 * si + 2 ) )
def updateValueUtil ( st , ss , se , i , diff , si ) :
    if i < ss or i > se :
        return
    st [ si ] = st [ si ] + diff
    if se != ss :
        mid = getMid ( ss , se )
        updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 )
        updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 )
def updateValue ( arr , st , n , i , new_val ) :
    if i < 0 or i > n - 1 :
        print ( - 1 )
        return
    diff = new_val - arr [ i ]
    prev_val = arr [ i ]
    arr [ i ] = new_val
    if prime [ new_val ] or prime [ prev_val ] :
        if not prime [ prev_val ] :
            updateValueUtil ( st , 0 , n - 1 , i , new_val , 0 )
        elif not prime [ new_val ] :
            updateValueUtil ( st , 0 , n - 1 , i , - prev_val , 0 )
        else :
            updateValueUtil ( st , 0 , n - 1 , i , diff , 0 )
def getSum ( st , n , qs , qe ) :
    if qs < 0 or qe > n - 1 or qs > qe :
        return - 1
    return getSumUtil ( st , 0 , n - 1 , qs , qe , 0 )
def constructSTUtil ( arr , ss , se , st , si ) :
    if ss == se :
        if prime [ arr [ ss ] ] :
            st [ si ] = arr [ ss ]
        else :
            st [ si ] = 0
        return st [ si ]
    mid = getMid ( ss , se )
    st [ si ] = ( constructSTUtil ( arr , ss , mid , st , 2 * si + 1 ) + constructSTUtil ( arr , mid + 1 , se , st , 2 * si + 2 ) )
    return st [ si ]
def constructST ( arr , n ) :
    x = int ( math . ceil ( math . log2 ( n ) ) )
    max_size = 2 * int ( pow ( 2 , x ) ) - 1
    st = [ 0 ] * max_size
    constructSTUtil ( arr , 0 , n - 1 , st , 0 )
    return st
arr = [ 1 , 3 , 5 , 7 , 9 , 11 ]
n = len ( arr )
Q = [ [ 1 , 1 , 3 ] , [ 2 , 1 , 10 ] , [ 1 , 1 , 3 ] ]
SieveOfEratosthenes ( )
st = constructST ( arr , n )
print ( getSum ( st , n , 1 , 3 ) )
updateValue ( arr , st , n , 1 , 10 )
print ( getSum ( st , n , 1 , 3 ) )

Index/key variables: i, new_val, p, prev_val, si, ss
Iterator variables: i
Python program:
mod = 1000000007
dp = [ [ - 1 for i in range ( 1000 ) ] for j in range ( 1000 ) ] ;
def calculate ( pos , prev , s , index ) :
    if ( pos == len ( s ) ) :
        return 1
    if ( dp [ pos ] [ prev ] != - 1 ) :
        return dp [ pos ] [ prev ]
    c = ord ( s [ pos ] ) - ord ( ' a ' ) ;
    answer = 0
    for i in range ( len ( index ) ) :
        if ( index [ i ] > prev ) :
            answer = ( answer % mod + calculate ( pos + 1 , index [ i ] , s , index ) % mod ) % mod
    dp [ pos ] [ prev ] = 4
    return dp [ pos ] [ prev ]
def countWays ( a , s ) :
    n = len ( a )
    index = [ [ ] for i in range ( 26 ) ]
    for i in range ( n ) :
        for j in range ( len ( a [ i ] ) ) :
            index [ ord ( a [ i ] [ j ] ) - ord ( ' a ' ) ] . append ( j + 1 ) ;
    return calculate ( 0 , 0 , s , index [ 0 ] )
if __name__ == ' _ _ main _ _ ' :
    A = [ ]
    A . append ( " adc " )
    A . append ( " aec " )
    A . append ( " erg " )
    S = " ac "
    print ( countWays ( A , S ) )

Index/key variables: i, j, pos, prev
Iterator variables: i, j
Python program:
MAX = 10005
MOD = 1000000007
def countNum ( idx , sum , tight , num , len1 , k ) :
    if ( len1 == idx ) :
        if ( sum == 0 ) :
            return 1
        else :
            return 0
    if ( dp [ idx ] [ sum ] [ tight ] != - 1 ) :
        return dp [ idx ] [ sum ] [ tight ]
    res = 0
    if ( tight == 0 ) :
        limit = num [ idx ]
    else :
        limit = 9
    for i in range ( limit + 1 ) :
        new_tight = tight
        if ( tight == 0 and i < limit ) :
            new_tight = 1
        res += countNum ( idx + 1 , ( sum + i ) % k , new_tight , num , len1 , k )
        res %= MOD
    if ( res < 0 ) :
        res += MOD
    dp [ idx ] [ sum ] [ tight ] = res
    return dp [ idx ] [ sum ] [ tight ]
def process ( s ) :
    num = [ ]
    for i in range ( len ( s ) ) :
        num . append ( ord ( s [ i ] ) - ord ( '0' ) )
    return num
if __name__ == ' _ _ main _ _ ' :
    n = "98765432109876543210"
    len1 = len ( n )
    k = 58
    dp = [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 101 ) ] for k in range ( MAX ) ]
    num = process ( n )
    print ( countNum ( 0 , 0 , 0 , num , len1 , k ) )

Index/key variables: i, idx, sum, tight
Accumulator variables: num, res
Iterator variables: i
Python program:
def maxWeight ( arr , n , w1_r , w2_r , i ) :
    if i == n :
        return 0
    if dp [ i ] [ w1_r ] [ w2_r ] != - 1 :
        return dp [ i ] [ w1_r ] [ w2_r ]
    fill_w1 , fill_w2 , fill_none = 0 , 0 , 0
    if w1_r >= arr [ i ] :
        fill_w1 = arr [ i ] + maxWeight ( arr , n , w1_r - arr [ i ] , w2_r , i + 1 )
    if w2_r >= arr [ i ] :
        fill_w2 = arr [ i ] + maxWeight ( arr , n , w1_r , w2_r - arr [ i ] , i + 1 )
    fill_none = maxWeight ( arr , n , w1_r , w2_r , i + 1 )
    dp [ i ] [ w1_r ] [ w2_r ] = max ( fill_none , max ( fill_w1 , fill_w2 ) )
    return dp [ i ] [ w1_r ] [ w2_r ]
if __name__ == " _ _ main _ _ " :
    arr = [ 8 , 2 , 3 ]
    maxN , maxW = 31 , 31
    dp = [ [ [ - 1 ] * maxW ] * maxW ] * maxN
    n = len ( arr )
    w1 , w2 = 10 , 3
    print ( maxWeight ( arr , n , w1 , w2 , 0 ) )

Index/key variables: i, w1_r, w2_r
Python program:
MAX = 105
def sieve ( ) :
    i = 2
    while ( i * i < MAX ) :
        if ( prime [ i ] == 0 ) :
            for j in range ( i * i , MAX , i ) :
                prime [ j ] = 1 ;
        i += 1
def dfs ( i , j , k , q , n , m ) :
    if ( mappedMatrix [ i ] [ j ] == 0 or i > n or j > m or mark [ i ] [ j ] or q != 0 ) :
        return q ;
    mark [ i ] [ j ] = 1 ;
    ans [ k ] = [ i , j ]
    if ( i == n and j == m ) :
        q = k ;
        return q ;
    q = dfs ( i + 1 , j + 1 , k + 1 , q , n , m ) ;
    q = dfs ( i + 1 , j , k + 1 , q , n , m ) ;
    q = dfs ( i , j + 1 , k + 1 , q , n , m ) ;
    return q
def lexicographicalPath ( n , m ) :
    q = 0 ;
    global ans , mark
    ans = [ [ 0 , 0 ] for i in range ( MAX ) ]
    mark = [ [ 0 for j in range ( MAX ) ] for i in range ( MAX ) ]
    q = dfs ( 1 , 1 , 1 , q , n , m ) ;
    for i in range ( 1 , q + 1 ) :
        print ( str ( ans [ i ] [ 0 ] ) + ' ▁ ' + str ( ans [ i ] [ 1 ] ) )
def countPrimePath ( n , m ) :
    global dp
    dp = [ [ 0 for j in range ( MAX ) ] for i in range ( MAX ) ]
    dp [ 1 ] [ 1 ] = 1 ;
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , m + 1 ) :
            if ( i == 1 and j == 1 ) :
                continue ;
            dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j - 1 ] ) ;
            if ( mappedMatrix [ i ] [ j ] == 0 ) :
                dp [ i ] [ j ] = 0 ;
    print ( dp [ n ] [ m ] )
def preprocessMatrix ( a , n , m ) :
    global prime
    prime = [ 0 for i in range ( MAX ) ]
    sieve ( ) ;
    for i in range ( n ) :
        for j in range ( m ) :
            if ( prime [ a [ i ] [ j ] ] == 0 ) :
                mappedMatrix [ i + 1 ] [ j + 1 ] = 1 ;
            else :
                mappedMatrix [ i + 1 ] [ j + 1 ] = 0 ;
if __name__ == " _ _ main _ _ " :
    n = 3 ;
    m = 3 ;
    a = [ [ 2 , 3 , 7 ] , [ 5 , 4 , 2 ] , [ 3 , 7 , 11 ] ] ;
    mappedMatrix = [ [ 0 for j in range ( MAX ) ] for i in range ( MAX ) ]
    preprocessMatrix ( a , n , m ) ;
    countPrimePath ( n , m ) ;
    lexicographicalPath ( n , m ) ;

Index/key variables: i, j, k, m, n
Iterator variables: i, j
Python program:
def isSubsetSum ( arr , n , sum ) :
    subset = [ [ False for x in range ( n + 1 ) ] for y in range ( sum + 1 ) ]
    count = [ [ 0 for x in range ( n + 1 ) ] for y in range ( sum + 1 ) ]
    for i in range ( n + 1 ) :
        subset [ 0 ] [ i ] = True
        count [ 0 ] [ i ] = 0
    for i in range ( 1 , sum + 1 ) :
        subset [ i ] [ 0 ] = False
        count [ i ] [ 0 ] = - 1
    for i in range ( 1 , sum + 1 ) :
        for j in range ( 1 , n + 1 ) :
            subset [ i ] [ j ] = subset [ i ] [ j - 1 ]
            count [ i ] [ j ] = count [ i ] [ j - 1 ]
            if ( i >= arr [ j - 1 ] ) :
                subset [ i ] [ j ] = ( subset [ i ] [ j ] or subset [ i - arr [ j - 1 ] ] [ j - 1 ] )
                if ( subset [ i ] [ j ] ) :
                    count [ i ] [ j ] = ( max ( count [ i ] [ j - 1 ] , count [ i - arr [ j - 1 ] ] [ j - 1 ] + 1 ) )
    return count [ sum ] [ n ]
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 3 , 5 , 10 ]
    sum = 20
    n = 4
    print ( isSubsetSum ( arr , n , sum ) )

Index/key variables: i, j, n, sum
Iterator variables: i, j
Python program:
def isMajority ( arr , n , x ) :
    last_index = ( n // 2 + 1 ) if n % 2 == 0 else ( n // 2 )
    for i in range ( last_index ) :
        if arr [ i ] == x and arr [ i + n // 2 ] == x :
            return 1
arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 4 ]
n = len ( arr )
x = 4
if ( isMajority ( arr , n , x ) ) :
    print ( " % ▁ d ▁ appears ▁ more ▁ than ▁ % ▁ d ▁ times ▁ in ▁ arr [ ] " % ( x , n // 2 ) )
else :
    print ( " % ▁ d ▁ does ▁ not ▁ appear ▁ more ▁ than ▁ % ▁ d ▁ times ▁ in ▁ arr [ ] " % ( x , n // 2 ) )

Index/key variables: i
Iterator variables: i
Python program:
def _binarySearch ( arr , low , high , x ) :
    if high >= low :
        mid = ( low + high ) // 2
        if ( mid == 0 or x > arr [ mid - 1 ] ) and ( arr [ mid ] == x ) :
            return mid
        elif x > arr [ mid ] :
            return _binarySearch ( arr , ( mid + 1 ) , high , x )
        else :
            return _binarySearch ( arr , low , ( mid - 1 ) , x )
    return - 1
def isMajority ( arr , n , x ) :
    i = _binarySearch ( arr , 0 , n - 1 , x )
    if i == - 1 :
        return False
    if ( ( i + n // 2 ) <= ( n - 1 ) ) and arr [ i + n // 2 ] == x :
        return True
    else :
        return False

Index/key variables: mid
Python program:
INT_MIN = - 32767
def cutRod ( price , n ) :
    val = [ 0 for x in range ( n + 1 ) ]
    val [ 0 ] = 0
    for i in range ( 1 , n + 1 ) :
        max_val = INT_MIN
        for j in range ( i ) :
            max_val = max ( max_val , price [ j ] + val [ i - j - 1 ] )
        val [ i ] = max_val
    return val [ n ]
arr = [ 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ]
size = len ( arr )
print ( " Maximum ▁ Obtainable ▁ Value ▁ is ▁ " + str ( cutRod ( arr , size ) ) )

Index/key variables: i, j, n
Iterator variables: i, j
Python program:
def nCr ( n , r ) :
    res = 1
    if ( r > n - r ) :
        r = n - r
    for i in range ( r ) :
        res *= ( n - i )
        res //= ( i + 1 )
    return res
if __name__ == ' _ _ main _ _ ' :
    n = 3
    m = 2
    k = 2
    print ( nCr ( n + m , k ) )

Accumulator variables: res
Iterator variables: i
Python program:
import math
def Is_possible ( N ) :
    C = 0
    D = 0
    while ( N % 10 == 0 ) :
        N = N / 10
        C += 1
    if ( math . log ( N , 2 ) - int ( math . log ( N , 2 ) ) == 0 ) :
        D = int ( math . log ( N , 2 ) )
        if ( C >= D ) :
            print ( " YES " )
        else :
            print ( " NO " )
    else :
        print ( " NO " )
N = 2000000000000
Is_possible ( N )

Accumulator variables: C
Python program:
def printSeries ( n , a , b , c ) :
    if ( n == 1 ) :
        print ( a , end = " ▁ " ) ;
        return ;
    if ( n == 2 ) :
        print ( a , b , end = " ▁ " ) ;
        return ;
    print ( a , b , c , end = " ▁ " ) ;
    for i in range ( 4 , n + 1 ) :
        d = a + b + c ;
        print ( d , end = " ▁ " ) ;
        a = b ;
        b = c ;
        c = d ;
N = 7 ; a = 1 ; b = 3 ;
c = 4 ;
printSeries ( N , a , b , c ) ;

Iterator variables: i
Python program:
def diameter ( n ) :
    L , H , templen = 0 , 0 , 0 ;
    L = 1 ;
    H = 0 ;
    if ( n == 1 ) :
        return 1 ;
    if ( n == 2 ) :
        return 2 ;
    if ( n == 3 ) :
        return 3 ;
    while ( L * 2 <= n ) :
        L *= 2 ;
        H += 1 ;
    if ( n >= L * 2 - 1 ) :
        return 2 * H + 1 ;
    elif ( n >= L + ( L / 2 ) - 1 ) :
        return 2 * H ;
    return 2 * H - 1 ;
n = 15 ;
print ( diameter ( n ) ) ;

Accumulator variables: H, L
Python program:
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 ] == True ) :
            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 ) )

Index/key variables: i, p
Accumulator variables: ans, n
Iterator variables: i, p
Python program:
def primeCount ( arr , n ) :
    max_val = max ( arr )
    prime = [ True ] * ( max_val + 1 )
    prime [ 0 ] = prime [ 1 ] = False
    p = 2
    while p * p <= max_val :
        if prime [ p ] == True :
            for i in range ( p * 2 , max_val + 1 , p ) :
                prime [ i ] = False
        p += 1
    count = 0
    for i in range ( 0 , n ) :
        if prime [ arr [ i ] ] :
            count += 1
    return count
def getPrefixArray ( arr , n , pre ) :
    pre [ 0 ] = arr [ 0 ]
    for i in range ( 1 , n ) :
        pre [ i ] = pre [ i - 1 ] + arr [ i ]
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 4 , 8 , 4 ]
    n = len ( arr )
    pre = [ None ] * n
    getPrefixArray ( arr , n , pre )
    print ( primeCount ( pre , n ) )

Index/key variables: i, p
Accumulator variables: count
Iterator variables: i
Python program:
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 " )

Iterator variables: i
Python program:
n = 5
fac1 = 1
for i in range ( 2 , n ) :
    fac1 = fac1 * i
fac2 = fac1 * n
totalWays = fac1 * fac2
print ( totalWays )

Iterator variables: i
Python program:
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 ) )

Accumulator variables: Sum
Iterator variables: i
Python program:
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 ) )

Iterator variables: i
Python program:
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 == True ) :
        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 ) ;

Boolean variables: flag
Python program:
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 " )

Iterator variables: i
Python program:
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 ) ) ;

Index/key variables: i, j
Accumulator variables: ans, prime
Iterator variables: i, j
Python program:
import random
def power ( x , y , p ) :
    res = 1 ;
    x = x % p ;
    while ( y > 0 ) :
        if ( y & 1 ) :
            res = ( res * x ) % p ;
        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 = " ▁ " ) ;

Accumulator variables: d
Iterator variables: i, n
Python program:
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 )

Index/key variables: i, u, x, y
Iterator variables: i
Python program:
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 )

Index/key variables: i, j
Iterator variables: i, j
Python program:
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 ) )

Accumulator variables: count
Iterator variables: a, b
Python program:
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 )

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
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 ) )

Index/key variables: i, j
Accumulator variables: length
Iterator variables: i, j
Python program:
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 )

Index/key variables: i
Accumulator variables: s
Iterator variables: i
Python program:
def countEvenOdd ( arr , n , K ) :
    even = 0 ; odd = 0 ;
    for i in range ( n ) :
        x = bin ( arr [ i ] ) . count ( '1' ) ;
        if ( x % 2 == 0 ) :
            even += 1 ;
        else :
            odd += 1 ;
    y = bin ( K ) . count ( '1' ) ;
    if ( y & 1 ) :
        print ( " Even ▁ = " , odd , " , ▁ Odd ▁ = " , even ) ;
    else :
        print ( " Even ▁ = " , even , " , ▁ Odd ▁ = " , odd ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 4 , 2 , 15 , 9 , 8 , 8 ] ;
    K = 3 ;
    n = len ( arr ) ;
    countEvenOdd ( arr , n , K ) ;

Index/key variables: i
Accumulator variables: even, odd
Iterator variables: i