A unit fraction contains 1 in the numerator.

The decimal representation of the unit fractions with denominators 2 to 10 are given: 1/2 = 0.5 1/3 = 0.(3) 1/4 = 0.25 1/5 = 0.2 1/6 = 0.1(6) 1/7 = 0.(142857) 1/8 = 0.125 1/9 = 0.(1) 1/10 = 0.1

Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

##运用了python的decimal模块取小数点精度,然后转换成str格式查找是否有重复的,从而计算出重复的长度。

#控制在1S内~


import decimal

decimal.getcontext().prec = 100

import time
start = time.time()
d = 7
l = 6
for i in range(1,1000):
s = str(decimal.Decimal(1)/decimal.Decimal(i))
for j in range(len(s)):
temp = s[j+3:].find(s[j:j+3])
if temp > 0 and 3+temp > l:
d = i
l = 3 + temp
# print(d,l)
decimal.getcontext().prec = 100 + l
break print(d,l)
print(time.time()-start)
>>>
983 982
0.6900279521942139
>>>

project euler 26:Reciprocal cycles的更多相关文章

  1. Python练习题 044:Project Euler 016:乘方结果各个数值之和

    本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...

  2. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  3. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  4. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  5. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

  6. Python练习题 046:Project Euler 019:每月1日是星期天

    本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...

  7. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

  8. Python练习题 043:Project Euler 015:方格路径

    本题来自 Project Euler 第15题:https://projecteuler.net/problem=15 ''' Project Euler: Problem 15: Lattice p ...

  9. Python练习题 042:Project Euler 014:最长的考拉兹序列

    本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest C ...

随机推荐

  1. gtest编译小结(ubuntu 12.10 , gtest 1.6.0)

    1 下载源码,解压之当前用户的主目录(~/) 2 进入make目录,执行make命令 cd ~/gtest-/make make 3 在ubuntu里编译出错,提示找不到lthread库.修改Make ...

  2. LeetCode_N-Queens

    The n-queens puzzle is the problem of placing n queens on an n�n chessboard such that no two queens ...

  3. 在Struts2中集成Spring详细讲解

    Spring的官方定义是:一个轻量级的IoC和Aop容器框架,它使用了一种叫做依赖注入的技术. 所谓依赖注入,就是指将创建对象以及协议依赖对象之间合作的责任从对象自身中转移到"工厂" ...

  4. JQuery 图片延迟加载并等比缩放插件

    原文地址:http://www.shangxueba.com/jingyan/1909987.html DEMO地址:http://demo.jb51.net/html/jquery_img/jque ...

  5. thinkpad t530 centos 6.4 有线网卡 设置

    由于新装的系统没有安装网卡驱动,各大厂商的标准又不一样,有的电脑在linux下不用安装无线网卡驱动,而很不幸,我的电脑是ret的网卡,只能自行安装,在安装无线网卡的过程中使用到了chkconfig的命 ...

  6. ecshop 管理员不需要旧密码

  7. sleep()函数的的意义

    ===============WINDOWS平台下:====================== 关于VOID Sleep(DWORD dwMilliseconds);函数,许多人都觉得,它是告诉系统 ...

  8. The Frog's Games(二分)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  9. POJ3260:The Fewest Coins(混合背包)

    Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...

  10. [Hapi.js] Replying to Requests

    hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serial ...