Demo entry 6734445

5634563465

   

Submitted by anonymous on Apr 20, 2018 at 13:25
Language: Python 3. Code size: 1.9 kB.

from math import log


calValue = lambda n, m, l: (2 ** n) * (3 ** m) * (5 ** l)
countOneFactor = lambda n, base: int( log( n, base ) )


def count2Factors(n,a,b):
    '''
    Count (a**?)*(b**?)-type from 1 to n,  exponet > 0,
    '''
    theSum=0
    remainFactors = [a,b]

    maxFactor = max(a,b)
    remainFactors.remove(maxFactor)
    # maxFactor for less iteration
    minExp = countOneFactor( maxFactor )
    for i in range(1,minExp ):
        n = n/ maxFactor
        theSum += countOneFactor( n ,*remainFactors )
    return theSum


def count3Factors(n,a,b,c):
    theSum=0
    remainFactors = [a, b,c]

    maxFactor = max(a, b,c)
    remainFactors.remove(maxFactors)
    minExp = countOneFactor( maxFactor )
    for i in range(1,minExp):
        n = n/maxFactor
        theSum += count2Factors(n,*remainInput )
    return theSum


def countFactors(n,*factors):
    theSum=0
    remainFactors = factors
    # max support args sequence and list of arges
    maxFactor = max( factors )
    remainFactors.remove(maxFactor)
    minExp = countOneFactor( maxFactor )
    for i in range(1,minExp):
        n = n/maxFactor
        theSum += countFactors(n,*remainFactors )
    return theSum


def count__(n):
    return sum(
        countOneFactor( n,2 ),
        countOneFactor( n,3 ),
        countOneFactor( n,5 ),

        count2Factors ( n,2,3),
        count2Factors ( n,3,5),
        count2Factors ( n,2,5),

        count3Factors ( n,2,3,5),
    )


def main(k):
    # binary search
    guessHigh = 2 ** k
    guessLow =  k
    middle = lambda x,y: int((x+y)/2)
    middleValue = middle(guessHigh, guessLow)

    while guessHigh > guessLow:
        tmp = count__(  middleValue )
        if tmp > k:
            guessHigh = middleValue
        elif tmp <k:
            guessLow = middleValue
        else:
            return middleValue

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).