We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
For n=100, I get -738. Not sure what is wrong.
Here is my code:
# Enter your code here. Read input from STDIN. Print output to STDOUT# None-terminating decimals that have a period are rational numbers that have (in their # most reduced from) a prime factor other than 2 or 5 in the denominatorfromfractionsimportFractionq=int(input().rstrip())cached_d=[0]*(10**6)primes=set([2,3])max_s_prime=3defprime(X):globalprimesglobalmax_s_primeifmax_s_prime>=X:ifXinprimes:returnTruereturnFalseelse:forpinprimes:ifX%p==0:returnFalseifp>X**0.5:breakwhilemax_s_prime<X**0.5:max_s_prime+=1cur_is_prime=prime(max_s_prime)ifcur_is_prime:primes.add(max_s_prime)ifX%max_s_prime==0:returnFalseprimes.add(X)returnTruedefmax_k(NN):left_n=1right_n=NNwhileleft_n<right_n:first_border=int((right_n-left_n)/3)+left_nsecond_border=int((right_n-left_n)*2/3)+left_nfirst_border_f=(NN/first_border)**first_borderiffirst_border_f==left_n:first_border_f=left_n+1second_border_f=(NN/second_border)**second_borderifsecond_border_f<left_n:second_border_f=left_niffirst_border_f<second_border_f:left_n=first_borderelse:right_n=second_borderiffirst_border>=(second_border-1):maxs=[]maxs.append((first_border_f,first_border))maxs.append((second_border_f,second_border))maxs.append(((NN/left_n)**left_n,left_n))maxs.append(((NN/right_n)**right_n,right_n))r=max(maxs)returnr[1]#if first_border_f < second_border_f:# return second_border#else:# return first_borderreturnleft_ndefterm(N):globalcached_difcached_d[N]!=0:ifcache_d[N]==1:returnTrueelse:returnFalseres=Falsek=max_k(N)f=Fraction(N,k)d=f.denominatorifd==2ord==5:res=Trueelse:res=notprime(d)ifres:cached_d[N]=1else:cached_d[N]=2returnresdefDD(N):ifterm(N):return-NreturnNforiinrange(0,q):n=int(input().rstrip())#print("cur n={}".format(n))sum1=0forjinrange(5,n+1):mk=max_k(j)#print("j={} DD={} maxk={} k/maxk={}".format(j, DD(j), mk, (j/mk)**mk))sum1+=DD(j)print("{}".format(sum1))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #183: Maximum product of parts
You are viewing a single comment's thread. Return to all comments →
For n=100, I get -738. Not sure what is wrong. Here is my code: