def generatePolydivisibles( _base ): base = int( _base ) result = list( range( 1, base ) ) newItems = list( range( 1, base ) ) for i in newItems: yield i while newItems: newCandidates = [ ] while newItems: item = newItems.pop( 0 ) digits = splitNumber( item, base ) place = len( digits ) + 1 if place % base == 0: newDigits = [ 0 ] elif base > 2 and isDivisible( base, 2 ): if place % ( base / 2 ) == 0: newDigits = [ 0, base / 2 ] elif place % 2 == 0: newDigits = list( range( 0, base, 2 ) ) else: newDigits = list( range( 0, base ) ) else: newDigits = list( range( 0, base ) ) newCandidateBase = fmul( item, base ) for digit in newDigits: testMe = fadd( newCandidateBase, digit ) if isDivisible( testMe, place ): newCandidates.append( testMe ) yield testMe newItems = newCandidates newCandidates = [ ]
def isPolydivisible( n ): if real_int( n ) < 0: raise ValueError( 'non-negative, real integer expected' ) strValue = getMPFIntegerAsString( n ) # a couple of cheats if ( len( strValue ) > 4 ) and ( strValue[ 4 ] not in [ '5', '0' ] ): return 0 if ( len( strValue ) > 9 ) and ( strValue[ 9 ] != '0' ): return 0 for i in range( len( strValue ), 1, -1 ): current = mpmathify( strValue[ : i ] ) if not isDivisible( current, i ): return 0 return 1
def isHarshadNumber( n, k ): digits = getBaseNDigits( real_int( n ), k ) return 1 if isDivisible( n, fsum( digits ) ) else 0