Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Best Number Ever by piway
from functools import reduce
import random
def permuteNumber(n) :
"""n = n0*10**0 + n1*10**1 + ... + nk*10**k
n = n0|n1|...|nk
permuteNumber(n) = [n0|n1|...|nk, n1|n2|...|nk|n0, ..., nk|n0|n1|...|n(k-1)]
"""
return reduce(lambda x,y : x + [int(str(n)[y:]+str(n)[:y])],\
range(len(str(n))),[])
def splitNumberIn2(n,p) :
"""Let n and p be integers in base 10.
splitNumberIn2(n,p) = (u,v) such as n = u*(10**p) + v
"""
return (n//(10**p),(n - (n//(10**p))*(10**p)))
def splitNumber(n,p) :
"""n = n0|n1|...|nk
splitNumber(n,p) = [n0|n1|...|n(p-1), n(p)|n(p+1)|...|n(2*p-1), ..., n(l*p)|n(l*p+1)|...|nk]
"""
firstTerm = len(str(n))%p
if firstTerm :
return reduce(lambda x,y : x + [str(n)[firstTerm+y*p:firstTerm+(y+1)*p]],\
range(len(str(n))//p), [str(n)[:firstTerm]])
else :
return reduce(lambda x,y : x + [str(n)[y*p:(y+1)*p]],\
range(len(str(n))//p), [])
def sumDigit(l,prnt=False):
k = len(str(l[-1]))
result = 0
for n in l :
result+=int(n)
if prnt :
sumString = reduce(lambda x,y : x+'+'+y ,l,'')[1:]
print('\t'+sumString+"="+str(result))
if len(str(result)) > k :
return sumDigit(splitNumber(result,k),prnt)
return result
def asSameDigit(n,m) :
return sorted(str(n)) == sorted(str(m))
def isPhoenix(n):
"""n is a Phoenix number if and only if
its first multiples are circular permutations of itself.
"""
circularPermutationList = permuteNumber(n)
return reduce(lambda x,y : x and (n*y in circularPermutationList),range(1,len(str(n))+1),True)
def isKaprekar(n) :
"""n is a Kaprekar number in base 10 if and only if
there is u,v,y three integers such as :
-> y != 0
-> n = u + v
-> n**2 = u**y + v
"""
n2 = n**2
return reduce(lambda x,y : x or (splitNumberIn2(n2,y)[1] != 0 and \
n == splitNumberIn2(n2,y)[0] + splitNumberIn2(n2,y)[1]),\
range(1,len(str(n2))+1),False)
def isHarshad(n) :
"""n is a Harshad number if and only if
it is divisible by the sum of its digit"""
return not n % sumDigit(splitNumber(n,1))
def propertyWith9(n):
return (n*(len(str(n))+1) == 10**(len(str(n)))-1) and \
(reduce(lambda x,y : x and (sumDigit(splitNumber(n,y)) == 10**y-1), range(1,4), True)) and \
(asSameDigit(int(reduce(lambda x,y : x + str((2**(y+1)) % 9), range(len(str(n))), '')),n))
def isExtendedKaprekar(n,prnt=False) :
result = True
for i in range(10) : # not a proof but...
integer = random.choice(range(10**9))
if prnt :
print('\t'+str(n)+'*'+str(integer)+'='+str(n*integer))
result = sumDigit(splitNumber(n*integer,len(str(n))),prnt)
print('\t'+str(n)+'*'+str(result//n)+'='+str(result))
result &= sumDigit(splitNumber(n*integer,len(str(n)))) \
in permuteNumber(n)+[n*(len(str(n))+1)]
return result
def checkio():
n = 0
while not (isPhoenix(n) and isKaprekar(n) and isHarshad(n) and \
propertyWith9(n) and isExtendedKaprekar(n)) :
n+=1
print("Verification :\n\n{0} is a Phoenix number :".format(n))
for i in range(1,len(str(n))+1) :
print("\t{0}*{1}={2}".format(n,i,n*i))
print("\n{0} is a Kaprekar number :\n\t{0}^2={1}".format(n,n**2))
for (left,right) in map(lambda p : splitNumberIn2(n**2,p),range(len(str(n**2)))) :
if left+right == n : print("\t{0}={1}+{2}".format(n,left,right))
sumString = reduce(lambda x,y : x+'+'+y,splitNumber(n,1),'')[1:]
print("\n{0} is a Harshad number :\n\t{0}/({1})={2}".\
format(n,sumString,n//sumDigit(splitNumber(n,1))))
print("\n{0} verify properties with 9, 99, 999, 999999 :".format(n))
for i in range(1,4) :
sumDigit(splitNumber(n,i),True)
print("\t{0}*{1}={2}".format(n,len(str(n))+1,n*(len(str(n))+1)))
print("\n{0} verify other fun properties (test with 10 random factor) :".format(n))
isExtendedKaprekar(n,True)
print("\nSo the best number ever is definitely : " + str(n))
return n
checkio()
May 6, 2013