NO33_Digit canceling fractions
来源:互联网 发布:白话文翻译文言文软件 编辑:程序博客网 时间:2024/06/08 08:19
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
没什么好思路。一句话,死算呗:
import datetimeimport fractionsstart_time=datetime.datetime.now()templist=[ x for x in xrange(10,100) if len(set(str(x)))>1]relist=[]for yindex, y in enumerate(templist): if yindex==0: continue y_str=str(y) y_set=set(y_str) for x in templist[:yindex]: x_str=str(x) x_set=set(x_str) inter=x_set&y_set if not inter: continue interlist=list(inter) for tempvalue in interlist: if y_set-set([tempvalue])!={'0'}: orgin_fra=fractions.Fraction(x,y) final_fra=fractions.Fraction(int(list(x_set-set([tempvalue]))[0]),int(list(y_set-set([tempvalue]))[0])) if orgin_fra==final_fra: relist.append((x,y))def maxdivisor(x,y): if x%y==0: return y return maxdivisor(max(x%y,y),min(x%y,y))relist=[(x,y) for x,y in relist if x*y%100]print relistx,y=1,1for tempx,tempy in relist: x*=tempx/maxdivisor(tempy,tempx) y*=tempy/maxdivisor(tempy,tempx)print y/maxdivisor(y,x)end_time=datetime.datetime.now()print end_time-start_time运行结果为:
[(16, 64), (26, 65), (19, 95), (49, 98)]
100
0:00:00.043000
别人的代码:
import math,timet=time.time()def red(r): #does a/b reduce, where r = a,b, [a,b],etc a,b = r x = (set(str(a)) & set(str(b))) - set(['0']) for n in x: a1 = filter(lambda z:str(a)[(z+1)%2] == n,[0,1]) a2 = filter(lambda z:str(b)[(z+1)%2] == n,[0,1]) for c in a1: for d in a2: if a*int(str(b)[d]) == b*int(str(a)[c]): return(True) return(False)def red2(r): # reduce a/b to lowest terms, where r= a,b, etc (a,b) = r #a,b=max(a,b),min(a,b) maxd=getmaxdivisor(b,a) return b/maxddef getmaxdivisor(a,b): if a%b==0: return b else: return getmaxdivisor(max(a%b,b),min(a%b,b))sols = filter(red,[(a,b) for a in range(10,100) for b in range(a+1,100)])sol = red2([reduce(lambda a,b:a*b,[x[k] for x in sols]) for k in [0,1]])print solsprint solprint time.time()-t运行结果:
[(16, 64), (19, 95), (26, 65), (49, 98)]
100
0.0210001468658
。。比我快一倍了。我了个去了。
0 0
- NO33_Digit canceling fractions
- (Problem 33)Digit canceling fractions
- projecteuler---->problem=33----Digit canceling fractions
- Ordered Fractions
- Ordered Fractions
- Ordered Fractions
- Vulgar Fractions
- Ordered Fractions
- Continued Fractions
- Ordered Fractions
- Octal Fractions
- Fractions Again?!
- Fractions Again?!
- Delphi: Canceling a TDataSet.Post in an OnBeforePost Event
- Octal Fractions c++ ACM
- Nine-digit Fractions(II)
- Ten-digit Fractions
- Nine-digit Fractions
- 持续改进----白狼族的故事(完结)
- RTTI
- MongoDB在Windows XP下的安装
- andriod下拉列表赋值
- C++标准转换运算符dynamic_cast
- NO33_Digit canceling fractions
- Android TextView设置高亮,颜色,点击跳转链接
- android真实项目教程(一)——App应用框架搭建_by_CJJ
- 容器
- C++标准转换运算符static_cast
- js验证IP及子网掩码的合法性
- XML学习笔记 4. xpath
- 异常
- C++标准转换运算符reinterpret_cast